blob: c54a0ed70798c09f60d8f62463f9022f816c4cce [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)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000971 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000972
973 if (!getLang().CPlusPlus0x)
974 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
975
Douglas Gregor23996282009-05-12 21:31:51 +0000976 Actions.SetDeclDeleted(ThisDecl, DelLoc);
Alexis Hunt5dafebc2011-05-06 01:42:00 +0000977 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt83dc3e82011-05-06 21:24:28 +0000978 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +0000979 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000980 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
981 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000982 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000983 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000984
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000985 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000986 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000987 ConsumeCodeCompletionToken();
988 SkipUntil(tok::comma, true, true);
989 return ThisDecl;
990 }
991
John McCalldadc5752010-08-24 06:29:42 +0000992 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000993
John McCall1f4ee7b2009-12-19 09:28:58 +0000994 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000995 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000996 ExitScope();
997 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000998
Douglas Gregor23996282009-05-12 21:31:51 +0000999 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001000 SkipUntil(tok::comma, true, true);
1001 Actions.ActOnInitializerError(ThisDecl);
1002 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001003 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1004 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001005 }
1006 } else if (Tok.is(tok::l_paren)) {
1007 // Parse C++ direct initializer: '(' expression-list ')'
1008 SourceLocation LParenLoc = ConsumeParen();
1009 ExprVector Exprs(Actions);
1010 CommaLocsTy CommaLocs;
1011
Douglas Gregor613bf102009-12-22 17:47:17 +00001012 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1013 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001014 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001015 }
1016
Douglas Gregor23996282009-05-12 21:31:51 +00001017 if (ParseExpressionList(Exprs, CommaLocs)) {
1018 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001019
1020 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001021 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001022 ExitScope();
1023 }
Douglas Gregor23996282009-05-12 21:31:51 +00001024 } else {
1025 // Match the ')'.
1026 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1027
1028 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1029 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001030
1031 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001032 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001033 ExitScope();
1034 }
1035
Douglas Gregor23996282009-05-12 21:31:51 +00001036 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1037 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +00001038 RParenLoc,
1039 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001040 }
1041 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001042 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001043 }
1044
Richard Smithb2bc2e62011-02-21 20:05:19 +00001045 Actions.FinalizeDeclaration(ThisDecl);
1046
Douglas Gregor23996282009-05-12 21:31:51 +00001047 return ThisDecl;
1048}
1049
Chris Lattner1890ac82006-08-13 01:16:23 +00001050/// ParseSpecifierQualifierList
1051/// specifier-qualifier-list:
1052/// type-specifier specifier-qualifier-list[opt]
1053/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001054/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001055///
1056void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1057 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1058 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +00001059 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001060
Chris Lattner1890ac82006-08-13 01:16:23 +00001061 // Validate declspec for type-name.
1062 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +00001063 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +00001064 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +00001065 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +00001066
Chris Lattner1b22eed2006-11-28 05:12:07 +00001067 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001068 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001069 if (DS.getStorageClassSpecLoc().isValid())
1070 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1071 else
1072 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001073 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001074 }
Mike Stump11289f42009-09-09 15:08:12 +00001075
Chris Lattner1b22eed2006-11-28 05:12:07 +00001076 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001077 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001078 if (DS.isInlineSpecified())
1079 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1080 if (DS.isVirtualSpecified())
1081 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1082 if (DS.isExplicitSpecified())
1083 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001084 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001085 }
1086}
Chris Lattner53361ac2006-08-10 05:19:57 +00001087
Chris Lattner6cc055a2009-04-12 20:42:31 +00001088/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1089/// specified token is valid after the identifier in a declarator which
1090/// immediately follows the declspec. For example, these things are valid:
1091///
1092/// int x [ 4]; // direct-declarator
1093/// int x ( int y); // direct-declarator
1094/// int(int x ) // direct-declarator
1095/// int x ; // simple-declaration
1096/// int x = 17; // init-declarator-list
1097/// int x , y; // init-declarator-list
1098/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001099/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001100/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001101///
1102/// This is not, because 'x' does not immediately follow the declspec (though
1103/// ')' happens to be valid anyway).
1104/// int (x)
1105///
1106static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1107 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1108 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001109 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001110}
1111
Chris Lattner20a0c612009-04-14 21:34:55 +00001112
1113/// ParseImplicitInt - This method is called when we have an non-typename
1114/// identifier in a declspec (which normally terminates the decl spec) when
1115/// the declspec has no type specifier. In this case, the declspec is either
1116/// malformed or is "implicit int" (in K&R and C89).
1117///
1118/// This method handles diagnosing this prettily and returns false if the
1119/// declspec is done being processed. If it recovers and thinks there may be
1120/// other pieces of declspec after it, it returns true.
1121///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001122bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001123 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001124 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001125 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001126
Chris Lattner20a0c612009-04-14 21:34:55 +00001127 SourceLocation Loc = Tok.getLocation();
1128 // If we see an identifier that is not a type name, we normally would
1129 // parse it as the identifer being declared. However, when a typename
1130 // is typo'd or the definition is not included, this will incorrectly
1131 // parse the typename as the identifier name and fall over misparsing
1132 // later parts of the diagnostic.
1133 //
1134 // As such, we try to do some look-ahead in cases where this would
1135 // otherwise be an "implicit-int" case to see if this is invalid. For
1136 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1137 // an identifier with implicit int, we'd get a parse error because the
1138 // next token is obviously invalid for a type. Parse these as a case
1139 // with an invalid type specifier.
1140 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001141
Chris Lattner20a0c612009-04-14 21:34:55 +00001142 // Since we know that this either implicit int (which is rare) or an
1143 // error, we'd do lookahead to try to do better recovery.
1144 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1145 // If this token is valid for implicit int, e.g. "static x = 4", then
1146 // we just avoid eating the identifier, so it will be parsed as the
1147 // identifier in the declarator.
1148 return false;
1149 }
Mike Stump11289f42009-09-09 15:08:12 +00001150
Chris Lattner20a0c612009-04-14 21:34:55 +00001151 // Otherwise, if we don't consume this token, we are going to emit an
1152 // error anyway. Try to recover from various common problems. Check
1153 // to see if this was a reference to a tag name without a tag specified.
1154 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001155 //
1156 // C++ doesn't need this, and isTagName doesn't take SS.
1157 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001158 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001159 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001160
Douglas Gregor0be31a22010-07-02 17:43:08 +00001161 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001162 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001163 case DeclSpec::TST_enum:
1164 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1165 case DeclSpec::TST_union:
1166 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1167 case DeclSpec::TST_struct:
1168 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1169 case DeclSpec::TST_class:
1170 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001171 }
Mike Stump11289f42009-09-09 15:08:12 +00001172
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001173 if (TagName) {
1174 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001175 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001176 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump11289f42009-09-09 15:08:12 +00001177
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001178 // Parse this as a tag as if the missing tag were present.
1179 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001180 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001181 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001182 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001183 return true;
1184 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001185 }
Mike Stump11289f42009-09-09 15:08:12 +00001186
Douglas Gregor15e56022009-10-13 23:27:22 +00001187 // This is almost certainly an invalid type name. Let the action emit a
1188 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001189 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001190 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001191 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001192 // The action emitted a diagnostic, so we don't have to.
1193 if (T) {
1194 // The action has suggested that the type T could be used. Set that as
1195 // the type in the declaration specifiers, consume the would-be type
1196 // name token, and we're done.
1197 const char *PrevSpec;
1198 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001199 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001200 DS.SetRangeEnd(Tok.getLocation());
1201 ConsumeToken();
1202
1203 // There may be other declaration specifiers after this.
1204 return true;
1205 }
1206
1207 // Fall through; the action had no suggestion for us.
1208 } else {
1209 // The action did not emit a diagnostic, so emit one now.
1210 SourceRange R;
1211 if (SS) R = SS->getRange();
1212 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1213 }
Mike Stump11289f42009-09-09 15:08:12 +00001214
Douglas Gregor15e56022009-10-13 23:27:22 +00001215 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001216 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001217 unsigned DiagID;
1218 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001219 DS.SetRangeEnd(Tok.getLocation());
1220 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001221
Chris Lattner20a0c612009-04-14 21:34:55 +00001222 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1223 // avoid rippling error messages on subsequent uses of the same type,
1224 // could be useful if #include was forgotten.
1225 return false;
1226}
1227
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001228/// \brief Determine the declaration specifier context from the declarator
1229/// context.
1230///
1231/// \param Context the declarator context, which is one of the
1232/// Declarator::TheContext enumerator values.
1233Parser::DeclSpecContext
1234Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1235 if (Context == Declarator::MemberContext)
1236 return DSC_class;
1237 if (Context == Declarator::FileContext)
1238 return DSC_top_level;
1239 return DSC_normal;
1240}
1241
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001242/// ParseDeclarationSpecifiers
1243/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001244/// storage-class-specifier declaration-specifiers[opt]
1245/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001246/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001247/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001248///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001249/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001250/// 'typedef'
1251/// 'extern'
1252/// 'static'
1253/// 'auto'
1254/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001255/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001256/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001257/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001258/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001259/// [C++] 'virtual'
1260/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001261/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001262/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001263/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001264
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001265///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001266void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001267 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001268 AccessSpecifier AS,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001269 DeclSpecContext DSContext) {
1270 if (DS.getSourceRange().isInvalid()) {
1271 DS.SetRangeStart(Tok.getLocation());
1272 DS.SetRangeEnd(Tok.getLocation());
1273 }
1274
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001275 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001276 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001277 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001278 unsigned DiagID = 0;
1279
Chris Lattner4d8f8732006-11-28 05:05:08 +00001280 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001281
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001282 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001283 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001284 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001285 // If this is not a declaration specifier token, we're done reading decl
1286 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001287 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001288 return;
Mike Stump11289f42009-09-09 15:08:12 +00001289
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001290 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001291 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001292 if (DS.hasTypeSpecifier()) {
1293 bool AllowNonIdentifiers
1294 = (getCurScope()->getFlags() & (Scope::ControlScope |
1295 Scope::BlockScope |
1296 Scope::TemplateParamScope |
1297 Scope::FunctionPrototypeScope |
1298 Scope::AtCatchScope)) == 0;
1299 bool AllowNestedNameSpecifiers
1300 = DSContext == DSC_top_level ||
1301 (DSContext == DSC_class && DS.isFriendSpecified());
1302
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001303 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1304 AllowNonIdentifiers,
1305 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001306 ConsumeCodeCompletionToken();
1307 return;
1308 }
1309
Douglas Gregor80039242011-02-15 20:33:25 +00001310 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1311 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1312 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001313 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1314 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001315 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001316 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001317 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001318 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001319
1320 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1321 ConsumeCodeCompletionToken();
1322 return;
1323 }
1324
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001325 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001326 // C++ scope specifier. Annotate and loop, or bail out on error.
1327 if (TryAnnotateCXXScopeToken(true)) {
1328 if (!DS.hasTypeSpecifier())
1329 DS.SetTypeSpecError();
1330 goto DoneWithDeclSpec;
1331 }
John McCall8bc2a702010-03-01 18:20:46 +00001332 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1333 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001334 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001335
1336 case tok::annot_cxxscope: {
1337 if (DS.hasTypeSpecifier())
1338 goto DoneWithDeclSpec;
1339
John McCall9dab4e62009-12-12 11:40:51 +00001340 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001341 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1342 Tok.getAnnotationRange(),
1343 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001344
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001345 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001346 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001347 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001348 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001349 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001350 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001351
1352 // C++ [class.qual]p2:
1353 // In a lookup in which the constructor is an acceptable lookup
1354 // result and the nested-name-specifier nominates a class C:
1355 //
1356 // - if the name specified after the
1357 // nested-name-specifier, when looked up in C, is the
1358 // injected-class-name of C (Clause 9), or
1359 //
1360 // - if the name specified after the nested-name-specifier
1361 // is the same as the identifier or the
1362 // simple-template-id's template-name in the last
1363 // component of the nested-name-specifier,
1364 //
1365 // the name is instead considered to name the constructor of
1366 // class C.
1367 //
1368 // Thus, if the template-name is actually the constructor
1369 // name, then the code is ill-formed; this interpretation is
1370 // reinforced by the NAD status of core issue 635.
1371 TemplateIdAnnotation *TemplateId
1372 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +00001373 if ((DSContext == DSC_top_level ||
1374 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1375 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001376 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001377 if (isConstructorDeclarator()) {
1378 // The user meant this to be an out-of-line constructor
1379 // definition, but template arguments are not allowed
1380 // there. Just allow this as a constructor; we'll
1381 // complain about it later.
1382 goto DoneWithDeclSpec;
1383 }
1384
1385 // The user meant this to name a type, but it actually names
1386 // a constructor with some extraneous template
1387 // arguments. Complain, then parse it as a type as the user
1388 // intended.
1389 Diag(TemplateId->TemplateNameLoc,
1390 diag::err_out_of_line_template_id_names_constructor)
1391 << TemplateId->Name;
1392 }
1393
John McCall9dab4e62009-12-12 11:40:51 +00001394 DS.getTypeSpecScope() = SS;
1395 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001396 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001397 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001398 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001399 continue;
1400 }
1401
Douglas Gregorc5790df2009-09-28 07:26:33 +00001402 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001403 DS.getTypeSpecScope() = SS;
1404 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001405 if (Tok.getAnnotationValue()) {
1406 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001407 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1408 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001409 PrevSpec, DiagID, T);
1410 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001411 else
1412 DS.SetTypeSpecError();
1413 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1414 ConsumeToken(); // The typename
1415 }
1416
Douglas Gregor167fa622009-03-25 15:40:00 +00001417 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001418 goto DoneWithDeclSpec;
1419
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001420 // If we're in a context where the identifier could be a class name,
1421 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001422 if ((DSContext == DSC_top_level ||
1423 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001424 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001425 &SS)) {
1426 if (isConstructorDeclarator())
1427 goto DoneWithDeclSpec;
1428
1429 // As noted in C++ [class.qual]p2 (cited above), when the name
1430 // of the class is qualified in a context where it could name
1431 // a constructor, its a constructor name. However, we've
1432 // looked at the declarator, and the user probably meant this
1433 // to be a type. Complain that it isn't supposed to be treated
1434 // as a type, then proceed to parse it as a type.
1435 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1436 << Next.getIdentifierInfo();
1437 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001438
John McCallba7bf592010-08-24 05:47:05 +00001439 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1440 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001441 getCurScope(), &SS,
1442 false, false, ParsedType(),
1443 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001444
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001445 // If the referenced identifier is not a type, then this declspec is
1446 // erroneous: We already checked about that it has no type specifier, and
1447 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001448 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001449 if (TypeRep == 0) {
1450 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001451 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001452 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001453 }
Mike Stump11289f42009-09-09 15:08:12 +00001454
John McCall9dab4e62009-12-12 11:40:51 +00001455 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001456 ConsumeToken(); // The C++ scope.
1457
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001458 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001459 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001460 if (isInvalid)
1461 break;
Mike Stump11289f42009-09-09 15:08:12 +00001462
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001463 DS.SetRangeEnd(Tok.getLocation());
1464 ConsumeToken(); // The typename.
1465
1466 continue;
1467 }
Mike Stump11289f42009-09-09 15:08:12 +00001468
Chris Lattnere387d9e2009-01-21 19:48:37 +00001469 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001470 if (Tok.getAnnotationValue()) {
1471 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001472 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001473 DiagID, T);
1474 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001475 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001476
1477 if (isInvalid)
1478 break;
1479
Chris Lattnere387d9e2009-01-21 19:48:37 +00001480 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1481 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001482
Chris Lattnere387d9e2009-01-21 19:48:37 +00001483 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1484 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001485 // Objective-C interface.
1486 if (Tok.is(tok::less) && getLang().ObjC1)
1487 ParseObjCProtocolQualifiers(DS);
1488
Chris Lattnere387d9e2009-01-21 19:48:37 +00001489 continue;
1490 }
Mike Stump11289f42009-09-09 15:08:12 +00001491
Douglas Gregor06873092011-04-28 15:48:45 +00001492 case tok::kw___is_signed:
1493 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1494 // typically treats it as a trait. If we see __is_signed as it appears
1495 // in libstdc++, e.g.,
1496 //
1497 // static const bool __is_signed;
1498 //
1499 // then treat __is_signed as an identifier rather than as a keyword.
1500 if (DS.getTypeSpecType() == TST_bool &&
1501 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1502 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1503 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1504 Tok.setKind(tok::identifier);
1505 }
1506
1507 // We're done with the declaration-specifiers.
1508 goto DoneWithDeclSpec;
1509
Chris Lattner16fac4f2008-07-26 01:18:38 +00001510 // typedef-name
1511 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001512 // In C++, check to see if this is a scope specifier like foo::bar::, if
1513 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001514 if (getLang().CPlusPlus) {
1515 if (TryAnnotateCXXScopeToken(true)) {
1516 if (!DS.hasTypeSpecifier())
1517 DS.SetTypeSpecError();
1518 goto DoneWithDeclSpec;
1519 }
1520 if (!Tok.is(tok::identifier))
1521 continue;
1522 }
Mike Stump11289f42009-09-09 15:08:12 +00001523
Chris Lattner16fac4f2008-07-26 01:18:38 +00001524 // This identifier can only be a typedef name if we haven't already seen
1525 // a type-specifier. Without this check we misparse:
1526 // typedef int X; struct Y { short X; }; as 'short int'.
1527 if (DS.hasTypeSpecifier())
1528 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001529
John Thompson22334602010-02-05 00:12:22 +00001530 // Check for need to substitute AltiVec keyword tokens.
1531 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1532 break;
1533
Chris Lattner16fac4f2008-07-26 01:18:38 +00001534 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001535 ParsedType TypeRep =
1536 Actions.getTypeName(*Tok.getIdentifierInfo(),
1537 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001538
Chris Lattner6cc055a2009-04-12 20:42:31 +00001539 // If this is not a typedef name, don't parse it as part of the declspec,
1540 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001541 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001542 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001543 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001544 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001545
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001546 // If we're in a context where the identifier could be a class name,
1547 // check whether this is a constructor declaration.
1548 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001549 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001550 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001551 goto DoneWithDeclSpec;
1552
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001553 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001554 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001555 if (isInvalid)
1556 break;
Mike Stump11289f42009-09-09 15:08:12 +00001557
Chris Lattner16fac4f2008-07-26 01:18:38 +00001558 DS.SetRangeEnd(Tok.getLocation());
1559 ConsumeToken(); // The identifier
1560
1561 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1562 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001563 // Objective-C interface.
1564 if (Tok.is(tok::less) && getLang().ObjC1)
1565 ParseObjCProtocolQualifiers(DS);
1566
Steve Naroffcd5e7822008-09-22 10:28:57 +00001567 // Need to support trailing type qualifiers (e.g. "id<p> const").
1568 // If a type specifier follows, it will be diagnosed elsewhere.
1569 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001570 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001571
1572 // type-name
1573 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001574 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001575 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001576 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001577 // This template-id does not refer to a type name, so we're
1578 // done with the type-specifiers.
1579 goto DoneWithDeclSpec;
1580 }
1581
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001582 // If we're in a context where the template-id could be a
1583 // constructor name or specialization, check whether this is a
1584 // constructor declaration.
1585 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001586 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001587 isConstructorDeclarator())
1588 goto DoneWithDeclSpec;
1589
Douglas Gregor7f741122009-02-25 19:37:18 +00001590 // Turn the template-id annotation token into a type annotation
1591 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001592 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001593 continue;
1594 }
1595
Chris Lattnere37e2332006-08-15 04:50:22 +00001596 // GNU attributes support.
1597 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001598 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001599 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001600
1601 // Microsoft declspec support.
1602 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001603 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001604 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001605
Steve Naroff44ac7772008-12-25 14:16:32 +00001606 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001607 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001608 // FIXME: Add handling here!
1609 break;
1610
1611 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001612 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001613 case tok::kw___cdecl:
1614 case tok::kw___stdcall:
1615 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001616 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001617 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001618 continue;
1619
Dawn Perchik335e16b2010-09-03 01:29:35 +00001620 // Borland single token adornments.
1621 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001622 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001623 continue;
1624
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001625 // OpenCL single token adornments.
1626 case tok::kw___kernel:
1627 ParseOpenCLAttributes(DS.getAttributes());
1628 continue;
1629
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001630 // storage-class-specifier
1631 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001632 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001633 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001634 break;
1635 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001636 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001637 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001638 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001639 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001640 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001641 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001642 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001643 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001644 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001645 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001646 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001647 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001648 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001649 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001650 break;
1651 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00001652 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001653 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1654 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1655 DiagID, getLang());
1656 if (!isInvalid)
1657 Diag(Tok, diag::auto_storage_class)
1658 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1659 }
1660 else
1661 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1662 DiagID);
1663 }
Anders Carlsson082acde2009-06-26 18:41:36 +00001664 else
John McCall49bfce42009-08-03 20:12:06 +00001665 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001666 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001667 break;
1668 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001669 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001670 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001671 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001672 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001673 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001674 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001675 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001676 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001677 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001678 break;
Mike Stump11289f42009-09-09 15:08:12 +00001679
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001680 // function-specifier
1681 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001682 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001683 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001684 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001685 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001686 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001687 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001688 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001689 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001690
Anders Carlssoncd8db412009-05-06 04:46:28 +00001691 // friend
1692 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001693 if (DSContext == DSC_class)
1694 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1695 else {
1696 PrevSpec = ""; // not actually used by the diagnostic
1697 DiagID = diag::err_friend_invalid_in_context;
1698 isInvalid = true;
1699 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001700 break;
Mike Stump11289f42009-09-09 15:08:12 +00001701
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001702 // constexpr
1703 case tok::kw_constexpr:
1704 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1705 break;
1706
Chris Lattnere387d9e2009-01-21 19:48:37 +00001707 // type-specifier
1708 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001709 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1710 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001711 break;
1712 case tok::kw_long:
1713 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001714 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1715 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001716 else
John McCall49bfce42009-08-03 20:12:06 +00001717 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1718 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001719 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001720 case tok::kw___int64:
1721 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1722 DiagID);
1723 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001724 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001725 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1726 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001727 break;
1728 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001729 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1730 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001731 break;
1732 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001733 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1734 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001735 break;
1736 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001737 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1738 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001739 break;
1740 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001741 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1742 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001743 break;
1744 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001745 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1746 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001747 break;
1748 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001749 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1750 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001751 break;
1752 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001753 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1754 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001755 break;
1756 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001757 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1758 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001759 break;
1760 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001761 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1762 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001763 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001764 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001765 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1766 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001767 break;
1768 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001769 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1770 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001771 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001772 case tok::kw_bool:
1773 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001774 if (Tok.is(tok::kw_bool) &&
1775 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1776 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1777 PrevSpec = ""; // Not used by the diagnostic.
1778 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00001779 // For better error recovery.
1780 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001781 isInvalid = true;
1782 } else {
1783 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1784 DiagID);
1785 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001786 break;
1787 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001788 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1789 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001790 break;
1791 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001792 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1793 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001794 break;
1795 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001796 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1797 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001798 break;
John Thompson22334602010-02-05 00:12:22 +00001799 case tok::kw___vector:
1800 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1801 break;
1802 case tok::kw___pixel:
1803 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1804 break;
John McCall39439732011-04-09 22:50:59 +00001805 case tok::kw___unknown_anytype:
1806 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1807 PrevSpec, DiagID);
1808 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001809
1810 // class-specifier:
1811 case tok::kw_class:
1812 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001813 case tok::kw_union: {
1814 tok::TokenKind Kind = Tok.getKind();
1815 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001816 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001817 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001818 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001819
1820 // enum-specifier:
1821 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001822 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001823 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001824 continue;
1825
1826 // cv-qualifier:
1827 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001828 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1829 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001830 break;
1831 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001832 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1833 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001834 break;
1835 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001836 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1837 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001838 break;
1839
Douglas Gregor333489b2009-03-27 23:10:48 +00001840 // C++ typename-specifier:
1841 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001842 if (TryAnnotateTypeOrScopeToken()) {
1843 DS.SetTypeSpecError();
1844 goto DoneWithDeclSpec;
1845 }
1846 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001847 continue;
1848 break;
1849
Chris Lattnere387d9e2009-01-21 19:48:37 +00001850 // GNU typeof support.
1851 case tok::kw_typeof:
1852 ParseTypeofSpecifier(DS);
1853 continue;
1854
Anders Carlsson74948d02009-06-24 17:47:40 +00001855 case tok::kw_decltype:
1856 ParseDecltypeSpecifier(DS);
1857 continue;
1858
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00001859 // OpenCL qualifiers:
1860 case tok::kw_private:
1861 if (!getLang().OpenCL)
1862 goto DoneWithDeclSpec;
1863 case tok::kw___private:
1864 case tok::kw___global:
1865 case tok::kw___local:
1866 case tok::kw___constant:
1867 case tok::kw___read_only:
1868 case tok::kw___write_only:
1869 case tok::kw___read_write:
1870 ParseOpenCLQualifiers(DS);
1871 break;
1872
Steve Naroffcfdf6162008-06-05 00:02:44 +00001873 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001874 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001875 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1876 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001877 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001878 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001879
Douglas Gregor3a001f42010-11-19 17:10:50 +00001880 if (!ParseObjCProtocolQualifiers(DS))
1881 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1882 << FixItHint::CreateInsertion(Loc, "id")
1883 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001884
1885 // Need to support trailing type qualifiers (e.g. "id<p> const").
1886 // If a type specifier follows, it will be diagnosed elsewhere.
1887 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001888 }
John McCall49bfce42009-08-03 20:12:06 +00001889 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001890 if (isInvalid) {
1891 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001892 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001893
1894 if (DiagID == diag::ext_duplicate_declspec)
1895 Diag(Tok, DiagID)
1896 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1897 else
1898 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001899 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001900
Chris Lattner2e232092008-03-13 06:29:04 +00001901 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00001902 if (DiagID != diag::err_bool_redeclaration)
1903 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001904 }
1905}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001906
Chris Lattnera448d752009-01-06 06:59:53 +00001907/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001908/// primarily follow the C++ grammar with additions for C99 and GNU,
1909/// which together subsume the C grammar. Note that the C++
1910/// type-specifier also includes the C type-qualifier (for const,
1911/// volatile, and C99 restrict). Returns true if a type-specifier was
1912/// found (and parsed), false otherwise.
1913///
1914/// type-specifier: [C++ 7.1.5]
1915/// simple-type-specifier
1916/// class-specifier
1917/// enum-specifier
1918/// elaborated-type-specifier [TODO]
1919/// cv-qualifier
1920///
1921/// cv-qualifier: [C++ 7.1.5.1]
1922/// 'const'
1923/// 'volatile'
1924/// [C99] 'restrict'
1925///
1926/// simple-type-specifier: [ C++ 7.1.5.2]
1927/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1928/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1929/// 'char'
1930/// 'wchar_t'
1931/// 'bool'
1932/// 'short'
1933/// 'int'
1934/// 'long'
1935/// 'signed'
1936/// 'unsigned'
1937/// 'float'
1938/// 'double'
1939/// 'void'
1940/// [C99] '_Bool'
1941/// [C99] '_Complex'
1942/// [C99] '_Imaginary' // Removed in TC2?
1943/// [GNU] '_Decimal32'
1944/// [GNU] '_Decimal64'
1945/// [GNU] '_Decimal128'
1946/// [GNU] typeof-specifier
1947/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1948/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001949/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001950/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001951bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001952 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001953 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001954 const ParsedTemplateInfo &TemplateInfo,
1955 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001956 SourceLocation Loc = Tok.getLocation();
1957
1958 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001959 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001960 // If we already have a type specifier, this identifier is not a type.
1961 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1962 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1963 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1964 return false;
John Thompson22334602010-02-05 00:12:22 +00001965 // Check for need to substitute AltiVec keyword tokens.
1966 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1967 break;
1968 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001969 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001970 // Annotate typenames and C++ scope specifiers. If we get one, just
1971 // recurse to handle whatever we get.
1972 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001973 return true;
1974 if (Tok.is(tok::identifier))
1975 return false;
1976 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1977 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001978 case tok::coloncolon: // ::foo::bar
1979 if (NextToken().is(tok::kw_new) || // ::new
1980 NextToken().is(tok::kw_delete)) // ::delete
1981 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001982
Chris Lattner020bab92009-01-04 23:41:41 +00001983 // Annotate typenames and C++ scope specifiers. If we get one, just
1984 // recurse to handle whatever we get.
1985 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001986 return true;
1987 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1988 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001989
Douglas Gregor450c75a2008-11-07 15:42:26 +00001990 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001991 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001992 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00001993 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1994 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001995 DiagID, T);
1996 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001997 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001998 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1999 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002000
Douglas Gregor450c75a2008-11-07 15:42:26 +00002001 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2002 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2003 // Objective-C interface. If we don't have Objective-C or a '<', this is
2004 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002005 if (Tok.is(tok::less) && getLang().ObjC1)
2006 ParseObjCProtocolQualifiers(DS);
2007
Douglas Gregor450c75a2008-11-07 15:42:26 +00002008 return true;
2009 }
2010
2011 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002012 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002013 break;
2014 case tok::kw_long:
2015 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002016 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2017 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002018 else
John McCall49bfce42009-08-03 20:12:06 +00002019 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2020 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002021 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002022 case tok::kw___int64:
2023 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2024 DiagID);
2025 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002026 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002027 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002028 break;
2029 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002030 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2031 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002032 break;
2033 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002034 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2035 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002036 break;
2037 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002038 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2039 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002040 break;
2041 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002042 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002043 break;
2044 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002045 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002046 break;
2047 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002049 break;
2050 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002051 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002052 break;
2053 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002054 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002055 break;
2056 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002057 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002058 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002059 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002060 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002061 break;
2062 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002063 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002064 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002065 case tok::kw_bool:
2066 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00002067 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002068 break;
2069 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002070 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2071 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002072 break;
2073 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002074 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2075 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002076 break;
2077 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002078 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2079 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002080 break;
John Thompson22334602010-02-05 00:12:22 +00002081 case tok::kw___vector:
2082 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2083 break;
2084 case tok::kw___pixel:
2085 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2086 break;
2087
Douglas Gregor450c75a2008-11-07 15:42:26 +00002088 // class-specifier:
2089 case tok::kw_class:
2090 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002091 case tok::kw_union: {
2092 tok::TokenKind Kind = Tok.getKind();
2093 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00002094 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2095 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002096 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002097 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00002098
2099 // enum-specifier:
2100 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002101 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002102 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002103 return true;
2104
2105 // cv-qualifier:
2106 case tok::kw_const:
2107 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002108 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002109 break;
2110 case tok::kw_volatile:
2111 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002112 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002113 break;
2114 case tok::kw_restrict:
2115 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002116 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002117 break;
2118
2119 // GNU typeof support.
2120 case tok::kw_typeof:
2121 ParseTypeofSpecifier(DS);
2122 return true;
2123
Anders Carlsson74948d02009-06-24 17:47:40 +00002124 // C++0x decltype support.
2125 case tok::kw_decltype:
2126 ParseDecltypeSpecifier(DS);
2127 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002128
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002129 // OpenCL qualifiers:
2130 case tok::kw_private:
2131 if (!getLang().OpenCL)
2132 return false;
2133 case tok::kw___private:
2134 case tok::kw___global:
2135 case tok::kw___local:
2136 case tok::kw___constant:
2137 case tok::kw___read_only:
2138 case tok::kw___write_only:
2139 case tok::kw___read_write:
2140 ParseOpenCLQualifiers(DS);
2141 break;
2142
Anders Carlssonbae27372009-06-26 23:44:14 +00002143 // C++0x auto support.
2144 case tok::kw_auto:
2145 if (!getLang().CPlusPlus0x)
2146 return false;
2147
John McCall49bfce42009-08-03 20:12:06 +00002148 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002149 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002150
Eli Friedman53339e02009-06-08 23:27:34 +00002151 case tok::kw___ptr64:
2152 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002153 case tok::kw___cdecl:
2154 case tok::kw___stdcall:
2155 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002156 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00002157 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002158 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002159
Dawn Perchik335e16b2010-09-03 01:29:35 +00002160 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002161 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002162 return true;
2163
Douglas Gregor450c75a2008-11-07 15:42:26 +00002164 default:
2165 // Not a type-specifier; do nothing.
2166 return false;
2167 }
2168
2169 // If the specifier combination wasn't legal, issue a diagnostic.
2170 if (isInvalid) {
2171 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002172 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002173 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002174 }
2175 DS.SetRangeEnd(Tok.getLocation());
2176 ConsumeToken(); // whatever we parsed above.
2177 return true;
2178}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002179
Chris Lattner70ae4912007-10-29 04:42:53 +00002180/// ParseStructDeclaration - Parse a struct declaration without the terminating
2181/// semicolon.
2182///
Chris Lattner90a26b02007-01-23 04:38:16 +00002183/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002184/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002185/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002186/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002187/// struct-declarator-list:
2188/// struct-declarator
2189/// struct-declarator-list ',' struct-declarator
2190/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2191/// struct-declarator:
2192/// declarator
2193/// [GNU] declarator attributes[opt]
2194/// declarator[opt] ':' constant-expression
2195/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2196///
Chris Lattnera12405b2008-04-10 06:46:29 +00002197void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002198ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002199 if (Tok.is(tok::kw___extension__)) {
2200 // __extension__ silences extension warnings in the subexpression.
2201 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002202 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002203 return ParseStructDeclaration(DS, Fields);
2204 }
Mike Stump11289f42009-09-09 15:08:12 +00002205
Steve Naroff97170802007-08-20 22:28:22 +00002206 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002207 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002208
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002209 // If there are no declarators, this is a free-standing declaration
2210 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002211 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002212 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002213 return;
2214 }
2215
2216 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002217 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002218 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002219 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002220 FieldDeclarator DeclaratorInfo(DS);
2221
2222 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002223 if (!FirstDeclarator)
2224 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002225
Steve Naroff97170802007-08-20 22:28:22 +00002226 /// struct-declarator: declarator
2227 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002228 if (Tok.isNot(tok::colon)) {
2229 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2230 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002231 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002232 }
Mike Stump11289f42009-09-09 15:08:12 +00002233
Chris Lattner76c72282007-10-09 17:33:22 +00002234 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002235 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002236 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002237 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002238 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002239 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002240 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002241 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002242
Steve Naroff97170802007-08-20 22:28:22 +00002243 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002244 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002245
John McCallcfefb6d2009-11-03 02:38:08 +00002246 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002247 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002248 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002249
Steve Naroff97170802007-08-20 22:28:22 +00002250 // If we don't have a comma, it is either the end of the list (a ';')
2251 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002252 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002253 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002254
Steve Naroff97170802007-08-20 22:28:22 +00002255 // Consume the comma.
2256 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002257
John McCallcfefb6d2009-11-03 02:38:08 +00002258 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002259 }
Steve Naroff97170802007-08-20 22:28:22 +00002260}
2261
2262/// ParseStructUnionBody
2263/// struct-contents:
2264/// struct-declaration-list
2265/// [EXT] empty
2266/// [GNU] "struct-declaration-list" without terminatoring ';'
2267/// struct-declaration-list:
2268/// struct-declaration
2269/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002270/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002271///
Chris Lattner1300fb92007-01-23 23:42:53 +00002272void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002273 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002274 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2275 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002276
Chris Lattner90a26b02007-01-23 04:38:16 +00002277 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002278
Douglas Gregor658b9552009-01-09 22:42:13 +00002279 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002280 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002281
Chris Lattner7b9ace62007-01-23 20:11:08 +00002282 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2283 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002284 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002285 Diag(Tok, diag::ext_empty_struct_union)
2286 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002287
John McCall48871652010-08-21 09:40:31 +00002288 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002289
Chris Lattner7b9ace62007-01-23 20:11:08 +00002290 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002291 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002292 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002293
Chris Lattner736ed5d2007-06-09 05:59:07 +00002294 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002295 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002296 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002297 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002298 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002299 ConsumeToken();
2300 continue;
2301 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002302
2303 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002304 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002305
John McCallcfefb6d2009-11-03 02:38:08 +00002306 if (!Tok.is(tok::at)) {
2307 struct CFieldCallback : FieldCallback {
2308 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002309 Decl *TagDecl;
2310 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002311
John McCall48871652010-08-21 09:40:31 +00002312 CFieldCallback(Parser &P, Decl *TagDecl,
2313 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002314 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2315
John McCall48871652010-08-21 09:40:31 +00002316 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002317 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002318 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002319 FD.D.getDeclSpec().getSourceRange().getBegin(),
2320 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002321 FieldDecls.push_back(Field);
2322 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002323 }
John McCallcfefb6d2009-11-03 02:38:08 +00002324 } Callback(*this, TagDecl, FieldDecls);
2325
2326 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002327 } else { // Handle @defs
2328 ConsumeToken();
2329 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2330 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002331 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002332 continue;
2333 }
2334 ConsumeToken();
2335 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2336 if (!Tok.is(tok::identifier)) {
2337 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002338 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002339 continue;
2340 }
John McCall48871652010-08-21 09:40:31 +00002341 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002342 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002343 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002344 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2345 ConsumeToken();
2346 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002347 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002348
Chris Lattner76c72282007-10-09 17:33:22 +00002349 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002350 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002351 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002352 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002353 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002354 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002355 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2356 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002357 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002358 // If we stopped at a ';', eat it.
2359 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002360 }
2361 }
Mike Stump11289f42009-09-09 15:08:12 +00002362
Steve Naroff33a1e802007-10-29 21:38:07 +00002363 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002364
John McCall084e83d2011-03-24 11:26:52 +00002365 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002366 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002367 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002368
Douglas Gregor0be31a22010-07-02 17:43:08 +00002369 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00002370 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00002371 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002372 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002373 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002374 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002375}
2376
Chris Lattner3b561a32006-08-13 00:12:11 +00002377/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002378/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002379/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002380///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002381/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2382/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002383/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002384/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002385///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002386/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2387/// [C++0x] enum-head '{' enumerator-list ',' '}'
2388///
2389/// enum-head: [C++0x]
2390/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2391/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2392///
2393/// enum-key: [C++0x]
2394/// 'enum'
2395/// 'enum' 'class'
2396/// 'enum' 'struct'
2397///
2398/// enum-base: [C++0x]
2399/// ':' type-specifier-seq
2400///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002401/// [C++] elaborated-type-specifier:
2402/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2403///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002404void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002405 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002406 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002407 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002408 if (Tok.is(tok::code_completion)) {
2409 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002410 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002411 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002412 }
2413
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002414 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002415 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002416 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002417
Abramo Bagnarad7548482010-05-19 21:37:53 +00002418 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002419 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00002420 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002421 return;
2422
2423 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002424 Diag(Tok, diag::err_expected_ident);
2425 if (Tok.isNot(tok::l_brace)) {
2426 // Has no name and is not a definition.
2427 // Skip the rest of this declarator, up until the comma or semicolon.
2428 SkipUntil(tok::comma, true);
2429 return;
2430 }
2431 }
2432 }
Mike Stump11289f42009-09-09 15:08:12 +00002433
Douglas Gregora1aec292011-02-22 20:32:04 +00002434 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002435 bool IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002436 bool IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002437
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002438 if (getLang().CPlusPlus0x &&
2439 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002440 IsScopedEnum = true;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002441 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2442 ConsumeToken();
Douglas Gregor0bf31402010-10-08 23:50:27 +00002443 }
2444
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002445 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002446 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2447 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002448 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002449
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002450 // Skip the rest of this declarator, up until the comma or semicolon.
2451 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002452 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002453 }
Mike Stump11289f42009-09-09 15:08:12 +00002454
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002455 // If an identifier is present, consume and remember it.
2456 IdentifierInfo *Name = 0;
2457 SourceLocation NameLoc;
2458 if (Tok.is(tok::identifier)) {
2459 Name = Tok.getIdentifierInfo();
2460 NameLoc = ConsumeToken();
2461 }
Mike Stump11289f42009-09-09 15:08:12 +00002462
Douglas Gregor0bf31402010-10-08 23:50:27 +00002463 if (!Name && IsScopedEnum) {
2464 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2465 // declaration of a scoped enumeration.
2466 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2467 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002468 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002469 }
2470
2471 TypeResult BaseType;
2472
Douglas Gregord1f69f62010-12-01 17:42:47 +00002473 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002474 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002475 bool PossibleBitfield = false;
2476 if (getCurScope()->getFlags() & Scope::ClassScope) {
2477 // If we're in class scope, this can either be an enum declaration with
2478 // an underlying type, or a declaration of a bitfield member. We try to
2479 // use a simple disambiguation scheme first to catch the common cases
2480 // (integer literal, sizeof); if it's still ambiguous, we then consider
2481 // anything that's a simple-type-specifier followed by '(' as an
2482 // expression. This suffices because function types are not valid
2483 // underlying types anyway.
2484 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2485 // If the next token starts an expression, we know we're parsing a
2486 // bit-field. This is the common case.
2487 if (TPR == TPResult::True())
2488 PossibleBitfield = true;
2489 // If the next token starts a type-specifier-seq, it may be either a
2490 // a fixed underlying type or the start of a function-style cast in C++;
2491 // lookahead one more token to see if it's obvious that we have a
2492 // fixed underlying type.
2493 else if (TPR == TPResult::False() &&
2494 GetLookAheadToken(2).getKind() == tok::semi) {
2495 // Consume the ':'.
2496 ConsumeToken();
2497 } else {
2498 // We have the start of a type-specifier-seq, so we have to perform
2499 // tentative parsing to determine whether we have an expression or a
2500 // type.
2501 TentativeParsingAction TPA(*this);
2502
2503 // Consume the ':'.
2504 ConsumeToken();
2505
Douglas Gregora1aec292011-02-22 20:32:04 +00002506 if ((getLang().CPlusPlus &&
2507 isCXXDeclarationSpecifier() != TPResult::True()) ||
2508 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002509 // We'll parse this as a bitfield later.
2510 PossibleBitfield = true;
2511 TPA.Revert();
2512 } else {
2513 // We have a type-specifier-seq.
2514 TPA.Commit();
2515 }
2516 }
2517 } else {
2518 // Consume the ':'.
2519 ConsumeToken();
2520 }
2521
2522 if (!PossibleBitfield) {
2523 SourceRange Range;
2524 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002525
2526 if (!getLang().CPlusPlus0x)
2527 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2528 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002529 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002530 }
2531
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002532 // There are three options here. If we have 'enum foo;', then this is a
2533 // forward declaration. If we have 'enum foo {...' then this is a
2534 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2535 //
2536 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2537 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2538 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2539 //
John McCallfaf5fb42010-08-26 23:41:50 +00002540 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002541 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002542 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002543 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002544 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002545 else
John McCallfaf5fb42010-08-26 23:41:50 +00002546 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002547
2548 // enums cannot be templates, although they can be referenced from a
2549 // template.
2550 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002551 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002552 Diag(Tok, diag::err_enum_template);
2553
2554 // Skip the rest of this declarator, up until the comma or semicolon.
2555 SkipUntil(tok::comma, true);
2556 return;
2557 }
2558
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002559 if (!Name && TUK != Sema::TUK_Definition) {
2560 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2561
2562 // Skip the rest of this declarator, up until the comma or semicolon.
2563 SkipUntil(tok::comma, true);
2564 return;
2565 }
2566
Douglas Gregord6ab8742009-05-28 23:31:59 +00002567 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002568 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002569 const char *PrevSpec = 0;
2570 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002571 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002572 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002573 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002574 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002575 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002576 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002577
Douglas Gregorba41d012010-04-24 16:38:41 +00002578 if (IsDependent) {
2579 // This enum has a dependent nested-name-specifier. Handle it as a
2580 // dependent tag.
2581 if (!Name) {
2582 DS.SetTypeSpecError();
2583 Diag(Tok, diag::err_expected_type_name_after_typename);
2584 return;
2585 }
2586
Douglas Gregor0be31a22010-07-02 17:43:08 +00002587 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002588 TUK, SS, Name, StartLoc,
2589 NameLoc);
2590 if (Type.isInvalid()) {
2591 DS.SetTypeSpecError();
2592 return;
2593 }
2594
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002595 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2596 NameLoc.isValid() ? NameLoc : StartLoc,
2597 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002598 Diag(StartLoc, DiagID) << PrevSpec;
2599
2600 return;
2601 }
Mike Stump11289f42009-09-09 15:08:12 +00002602
John McCall48871652010-08-21 09:40:31 +00002603 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002604 // The action failed to produce an enumeration tag. If this is a
2605 // definition, consume the entire definition.
2606 if (Tok.is(tok::l_brace)) {
2607 ConsumeBrace();
2608 SkipUntil(tok::r_brace);
2609 }
2610
2611 DS.SetTypeSpecError();
2612 return;
2613 }
2614
Chris Lattner76c72282007-10-09 17:33:22 +00002615 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002616 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002617
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002618 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2619 NameLoc.isValid() ? NameLoc : StartLoc,
2620 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002621 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002622}
2623
Chris Lattnerc1915e22007-01-25 07:29:02 +00002624/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2625/// enumerator-list:
2626/// enumerator
2627/// enumerator-list ',' enumerator
2628/// enumerator:
2629/// enumeration-constant
2630/// enumeration-constant '=' constant-expression
2631/// enumeration-constant:
2632/// identifier
2633///
John McCall48871652010-08-21 09:40:31 +00002634void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002635 // Enter the scope of the enum body and start the definition.
2636 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002637 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002638
Chris Lattnerc1915e22007-01-25 07:29:02 +00002639 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002640
Chris Lattner37256fb2007-08-27 17:24:30 +00002641 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002642 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002643 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002644
John McCall48871652010-08-21 09:40:31 +00002645 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002646
John McCall48871652010-08-21 09:40:31 +00002647 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002648
Chris Lattnerc1915e22007-01-25 07:29:02 +00002649 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002650 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002651 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2652 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002653
John McCall811a0f52010-10-22 23:36:17 +00002654 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002655 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002656 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002657
Chris Lattnerc1915e22007-01-25 07:29:02 +00002658 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002659 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002660 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002661 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002662 AssignedVal = ParseConstantExpression();
2663 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002664 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002665 }
Mike Stump11289f42009-09-09 15:08:12 +00002666
Chris Lattnerc1915e22007-01-25 07:29:02 +00002667 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002668 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2669 LastEnumConstDecl,
2670 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002671 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002672 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002673 EnumConstantDecls.push_back(EnumConstDecl);
2674 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002675
Douglas Gregorce66d022010-09-07 14:51:08 +00002676 if (Tok.is(tok::identifier)) {
2677 // We're missing a comma between enumerators.
2678 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2679 Diag(Loc, diag::err_enumerator_list_missing_comma)
2680 << FixItHint::CreateInsertion(Loc, ", ");
2681 continue;
2682 }
2683
Chris Lattner76c72282007-10-09 17:33:22 +00002684 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002685 break;
2686 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002687
2688 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002689 !(getLang().C99 || getLang().CPlusPlus0x))
2690 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2691 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002692 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002693 }
Mike Stump11289f42009-09-09 15:08:12 +00002694
Chris Lattnerc1915e22007-01-25 07:29:02 +00002695 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002696 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002697
Chris Lattnerc1915e22007-01-25 07:29:02 +00002698 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002699 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002700 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002701
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002702 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2703 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002704 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002705
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002706 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002707 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002708}
Chris Lattner3b561a32006-08-13 00:12:11 +00002709
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002710/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002711/// start of a type-qualifier-list.
2712bool Parser::isTypeQualifier() const {
2713 switch (Tok.getKind()) {
2714 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002715
2716 // type-qualifier only in OpenCL
2717 case tok::kw_private:
2718 return getLang().OpenCL;
2719
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002720 // type-qualifier
2721 case tok::kw_const:
2722 case tok::kw_volatile:
2723 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002724 case tok::kw___private:
2725 case tok::kw___local:
2726 case tok::kw___global:
2727 case tok::kw___constant:
2728 case tok::kw___read_only:
2729 case tok::kw___read_write:
2730 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002731 return true;
2732 }
2733}
2734
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002735/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2736/// is definitely a type-specifier. Return false if it isn't part of a type
2737/// specifier or if we're not sure.
2738bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2739 switch (Tok.getKind()) {
2740 default: return false;
2741 // type-specifiers
2742 case tok::kw_short:
2743 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002744 case tok::kw___int64:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002745 case tok::kw_signed:
2746 case tok::kw_unsigned:
2747 case tok::kw__Complex:
2748 case tok::kw__Imaginary:
2749 case tok::kw_void:
2750 case tok::kw_char:
2751 case tok::kw_wchar_t:
2752 case tok::kw_char16_t:
2753 case tok::kw_char32_t:
2754 case tok::kw_int:
2755 case tok::kw_float:
2756 case tok::kw_double:
2757 case tok::kw_bool:
2758 case tok::kw__Bool:
2759 case tok::kw__Decimal32:
2760 case tok::kw__Decimal64:
2761 case tok::kw__Decimal128:
2762 case tok::kw___vector:
2763
2764 // struct-or-union-specifier (C99) or class-specifier (C++)
2765 case tok::kw_class:
2766 case tok::kw_struct:
2767 case tok::kw_union:
2768 // enum-specifier
2769 case tok::kw_enum:
2770
2771 // typedef-name
2772 case tok::annot_typename:
2773 return true;
2774 }
2775}
2776
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002777/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002778/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002779bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002780 switch (Tok.getKind()) {
2781 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002782
Chris Lattner020bab92009-01-04 23:41:41 +00002783 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002784 if (TryAltiVecVectorToken())
2785 return true;
2786 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002787 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002788 // Annotate typenames and C++ scope specifiers. If we get one, just
2789 // recurse to handle whatever we get.
2790 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002791 return true;
2792 if (Tok.is(tok::identifier))
2793 return false;
2794 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002795
Chris Lattner020bab92009-01-04 23:41:41 +00002796 case tok::coloncolon: // ::foo::bar
2797 if (NextToken().is(tok::kw_new) || // ::new
2798 NextToken().is(tok::kw_delete)) // ::delete
2799 return false;
2800
Chris Lattner020bab92009-01-04 23:41:41 +00002801 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002802 return true;
2803 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002804
Chris Lattnere37e2332006-08-15 04:50:22 +00002805 // GNU attributes support.
2806 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002807 // GNU typeof support.
2808 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002809
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002810 // type-specifiers
2811 case tok::kw_short:
2812 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002813 case tok::kw___int64:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002814 case tok::kw_signed:
2815 case tok::kw_unsigned:
2816 case tok::kw__Complex:
2817 case tok::kw__Imaginary:
2818 case tok::kw_void:
2819 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002820 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002821 case tok::kw_char16_t:
2822 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002823 case tok::kw_int:
2824 case tok::kw_float:
2825 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002826 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002827 case tok::kw__Bool:
2828 case tok::kw__Decimal32:
2829 case tok::kw__Decimal64:
2830 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002831 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002832
Chris Lattner861a2262008-04-13 18:59:07 +00002833 // struct-or-union-specifier (C99) or class-specifier (C++)
2834 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002835 case tok::kw_struct:
2836 case tok::kw_union:
2837 // enum-specifier
2838 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002839
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002840 // type-qualifier
2841 case tok::kw_const:
2842 case tok::kw_volatile:
2843 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002844
2845 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002846 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002847 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002848
Chris Lattner409bf7d2008-10-20 00:25:30 +00002849 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2850 case tok::less:
2851 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002852
Steve Naroff44ac7772008-12-25 14:16:32 +00002853 case tok::kw___cdecl:
2854 case tok::kw___stdcall:
2855 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002856 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002857 case tok::kw___w64:
2858 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002859 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002860
2861 case tok::kw___private:
2862 case tok::kw___local:
2863 case tok::kw___global:
2864 case tok::kw___constant:
2865 case tok::kw___read_only:
2866 case tok::kw___read_write:
2867 case tok::kw___write_only:
2868
Eli Friedman53339e02009-06-08 23:27:34 +00002869 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002870
2871 case tok::kw_private:
2872 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002873 }
2874}
2875
Chris Lattneracd58a32006-08-06 17:24:14 +00002876/// isDeclarationSpecifier() - Return true if the current token is part of a
2877/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002878///
2879/// \param DisambiguatingWithExpression True to indicate that the purpose of
2880/// this check is to disambiguate between an expression and a declaration.
2881bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002882 switch (Tok.getKind()) {
2883 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002884
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002885 case tok::kw_private:
2886 return getLang().OpenCL;
2887
Chris Lattner020bab92009-01-04 23:41:41 +00002888 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002889 // Unfortunate hack to support "Class.factoryMethod" notation.
2890 if (getLang().ObjC1 && NextToken().is(tok::period))
2891 return false;
John Thompson22334602010-02-05 00:12:22 +00002892 if (TryAltiVecVectorToken())
2893 return true;
2894 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002895 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002896 // Annotate typenames and C++ scope specifiers. If we get one, just
2897 // recurse to handle whatever we get.
2898 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002899 return true;
2900 if (Tok.is(tok::identifier))
2901 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002902
2903 // If we're in Objective-C and we have an Objective-C class type followed
2904 // by an identifier and then either ':' or ']', in a place where an
2905 // expression is permitted, then this is probably a class message send
2906 // missing the initial '['. In this case, we won't consider this to be
2907 // the start of a declaration.
2908 if (DisambiguatingWithExpression &&
2909 isStartOfObjCClassMessageMissingOpenBracket())
2910 return false;
2911
John McCall1f476a12010-02-26 08:45:28 +00002912 return isDeclarationSpecifier();
2913
Chris Lattner020bab92009-01-04 23:41:41 +00002914 case tok::coloncolon: // ::foo::bar
2915 if (NextToken().is(tok::kw_new) || // ::new
2916 NextToken().is(tok::kw_delete)) // ::delete
2917 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002918
Chris Lattner020bab92009-01-04 23:41:41 +00002919 // Annotate typenames and C++ scope specifiers. If we get one, just
2920 // recurse to handle whatever we get.
2921 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002922 return true;
2923 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002924
Chris Lattneracd58a32006-08-06 17:24:14 +00002925 // storage-class-specifier
2926 case tok::kw_typedef:
2927 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002928 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002929 case tok::kw_static:
2930 case tok::kw_auto:
2931 case tok::kw_register:
2932 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002933
Chris Lattneracd58a32006-08-06 17:24:14 +00002934 // type-specifiers
2935 case tok::kw_short:
2936 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002937 case tok::kw___int64:
Chris Lattneracd58a32006-08-06 17:24:14 +00002938 case tok::kw_signed:
2939 case tok::kw_unsigned:
2940 case tok::kw__Complex:
2941 case tok::kw__Imaginary:
2942 case tok::kw_void:
2943 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002944 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002945 case tok::kw_char16_t:
2946 case tok::kw_char32_t:
2947
Chris Lattneracd58a32006-08-06 17:24:14 +00002948 case tok::kw_int:
2949 case tok::kw_float:
2950 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002951 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002952 case tok::kw__Bool:
2953 case tok::kw__Decimal32:
2954 case tok::kw__Decimal64:
2955 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002956 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002957
Chris Lattner861a2262008-04-13 18:59:07 +00002958 // struct-or-union-specifier (C99) or class-specifier (C++)
2959 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002960 case tok::kw_struct:
2961 case tok::kw_union:
2962 // enum-specifier
2963 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002964
Chris Lattneracd58a32006-08-06 17:24:14 +00002965 // type-qualifier
2966 case tok::kw_const:
2967 case tok::kw_volatile:
2968 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002969
Chris Lattneracd58a32006-08-06 17:24:14 +00002970 // function-specifier
2971 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002972 case tok::kw_virtual:
2973 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002974
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00002975 // static_assert-declaration
2976 case tok::kw__Static_assert:
2977
Chris Lattner599e47e2007-08-09 17:01:07 +00002978 // GNU typeof support.
2979 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002980
Chris Lattner599e47e2007-08-09 17:01:07 +00002981 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002982 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002983 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002984
Chris Lattner8b2ec162008-07-26 03:38:44 +00002985 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2986 case tok::less:
2987 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002988
Douglas Gregor19b7acf2011-04-27 05:41:15 +00002989 // typedef-name
2990 case tok::annot_typename:
2991 return !DisambiguatingWithExpression ||
2992 !isStartOfObjCClassMessageMissingOpenBracket();
2993
Steve Narofff192fab2009-01-06 19:34:12 +00002994 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002995 case tok::kw___cdecl:
2996 case tok::kw___stdcall:
2997 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002998 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002999 case tok::kw___w64:
3000 case tok::kw___ptr64:
3001 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003002 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003003
3004 case tok::kw___private:
3005 case tok::kw___local:
3006 case tok::kw___global:
3007 case tok::kw___constant:
3008 case tok::kw___read_only:
3009 case tok::kw___read_write:
3010 case tok::kw___write_only:
3011
Eli Friedman53339e02009-06-08 23:27:34 +00003012 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003013 }
3014}
3015
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003016bool Parser::isConstructorDeclarator() {
3017 TentativeParsingAction TPA(*this);
3018
3019 // Parse the C++ scope specifier.
3020 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003021 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00003022 TPA.Revert();
3023 return false;
3024 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003025
3026 // Parse the constructor name.
3027 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3028 // We already know that we have a constructor name; just consume
3029 // the token.
3030 ConsumeToken();
3031 } else {
3032 TPA.Revert();
3033 return false;
3034 }
3035
3036 // Current class name must be followed by a left parentheses.
3037 if (Tok.isNot(tok::l_paren)) {
3038 TPA.Revert();
3039 return false;
3040 }
3041 ConsumeParen();
3042
3043 // A right parentheses or ellipsis signals that we have a constructor.
3044 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3045 TPA.Revert();
3046 return true;
3047 }
3048
3049 // If we need to, enter the specified scope.
3050 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003051 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003052 DeclScopeObj.EnterDeclaratorScope();
3053
Francois Pichet79f3a872011-01-31 04:54:32 +00003054 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003055 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003056 MaybeParseMicrosoftAttributes(Attrs);
3057
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003058 // Check whether the next token(s) are part of a declaration
3059 // specifier, in which case we have the start of a parameter and,
3060 // therefore, we know that this is a constructor.
3061 bool IsConstructor = isDeclarationSpecifier();
3062 TPA.Revert();
3063 return IsConstructor;
3064}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003065
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003066/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003067/// type-qualifier-list: [C99 6.7.5]
3068/// type-qualifier
3069/// [vendor] attributes
3070/// [ only if VendorAttributesAllowed=true ]
3071/// type-qualifier-list type-qualifier
3072/// [vendor] type-qualifier-list attributes
3073/// [ only if VendorAttributesAllowed=true ]
3074/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3075/// [ only if CXX0XAttributesAllowed=true ]
3076/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003077///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003078void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3079 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00003080 bool CXX0XAttributesAllowed) {
3081 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3082 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00003083 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003084 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003085 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00003086 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003087 else
3088 Diag(Loc, diag::err_attributes_not_allowed);
3089 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003090
3091 SourceLocation EndLoc;
3092
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003093 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003094 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003095 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003096 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003097 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003098
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003099 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003100 case tok::code_completion:
3101 Actions.CodeCompleteTypeQualifiers(DS);
3102 ConsumeCodeCompletionToken();
3103 break;
3104
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003105 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003106 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3107 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003108 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003109 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003110 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3111 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003112 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003113 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003114 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3115 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003116 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003117
3118 // OpenCL qualifiers:
3119 case tok::kw_private:
3120 if (!getLang().OpenCL)
3121 goto DoneWithTypeQuals;
3122 case tok::kw___private:
3123 case tok::kw___global:
3124 case tok::kw___local:
3125 case tok::kw___constant:
3126 case tok::kw___read_only:
3127 case tok::kw___write_only:
3128 case tok::kw___read_write:
3129 ParseOpenCLQualifiers(DS);
3130 break;
3131
Eli Friedman53339e02009-06-08 23:27:34 +00003132 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003133 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00003134 case tok::kw___cdecl:
3135 case tok::kw___stdcall:
3136 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003137 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003138 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003139 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003140 continue;
3141 }
3142 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003143 case tok::kw___pascal:
3144 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003145 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003146 continue;
3147 }
3148 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003149 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003150 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003151 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003152 continue; // do *not* consume the next token!
3153 }
3154 // otherwise, FALL THROUGH!
3155 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003156 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003157 // If this is not a type-qualifier token, we're done reading type
3158 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003159 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003160 if (EndLoc.isValid())
3161 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003162 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003163 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003164
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003165 // If the specifier combination wasn't legal, issue a diagnostic.
3166 if (isInvalid) {
3167 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003168 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003169 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003170 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003171 }
3172}
3173
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003174
3175/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3176///
3177void Parser::ParseDeclarator(Declarator &D) {
3178 /// This implements the 'declarator' production in the C grammar, then checks
3179 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003180 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003181}
3182
Sebastian Redlbd150f42008-11-21 19:14:01 +00003183/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3184/// is parsed by the function passed to it. Pass null, and the direct-declarator
3185/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003186/// ptr-operator production.
3187///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003188/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3189/// [C] pointer[opt] direct-declarator
3190/// [C++] direct-declarator
3191/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003192///
3193/// pointer: [C99 6.7.5]
3194/// '*' type-qualifier-list[opt]
3195/// '*' type-qualifier-list[opt] pointer
3196///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003197/// ptr-operator:
3198/// '*' cv-qualifier-seq[opt]
3199/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003200/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003201/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003202/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003203/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003204void Parser::ParseDeclaratorInternal(Declarator &D,
3205 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003206 if (Diags.hasAllExtensionsSilenced())
3207 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003208
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003209 // C++ member pointers start with a '::' or a nested-name.
3210 // Member pointers get special handling, since there's no place for the
3211 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003212 if (getLang().CPlusPlus &&
3213 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3214 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003215 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003216 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003217
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003218 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003219 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003220 // The scope spec really belongs to the direct-declarator.
3221 D.getCXXScopeSpec() = SS;
3222 if (DirectDeclParser)
3223 (this->*DirectDeclParser)(D);
3224 return;
3225 }
3226
3227 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003228 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003229 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003230 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003231 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003232
3233 // Recurse to parse whatever is left.
3234 ParseDeclaratorInternal(D, DirectDeclParser);
3235
3236 // Sema will have to catch (syntactically invalid) pointers into global
3237 // scope. It has to catch pointers into namespace scope anyway.
3238 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003239 Loc),
3240 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003241 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003242 return;
3243 }
3244 }
3245
3246 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003247 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003248 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003249 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003250 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003251 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003252 if (DirectDeclParser)
3253 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003254 return;
3255 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003256
Sebastian Redled0f3b02009-03-15 22:02:01 +00003257 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3258 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003259 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003260 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003261
Chris Lattner9eac9312009-03-27 04:18:06 +00003262 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003263 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003264 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003265
Bill Wendling3708c182007-05-27 10:15:43 +00003266 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003267 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003268
Bill Wendling3708c182007-05-27 10:15:43 +00003269 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003270 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003271 if (Kind == tok::star)
3272 // Remember that we parsed a pointer type, and remember the type-quals.
3273 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003274 DS.getConstSpecLoc(),
3275 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003276 DS.getRestrictSpecLoc()),
3277 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003278 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003279 else
3280 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003281 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003282 Loc),
3283 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003284 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003285 } else {
3286 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003287 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003288
Sebastian Redl3b27be62009-03-23 00:00:23 +00003289 // Complain about rvalue references in C++03, but then go on and build
3290 // the declarator.
3291 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003292 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003293
Bill Wendling93efb222007-06-02 23:28:54 +00003294 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3295 // cv-qualifiers are introduced through the use of a typedef or of a
3296 // template type argument, in which case the cv-qualifiers are ignored.
3297 //
3298 // [GNU] Retricted references are allowed.
3299 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003300 // [C++0x] Attributes on references are not allowed.
3301 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003302 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003303
3304 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3305 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3306 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003307 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003308 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3309 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003310 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003311 }
Bill Wendling3708c182007-05-27 10:15:43 +00003312
3313 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003314 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003315
Douglas Gregor66583c52008-11-03 15:51:28 +00003316 if (D.getNumTypeObjects() > 0) {
3317 // C++ [dcl.ref]p4: There shall be no references to references.
3318 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3319 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003320 if (const IdentifierInfo *II = D.getIdentifier())
3321 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3322 << II;
3323 else
3324 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3325 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003326
Sebastian Redlbd150f42008-11-21 19:14:01 +00003327 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003328 // can go ahead and build the (technically ill-formed)
3329 // declarator: reference collapsing will take care of it.
3330 }
3331 }
3332
Bill Wendling3708c182007-05-27 10:15:43 +00003333 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003334 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003335 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003336 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003337 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003338 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003339}
3340
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003341/// ParseDirectDeclarator
3342/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003343/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003344/// '(' declarator ')'
3345/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003346/// [C90] direct-declarator '[' constant-expression[opt] ']'
3347/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3348/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3349/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3350/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003351/// direct-declarator '(' parameter-type-list ')'
3352/// direct-declarator '(' identifier-list[opt] ')'
3353/// [GNU] direct-declarator '(' parameter-forward-declarations
3354/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003355/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3356/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003357/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003358///
3359/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003360/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003361/// '::'[opt] nested-name-specifier[opt] type-name
3362///
3363/// id-expression: [C++ 5.1]
3364/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003365/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003366///
3367/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003368/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003369/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003370/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003371/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003372/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003373///
Chris Lattneracd58a32006-08-06 17:24:14 +00003374void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003375 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003376
Douglas Gregor7861a802009-11-03 01:35:08 +00003377 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3378 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003379 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003380 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003381 }
3382
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003383 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003384 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003385 // Change the declaration context for name lookup, until this function
3386 // is exited (and the declarator has been parsed).
3387 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003388 }
3389
Douglas Gregor27b4c162010-12-23 22:44:42 +00003390 // C++0x [dcl.fct]p14:
3391 // There is a syntactic ambiguity when an ellipsis occurs at the end
3392 // of a parameter-declaration-clause without a preceding comma. In
3393 // this case, the ellipsis is parsed as part of the
3394 // abstract-declarator if the type of the parameter names a template
3395 // parameter pack that has not been expanded; otherwise, it is parsed
3396 // as part of the parameter-declaration-clause.
3397 if (Tok.is(tok::ellipsis) &&
3398 !((D.getContext() == Declarator::PrototypeContext ||
3399 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003400 NextToken().is(tok::r_paren) &&
3401 !Actions.containsUnexpandedParameterPacks(D)))
3402 D.setEllipsisLoc(ConsumeToken());
3403
Douglas Gregor7861a802009-11-03 01:35:08 +00003404 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3405 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3406 // We found something that indicates the start of an unqualified-id.
3407 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003408 bool AllowConstructorName;
3409 if (D.getDeclSpec().hasTypeSpecifier())
3410 AllowConstructorName = false;
3411 else if (D.getCXXScopeSpec().isSet())
3412 AllowConstructorName =
3413 (D.getContext() == Declarator::FileContext ||
3414 (D.getContext() == Declarator::MemberContext &&
3415 D.getDeclSpec().isFriendSpecified()));
3416 else
3417 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3418
Douglas Gregor7861a802009-11-03 01:35:08 +00003419 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3420 /*EnteringContext=*/true,
3421 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003422 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003423 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003424 D.getName()) ||
3425 // Once we're past the identifier, if the scope was bad, mark the
3426 // whole declarator bad.
3427 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003428 D.SetIdentifier(0, Tok.getLocation());
3429 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003430 } else {
3431 // Parsed the unqualified-id; update range information and move along.
3432 if (D.getSourceRange().getBegin().isInvalid())
3433 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3434 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003435 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003436 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003437 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003438 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003439 assert(!getLang().CPlusPlus &&
3440 "There's a C++-specific check for tok::identifier above");
3441 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3442 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3443 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003444 goto PastIdentifier;
3445 }
3446
3447 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003448 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003449 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003450 // Example: 'char (*X)' or 'int (*XX)(void)'
3451 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003452
3453 // If the declarator was parenthesized, we entered the declarator
3454 // scope when parsing the parenthesized declarator, then exited
3455 // the scope already. Re-enter the scope, if we need to.
3456 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003457 // If there was an error parsing parenthesized declarator, declarator
3458 // scope may have been enterred before. Don't do it again.
3459 if (!D.isInvalidType() &&
3460 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003461 // Change the declaration context for name lookup, until this function
3462 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003463 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003464 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003465 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003466 // This could be something simple like "int" (in which case the declarator
3467 // portion is empty), if an abstract-declarator is allowed.
3468 D.SetIdentifier(0, Tok.getLocation());
3469 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003470 if (D.getContext() == Declarator::MemberContext)
3471 Diag(Tok, diag::err_expected_member_name_or_semi)
3472 << D.getDeclSpec().getSourceRange();
3473 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003474 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003475 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003476 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003477 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003478 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003479 }
Mike Stump11289f42009-09-09 15:08:12 +00003480
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003481 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003482 assert(D.isPastIdentifier() &&
3483 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003484
Alexis Hunt96d5c762009-11-21 08:43:09 +00003485 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003486 if (D.getIdentifier())
3487 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003488
Chris Lattneracd58a32006-08-06 17:24:14 +00003489 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003490 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003491 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3492 // In such a case, check if we actually have a function declarator; if it
3493 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003494 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3495 // When not in file scope, warn for ambiguous function declarators, just
3496 // in case the author intended it as a variable definition.
3497 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3498 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3499 break;
3500 }
John McCall084e83d2011-03-24 11:26:52 +00003501 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003502 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003503 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003504 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003505 } else {
3506 break;
3507 }
3508 }
3509}
3510
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003511/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3512/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003513/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003514/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3515///
3516/// direct-declarator:
3517/// '(' declarator ')'
3518/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003519/// direct-declarator '(' parameter-type-list ')'
3520/// direct-declarator '(' identifier-list[opt] ')'
3521/// [GNU] direct-declarator '(' parameter-forward-declarations
3522/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003523///
3524void Parser::ParseParenDeclarator(Declarator &D) {
3525 SourceLocation StartLoc = ConsumeParen();
3526 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003527
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003528 // Eat any attributes before we look at whether this is a grouping or function
3529 // declarator paren. If this is a grouping paren, the attribute applies to
3530 // the type being built up, for example:
3531 // int (__attribute__(()) *x)(long y)
3532 // If this ends up not being a grouping paren, the attribute applies to the
3533 // first argument, for example:
3534 // int (__attribute__(()) int x)
3535 // In either case, we need to eat any attributes to be able to determine what
3536 // sort of paren this is.
3537 //
John McCall084e83d2011-03-24 11:26:52 +00003538 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003539 bool RequiresArg = false;
3540 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003541 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003542
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003543 // We require that the argument list (if this is a non-grouping paren) be
3544 // present even if the attribute list was empty.
3545 RequiresArg = true;
3546 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003547 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003548 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003549 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3550 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall53fa7142010-12-24 02:08:15 +00003551 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003552 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003553 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003554 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003555 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003556
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003557 // If we haven't past the identifier yet (or where the identifier would be
3558 // stored, if this is an abstract declarator), then this is probably just
3559 // grouping parens. However, if this could be an abstract-declarator, then
3560 // this could also be the start of function arguments (consider 'void()').
3561 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003562
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003563 if (!D.mayOmitIdentifier()) {
3564 // If this can't be an abstract-declarator, this *must* be a grouping
3565 // paren, because we haven't seen the identifier yet.
3566 isGrouping = true;
3567 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003568 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003569 isDeclarationSpecifier()) { // 'int(int)' is a function.
3570 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3571 // considered to be a type, not a K&R identifier-list.
3572 isGrouping = false;
3573 } else {
3574 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3575 isGrouping = true;
3576 }
Mike Stump11289f42009-09-09 15:08:12 +00003577
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003578 // If this is a grouping paren, handle:
3579 // direct-declarator: '(' declarator ')'
3580 // direct-declarator: '(' attributes declarator ')'
3581 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003582 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003583 D.setGroupingParens(true);
3584
Sebastian Redlbd150f42008-11-21 19:14:01 +00003585 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003586 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003587 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003588 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3589 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003590
3591 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003592 return;
3593 }
Mike Stump11289f42009-09-09 15:08:12 +00003594
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003595 // Okay, if this wasn't a grouping paren, it must be the start of a function
3596 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003597 // identifier (and remember where it would have been), then call into
3598 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003599 D.SetIdentifier(0, Tok.getLocation());
3600
John McCall53fa7142010-12-24 02:08:15 +00003601 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003602}
3603
3604/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3605/// declarator D up to a paren, which indicates that we are parsing function
3606/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003607///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003608/// If AttrList is non-null, then the caller parsed those arguments immediately
3609/// after the open paren - they should be considered to be the first argument of
3610/// a parameter. If RequiresArg is true, then the first argument of the
3611/// function is required to be present and required to not be an identifier
3612/// list.
3613///
Chris Lattneracd58a32006-08-06 17:24:14 +00003614/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003615/// parameter-type-list: [C99 6.7.5]
3616/// parameter-list
3617/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003618/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003619///
3620/// parameter-list: [C99 6.7.5]
3621/// parameter-declaration
3622/// parameter-list ',' parameter-declaration
3623///
3624/// parameter-declaration: [C99 6.7.5]
3625/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003626/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003627/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003628/// declaration-specifiers abstract-declarator[opt]
3629/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003630/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003631/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003632///
Douglas Gregor54992352011-01-26 03:43:54 +00003633/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3634/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003635///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003636/// [C++0x] exception-specification:
3637/// dynamic-exception-specification
3638/// noexcept-specification
3639///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003640void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall53fa7142010-12-24 02:08:15 +00003641 ParsedAttributes &attrs,
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003642 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003643 // lparen is already consumed!
3644 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003645
Douglas Gregor7fb25412010-10-01 18:44:50 +00003646 ParsedType TrailingReturnType;
3647
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003648 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003649 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003650 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003651 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003652
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003653 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003654
3655 // cv-qualifier-seq[opt].
John McCall084e83d2011-03-24 11:26:52 +00003656 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003657 SourceLocation RefQualifierLoc;
3658 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003659 ExceptionSpecificationType ESpecType = EST_None;
3660 SourceRange ESpecRange;
3661 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3662 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3663 ExprResult NoexceptExpr;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003664 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003665 MaybeParseCXX0XAttributes(attrs);
3666
Chris Lattnercf0bab22008-12-18 07:02:59 +00003667 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003668 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003669 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003670
Douglas Gregor54992352011-01-26 03:43:54 +00003671 // Parse ref-qualifier[opt]
3672 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3673 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003674 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003675
Douglas Gregor54992352011-01-26 03:43:54 +00003676 RefQualifierIsLValueRef = Tok.is(tok::amp);
3677 RefQualifierLoc = ConsumeToken();
3678 EndLoc = RefQualifierLoc;
3679 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003680
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003681 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003682 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3683 DynamicExceptions,
3684 DynamicExceptionRanges,
3685 NoexceptExpr);
3686 if (ESpecType != EST_None)
3687 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003688
3689 // Parse trailing-return-type.
3690 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3691 TrailingReturnType = ParseTrailingReturnType().get();
3692 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003693 }
3694
Chris Lattner371ed4e2008-04-06 06:57:35 +00003695 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003696 // int() -> no prototype, no '...'.
John McCall084e83d2011-03-24 11:26:52 +00003697 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003698 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003699 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003700 /*arglist*/ 0, 0,
3701 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003702 RefQualifierIsLValueRef,
3703 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003704 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003705 DynamicExceptions.data(),
3706 DynamicExceptionRanges.data(),
3707 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003708 NoexceptExpr.isUsable() ?
3709 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003710 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003711 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003712 attrs, EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003713 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003714 }
3715
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003716 // Alternatively, this parameter list may be an identifier list form for a
3717 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003718 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3719 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003720 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003721 // K&R identifier lists can't have typedefs as identifiers, per
3722 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003723 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003724 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003725
Steve Naroffb0486722009-01-28 19:16:40 +00003726 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003727 // normal declarators, not for abstract-declarators. Get the first
3728 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003729 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003730 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003731
3732 // Identifier lists follow a really simple grammar: the identifiers can
3733 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3734 // identifier lists are really rare in the brave new modern world, and it
3735 // is very common for someone to typo a type in a non-k&r style list. If
3736 // we are presented with something like: "void foo(intptr x, float y)",
3737 // we don't want to start parsing the function declarator as though it is
3738 // a K&R style declarator just because intptr is an invalid type.
3739 //
3740 // To handle this, we check to see if the token after the first identifier
3741 // is a "," or ")". Only if so, do we parse it as an identifier list.
3742 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3743 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3744 FirstTok.getIdentifierInfo(),
3745 FirstTok.getLocation(), D);
3746
3747 // If we get here, the code is invalid. Push the first identifier back
3748 // into the token stream and parse the first argument as an (invalid)
3749 // normal argument declarator.
3750 PP.EnterToken(Tok);
3751 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003752 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003753 }
Mike Stump11289f42009-09-09 15:08:12 +00003754
Chris Lattner371ed4e2008-04-06 06:57:35 +00003755 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003756
Chris Lattner371ed4e2008-04-06 06:57:35 +00003757 // Build up an array of information about the parsed arguments.
3758 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003759
3760 // Enter function-declaration scope, limiting any declarators to the
3761 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003762 ParseScope PrototypeScope(this,
3763 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003764
Chris Lattner371ed4e2008-04-06 06:57:35 +00003765 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003766 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003767 while (1) {
3768 if (Tok.is(tok::ellipsis)) {
3769 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003770 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003771 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003772 }
Mike Stump11289f42009-09-09 15:08:12 +00003773
Chris Lattner371ed4e2008-04-06 06:57:35 +00003774 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003775 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00003776 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003777
3778 // Skip any Microsoft attributes before a param.
3779 if (getLang().Microsoft && Tok.is(tok::l_square))
3780 ParseMicrosoftAttributes(DS.getAttributes());
3781
3782 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003783
3784 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00003785 // Take them so that we only apply the attributes to the first parameter.
3786 DS.takeAttributesFrom(attrs);
3787
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003788 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003789
Chris Lattner371ed4e2008-04-06 06:57:35 +00003790 // Parse the declarator. This is "PrototypeContext", because we must
3791 // accept either 'declarator' or 'abstract-declarator' here.
3792 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3793 ParseDeclarator(ParmDecl);
3794
3795 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00003796 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003797
Chris Lattner371ed4e2008-04-06 06:57:35 +00003798 // Remember this parsed parameter in ParamInfo.
3799 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003800
Douglas Gregor4d87df52008-12-16 21:30:33 +00003801 // DefArgToks is used when the parsing of default arguments needs
3802 // to be delayed.
3803 CachedTokens *DefArgToks = 0;
3804
Chris Lattner371ed4e2008-04-06 06:57:35 +00003805 // If no parameter was specified, verify that *something* was specified,
3806 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003807 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3808 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003809 // Completely missing, emit error.
3810 Diag(DSStart, diag::err_missing_param);
3811 } else {
3812 // Otherwise, we have something. Add it and let semantic analysis try
3813 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003814
Chris Lattner371ed4e2008-04-06 06:57:35 +00003815 // Inform the actions module about the parameter declarator, so it gets
3816 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003817 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003818
3819 // Parse the default argument, if any. We parse the default
3820 // arguments in all dialects; the semantic analysis in
3821 // ActOnParamDefaultArgument will reject the default argument in
3822 // C.
3823 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003824 SourceLocation EqualLoc = Tok.getLocation();
3825
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003826 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003827 if (D.getContext() == Declarator::MemberContext) {
3828 // If we're inside a class definition, cache the tokens
3829 // corresponding to the default argument. We'll actually parse
3830 // them when we see the end of the class definition.
3831 // FIXME: Templates will require something similar.
3832 // FIXME: Can we use a smart pointer for Toks?
3833 DefArgToks = new CachedTokens;
3834
Mike Stump11289f42009-09-09 15:08:12 +00003835 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003836 /*StopAtSemi=*/true,
3837 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003838 delete DefArgToks;
3839 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003840 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003841 } else {
3842 // Mark the end of the default argument so that we know when to
3843 // stop when we parse it later on.
3844 Token DefArgEnd;
3845 DefArgEnd.startToken();
3846 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3847 DefArgEnd.setLocation(Tok.getLocation());
3848 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003849 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003850 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003851 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003852 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003853 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003854 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003855
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003856 // The argument isn't actually potentially evaluated unless it is
3857 // used.
3858 EnterExpressionEvaluationContext Eval(Actions,
3859 Sema::PotentiallyEvaluatedIfUsed);
3860
John McCalldadc5752010-08-24 06:29:42 +00003861 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003862 if (DefArgResult.isInvalid()) {
3863 Actions.ActOnParamDefaultArgumentError(Param);
3864 SkipUntil(tok::comma, tok::r_paren, true, true);
3865 } else {
3866 // Inform the actions module about the default argument
3867 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003868 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003869 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003870 }
3871 }
Mike Stump11289f42009-09-09 15:08:12 +00003872
3873 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3874 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003875 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003876 }
3877
3878 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003879 if (Tok.isNot(tok::comma)) {
3880 if (Tok.is(tok::ellipsis)) {
3881 IsVariadic = true;
3882 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3883
3884 if (!getLang().CPlusPlus) {
3885 // We have ellipsis without a preceding ',', which is ill-formed
3886 // in C. Complain and provide the fix.
3887 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003888 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003889 }
3890 }
3891
3892 break;
3893 }
Mike Stump11289f42009-09-09 15:08:12 +00003894
Chris Lattner371ed4e2008-04-06 06:57:35 +00003895 // Consume the comma.
3896 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003897 }
Mike Stump11289f42009-09-09 15:08:12 +00003898
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003899 // If we have the closing ')', eat it.
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003900 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003901
John McCall084e83d2011-03-24 11:26:52 +00003902 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003903 SourceLocation RefQualifierLoc;
3904 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003905 ExceptionSpecificationType ESpecType = EST_None;
3906 SourceRange ESpecRange;
3907 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3908 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3909 ExprResult NoexceptExpr;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003910
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003911 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003912 MaybeParseCXX0XAttributes(attrs);
3913
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003914 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003915 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003916 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003917 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003918
Douglas Gregor54992352011-01-26 03:43:54 +00003919 // Parse ref-qualifier[opt]
3920 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3921 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003922 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003923
3924 RefQualifierIsLValueRef = Tok.is(tok::amp);
3925 RefQualifierLoc = ConsumeToken();
3926 EndLoc = RefQualifierLoc;
3927 }
3928
Sebastian Redl965b0e32011-03-05 14:45:16 +00003929 // FIXME: We should leave the prototype scope before parsing the exception
3930 // specification, and then reenter it when parsing the trailing return type.
3931 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3932
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003933 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003934 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3935 DynamicExceptions,
3936 DynamicExceptionRanges,
3937 NoexceptExpr);
3938 if (ESpecType != EST_None)
3939 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003940
3941 // Parse trailing-return-type.
3942 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3943 TrailingReturnType = ParseTrailingReturnType().get();
3944 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003945 }
3946
Douglas Gregor7fb25412010-10-01 18:44:50 +00003947 // Leave prototype scope.
3948 PrototypeScope.Exit();
3949
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003950 // Remember that we parsed a function type, and remember the attributes.
John McCall084e83d2011-03-24 11:26:52 +00003951 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003952 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003953 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003954 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003955 RefQualifierIsLValueRef,
3956 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003957 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003958 DynamicExceptions.data(),
3959 DynamicExceptionRanges.data(),
3960 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003961 NoexceptExpr.isUsable() ?
3962 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003963 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003964 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003965 attrs, EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003966}
Chris Lattneracd58a32006-08-06 17:24:14 +00003967
Chris Lattner6c940e62008-04-06 06:34:08 +00003968/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3969/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003970/// first identifier has already been consumed, and the current token is the
3971/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003972///
3973/// identifier-list: [C99 6.7.5]
3974/// identifier
3975/// identifier-list ',' identifier
3976///
3977void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003978 IdentifierInfo *FirstIdent,
3979 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003980 Declarator &D) {
3981 // Build up an array of information about the parsed arguments.
3982 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3983 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003984
Chris Lattner6c940e62008-04-06 06:34:08 +00003985 // If there was no identifier specified for the declarator, either we are in
3986 // an abstract-declarator, or we are in a parameter declarator which was found
3987 // to be abstract. In abstract-declarators, identifier lists are not valid:
3988 // diagnose this.
3989 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003990 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003991
Chris Lattner9453ab82010-05-14 17:23:36 +00003992 // The first identifier was already read, and is known to be the first
3993 // identifier in the list. Remember this identifier in ParamInfo.
3994 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003995 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003996
Chris Lattner6c940e62008-04-06 06:34:08 +00003997 while (Tok.is(tok::comma)) {
3998 // Eat the comma.
3999 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004000
Chris Lattner9186f552008-04-06 06:39:19 +00004001 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00004002 if (Tok.isNot(tok::identifier)) {
4003 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00004004 SkipUntil(tok::r_paren);
4005 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00004006 }
Chris Lattner67b450c2008-04-06 06:47:48 +00004007
Chris Lattner6c940e62008-04-06 06:34:08 +00004008 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00004009
4010 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00004011 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00004012 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00004013
Chris Lattner6c940e62008-04-06 06:34:08 +00004014 // Verify that the argument identifier has not already been mentioned.
4015 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004016 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00004017 } else {
4018 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00004019 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00004020 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00004021 0));
Chris Lattner9186f552008-04-06 06:39:19 +00004022 }
Mike Stump11289f42009-09-09 15:08:12 +00004023
Chris Lattner6c940e62008-04-06 06:34:08 +00004024 // Eat the identifier.
4025 ConsumeToken();
4026 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004027
4028 // If we have the closing ')', eat it and we're done.
4029 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4030
Chris Lattner9186f552008-04-06 06:39:19 +00004031 // Remember that we parsed a function type, and remember the attributes. This
4032 // function type is always a K&R style function type, which is not varargs and
4033 // has no prototype.
John McCall084e83d2011-03-24 11:26:52 +00004034 ParsedAttributes attrs(AttrFactory);
4035 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00004036 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00004037 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00004038 /*TypeQuals*/0,
Douglas Gregor54992352011-01-26 03:43:54 +00004039 true, SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00004040 EST_None, SourceLocation(), 0, 0,
4041 0, 0, LParenLoc, RLoc, D),
John McCall084e83d2011-03-24 11:26:52 +00004042 attrs, RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00004043}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004044
Chris Lattnere8074e62006-08-06 18:30:15 +00004045/// [C90] direct-declarator '[' constant-expression[opt] ']'
4046/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4047/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4048/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4049/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4050void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00004051 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00004052
Chris Lattner84a11622008-12-18 07:27:21 +00004053 // C array syntax has many features, but by-far the most common is [] and [4].
4054 // This code does a fast path to handle some of the most obvious cases.
4055 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004056 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004057 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004058 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004059
Chris Lattner84a11622008-12-18 07:27:21 +00004060 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004061 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004062 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00004063 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004064 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004065 return;
4066 } else if (Tok.getKind() == tok::numeric_constant &&
4067 GetLookAheadToken(1).is(tok::r_square)) {
4068 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00004069 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00004070 ConsumeToken();
4071
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004072 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004073 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004074 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004075
Chris Lattner84a11622008-12-18 07:27:21 +00004076 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004077 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004078 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00004079 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004080 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004081 return;
4082 }
Mike Stump11289f42009-09-09 15:08:12 +00004083
Chris Lattnere8074e62006-08-06 18:30:15 +00004084 // If valid, this location is the position where we read the 'static' keyword.
4085 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004086 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004087 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004088
Chris Lattnere8074e62006-08-06 18:30:15 +00004089 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004090 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004091 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004092 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004093
Chris Lattnere8074e62006-08-06 18:30:15 +00004094 // If we haven't already read 'static', check to see if there is one after the
4095 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004096 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004097 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004098
Chris Lattnere8074e62006-08-06 18:30:15 +00004099 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004100 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004101 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004102
Chris Lattner521ff2b2008-04-06 05:26:30 +00004103 // Handle the case where we have '[*]' as the array size. However, a leading
4104 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4105 // the the token after the star is a ']'. Since stars in arrays are
4106 // infrequent, use of lookahead is not costly here.
4107 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004108 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004109
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004110 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004111 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004112 StaticLoc = SourceLocation(); // Drop the static.
4113 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004114 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004115 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004116 // Note, in C89, this production uses the constant-expr production instead
4117 // of assignment-expr. The only difference is that assignment-expr allows
4118 // things like '=' and '*='. Sema rejects these in C89 mode because they
4119 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004120
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004121 // Parse the constant-expression or assignment-expression now (depending
4122 // on dialect).
4123 if (getLang().CPlusPlus)
4124 NumElements = ParseConstantExpression();
4125 else
4126 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004127 }
Mike Stump11289f42009-09-09 15:08:12 +00004128
Chris Lattner62591722006-08-12 18:40:58 +00004129 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004130 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004131 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004132 // If the expression was invalid, skip it.
4133 SkipUntil(tok::r_square);
4134 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004135 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004136
4137 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4138
John McCall084e83d2011-03-24 11:26:52 +00004139 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004140 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004141
Chris Lattner84a11622008-12-18 07:27:21 +00004142 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004143 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004144 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004145 NumElements.release(),
4146 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004147 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004148}
4149
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004150/// [GNU] typeof-specifier:
4151/// typeof ( expressions )
4152/// typeof ( type-name )
4153/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004154///
4155void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004156 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004157 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004158 SourceLocation StartLoc = ConsumeToken();
4159
John McCalle8595032010-01-13 20:03:27 +00004160 const bool hasParens = Tok.is(tok::l_paren);
4161
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004162 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004163 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004164 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004165 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4166 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004167 if (hasParens)
4168 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004169
4170 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004171 // FIXME: Not accurate, the range gets one token more than it should.
4172 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004173 else
4174 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004175
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004176 if (isCastExpr) {
4177 if (!CastTy) {
4178 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004179 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004180 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004181
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004182 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004183 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004184 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4185 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004186 DiagID, CastTy))
4187 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004188 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004189 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004190
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004191 // If we get here, the operand to the typeof was an expresion.
4192 if (Operand.isInvalid()) {
4193 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004194 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004195 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004196
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004197 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004198 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004199 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4200 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004201 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004202 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004203}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004204
4205
4206/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4207/// from TryAltiVecVectorToken.
4208bool Parser::TryAltiVecVectorTokenOutOfLine() {
4209 Token Next = NextToken();
4210 switch (Next.getKind()) {
4211 default: return false;
4212 case tok::kw_short:
4213 case tok::kw_long:
4214 case tok::kw_signed:
4215 case tok::kw_unsigned:
4216 case tok::kw_void:
4217 case tok::kw_char:
4218 case tok::kw_int:
4219 case tok::kw_float:
4220 case tok::kw_double:
4221 case tok::kw_bool:
4222 case tok::kw___pixel:
4223 Tok.setKind(tok::kw___vector);
4224 return true;
4225 case tok::identifier:
4226 if (Next.getIdentifierInfo() == Ident_pixel) {
4227 Tok.setKind(tok::kw___vector);
4228 return true;
4229 }
4230 return false;
4231 }
4232}
4233
4234bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4235 const char *&PrevSpec, unsigned &DiagID,
4236 bool &isInvalid) {
4237 if (Tok.getIdentifierInfo() == Ident_vector) {
4238 Token Next = NextToken();
4239 switch (Next.getKind()) {
4240 case tok::kw_short:
4241 case tok::kw_long:
4242 case tok::kw_signed:
4243 case tok::kw_unsigned:
4244 case tok::kw_void:
4245 case tok::kw_char:
4246 case tok::kw_int:
4247 case tok::kw_float:
4248 case tok::kw_double:
4249 case tok::kw_bool:
4250 case tok::kw___pixel:
4251 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4252 return true;
4253 case tok::identifier:
4254 if (Next.getIdentifierInfo() == Ident_pixel) {
4255 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4256 return true;
4257 }
4258 break;
4259 default:
4260 break;
4261 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004262 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004263 DS.isTypeAltiVecVector()) {
4264 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4265 return true;
4266 }
4267 return false;
4268}