blob: 24a487c89c35df3e328ff7550f929de400b4a9a6 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000021#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// C99 6.7: Declarations.
26//===----------------------------------------------------------------------===//
27
Chris Lattnerf5fbd792006-08-10 23:56:11 +000028/// ParseTypeName
29/// type-name: [C99 6.7.6]
30/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000031///
32/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000033TypeResult Parser::ParseTypeName(SourceRange *Range,
34 Declarator::TheContext Context) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000035 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000036 DeclSpec DS(AttrFactory);
Chris Lattner1890ac82006-08-13 01:16:23 +000037 ParseSpecifierQualifierList(DS);
Sebastian Redld6434562009-05-29 18:02:33 +000038
Chris Lattnerf5fbd792006-08-10 23:56:11 +000039 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000040 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000041 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000042 if (Range)
43 *Range = DeclaratorInfo.getSourceRange();
44
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000045 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000046 return true;
47
Douglas Gregor0be31a22010-07-02 17:43:08 +000048 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000049}
50
Alexis Hunt96d5c762009-11-21 08:43:09 +000051/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000052///
53/// [GNU] attributes:
54/// attribute
55/// attributes attribute
56///
57/// [GNU] attribute:
58/// '__attribute__' '(' '(' attribute-list ')' ')'
59///
60/// [GNU] attribute-list:
61/// attrib
62/// attribute_list ',' attrib
63///
64/// [GNU] attrib:
65/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000066/// attrib-name
67/// attrib-name '(' identifier ')'
68/// attrib-name '(' identifier ',' nonempty-expr-list ')'
69/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000070///
Steve Naroff0f2fe172007-06-01 17:11:19 +000071/// [GNU] attrib-name:
72/// identifier
73/// typespec
74/// typequal
75/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000076///
Steve Naroff0f2fe172007-06-01 17:11:19 +000077/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000078/// token lookahead. Comment from gcc: "If they start with an identifier
79/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +000080/// start with that identifier; otherwise they are an expression list."
81///
82/// At the moment, I am not doing 2 token lookahead. I am also unaware of
83/// any attributes that don't work (based on my limited testing). Most
84/// attributes are very simple in practice. Until we find a bug, I don't see
85/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000086
John McCall53fa7142010-12-24 02:08:15 +000087void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
88 SourceLocation *endLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +000089 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattner76c72282007-10-09 17:33:22 +000091 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000092 ConsumeToken();
93 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
94 "attribute")) {
95 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +000096 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +000097 }
98 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
99 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000100 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000101 }
102 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000103 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
104 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000105
106 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000107 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
108 ConsumeToken();
109 continue;
110 }
111 // we have an identifier or declaration specifier (const, int, etc.)
112 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
113 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000114
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000115 // Availability attributes have their own grammar.
116 if (AttrName->isStr("availability"))
117 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Douglas Gregora2f49452010-03-16 19:09:18 +0000118 // check if we have a "parameterized" attribute
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000119 else if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000120 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000121
Chris Lattner76c72282007-10-09 17:33:22 +0000122 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000123 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
124 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000125
126 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000127 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000128 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000129 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
130 ParmName, ParmLoc, 0, 0);
Chris Lattner76c72282007-10-09 17:33:22 +0000131 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000132 ConsumeToken();
133 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000134 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000135 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000136
Steve Naroff0f2fe172007-06-01 17:11:19 +0000137 // now parse the non-empty comma separated list of expressions
138 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000139 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000140 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 ArgExprsOk = false;
142 SkipUntil(tok::r_paren);
143 break;
144 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000145 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000146 }
Chris Lattner76c72282007-10-09 17:33:22 +0000147 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000148 break;
149 ConsumeToken(); // Eat the comma, move to the next argument
150 }
Chris Lattner76c72282007-10-09 17:33:22 +0000151 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000152 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000153 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
154 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000155 }
156 }
157 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000158 switch (Tok.getKind()) {
159 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000160 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000161 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000162 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000163 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
164 0, SourceLocation(), 0, 0);
Nate Begemanf2758702009-06-26 06:32:41 +0000165 break;
166 case tok::kw_char:
167 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000168 case tok::kw_char16_t:
169 case tok::kw_char32_t:
Nate Begemanf2758702009-06-26 06:32:41 +0000170 case tok::kw_bool:
171 case tok::kw_short:
172 case tok::kw_int:
173 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +0000174 case tok::kw___int64:
Nate Begemanf2758702009-06-26 06:32:41 +0000175 case tok::kw_signed:
176 case tok::kw_unsigned:
177 case tok::kw_float:
178 case tok::kw_double:
179 case tok::kw_void:
John McCall53fa7142010-12-24 02:08:15 +0000180 case tok::kw_typeof: {
181 AttributeList *attr
John McCall084e83d2011-03-24 11:26:52 +0000182 = attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
183 0, SourceLocation(), 0, 0);
John McCall53fa7142010-12-24 02:08:15 +0000184 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian9d7d3d82010-08-17 23:19:16 +0000185 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begemanf2758702009-06-26 06:32:41 +0000186 // If it's a builtin type name, eat it and expect a rparen
187 // __attribute__(( vec_type_hint(char) ))
188 ConsumeToken();
Nate Begemanf2758702009-06-26 06:32:41 +0000189 if (Tok.is(tok::r_paren))
190 ConsumeParen();
191 break;
John McCall53fa7142010-12-24 02:08:15 +0000192 }
Nate Begemanf2758702009-06-26 06:32:41 +0000193 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000194 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000195 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000196 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000197
Steve Naroff0f2fe172007-06-01 17:11:19 +0000198 // now parse the list of expressions
199 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000200 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000201 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000202 ArgExprsOk = false;
203 SkipUntil(tok::r_paren);
204 break;
205 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000206 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000207 }
Chris Lattner76c72282007-10-09 17:33:22 +0000208 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000209 break;
210 ConsumeToken(); // Eat the comma, move to the next argument
211 }
212 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000213 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000214 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000215 attrs.addNew(AttrName, AttrNameLoc, 0,
216 AttrNameLoc, 0, SourceLocation(),
217 ArgExprs.take(), ArgExprs.size());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000218 }
Nate Begemanf2758702009-06-26 06:32:41 +0000219 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000220 }
221 }
222 } else {
John McCall084e83d2011-03-24 11:26:52 +0000223 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
224 0, SourceLocation(), 0, 0);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000225 }
226 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000227 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000228 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000229 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000230 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
231 SkipUntil(tok::r_paren, false);
232 }
John McCall53fa7142010-12-24 02:08:15 +0000233 if (endLoc)
234 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000235 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000236}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000237
Eli Friedman06de2b52009-06-08 07:21:15 +0000238/// ParseMicrosoftDeclSpec - Parse an __declspec construct
239///
240/// [MS] decl-specifier:
241/// __declspec ( extended-decl-modifier-seq )
242///
243/// [MS] extended-decl-modifier-seq:
244/// extended-decl-modifier[opt]
245/// extended-decl-modifier extended-decl-modifier-seq
246
John McCall53fa7142010-12-24 02:08:15 +0000247void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000248 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000249
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000250 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000251 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
252 "declspec")) {
253 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000254 return;
Eli Friedman06de2b52009-06-08 07:21:15 +0000255 }
Eli Friedman53339e02009-06-08 23:27:34 +0000256 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000257 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
258 SourceLocation AttrNameLoc = ConsumeToken();
259 if (Tok.is(tok::l_paren)) {
260 ConsumeParen();
261 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
262 // correctly.
John McCalldadc5752010-08-24 06:29:42 +0000263 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000264 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000265 Expr *ExprList = ArgExpr.take();
John McCall084e83d2011-03-24 11:26:52 +0000266 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
267 SourceLocation(), &ExprList, 1, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000268 }
269 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
270 SkipUntil(tok::r_paren, false);
271 } else {
John McCall084e83d2011-03-24 11:26:52 +0000272 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
273 0, SourceLocation(), 0, 0, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000274 }
275 }
276 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
277 SkipUntil(tok::r_paren, false);
John McCall53fa7142010-12-24 02:08:15 +0000278 return;
Eli Friedman53339e02009-06-08 23:27:34 +0000279}
280
John McCall53fa7142010-12-24 02:08:15 +0000281void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000282 // Treat these like attributes
283 // FIXME: Allow Sema to distinguish between these and real attributes!
284 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000285 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
286 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000287 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
288 SourceLocation AttrNameLoc = ConsumeToken();
289 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
290 // FIXME: Support these properly!
291 continue;
John McCall084e83d2011-03-24 11:26:52 +0000292 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
293 SourceLocation(), 0, 0, true);
Eli Friedman53339e02009-06-08 23:27:34 +0000294 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000295}
296
John McCall53fa7142010-12-24 02:08:15 +0000297void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000298 // Treat these like attributes
299 while (Tok.is(tok::kw___pascal)) {
300 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
301 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000302 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
303 SourceLocation(), 0, 0, true);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000304 }
John McCall53fa7142010-12-24 02:08:15 +0000305}
306
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000307void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
308 // Treat these like attributes
309 while (Tok.is(tok::kw___kernel)) {
310 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000311 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
312 AttrNameLoc, 0, AttrNameLoc, 0,
313 SourceLocation(), 0, 0, false);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000314 }
315}
316
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000317void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
318 SourceLocation Loc = Tok.getLocation();
319 switch(Tok.getKind()) {
320 // OpenCL qualifiers:
321 case tok::kw___private:
322 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000323 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000324 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000325 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000326 break;
327
328 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000329 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000330 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000331 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000332 break;
333
334 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000335 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000336 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000337 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000338 break;
339
340 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000341 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000342 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000343 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000344 break;
345
346 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000347 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000348 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000349 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000350 break;
351
352 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000353 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000354 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000355 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000356 break;
357
358 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000359 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000360 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000361 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000362 break;
363 default: break;
364 }
365}
366
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000367/// \brief Parse a version number.
368///
369/// version:
370/// simple-integer
371/// simple-integer ',' simple-integer
372/// simple-integer ',' simple-integer ',' simple-integer
373VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
374 Range = Tok.getLocation();
375
376 if (!Tok.is(tok::numeric_constant)) {
377 Diag(Tok, diag::err_expected_version);
378 SkipUntil(tok::comma, tok::r_paren, true, true, true);
379 return VersionTuple();
380 }
381
382 // Parse the major (and possibly minor and subminor) versions, which
383 // are stored in the numeric constant. We utilize a quirk of the
384 // lexer, which is that it handles something like 1.2.3 as a single
385 // numeric constant, rather than two separate tokens.
386 llvm::SmallString<512> Buffer;
387 Buffer.resize(Tok.getLength()+1);
388 const char *ThisTokBegin = &Buffer[0];
389
390 // Get the spelling of the token, which eliminates trigraphs, etc.
391 bool Invalid = false;
392 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
393 if (Invalid)
394 return VersionTuple();
395
396 // Parse the major version.
397 unsigned AfterMajor = 0;
398 unsigned Major = 0;
399 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
400 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
401 ++AfterMajor;
402 }
403
404 if (AfterMajor == 0) {
405 Diag(Tok, diag::err_expected_version);
406 SkipUntil(tok::comma, tok::r_paren, true, true, true);
407 return VersionTuple();
408 }
409
410 if (AfterMajor == ActualLength) {
411 ConsumeToken();
412
413 // We only had a single version component.
414 if (Major == 0) {
415 Diag(Tok, diag::err_zero_version);
416 return VersionTuple();
417 }
418
419 return VersionTuple(Major);
420 }
421
422 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
423 Diag(Tok, diag::err_expected_version);
424 SkipUntil(tok::comma, tok::r_paren, true, true, true);
425 return VersionTuple();
426 }
427
428 // Parse the minor version.
429 unsigned AfterMinor = AfterMajor + 1;
430 unsigned Minor = 0;
431 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
432 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
433 ++AfterMinor;
434 }
435
436 if (AfterMinor == ActualLength) {
437 ConsumeToken();
438
439 // We had major.minor.
440 if (Major == 0 && Minor == 0) {
441 Diag(Tok, diag::err_zero_version);
442 return VersionTuple();
443 }
444
445 return VersionTuple(Major, Minor);
446 }
447
448 // If what follows is not a '.', we have a problem.
449 if (ThisTokBegin[AfterMinor] != '.') {
450 Diag(Tok, diag::err_expected_version);
451 SkipUntil(tok::comma, tok::r_paren, true, true, true);
452 return VersionTuple();
453 }
454
455 // Parse the subminor version.
456 unsigned AfterSubminor = AfterMinor + 1;
457 unsigned Subminor = 0;
458 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
459 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
460 ++AfterSubminor;
461 }
462
463 if (AfterSubminor != ActualLength) {
464 Diag(Tok, diag::err_expected_version);
465 SkipUntil(tok::comma, tok::r_paren, true, true, true);
466 return VersionTuple();
467 }
468 ConsumeToken();
469 return VersionTuple(Major, Minor, Subminor);
470}
471
472/// \brief Parse the contents of the "availability" attribute.
473///
474/// availability-attribute:
475/// 'availability' '(' platform ',' version-arg-list ')'
476///
477/// platform:
478/// identifier
479///
480/// version-arg-list:
481/// version-arg
482/// version-arg ',' version-arg-list
483///
484/// version-arg:
485/// 'introduced' '=' version
486/// 'deprecated' '=' version
487/// 'removed' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000488/// 'unavailable'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000489void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
490 SourceLocation AvailabilityLoc,
491 ParsedAttributes &attrs,
492 SourceLocation *endLoc) {
493 SourceLocation PlatformLoc;
494 IdentifierInfo *Platform = 0;
495
496 enum { Introduced, Deprecated, Obsoleted, Unknown };
497 AvailabilityChange Changes[Unknown];
498
499 // Opening '('.
500 SourceLocation LParenLoc;
501 if (!Tok.is(tok::l_paren)) {
502 Diag(Tok, diag::err_expected_lparen);
503 return;
504 }
505 LParenLoc = ConsumeParen();
506
507 // Parse the platform name,
508 if (Tok.isNot(tok::identifier)) {
509 Diag(Tok, diag::err_availability_expected_platform);
510 SkipUntil(tok::r_paren);
511 return;
512 }
513 Platform = Tok.getIdentifierInfo();
514 PlatformLoc = ConsumeToken();
515
516 // Parse the ',' following the platform name.
517 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
518 return;
519
520 // If we haven't grabbed the pointers for the identifiers
521 // "introduced", "deprecated", and "obsoleted", do so now.
522 if (!Ident_introduced) {
523 Ident_introduced = PP.getIdentifierInfo("introduced");
524 Ident_deprecated = PP.getIdentifierInfo("deprecated");
525 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000526 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000527 }
528
529 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000530 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000531 do {
532 if (Tok.isNot(tok::identifier)) {
533 Diag(Tok, diag::err_availability_expected_change);
534 SkipUntil(tok::r_paren);
535 return;
536 }
537 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
538 SourceLocation KeywordLoc = ConsumeToken();
539
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000540 if (Keyword == Ident_unavailable) {
541 if (UnavailableLoc.isValid()) {
542 Diag(KeywordLoc, diag::err_availability_redundant)
543 << Keyword << SourceRange(UnavailableLoc);
544 }
545 UnavailableLoc = KeywordLoc;
546
547 if (Tok.isNot(tok::comma))
548 break;
549
550 ConsumeToken();
551 continue;
552 }
553
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000554 if (Tok.isNot(tok::equal)) {
555 Diag(Tok, diag::err_expected_equal_after)
556 << Keyword;
557 SkipUntil(tok::r_paren);
558 return;
559 }
560 ConsumeToken();
561
562 SourceRange VersionRange;
563 VersionTuple Version = ParseVersionTuple(VersionRange);
564
565 if (Version.empty()) {
566 SkipUntil(tok::r_paren);
567 return;
568 }
569
570 unsigned Index;
571 if (Keyword == Ident_introduced)
572 Index = Introduced;
573 else if (Keyword == Ident_deprecated)
574 Index = Deprecated;
575 else if (Keyword == Ident_obsoleted)
576 Index = Obsoleted;
577 else
578 Index = Unknown;
579
580 if (Index < Unknown) {
581 if (!Changes[Index].KeywordLoc.isInvalid()) {
582 Diag(KeywordLoc, diag::err_availability_redundant)
583 << Keyword
584 << SourceRange(Changes[Index].KeywordLoc,
585 Changes[Index].VersionRange.getEnd());
586 }
587
588 Changes[Index].KeywordLoc = KeywordLoc;
589 Changes[Index].Version = Version;
590 Changes[Index].VersionRange = VersionRange;
591 } else {
592 Diag(KeywordLoc, diag::err_availability_unknown_change)
593 << Keyword << VersionRange;
594 }
595
596 if (Tok.isNot(tok::comma))
597 break;
598
599 ConsumeToken();
600 } while (true);
601
602 // Closing ')'.
603 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
604 if (RParenLoc.isInvalid())
605 return;
606
607 if (endLoc)
608 *endLoc = RParenLoc;
609
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000610 // The 'unavailable' availability cannot be combined with any other
611 // availability changes. Make sure that hasn't happened.
612 if (UnavailableLoc.isValid()) {
613 bool Complained = false;
614 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
615 if (Changes[Index].KeywordLoc.isValid()) {
616 if (!Complained) {
617 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
618 << SourceRange(Changes[Index].KeywordLoc,
619 Changes[Index].VersionRange.getEnd());
620 Complained = true;
621 }
622
623 // Clear out the availability.
624 Changes[Index] = AvailabilityChange();
625 }
626 }
627 }
628
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000629 // Record this attribute
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000630 attrs.addNew(&Availability, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000631 0, SourceLocation(),
632 Platform, PlatformLoc,
633 Changes[Introduced],
634 Changes[Deprecated],
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000635 Changes[Obsoleted],
636 UnavailableLoc, false, false);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000637}
638
John McCall53fa7142010-12-24 02:08:15 +0000639void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
640 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
641 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000642}
643
Chris Lattner53361ac2006-08-10 05:19:57 +0000644/// ParseDeclaration - Parse a full 'declaration', which consists of
645/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000646/// 'Context' should be a Declarator::TheContext value. This returns the
647/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000648///
649/// declaration: [C99 6.7]
650/// block-declaration ->
651/// simple-declaration
652/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000653/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000654/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000655/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000656/// [C++] using-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000657/// [C++0x/C1X] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000658/// others... [FIXME]
659///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000660Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
661 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000662 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000663 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000664 ParenBraceBracketBalancer BalancerRAIIObj(*this);
665
John McCall48871652010-08-21 09:40:31 +0000666 Decl *SingleDecl = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000667 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000668 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000669 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000670 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000671 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000672 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000673 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000674 // Could be the start of an inline namespace. Allowed as an ext in C++03.
675 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +0000676 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000677 SourceLocation InlineLoc = ConsumeToken();
678 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
679 break;
680 }
John McCall53fa7142010-12-24 02:08:15 +0000681 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000682 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000683 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000684 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000685 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000686 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000687 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000688 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall53fa7142010-12-24 02:08:15 +0000689 DeclEnd, attrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000690 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000691 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000692 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000693 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000694 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000695 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000696 default:
John McCall53fa7142010-12-24 02:08:15 +0000697 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000698 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000699
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000700 // This routine returns a DeclGroup, if the thing we parsed only contains a
701 // single decl, convert it now.
702 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000703}
704
705/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
706/// declaration-specifiers init-declarator-list[opt] ';'
707///[C90/C++]init-declarator-list ';' [TODO]
708/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000709///
Richard Smith02e85f32011-04-14 22:09:26 +0000710/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
711/// attribute-specifier-seq[opt] type-specifier-seq declarator
712///
Chris Lattner32dc41c2009-03-29 17:27:48 +0000713/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000714/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +0000715///
716/// If FRI is non-null, we might be parsing a for-range-declaration instead
717/// of a simple-declaration. If we find that we are, we also parse the
718/// for-range-initializer, and place it here.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000719Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
720 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000721 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000722 ParsedAttributes &attrs,
Richard Smith02e85f32011-04-14 22:09:26 +0000723 bool RequireSemi,
724 ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000725 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000726 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000727 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000728
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000729 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000730 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000731 StmtResult R = Actions.ActOnVlaStmt(DS);
732 if (R.isUsable())
733 Stmts.push_back(R.release());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000734
Chris Lattner0e894622006-08-13 19:58:17 +0000735 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
736 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000737 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000738 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000739 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000740 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000741 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000742 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000743 }
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000744
745 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +0000746}
Mike Stump11289f42009-09-09 15:08:12 +0000747
John McCalld5a36322009-11-03 19:26:08 +0000748/// ParseDeclGroup - Having concluded that this is either a function
749/// definition or a group of object declarations, actually parse the
750/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000751Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
752 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000753 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +0000754 SourceLocation *DeclEnd,
755 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +0000756 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000757 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000758 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000759
John McCalld5a36322009-11-03 19:26:08 +0000760 // Bail out if the first declarator didn't seem well-formed.
761 if (!D.hasName() && !D.mayOmitIdentifier()) {
762 // Skip until ; or }.
763 SkipUntil(tok::r_brace, true, true);
764 if (Tok.is(tok::semi))
765 ConsumeToken();
766 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000767 }
Mike Stump11289f42009-09-09 15:08:12 +0000768
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000769 // Check to see if we have a function *definition* which must have a body.
770 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
771 // Look at the next token to make sure that this isn't a function
772 // declaration. We have to check this because __attribute__ might be the
773 // start of a function definition in GCC-extended K&R C.
774 !isDeclarationAfterDeclarator()) {
775
Chris Lattner13901342010-07-11 22:42:07 +0000776 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000777 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
778 Diag(Tok, diag::err_function_declared_typedef);
779
780 // Recover by treating the 'typedef' as spurious.
781 DS.ClearStorageClassSpecs();
782 }
783
John McCall48871652010-08-21 09:40:31 +0000784 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000785 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000786 }
787
788 if (isDeclarationSpecifier()) {
789 // If there is an invalid declaration specifier right after the function
790 // prototype, then we must be in a missing semicolon case where this isn't
791 // actually a body. Just fall through into the code that handles it as a
792 // prototype, and let the top-level code handle the erroneous declspec
793 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000794 } else {
795 Diag(Tok, diag::err_expected_fn_body);
796 SkipUntil(tok::semi);
797 return DeclGroupPtrTy();
798 }
799 }
800
Richard Smith02e85f32011-04-14 22:09:26 +0000801 if (ParseAttributesAfterDeclarator(D))
802 return DeclGroupPtrTy();
803
804 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
805 // must parse and analyze the for-range-initializer before the declaration is
806 // analyzed.
807 if (FRI && Tok.is(tok::colon)) {
808 FRI->ColonLoc = ConsumeToken();
809 // FIXME: handle braced-init-list here.
810 FRI->RangeExpr = ParseExpression();
811 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
812 Actions.ActOnCXXForRangeDecl(ThisDecl);
813 Actions.FinalizeDeclaration(ThisDecl);
814 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
815 }
816
John McCall48871652010-08-21 09:40:31 +0000817 llvm::SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +0000818 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall28a6aea2009-11-04 02:18:39 +0000819 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000820 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000821 DeclsInGroup.push_back(FirstDecl);
822
823 // If we don't have a comma, it is either the end of the list (a ';') or an
824 // error, bail out.
825 while (Tok.is(tok::comma)) {
826 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000827 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000828
829 // Parse the next declarator.
830 D.clear();
831
832 // Accept attributes in an init-declarator. In the first declarator in a
833 // declaration, these would be part of the declspec. In subsequent
834 // declarators, they become part of the declarator itself, so that they
835 // don't apply to declarators after *this* one. Examples:
836 // short __attribute__((common)) var; -> declspec
837 // short var __attribute__((common)); -> declarator
838 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +0000839 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +0000840
841 ParseDeclarator(D);
842
John McCall48871652010-08-21 09:40:31 +0000843 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000844 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000845 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000846 DeclsInGroup.push_back(ThisDecl);
847 }
848
849 if (DeclEnd)
850 *DeclEnd = Tok.getLocation();
851
852 if (Context != Declarator::ForContext &&
853 ExpectAndConsume(tok::semi,
854 Context == Declarator::FileContext
855 ? diag::err_invalid_token_after_toplevel_declarator
856 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000857 // Okay, there was no semicolon and one was expected. If we see a
858 // declaration specifier, just assume it was missing and continue parsing.
859 // Otherwise things are very confused and we skip to recover.
860 if (!isDeclarationSpecifier()) {
861 SkipUntil(tok::r_brace, true, true);
862 if (Tok.is(tok::semi))
863 ConsumeToken();
864 }
John McCalld5a36322009-11-03 19:26:08 +0000865 }
866
Douglas Gregor0be31a22010-07-02 17:43:08 +0000867 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000868 DeclsInGroup.data(),
869 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000870}
871
Richard Smith02e85f32011-04-14 22:09:26 +0000872/// Parse an optional simple-asm-expr and attributes, and attach them to a
873/// declarator. Returns true on an error.
874bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
875 // If a simple-asm-expr is present, parse it.
876 if (Tok.is(tok::kw_asm)) {
877 SourceLocation Loc;
878 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
879 if (AsmLabel.isInvalid()) {
880 SkipUntil(tok::semi, true, true);
881 return true;
882 }
883
884 D.setAsmLabel(AsmLabel.release());
885 D.SetRangeEnd(Loc);
886 }
887
888 MaybeParseGNUAttributes(D);
889 return false;
890}
891
Douglas Gregor23996282009-05-12 21:31:51 +0000892/// \brief Parse 'declaration' after parsing 'declaration-specifiers
893/// declarator'. This method parses the remainder of the declaration
894/// (including any attributes or initializer, among other things) and
895/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000896///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000897/// init-declarator: [C99 6.7]
898/// declarator
899/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000900/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
901/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000902/// [C++] declarator initializer[opt]
903///
904/// [C++] initializer:
905/// [C++] '=' initializer-clause
906/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000907/// [C++0x] '=' 'default' [TODO]
908/// [C++0x] '=' 'delete'
909///
910/// According to the standard grammar, =default and =delete are function
911/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000912///
John McCall48871652010-08-21 09:40:31 +0000913Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000914 const ParsedTemplateInfo &TemplateInfo) {
Richard Smith02e85f32011-04-14 22:09:26 +0000915 if (ParseAttributesAfterDeclarator(D))
916 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000917
Richard Smith02e85f32011-04-14 22:09:26 +0000918 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
919}
Mike Stump11289f42009-09-09 15:08:12 +0000920
Richard Smith02e85f32011-04-14 22:09:26 +0000921Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
922 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000923 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000924 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000925 switch (TemplateInfo.Kind) {
926 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000927 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000928 break;
929
930 case ParsedTemplateInfo::Template:
931 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000932 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000933 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000934 TemplateInfo.TemplateParams->data(),
935 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000936 D);
937 break;
938
939 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000940 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000941 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000942 TemplateInfo.ExternLoc,
943 TemplateInfo.TemplateLoc,
944 D);
945 if (ThisRes.isInvalid()) {
946 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000947 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000948 }
949
950 ThisDecl = ThisRes.get();
951 break;
952 }
953 }
Mike Stump11289f42009-09-09 15:08:12 +0000954
Richard Smith30482bc2011-02-20 03:19:35 +0000955 bool TypeContainsAuto =
956 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
957
Douglas Gregor23996282009-05-12 21:31:51 +0000958 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000959 if (isTokenEqualOrMistypedEqualEqual(
960 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000961 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000962 if (Tok.is(tok::kw_delete)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000963 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000964
965 if (!getLang().CPlusPlus0x)
966 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
967
Douglas Gregor23996282009-05-12 21:31:51 +0000968 Actions.SetDeclDeleted(ThisDecl, DelLoc);
969 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000970 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
971 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000972 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000973 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000974
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000975 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000976 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000977 ConsumeCodeCompletionToken();
978 SkipUntil(tok::comma, true, true);
979 return ThisDecl;
980 }
981
John McCalldadc5752010-08-24 06:29:42 +0000982 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000983
John McCall1f4ee7b2009-12-19 09:28:58 +0000984 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000985 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000986 ExitScope();
987 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000988
Douglas Gregor23996282009-05-12 21:31:51 +0000989 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000990 SkipUntil(tok::comma, true, true);
991 Actions.ActOnInitializerError(ThisDecl);
992 } else
Richard Smith30482bc2011-02-20 03:19:35 +0000993 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
994 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000995 }
996 } else if (Tok.is(tok::l_paren)) {
997 // Parse C++ direct initializer: '(' expression-list ')'
998 SourceLocation LParenLoc = ConsumeParen();
999 ExprVector Exprs(Actions);
1000 CommaLocsTy CommaLocs;
1001
Douglas Gregor613bf102009-12-22 17:47:17 +00001002 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1003 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001004 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001005 }
1006
Douglas Gregor23996282009-05-12 21:31:51 +00001007 if (ParseExpressionList(Exprs, CommaLocs)) {
1008 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001009
1010 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001011 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001012 ExitScope();
1013 }
Douglas Gregor23996282009-05-12 21:31:51 +00001014 } else {
1015 // Match the ')'.
1016 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1017
1018 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1019 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001020
1021 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001022 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001023 ExitScope();
1024 }
1025
Douglas Gregor23996282009-05-12 21:31:51 +00001026 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1027 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +00001028 RParenLoc,
1029 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001030 }
1031 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001032 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001033 }
1034
Richard Smithb2bc2e62011-02-21 20:05:19 +00001035 Actions.FinalizeDeclaration(ThisDecl);
1036
Douglas Gregor23996282009-05-12 21:31:51 +00001037 return ThisDecl;
1038}
1039
Chris Lattner1890ac82006-08-13 01:16:23 +00001040/// ParseSpecifierQualifierList
1041/// specifier-qualifier-list:
1042/// type-specifier specifier-qualifier-list[opt]
1043/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001044/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001045///
1046void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
1047 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1048 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +00001049 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001050
Chris Lattner1890ac82006-08-13 01:16:23 +00001051 // Validate declspec for type-name.
1052 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +00001053 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +00001054 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +00001055 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +00001056
Chris Lattner1b22eed2006-11-28 05:12:07 +00001057 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001058 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001059 if (DS.getStorageClassSpecLoc().isValid())
1060 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1061 else
1062 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001063 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001064 }
Mike Stump11289f42009-09-09 15:08:12 +00001065
Chris Lattner1b22eed2006-11-28 05:12:07 +00001066 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001067 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001068 if (DS.isInlineSpecified())
1069 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1070 if (DS.isVirtualSpecified())
1071 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1072 if (DS.isExplicitSpecified())
1073 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001074 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001075 }
1076}
Chris Lattner53361ac2006-08-10 05:19:57 +00001077
Chris Lattner6cc055a2009-04-12 20:42:31 +00001078/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1079/// specified token is valid after the identifier in a declarator which
1080/// immediately follows the declspec. For example, these things are valid:
1081///
1082/// int x [ 4]; // direct-declarator
1083/// int x ( int y); // direct-declarator
1084/// int(int x ) // direct-declarator
1085/// int x ; // simple-declaration
1086/// int x = 17; // init-declarator-list
1087/// int x , y; // init-declarator-list
1088/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001089/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001090/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001091///
1092/// This is not, because 'x' does not immediately follow the declspec (though
1093/// ')' happens to be valid anyway).
1094/// int (x)
1095///
1096static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1097 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1098 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001099 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001100}
1101
Chris Lattner20a0c612009-04-14 21:34:55 +00001102
1103/// ParseImplicitInt - This method is called when we have an non-typename
1104/// identifier in a declspec (which normally terminates the decl spec) when
1105/// the declspec has no type specifier. In this case, the declspec is either
1106/// malformed or is "implicit int" (in K&R and C89).
1107///
1108/// This method handles diagnosing this prettily and returns false if the
1109/// declspec is done being processed. If it recovers and thinks there may be
1110/// other pieces of declspec after it, it returns true.
1111///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001112bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001113 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001114 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001115 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001116
Chris Lattner20a0c612009-04-14 21:34:55 +00001117 SourceLocation Loc = Tok.getLocation();
1118 // If we see an identifier that is not a type name, we normally would
1119 // parse it as the identifer being declared. However, when a typename
1120 // is typo'd or the definition is not included, this will incorrectly
1121 // parse the typename as the identifier name and fall over misparsing
1122 // later parts of the diagnostic.
1123 //
1124 // As such, we try to do some look-ahead in cases where this would
1125 // otherwise be an "implicit-int" case to see if this is invalid. For
1126 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1127 // an identifier with implicit int, we'd get a parse error because the
1128 // next token is obviously invalid for a type. Parse these as a case
1129 // with an invalid type specifier.
1130 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001131
Chris Lattner20a0c612009-04-14 21:34:55 +00001132 // Since we know that this either implicit int (which is rare) or an
1133 // error, we'd do lookahead to try to do better recovery.
1134 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1135 // If this token is valid for implicit int, e.g. "static x = 4", then
1136 // we just avoid eating the identifier, so it will be parsed as the
1137 // identifier in the declarator.
1138 return false;
1139 }
Mike Stump11289f42009-09-09 15:08:12 +00001140
Chris Lattner20a0c612009-04-14 21:34:55 +00001141 // Otherwise, if we don't consume this token, we are going to emit an
1142 // error anyway. Try to recover from various common problems. Check
1143 // to see if this was a reference to a tag name without a tag specified.
1144 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001145 //
1146 // C++ doesn't need this, and isTagName doesn't take SS.
1147 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001148 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001149 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001150
Douglas Gregor0be31a22010-07-02 17:43:08 +00001151 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001152 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001153 case DeclSpec::TST_enum:
1154 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1155 case DeclSpec::TST_union:
1156 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1157 case DeclSpec::TST_struct:
1158 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1159 case DeclSpec::TST_class:
1160 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001161 }
Mike Stump11289f42009-09-09 15:08:12 +00001162
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001163 if (TagName) {
1164 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001165 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001166 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump11289f42009-09-09 15:08:12 +00001167
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001168 // Parse this as a tag as if the missing tag were present.
1169 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001170 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001171 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001172 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001173 return true;
1174 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001175 }
Mike Stump11289f42009-09-09 15:08:12 +00001176
Douglas Gregor15e56022009-10-13 23:27:22 +00001177 // This is almost certainly an invalid type name. Let the action emit a
1178 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001179 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001180 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001181 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001182 // The action emitted a diagnostic, so we don't have to.
1183 if (T) {
1184 // The action has suggested that the type T could be used. Set that as
1185 // the type in the declaration specifiers, consume the would-be type
1186 // name token, and we're done.
1187 const char *PrevSpec;
1188 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001189 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001190 DS.SetRangeEnd(Tok.getLocation());
1191 ConsumeToken();
1192
1193 // There may be other declaration specifiers after this.
1194 return true;
1195 }
1196
1197 // Fall through; the action had no suggestion for us.
1198 } else {
1199 // The action did not emit a diagnostic, so emit one now.
1200 SourceRange R;
1201 if (SS) R = SS->getRange();
1202 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1203 }
Mike Stump11289f42009-09-09 15:08:12 +00001204
Douglas Gregor15e56022009-10-13 23:27:22 +00001205 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001206 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001207 unsigned DiagID;
1208 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001209 DS.SetRangeEnd(Tok.getLocation());
1210 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001211
Chris Lattner20a0c612009-04-14 21:34:55 +00001212 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1213 // avoid rippling error messages on subsequent uses of the same type,
1214 // could be useful if #include was forgotten.
1215 return false;
1216}
1217
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001218/// \brief Determine the declaration specifier context from the declarator
1219/// context.
1220///
1221/// \param Context the declarator context, which is one of the
1222/// Declarator::TheContext enumerator values.
1223Parser::DeclSpecContext
1224Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1225 if (Context == Declarator::MemberContext)
1226 return DSC_class;
1227 if (Context == Declarator::FileContext)
1228 return DSC_top_level;
1229 return DSC_normal;
1230}
1231
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001232/// ParseDeclarationSpecifiers
1233/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001234/// storage-class-specifier declaration-specifiers[opt]
1235/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001236/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001237/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001238///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001239/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001240/// 'typedef'
1241/// 'extern'
1242/// 'static'
1243/// 'auto'
1244/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001245/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001246/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001247/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001248/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001249/// [C++] 'virtual'
1250/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001251/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001252/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001253/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001254
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001255///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001256void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001257 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001258 AccessSpecifier AS,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001259 DeclSpecContext DSContext) {
1260 if (DS.getSourceRange().isInvalid()) {
1261 DS.SetRangeStart(Tok.getLocation());
1262 DS.SetRangeEnd(Tok.getLocation());
1263 }
1264
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001265 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001266 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001267 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001268 unsigned DiagID = 0;
1269
Chris Lattner4d8f8732006-11-28 05:05:08 +00001270 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001271
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001272 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001273 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001274 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001275 // If this is not a declaration specifier token, we're done reading decl
1276 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001277 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001278 return;
Mike Stump11289f42009-09-09 15:08:12 +00001279
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001280 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001281 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001282 if (DS.hasTypeSpecifier()) {
1283 bool AllowNonIdentifiers
1284 = (getCurScope()->getFlags() & (Scope::ControlScope |
1285 Scope::BlockScope |
1286 Scope::TemplateParamScope |
1287 Scope::FunctionPrototypeScope |
1288 Scope::AtCatchScope)) == 0;
1289 bool AllowNestedNameSpecifiers
1290 = DSContext == DSC_top_level ||
1291 (DSContext == DSC_class && DS.isFriendSpecified());
1292
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001293 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1294 AllowNonIdentifiers,
1295 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001296 ConsumeCodeCompletionToken();
1297 return;
1298 }
1299
Douglas Gregor80039242011-02-15 20:33:25 +00001300 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1301 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1302 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001303 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1304 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001305 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001306 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001307 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001308 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001309
1310 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1311 ConsumeCodeCompletionToken();
1312 return;
1313 }
1314
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001315 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001316 // C++ scope specifier. Annotate and loop, or bail out on error.
1317 if (TryAnnotateCXXScopeToken(true)) {
1318 if (!DS.hasTypeSpecifier())
1319 DS.SetTypeSpecError();
1320 goto DoneWithDeclSpec;
1321 }
John McCall8bc2a702010-03-01 18:20:46 +00001322 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1323 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001324 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001325
1326 case tok::annot_cxxscope: {
1327 if (DS.hasTypeSpecifier())
1328 goto DoneWithDeclSpec;
1329
John McCall9dab4e62009-12-12 11:40:51 +00001330 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001331 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1332 Tok.getAnnotationRange(),
1333 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001334
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001335 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001336 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001337 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001338 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001339 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001340 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001341
1342 // C++ [class.qual]p2:
1343 // In a lookup in which the constructor is an acceptable lookup
1344 // result and the nested-name-specifier nominates a class C:
1345 //
1346 // - if the name specified after the
1347 // nested-name-specifier, when looked up in C, is the
1348 // injected-class-name of C (Clause 9), or
1349 //
1350 // - if the name specified after the nested-name-specifier
1351 // is the same as the identifier or the
1352 // simple-template-id's template-name in the last
1353 // component of the nested-name-specifier,
1354 //
1355 // the name is instead considered to name the constructor of
1356 // class C.
1357 //
1358 // Thus, if the template-name is actually the constructor
1359 // name, then the code is ill-formed; this interpretation is
1360 // reinforced by the NAD status of core issue 635.
1361 TemplateIdAnnotation *TemplateId
1362 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +00001363 if ((DSContext == DSC_top_level ||
1364 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1365 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001366 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001367 if (isConstructorDeclarator()) {
1368 // The user meant this to be an out-of-line constructor
1369 // definition, but template arguments are not allowed
1370 // there. Just allow this as a constructor; we'll
1371 // complain about it later.
1372 goto DoneWithDeclSpec;
1373 }
1374
1375 // The user meant this to name a type, but it actually names
1376 // a constructor with some extraneous template
1377 // arguments. Complain, then parse it as a type as the user
1378 // intended.
1379 Diag(TemplateId->TemplateNameLoc,
1380 diag::err_out_of_line_template_id_names_constructor)
1381 << TemplateId->Name;
1382 }
1383
John McCall9dab4e62009-12-12 11:40:51 +00001384 DS.getTypeSpecScope() = SS;
1385 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001386 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001387 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001388 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001389 continue;
1390 }
1391
Douglas Gregorc5790df2009-09-28 07:26:33 +00001392 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001393 DS.getTypeSpecScope() = SS;
1394 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001395 if (Tok.getAnnotationValue()) {
1396 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001397 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1398 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001399 PrevSpec, DiagID, T);
1400 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001401 else
1402 DS.SetTypeSpecError();
1403 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1404 ConsumeToken(); // The typename
1405 }
1406
Douglas Gregor167fa622009-03-25 15:40:00 +00001407 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001408 goto DoneWithDeclSpec;
1409
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001410 // If we're in a context where the identifier could be a class name,
1411 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001412 if ((DSContext == DSC_top_level ||
1413 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001414 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001415 &SS)) {
1416 if (isConstructorDeclarator())
1417 goto DoneWithDeclSpec;
1418
1419 // As noted in C++ [class.qual]p2 (cited above), when the name
1420 // of the class is qualified in a context where it could name
1421 // a constructor, its a constructor name. However, we've
1422 // looked at the declarator, and the user probably meant this
1423 // to be a type. Complain that it isn't supposed to be treated
1424 // as a type, then proceed to parse it as a type.
1425 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1426 << Next.getIdentifierInfo();
1427 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001428
John McCallba7bf592010-08-24 05:47:05 +00001429 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1430 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001431 getCurScope(), &SS,
1432 false, false, ParsedType(),
1433 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001434
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001435 // If the referenced identifier is not a type, then this declspec is
1436 // erroneous: We already checked about that it has no type specifier, and
1437 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001438 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001439 if (TypeRep == 0) {
1440 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001441 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001442 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001443 }
Mike Stump11289f42009-09-09 15:08:12 +00001444
John McCall9dab4e62009-12-12 11:40:51 +00001445 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001446 ConsumeToken(); // The C++ scope.
1447
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001448 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001449 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001450 if (isInvalid)
1451 break;
Mike Stump11289f42009-09-09 15:08:12 +00001452
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001453 DS.SetRangeEnd(Tok.getLocation());
1454 ConsumeToken(); // The typename.
1455
1456 continue;
1457 }
Mike Stump11289f42009-09-09 15:08:12 +00001458
Chris Lattnere387d9e2009-01-21 19:48:37 +00001459 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001460 if (Tok.getAnnotationValue()) {
1461 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001462 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001463 DiagID, T);
1464 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001465 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001466
1467 if (isInvalid)
1468 break;
1469
Chris Lattnere387d9e2009-01-21 19:48:37 +00001470 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1471 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001472
Chris Lattnere387d9e2009-01-21 19:48:37 +00001473 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1474 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001475 // Objective-C interface.
1476 if (Tok.is(tok::less) && getLang().ObjC1)
1477 ParseObjCProtocolQualifiers(DS);
1478
Chris Lattnere387d9e2009-01-21 19:48:37 +00001479 continue;
1480 }
Mike Stump11289f42009-09-09 15:08:12 +00001481
Chris Lattner16fac4f2008-07-26 01:18:38 +00001482 // typedef-name
1483 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001484 // In C++, check to see if this is a scope specifier like foo::bar::, if
1485 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001486 if (getLang().CPlusPlus) {
1487 if (TryAnnotateCXXScopeToken(true)) {
1488 if (!DS.hasTypeSpecifier())
1489 DS.SetTypeSpecError();
1490 goto DoneWithDeclSpec;
1491 }
1492 if (!Tok.is(tok::identifier))
1493 continue;
1494 }
Mike Stump11289f42009-09-09 15:08:12 +00001495
Chris Lattner16fac4f2008-07-26 01:18:38 +00001496 // This identifier can only be a typedef name if we haven't already seen
1497 // a type-specifier. Without this check we misparse:
1498 // typedef int X; struct Y { short X; }; as 'short int'.
1499 if (DS.hasTypeSpecifier())
1500 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001501
John Thompson22334602010-02-05 00:12:22 +00001502 // Check for need to substitute AltiVec keyword tokens.
1503 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1504 break;
1505
Chris Lattner16fac4f2008-07-26 01:18:38 +00001506 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001507 ParsedType TypeRep =
1508 Actions.getTypeName(*Tok.getIdentifierInfo(),
1509 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001510
Chris Lattner6cc055a2009-04-12 20:42:31 +00001511 // If this is not a typedef name, don't parse it as part of the declspec,
1512 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001513 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001514 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001515 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001516 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001517
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001518 // If we're in a context where the identifier could be a class name,
1519 // check whether this is a constructor declaration.
1520 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001521 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001522 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001523 goto DoneWithDeclSpec;
1524
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001525 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001526 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001527 if (isInvalid)
1528 break;
Mike Stump11289f42009-09-09 15:08:12 +00001529
Chris Lattner16fac4f2008-07-26 01:18:38 +00001530 DS.SetRangeEnd(Tok.getLocation());
1531 ConsumeToken(); // The identifier
1532
1533 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1534 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001535 // Objective-C interface.
1536 if (Tok.is(tok::less) && getLang().ObjC1)
1537 ParseObjCProtocolQualifiers(DS);
1538
Steve Naroffcd5e7822008-09-22 10:28:57 +00001539 // Need to support trailing type qualifiers (e.g. "id<p> const").
1540 // If a type specifier follows, it will be diagnosed elsewhere.
1541 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001542 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001543
1544 // type-name
1545 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001546 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001547 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001548 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001549 // This template-id does not refer to a type name, so we're
1550 // done with the type-specifiers.
1551 goto DoneWithDeclSpec;
1552 }
1553
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001554 // If we're in a context where the template-id could be a
1555 // constructor name or specialization, check whether this is a
1556 // constructor declaration.
1557 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001558 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001559 isConstructorDeclarator())
1560 goto DoneWithDeclSpec;
1561
Douglas Gregor7f741122009-02-25 19:37:18 +00001562 // Turn the template-id annotation token into a type annotation
1563 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001564 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001565 continue;
1566 }
1567
Chris Lattnere37e2332006-08-15 04:50:22 +00001568 // GNU attributes support.
1569 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001570 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001571 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001572
1573 // Microsoft declspec support.
1574 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001575 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001576 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001577
Steve Naroff44ac7772008-12-25 14:16:32 +00001578 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001579 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001580 // FIXME: Add handling here!
1581 break;
1582
1583 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001584 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001585 case tok::kw___cdecl:
1586 case tok::kw___stdcall:
1587 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001588 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001589 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001590 continue;
1591
Dawn Perchik335e16b2010-09-03 01:29:35 +00001592 // Borland single token adornments.
1593 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001594 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001595 continue;
1596
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001597 // OpenCL single token adornments.
1598 case tok::kw___kernel:
1599 ParseOpenCLAttributes(DS.getAttributes());
1600 continue;
1601
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001602 // storage-class-specifier
1603 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001604 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001605 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001606 break;
1607 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001608 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001609 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001610 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001611 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001612 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001613 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001614 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001615 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001616 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001617 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001618 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001619 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001620 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001621 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001622 break;
1623 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00001624 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001625 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1626 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1627 DiagID, getLang());
1628 if (!isInvalid)
1629 Diag(Tok, diag::auto_storage_class)
1630 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1631 }
1632 else
1633 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1634 DiagID);
1635 }
Anders Carlsson082acde2009-06-26 18:41:36 +00001636 else
John McCall49bfce42009-08-03 20:12:06 +00001637 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001638 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001639 break;
1640 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001641 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001642 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001643 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001644 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001645 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001646 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001647 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001648 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001649 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001650 break;
Mike Stump11289f42009-09-09 15:08:12 +00001651
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001652 // function-specifier
1653 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001654 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001655 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001656 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001657 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001658 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001659 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001660 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001661 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001662
Anders Carlssoncd8db412009-05-06 04:46:28 +00001663 // friend
1664 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001665 if (DSContext == DSC_class)
1666 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1667 else {
1668 PrevSpec = ""; // not actually used by the diagnostic
1669 DiagID = diag::err_friend_invalid_in_context;
1670 isInvalid = true;
1671 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001672 break;
Mike Stump11289f42009-09-09 15:08:12 +00001673
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001674 // constexpr
1675 case tok::kw_constexpr:
1676 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1677 break;
1678
Chris Lattnere387d9e2009-01-21 19:48:37 +00001679 // type-specifier
1680 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001681 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1682 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001683 break;
1684 case tok::kw_long:
1685 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001686 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1687 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001688 else
John McCall49bfce42009-08-03 20:12:06 +00001689 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1690 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001691 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001692 case tok::kw___int64:
1693 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1694 DiagID);
1695 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001696 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001697 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1698 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001699 break;
1700 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001701 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1702 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001703 break;
1704 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001705 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1706 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001707 break;
1708 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001709 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1710 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001711 break;
1712 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001713 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1714 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001715 break;
1716 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001717 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1718 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001719 break;
1720 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001721 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1722 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001723 break;
1724 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001725 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1726 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001727 break;
1728 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001729 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1730 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001731 break;
1732 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001733 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1734 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001735 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001736 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001737 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1738 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001739 break;
1740 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001741 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1742 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001743 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001744 case tok::kw_bool:
1745 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001746 if (Tok.is(tok::kw_bool) &&
1747 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1748 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1749 PrevSpec = ""; // Not used by the diagnostic.
1750 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00001751 // For better error recovery.
1752 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001753 isInvalid = true;
1754 } else {
1755 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1756 DiagID);
1757 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001758 break;
1759 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1761 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001762 break;
1763 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001764 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1765 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001766 break;
1767 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001768 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1769 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001770 break;
John Thompson22334602010-02-05 00:12:22 +00001771 case tok::kw___vector:
1772 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1773 break;
1774 case tok::kw___pixel:
1775 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1776 break;
John McCall39439732011-04-09 22:50:59 +00001777 case tok::kw___unknown_anytype:
1778 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1779 PrevSpec, DiagID);
1780 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001781
1782 // class-specifier:
1783 case tok::kw_class:
1784 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001785 case tok::kw_union: {
1786 tok::TokenKind Kind = Tok.getKind();
1787 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001788 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001789 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001790 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001791
1792 // enum-specifier:
1793 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001794 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001795 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001796 continue;
1797
1798 // cv-qualifier:
1799 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001800 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1801 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001802 break;
1803 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001804 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1805 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001806 break;
1807 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001808 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1809 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001810 break;
1811
Douglas Gregor333489b2009-03-27 23:10:48 +00001812 // C++ typename-specifier:
1813 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001814 if (TryAnnotateTypeOrScopeToken()) {
1815 DS.SetTypeSpecError();
1816 goto DoneWithDeclSpec;
1817 }
1818 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001819 continue;
1820 break;
1821
Chris Lattnere387d9e2009-01-21 19:48:37 +00001822 // GNU typeof support.
1823 case tok::kw_typeof:
1824 ParseTypeofSpecifier(DS);
1825 continue;
1826
Anders Carlsson74948d02009-06-24 17:47:40 +00001827 case tok::kw_decltype:
1828 ParseDecltypeSpecifier(DS);
1829 continue;
1830
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00001831 // OpenCL qualifiers:
1832 case tok::kw_private:
1833 if (!getLang().OpenCL)
1834 goto DoneWithDeclSpec;
1835 case tok::kw___private:
1836 case tok::kw___global:
1837 case tok::kw___local:
1838 case tok::kw___constant:
1839 case tok::kw___read_only:
1840 case tok::kw___write_only:
1841 case tok::kw___read_write:
1842 ParseOpenCLQualifiers(DS);
1843 break;
1844
Steve Naroffcfdf6162008-06-05 00:02:44 +00001845 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001846 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001847 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1848 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001849 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001850 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001851
Douglas Gregor3a001f42010-11-19 17:10:50 +00001852 if (!ParseObjCProtocolQualifiers(DS))
1853 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1854 << FixItHint::CreateInsertion(Loc, "id")
1855 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001856
1857 // Need to support trailing type qualifiers (e.g. "id<p> const").
1858 // If a type specifier follows, it will be diagnosed elsewhere.
1859 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001860 }
John McCall49bfce42009-08-03 20:12:06 +00001861 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001862 if (isInvalid) {
1863 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001864 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001865
1866 if (DiagID == diag::ext_duplicate_declspec)
1867 Diag(Tok, DiagID)
1868 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1869 else
1870 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001871 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001872
Chris Lattner2e232092008-03-13 06:29:04 +00001873 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00001874 if (DiagID != diag::err_bool_redeclaration)
1875 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001876 }
1877}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001878
Chris Lattnera448d752009-01-06 06:59:53 +00001879/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001880/// primarily follow the C++ grammar with additions for C99 and GNU,
1881/// which together subsume the C grammar. Note that the C++
1882/// type-specifier also includes the C type-qualifier (for const,
1883/// volatile, and C99 restrict). Returns true if a type-specifier was
1884/// found (and parsed), false otherwise.
1885///
1886/// type-specifier: [C++ 7.1.5]
1887/// simple-type-specifier
1888/// class-specifier
1889/// enum-specifier
1890/// elaborated-type-specifier [TODO]
1891/// cv-qualifier
1892///
1893/// cv-qualifier: [C++ 7.1.5.1]
1894/// 'const'
1895/// 'volatile'
1896/// [C99] 'restrict'
1897///
1898/// simple-type-specifier: [ C++ 7.1.5.2]
1899/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1900/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1901/// 'char'
1902/// 'wchar_t'
1903/// 'bool'
1904/// 'short'
1905/// 'int'
1906/// 'long'
1907/// 'signed'
1908/// 'unsigned'
1909/// 'float'
1910/// 'double'
1911/// 'void'
1912/// [C99] '_Bool'
1913/// [C99] '_Complex'
1914/// [C99] '_Imaginary' // Removed in TC2?
1915/// [GNU] '_Decimal32'
1916/// [GNU] '_Decimal64'
1917/// [GNU] '_Decimal128'
1918/// [GNU] typeof-specifier
1919/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1920/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001921/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001922/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001923bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001924 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001925 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001926 const ParsedTemplateInfo &TemplateInfo,
1927 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001928 SourceLocation Loc = Tok.getLocation();
1929
1930 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001931 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001932 // If we already have a type specifier, this identifier is not a type.
1933 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1934 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1935 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1936 return false;
John Thompson22334602010-02-05 00:12:22 +00001937 // Check for need to substitute AltiVec keyword tokens.
1938 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1939 break;
1940 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001941 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001942 // Annotate typenames and C++ scope specifiers. If we get one, just
1943 // recurse to handle whatever we get.
1944 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001945 return true;
1946 if (Tok.is(tok::identifier))
1947 return false;
1948 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1949 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001950 case tok::coloncolon: // ::foo::bar
1951 if (NextToken().is(tok::kw_new) || // ::new
1952 NextToken().is(tok::kw_delete)) // ::delete
1953 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001954
Chris Lattner020bab92009-01-04 23:41:41 +00001955 // Annotate typenames and C++ scope specifiers. If we get one, just
1956 // recurse to handle whatever we get.
1957 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001958 return true;
1959 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1960 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001961
Douglas Gregor450c75a2008-11-07 15:42:26 +00001962 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001963 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001964 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00001965 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1966 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001967 DiagID, T);
1968 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001969 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001970 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1971 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001972
Douglas Gregor450c75a2008-11-07 15:42:26 +00001973 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1974 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1975 // Objective-C interface. If we don't have Objective-C or a '<', this is
1976 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001977 if (Tok.is(tok::less) && getLang().ObjC1)
1978 ParseObjCProtocolQualifiers(DS);
1979
Douglas Gregor450c75a2008-11-07 15:42:26 +00001980 return true;
1981 }
1982
1983 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001984 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001985 break;
1986 case tok::kw_long:
1987 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001988 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1989 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001990 else
John McCall49bfce42009-08-03 20:12:06 +00001991 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1992 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001993 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001994 case tok::kw___int64:
1995 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1996 DiagID);
1997 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001998 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001999 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002000 break;
2001 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002002 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2003 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002004 break;
2005 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002006 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2007 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002008 break;
2009 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002010 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2011 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002012 break;
2013 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002014 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002015 break;
2016 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002017 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002018 break;
2019 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002020 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002021 break;
2022 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002023 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002024 break;
2025 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002026 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002027 break;
2028 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002029 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002030 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002031 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002032 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002033 break;
2034 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002035 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002036 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002037 case tok::kw_bool:
2038 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00002039 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002040 break;
2041 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002042 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2043 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002044 break;
2045 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002046 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2047 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002048 break;
2049 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002050 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2051 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002052 break;
John Thompson22334602010-02-05 00:12:22 +00002053 case tok::kw___vector:
2054 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2055 break;
2056 case tok::kw___pixel:
2057 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2058 break;
2059
Douglas Gregor450c75a2008-11-07 15:42:26 +00002060 // class-specifier:
2061 case tok::kw_class:
2062 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002063 case tok::kw_union: {
2064 tok::TokenKind Kind = Tok.getKind();
2065 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00002066 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2067 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002068 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002069 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00002070
2071 // enum-specifier:
2072 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002073 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002074 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002075 return true;
2076
2077 // cv-qualifier:
2078 case tok::kw_const:
2079 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002080 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002081 break;
2082 case tok::kw_volatile:
2083 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002084 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002085 break;
2086 case tok::kw_restrict:
2087 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002088 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002089 break;
2090
2091 // GNU typeof support.
2092 case tok::kw_typeof:
2093 ParseTypeofSpecifier(DS);
2094 return true;
2095
Anders Carlsson74948d02009-06-24 17:47:40 +00002096 // C++0x decltype support.
2097 case tok::kw_decltype:
2098 ParseDecltypeSpecifier(DS);
2099 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002100
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002101 // OpenCL qualifiers:
2102 case tok::kw_private:
2103 if (!getLang().OpenCL)
2104 return false;
2105 case tok::kw___private:
2106 case tok::kw___global:
2107 case tok::kw___local:
2108 case tok::kw___constant:
2109 case tok::kw___read_only:
2110 case tok::kw___write_only:
2111 case tok::kw___read_write:
2112 ParseOpenCLQualifiers(DS);
2113 break;
2114
Anders Carlssonbae27372009-06-26 23:44:14 +00002115 // C++0x auto support.
2116 case tok::kw_auto:
2117 if (!getLang().CPlusPlus0x)
2118 return false;
2119
John McCall49bfce42009-08-03 20:12:06 +00002120 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002121 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002122
Eli Friedman53339e02009-06-08 23:27:34 +00002123 case tok::kw___ptr64:
2124 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002125 case tok::kw___cdecl:
2126 case tok::kw___stdcall:
2127 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002128 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00002129 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002130 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002131
Dawn Perchik335e16b2010-09-03 01:29:35 +00002132 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002133 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002134 return true;
2135
Douglas Gregor450c75a2008-11-07 15:42:26 +00002136 default:
2137 // Not a type-specifier; do nothing.
2138 return false;
2139 }
2140
2141 // If the specifier combination wasn't legal, issue a diagnostic.
2142 if (isInvalid) {
2143 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002144 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002145 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002146 }
2147 DS.SetRangeEnd(Tok.getLocation());
2148 ConsumeToken(); // whatever we parsed above.
2149 return true;
2150}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002151
Chris Lattner70ae4912007-10-29 04:42:53 +00002152/// ParseStructDeclaration - Parse a struct declaration without the terminating
2153/// semicolon.
2154///
Chris Lattner90a26b02007-01-23 04:38:16 +00002155/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002156/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002157/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002158/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002159/// struct-declarator-list:
2160/// struct-declarator
2161/// struct-declarator-list ',' struct-declarator
2162/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2163/// struct-declarator:
2164/// declarator
2165/// [GNU] declarator attributes[opt]
2166/// declarator[opt] ':' constant-expression
2167/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2168///
Chris Lattnera12405b2008-04-10 06:46:29 +00002169void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002170ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002171 if (Tok.is(tok::kw___extension__)) {
2172 // __extension__ silences extension warnings in the subexpression.
2173 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002174 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002175 return ParseStructDeclaration(DS, Fields);
2176 }
Mike Stump11289f42009-09-09 15:08:12 +00002177
Steve Naroff97170802007-08-20 22:28:22 +00002178 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002179 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002180
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002181 // If there are no declarators, this is a free-standing declaration
2182 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002183 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002184 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002185 return;
2186 }
2187
2188 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002189 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002190 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002191 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002192 FieldDeclarator DeclaratorInfo(DS);
2193
2194 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002195 if (!FirstDeclarator)
2196 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002197
Steve Naroff97170802007-08-20 22:28:22 +00002198 /// struct-declarator: declarator
2199 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002200 if (Tok.isNot(tok::colon)) {
2201 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2202 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002203 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002204 }
Mike Stump11289f42009-09-09 15:08:12 +00002205
Chris Lattner76c72282007-10-09 17:33:22 +00002206 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002207 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002208 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002209 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002210 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002211 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002212 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002213 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002214
Steve Naroff97170802007-08-20 22:28:22 +00002215 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002216 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002217
John McCallcfefb6d2009-11-03 02:38:08 +00002218 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002219 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002220 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002221
Steve Naroff97170802007-08-20 22:28:22 +00002222 // If we don't have a comma, it is either the end of the list (a ';')
2223 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002224 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002225 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002226
Steve Naroff97170802007-08-20 22:28:22 +00002227 // Consume the comma.
2228 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002229
John McCallcfefb6d2009-11-03 02:38:08 +00002230 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002231 }
Steve Naroff97170802007-08-20 22:28:22 +00002232}
2233
2234/// ParseStructUnionBody
2235/// struct-contents:
2236/// struct-declaration-list
2237/// [EXT] empty
2238/// [GNU] "struct-declaration-list" without terminatoring ';'
2239/// struct-declaration-list:
2240/// struct-declaration
2241/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002242/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002243///
Chris Lattner1300fb92007-01-23 23:42:53 +00002244void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002245 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002246 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2247 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002248
Chris Lattner90a26b02007-01-23 04:38:16 +00002249 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002250
Douglas Gregor658b9552009-01-09 22:42:13 +00002251 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002252 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002253
Chris Lattner7b9ace62007-01-23 20:11:08 +00002254 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2255 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002256 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002257 Diag(Tok, diag::ext_empty_struct_union)
2258 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002259
John McCall48871652010-08-21 09:40:31 +00002260 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002261
Chris Lattner7b9ace62007-01-23 20:11:08 +00002262 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002263 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002264 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002265
Chris Lattner736ed5d2007-06-09 05:59:07 +00002266 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002267 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002268 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002269 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002270 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002271 ConsumeToken();
2272 continue;
2273 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002274
2275 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002276 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002277
John McCallcfefb6d2009-11-03 02:38:08 +00002278 if (!Tok.is(tok::at)) {
2279 struct CFieldCallback : FieldCallback {
2280 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002281 Decl *TagDecl;
2282 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002283
John McCall48871652010-08-21 09:40:31 +00002284 CFieldCallback(Parser &P, Decl *TagDecl,
2285 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002286 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2287
John McCall48871652010-08-21 09:40:31 +00002288 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002289 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002290 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002291 FD.D.getDeclSpec().getSourceRange().getBegin(),
2292 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002293 FieldDecls.push_back(Field);
2294 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002295 }
John McCallcfefb6d2009-11-03 02:38:08 +00002296 } Callback(*this, TagDecl, FieldDecls);
2297
2298 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002299 } else { // Handle @defs
2300 ConsumeToken();
2301 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2302 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002303 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002304 continue;
2305 }
2306 ConsumeToken();
2307 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2308 if (!Tok.is(tok::identifier)) {
2309 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002310 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002311 continue;
2312 }
John McCall48871652010-08-21 09:40:31 +00002313 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002314 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002315 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002316 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2317 ConsumeToken();
2318 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002319 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002320
Chris Lattner76c72282007-10-09 17:33:22 +00002321 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002322 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002323 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002324 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002325 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002326 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002327 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2328 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002329 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002330 // If we stopped at a ';', eat it.
2331 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002332 }
2333 }
Mike Stump11289f42009-09-09 15:08:12 +00002334
Steve Naroff33a1e802007-10-29 21:38:07 +00002335 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002336
John McCall084e83d2011-03-24 11:26:52 +00002337 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002338 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002339 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002340
Douglas Gregor0be31a22010-07-02 17:43:08 +00002341 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00002342 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00002343 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002344 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002345 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002346 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002347}
2348
Chris Lattner3b561a32006-08-13 00:12:11 +00002349/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002350/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002351/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002352///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002353/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2354/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002355/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002356/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002357///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002358/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2359/// [C++0x] enum-head '{' enumerator-list ',' '}'
2360///
2361/// enum-head: [C++0x]
2362/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2363/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2364///
2365/// enum-key: [C++0x]
2366/// 'enum'
2367/// 'enum' 'class'
2368/// 'enum' 'struct'
2369///
2370/// enum-base: [C++0x]
2371/// ':' type-specifier-seq
2372///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002373/// [C++] elaborated-type-specifier:
2374/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2375///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002376void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002377 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002378 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002379 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002380 if (Tok.is(tok::code_completion)) {
2381 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002382 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002383 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002384 }
2385
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002386 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002387 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002388 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002389
Abramo Bagnarad7548482010-05-19 21:37:53 +00002390 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002391 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00002392 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002393 return;
2394
2395 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002396 Diag(Tok, diag::err_expected_ident);
2397 if (Tok.isNot(tok::l_brace)) {
2398 // Has no name and is not a definition.
2399 // Skip the rest of this declarator, up until the comma or semicolon.
2400 SkipUntil(tok::comma, true);
2401 return;
2402 }
2403 }
2404 }
Mike Stump11289f42009-09-09 15:08:12 +00002405
Douglas Gregora1aec292011-02-22 20:32:04 +00002406 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002407 bool IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002408 bool IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002409
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002410 if (getLang().CPlusPlus0x &&
2411 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002412 IsScopedEnum = true;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002413 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2414 ConsumeToken();
Douglas Gregor0bf31402010-10-08 23:50:27 +00002415 }
2416
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002417 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002418 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2419 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002420 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002421
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002422 // Skip the rest of this declarator, up until the comma or semicolon.
2423 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002424 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002425 }
Mike Stump11289f42009-09-09 15:08:12 +00002426
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002427 // If an identifier is present, consume and remember it.
2428 IdentifierInfo *Name = 0;
2429 SourceLocation NameLoc;
2430 if (Tok.is(tok::identifier)) {
2431 Name = Tok.getIdentifierInfo();
2432 NameLoc = ConsumeToken();
2433 }
Mike Stump11289f42009-09-09 15:08:12 +00002434
Douglas Gregor0bf31402010-10-08 23:50:27 +00002435 if (!Name && IsScopedEnum) {
2436 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2437 // declaration of a scoped enumeration.
2438 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2439 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002440 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002441 }
2442
2443 TypeResult BaseType;
2444
Douglas Gregord1f69f62010-12-01 17:42:47 +00002445 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002446 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002447 bool PossibleBitfield = false;
2448 if (getCurScope()->getFlags() & Scope::ClassScope) {
2449 // If we're in class scope, this can either be an enum declaration with
2450 // an underlying type, or a declaration of a bitfield member. We try to
2451 // use a simple disambiguation scheme first to catch the common cases
2452 // (integer literal, sizeof); if it's still ambiguous, we then consider
2453 // anything that's a simple-type-specifier followed by '(' as an
2454 // expression. This suffices because function types are not valid
2455 // underlying types anyway.
2456 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2457 // If the next token starts an expression, we know we're parsing a
2458 // bit-field. This is the common case.
2459 if (TPR == TPResult::True())
2460 PossibleBitfield = true;
2461 // If the next token starts a type-specifier-seq, it may be either a
2462 // a fixed underlying type or the start of a function-style cast in C++;
2463 // lookahead one more token to see if it's obvious that we have a
2464 // fixed underlying type.
2465 else if (TPR == TPResult::False() &&
2466 GetLookAheadToken(2).getKind() == tok::semi) {
2467 // Consume the ':'.
2468 ConsumeToken();
2469 } else {
2470 // We have the start of a type-specifier-seq, so we have to perform
2471 // tentative parsing to determine whether we have an expression or a
2472 // type.
2473 TentativeParsingAction TPA(*this);
2474
2475 // Consume the ':'.
2476 ConsumeToken();
2477
Douglas Gregora1aec292011-02-22 20:32:04 +00002478 if ((getLang().CPlusPlus &&
2479 isCXXDeclarationSpecifier() != TPResult::True()) ||
2480 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002481 // We'll parse this as a bitfield later.
2482 PossibleBitfield = true;
2483 TPA.Revert();
2484 } else {
2485 // We have a type-specifier-seq.
2486 TPA.Commit();
2487 }
2488 }
2489 } else {
2490 // Consume the ':'.
2491 ConsumeToken();
2492 }
2493
2494 if (!PossibleBitfield) {
2495 SourceRange Range;
2496 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002497
2498 if (!getLang().CPlusPlus0x)
2499 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2500 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002501 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002502 }
2503
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002504 // There are three options here. If we have 'enum foo;', then this is a
2505 // forward declaration. If we have 'enum foo {...' then this is a
2506 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2507 //
2508 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2509 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2510 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2511 //
John McCallfaf5fb42010-08-26 23:41:50 +00002512 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002513 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002514 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002515 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002516 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002517 else
John McCallfaf5fb42010-08-26 23:41:50 +00002518 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002519
2520 // enums cannot be templates, although they can be referenced from a
2521 // template.
2522 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002523 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002524 Diag(Tok, diag::err_enum_template);
2525
2526 // Skip the rest of this declarator, up until the comma or semicolon.
2527 SkipUntil(tok::comma, true);
2528 return;
2529 }
2530
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002531 if (!Name && TUK != Sema::TUK_Definition) {
2532 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2533
2534 // Skip the rest of this declarator, up until the comma or semicolon.
2535 SkipUntil(tok::comma, true);
2536 return;
2537 }
2538
Douglas Gregord6ab8742009-05-28 23:31:59 +00002539 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002540 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002541 const char *PrevSpec = 0;
2542 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002543 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002544 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002545 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002546 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002547 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002548 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002549
Douglas Gregorba41d012010-04-24 16:38:41 +00002550 if (IsDependent) {
2551 // This enum has a dependent nested-name-specifier. Handle it as a
2552 // dependent tag.
2553 if (!Name) {
2554 DS.SetTypeSpecError();
2555 Diag(Tok, diag::err_expected_type_name_after_typename);
2556 return;
2557 }
2558
Douglas Gregor0be31a22010-07-02 17:43:08 +00002559 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002560 TUK, SS, Name, StartLoc,
2561 NameLoc);
2562 if (Type.isInvalid()) {
2563 DS.SetTypeSpecError();
2564 return;
2565 }
2566
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002567 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2568 NameLoc.isValid() ? NameLoc : StartLoc,
2569 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002570 Diag(StartLoc, DiagID) << PrevSpec;
2571
2572 return;
2573 }
Mike Stump11289f42009-09-09 15:08:12 +00002574
John McCall48871652010-08-21 09:40:31 +00002575 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002576 // The action failed to produce an enumeration tag. If this is a
2577 // definition, consume the entire definition.
2578 if (Tok.is(tok::l_brace)) {
2579 ConsumeBrace();
2580 SkipUntil(tok::r_brace);
2581 }
2582
2583 DS.SetTypeSpecError();
2584 return;
2585 }
2586
Chris Lattner76c72282007-10-09 17:33:22 +00002587 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002588 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002589
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002590 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2591 NameLoc.isValid() ? NameLoc : StartLoc,
2592 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002593 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002594}
2595
Chris Lattnerc1915e22007-01-25 07:29:02 +00002596/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2597/// enumerator-list:
2598/// enumerator
2599/// enumerator-list ',' enumerator
2600/// enumerator:
2601/// enumeration-constant
2602/// enumeration-constant '=' constant-expression
2603/// enumeration-constant:
2604/// identifier
2605///
John McCall48871652010-08-21 09:40:31 +00002606void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002607 // Enter the scope of the enum body and start the definition.
2608 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002609 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002610
Chris Lattnerc1915e22007-01-25 07:29:02 +00002611 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002612
Chris Lattner37256fb2007-08-27 17:24:30 +00002613 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002614 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002615 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002616
John McCall48871652010-08-21 09:40:31 +00002617 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002618
John McCall48871652010-08-21 09:40:31 +00002619 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002620
Chris Lattnerc1915e22007-01-25 07:29:02 +00002621 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002622 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002623 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2624 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002625
John McCall811a0f52010-10-22 23:36:17 +00002626 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002627 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002628 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002629
Chris Lattnerc1915e22007-01-25 07:29:02 +00002630 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002631 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002632 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002633 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002634 AssignedVal = ParseConstantExpression();
2635 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002636 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002637 }
Mike Stump11289f42009-09-09 15:08:12 +00002638
Chris Lattnerc1915e22007-01-25 07:29:02 +00002639 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002640 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2641 LastEnumConstDecl,
2642 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002643 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002644 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002645 EnumConstantDecls.push_back(EnumConstDecl);
2646 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002647
Douglas Gregorce66d022010-09-07 14:51:08 +00002648 if (Tok.is(tok::identifier)) {
2649 // We're missing a comma between enumerators.
2650 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2651 Diag(Loc, diag::err_enumerator_list_missing_comma)
2652 << FixItHint::CreateInsertion(Loc, ", ");
2653 continue;
2654 }
2655
Chris Lattner76c72282007-10-09 17:33:22 +00002656 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002657 break;
2658 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002659
2660 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002661 !(getLang().C99 || getLang().CPlusPlus0x))
2662 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2663 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002664 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002665 }
Mike Stump11289f42009-09-09 15:08:12 +00002666
Chris Lattnerc1915e22007-01-25 07:29:02 +00002667 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002668 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002669
Chris Lattnerc1915e22007-01-25 07:29:02 +00002670 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002671 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002672 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002673
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002674 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2675 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002676 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002677
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002678 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002679 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002680}
Chris Lattner3b561a32006-08-13 00:12:11 +00002681
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002682/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002683/// start of a type-qualifier-list.
2684bool Parser::isTypeQualifier() const {
2685 switch (Tok.getKind()) {
2686 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002687
2688 // type-qualifier only in OpenCL
2689 case tok::kw_private:
2690 return getLang().OpenCL;
2691
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002692 // type-qualifier
2693 case tok::kw_const:
2694 case tok::kw_volatile:
2695 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002696 case tok::kw___private:
2697 case tok::kw___local:
2698 case tok::kw___global:
2699 case tok::kw___constant:
2700 case tok::kw___read_only:
2701 case tok::kw___read_write:
2702 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002703 return true;
2704 }
2705}
2706
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002707/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2708/// is definitely a type-specifier. Return false if it isn't part of a type
2709/// specifier or if we're not sure.
2710bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2711 switch (Tok.getKind()) {
2712 default: return false;
2713 // type-specifiers
2714 case tok::kw_short:
2715 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002716 case tok::kw___int64:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002717 case tok::kw_signed:
2718 case tok::kw_unsigned:
2719 case tok::kw__Complex:
2720 case tok::kw__Imaginary:
2721 case tok::kw_void:
2722 case tok::kw_char:
2723 case tok::kw_wchar_t:
2724 case tok::kw_char16_t:
2725 case tok::kw_char32_t:
2726 case tok::kw_int:
2727 case tok::kw_float:
2728 case tok::kw_double:
2729 case tok::kw_bool:
2730 case tok::kw__Bool:
2731 case tok::kw__Decimal32:
2732 case tok::kw__Decimal64:
2733 case tok::kw__Decimal128:
2734 case tok::kw___vector:
2735
2736 // struct-or-union-specifier (C99) or class-specifier (C++)
2737 case tok::kw_class:
2738 case tok::kw_struct:
2739 case tok::kw_union:
2740 // enum-specifier
2741 case tok::kw_enum:
2742
2743 // typedef-name
2744 case tok::annot_typename:
2745 return true;
2746 }
2747}
2748
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002749/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002750/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002751bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002752 switch (Tok.getKind()) {
2753 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002754
Chris Lattner020bab92009-01-04 23:41:41 +00002755 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002756 if (TryAltiVecVectorToken())
2757 return true;
2758 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002759 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002760 // Annotate typenames and C++ scope specifiers. If we get one, just
2761 // recurse to handle whatever we get.
2762 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002763 return true;
2764 if (Tok.is(tok::identifier))
2765 return false;
2766 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002767
Chris Lattner020bab92009-01-04 23:41:41 +00002768 case tok::coloncolon: // ::foo::bar
2769 if (NextToken().is(tok::kw_new) || // ::new
2770 NextToken().is(tok::kw_delete)) // ::delete
2771 return false;
2772
Chris Lattner020bab92009-01-04 23:41:41 +00002773 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002774 return true;
2775 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002776
Chris Lattnere37e2332006-08-15 04:50:22 +00002777 // GNU attributes support.
2778 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002779 // GNU typeof support.
2780 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002781
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002782 // type-specifiers
2783 case tok::kw_short:
2784 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002785 case tok::kw___int64:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002786 case tok::kw_signed:
2787 case tok::kw_unsigned:
2788 case tok::kw__Complex:
2789 case tok::kw__Imaginary:
2790 case tok::kw_void:
2791 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002792 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002793 case tok::kw_char16_t:
2794 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002795 case tok::kw_int:
2796 case tok::kw_float:
2797 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002798 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002799 case tok::kw__Bool:
2800 case tok::kw__Decimal32:
2801 case tok::kw__Decimal64:
2802 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002803 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002804
Chris Lattner861a2262008-04-13 18:59:07 +00002805 // struct-or-union-specifier (C99) or class-specifier (C++)
2806 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002807 case tok::kw_struct:
2808 case tok::kw_union:
2809 // enum-specifier
2810 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002811
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002812 // type-qualifier
2813 case tok::kw_const:
2814 case tok::kw_volatile:
2815 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002816
2817 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002818 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002819 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002820
Chris Lattner409bf7d2008-10-20 00:25:30 +00002821 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2822 case tok::less:
2823 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002824
Steve Naroff44ac7772008-12-25 14:16:32 +00002825 case tok::kw___cdecl:
2826 case tok::kw___stdcall:
2827 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002828 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002829 case tok::kw___w64:
2830 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002831 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002832
2833 case tok::kw___private:
2834 case tok::kw___local:
2835 case tok::kw___global:
2836 case tok::kw___constant:
2837 case tok::kw___read_only:
2838 case tok::kw___read_write:
2839 case tok::kw___write_only:
2840
Eli Friedman53339e02009-06-08 23:27:34 +00002841 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002842
2843 case tok::kw_private:
2844 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002845 }
2846}
2847
Chris Lattneracd58a32006-08-06 17:24:14 +00002848/// isDeclarationSpecifier() - Return true if the current token is part of a
2849/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002850///
2851/// \param DisambiguatingWithExpression True to indicate that the purpose of
2852/// this check is to disambiguate between an expression and a declaration.
2853bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002854 switch (Tok.getKind()) {
2855 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002856
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002857 case tok::kw_private:
2858 return getLang().OpenCL;
2859
Chris Lattner020bab92009-01-04 23:41:41 +00002860 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002861 // Unfortunate hack to support "Class.factoryMethod" notation.
2862 if (getLang().ObjC1 && NextToken().is(tok::period))
2863 return false;
John Thompson22334602010-02-05 00:12:22 +00002864 if (TryAltiVecVectorToken())
2865 return true;
2866 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002867 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002868 // Annotate typenames and C++ scope specifiers. If we get one, just
2869 // recurse to handle whatever we get.
2870 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002871 return true;
2872 if (Tok.is(tok::identifier))
2873 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002874
2875 // If we're in Objective-C and we have an Objective-C class type followed
2876 // by an identifier and then either ':' or ']', in a place where an
2877 // expression is permitted, then this is probably a class message send
2878 // missing the initial '['. In this case, we won't consider this to be
2879 // the start of a declaration.
2880 if (DisambiguatingWithExpression &&
2881 isStartOfObjCClassMessageMissingOpenBracket())
2882 return false;
2883
John McCall1f476a12010-02-26 08:45:28 +00002884 return isDeclarationSpecifier();
2885
Chris Lattner020bab92009-01-04 23:41:41 +00002886 case tok::coloncolon: // ::foo::bar
2887 if (NextToken().is(tok::kw_new) || // ::new
2888 NextToken().is(tok::kw_delete)) // ::delete
2889 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002890
Chris Lattner020bab92009-01-04 23:41:41 +00002891 // Annotate typenames and C++ scope specifiers. If we get one, just
2892 // recurse to handle whatever we get.
2893 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002894 return true;
2895 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002896
Chris Lattneracd58a32006-08-06 17:24:14 +00002897 // storage-class-specifier
2898 case tok::kw_typedef:
2899 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002900 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002901 case tok::kw_static:
2902 case tok::kw_auto:
2903 case tok::kw_register:
2904 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002905
Chris Lattneracd58a32006-08-06 17:24:14 +00002906 // type-specifiers
2907 case tok::kw_short:
2908 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002909 case tok::kw___int64:
Chris Lattneracd58a32006-08-06 17:24:14 +00002910 case tok::kw_signed:
2911 case tok::kw_unsigned:
2912 case tok::kw__Complex:
2913 case tok::kw__Imaginary:
2914 case tok::kw_void:
2915 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002916 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002917 case tok::kw_char16_t:
2918 case tok::kw_char32_t:
2919
Chris Lattneracd58a32006-08-06 17:24:14 +00002920 case tok::kw_int:
2921 case tok::kw_float:
2922 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002923 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002924 case tok::kw__Bool:
2925 case tok::kw__Decimal32:
2926 case tok::kw__Decimal64:
2927 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002928 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002929
Chris Lattner861a2262008-04-13 18:59:07 +00002930 // struct-or-union-specifier (C99) or class-specifier (C++)
2931 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002932 case tok::kw_struct:
2933 case tok::kw_union:
2934 // enum-specifier
2935 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002936
Chris Lattneracd58a32006-08-06 17:24:14 +00002937 // type-qualifier
2938 case tok::kw_const:
2939 case tok::kw_volatile:
2940 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002941
Chris Lattneracd58a32006-08-06 17:24:14 +00002942 // function-specifier
2943 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002944 case tok::kw_virtual:
2945 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002946
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00002947 // static_assert-declaration
2948 case tok::kw__Static_assert:
2949
Chris Lattner599e47e2007-08-09 17:01:07 +00002950 // GNU typeof support.
2951 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002952
Chris Lattner599e47e2007-08-09 17:01:07 +00002953 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002954 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002955 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002956
Chris Lattner8b2ec162008-07-26 03:38:44 +00002957 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2958 case tok::less:
2959 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002960
Douglas Gregor19b7acf2011-04-27 05:41:15 +00002961 // typedef-name
2962 case tok::annot_typename:
2963 return !DisambiguatingWithExpression ||
2964 !isStartOfObjCClassMessageMissingOpenBracket();
2965
Steve Narofff192fab2009-01-06 19:34:12 +00002966 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002967 case tok::kw___cdecl:
2968 case tok::kw___stdcall:
2969 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002970 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002971 case tok::kw___w64:
2972 case tok::kw___ptr64:
2973 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002974 case tok::kw___pascal:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002975
2976 case tok::kw___private:
2977 case tok::kw___local:
2978 case tok::kw___global:
2979 case tok::kw___constant:
2980 case tok::kw___read_only:
2981 case tok::kw___read_write:
2982 case tok::kw___write_only:
2983
Eli Friedman53339e02009-06-08 23:27:34 +00002984 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002985 }
2986}
2987
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002988bool Parser::isConstructorDeclarator() {
2989 TentativeParsingAction TPA(*this);
2990
2991 // Parse the C++ scope specifier.
2992 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002993 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002994 TPA.Revert();
2995 return false;
2996 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002997
2998 // Parse the constructor name.
2999 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3000 // We already know that we have a constructor name; just consume
3001 // the token.
3002 ConsumeToken();
3003 } else {
3004 TPA.Revert();
3005 return false;
3006 }
3007
3008 // Current class name must be followed by a left parentheses.
3009 if (Tok.isNot(tok::l_paren)) {
3010 TPA.Revert();
3011 return false;
3012 }
3013 ConsumeParen();
3014
3015 // A right parentheses or ellipsis signals that we have a constructor.
3016 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3017 TPA.Revert();
3018 return true;
3019 }
3020
3021 // If we need to, enter the specified scope.
3022 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003023 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003024 DeclScopeObj.EnterDeclaratorScope();
3025
Francois Pichet79f3a872011-01-31 04:54:32 +00003026 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003027 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003028 MaybeParseMicrosoftAttributes(Attrs);
3029
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003030 // Check whether the next token(s) are part of a declaration
3031 // specifier, in which case we have the start of a parameter and,
3032 // therefore, we know that this is a constructor.
3033 bool IsConstructor = isDeclarationSpecifier();
3034 TPA.Revert();
3035 return IsConstructor;
3036}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003037
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003038/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003039/// type-qualifier-list: [C99 6.7.5]
3040/// type-qualifier
3041/// [vendor] attributes
3042/// [ only if VendorAttributesAllowed=true ]
3043/// type-qualifier-list type-qualifier
3044/// [vendor] type-qualifier-list attributes
3045/// [ only if VendorAttributesAllowed=true ]
3046/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3047/// [ only if CXX0XAttributesAllowed=true ]
3048/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003049///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003050void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3051 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00003052 bool CXX0XAttributesAllowed) {
3053 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3054 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00003055 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003056 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003057 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00003058 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003059 else
3060 Diag(Loc, diag::err_attributes_not_allowed);
3061 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003062
3063 SourceLocation EndLoc;
3064
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003065 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003066 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003067 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003068 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003069 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003070
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003071 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003072 case tok::code_completion:
3073 Actions.CodeCompleteTypeQualifiers(DS);
3074 ConsumeCodeCompletionToken();
3075 break;
3076
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003077 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003078 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3079 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003080 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003081 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003082 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3083 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003084 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003085 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003086 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3087 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003088 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003089
3090 // OpenCL qualifiers:
3091 case tok::kw_private:
3092 if (!getLang().OpenCL)
3093 goto DoneWithTypeQuals;
3094 case tok::kw___private:
3095 case tok::kw___global:
3096 case tok::kw___local:
3097 case tok::kw___constant:
3098 case tok::kw___read_only:
3099 case tok::kw___write_only:
3100 case tok::kw___read_write:
3101 ParseOpenCLQualifiers(DS);
3102 break;
3103
Eli Friedman53339e02009-06-08 23:27:34 +00003104 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003105 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00003106 case tok::kw___cdecl:
3107 case tok::kw___stdcall:
3108 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003109 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003110 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003111 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003112 continue;
3113 }
3114 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003115 case tok::kw___pascal:
3116 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003117 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003118 continue;
3119 }
3120 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003121 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003122 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003123 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003124 continue; // do *not* consume the next token!
3125 }
3126 // otherwise, FALL THROUGH!
3127 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003128 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003129 // If this is not a type-qualifier token, we're done reading type
3130 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003131 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003132 if (EndLoc.isValid())
3133 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003134 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003135 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003136
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003137 // If the specifier combination wasn't legal, issue a diagnostic.
3138 if (isInvalid) {
3139 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003140 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003141 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003142 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003143 }
3144}
3145
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003146
3147/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3148///
3149void Parser::ParseDeclarator(Declarator &D) {
3150 /// This implements the 'declarator' production in the C grammar, then checks
3151 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003152 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003153}
3154
Sebastian Redlbd150f42008-11-21 19:14:01 +00003155/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3156/// is parsed by the function passed to it. Pass null, and the direct-declarator
3157/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003158/// ptr-operator production.
3159///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003160/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3161/// [C] pointer[opt] direct-declarator
3162/// [C++] direct-declarator
3163/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003164///
3165/// pointer: [C99 6.7.5]
3166/// '*' type-qualifier-list[opt]
3167/// '*' type-qualifier-list[opt] pointer
3168///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003169/// ptr-operator:
3170/// '*' cv-qualifier-seq[opt]
3171/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003172/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003173/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003174/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003175/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003176void Parser::ParseDeclaratorInternal(Declarator &D,
3177 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003178 if (Diags.hasAllExtensionsSilenced())
3179 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003180
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003181 // C++ member pointers start with a '::' or a nested-name.
3182 // Member pointers get special handling, since there's no place for the
3183 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003184 if (getLang().CPlusPlus &&
3185 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3186 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003187 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003188 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003189
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003190 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003191 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003192 // The scope spec really belongs to the direct-declarator.
3193 D.getCXXScopeSpec() = SS;
3194 if (DirectDeclParser)
3195 (this->*DirectDeclParser)(D);
3196 return;
3197 }
3198
3199 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003200 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003201 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003202 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003203 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003204
3205 // Recurse to parse whatever is left.
3206 ParseDeclaratorInternal(D, DirectDeclParser);
3207
3208 // Sema will have to catch (syntactically invalid) pointers into global
3209 // scope. It has to catch pointers into namespace scope anyway.
3210 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003211 Loc),
3212 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003213 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003214 return;
3215 }
3216 }
3217
3218 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003219 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003220 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003221 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003222 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003223 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003224 if (DirectDeclParser)
3225 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003226 return;
3227 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003228
Sebastian Redled0f3b02009-03-15 22:02:01 +00003229 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3230 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003231 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003232 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003233
Chris Lattner9eac9312009-03-27 04:18:06 +00003234 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003235 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003236 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003237
Bill Wendling3708c182007-05-27 10:15:43 +00003238 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003239 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003240
Bill Wendling3708c182007-05-27 10:15:43 +00003241 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003242 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003243 if (Kind == tok::star)
3244 // Remember that we parsed a pointer type, and remember the type-quals.
3245 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003246 DS.getConstSpecLoc(),
3247 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003248 DS.getRestrictSpecLoc()),
3249 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003250 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003251 else
3252 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003253 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003254 Loc),
3255 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003256 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003257 } else {
3258 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003259 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003260
Sebastian Redl3b27be62009-03-23 00:00:23 +00003261 // Complain about rvalue references in C++03, but then go on and build
3262 // the declarator.
3263 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003264 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003265
Bill Wendling93efb222007-06-02 23:28:54 +00003266 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3267 // cv-qualifiers are introduced through the use of a typedef or of a
3268 // template type argument, in which case the cv-qualifiers are ignored.
3269 //
3270 // [GNU] Retricted references are allowed.
3271 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003272 // [C++0x] Attributes on references are not allowed.
3273 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003274 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003275
3276 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3277 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3278 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003279 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003280 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3281 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003282 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003283 }
Bill Wendling3708c182007-05-27 10:15:43 +00003284
3285 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003286 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003287
Douglas Gregor66583c52008-11-03 15:51:28 +00003288 if (D.getNumTypeObjects() > 0) {
3289 // C++ [dcl.ref]p4: There shall be no references to references.
3290 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3291 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003292 if (const IdentifierInfo *II = D.getIdentifier())
3293 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3294 << II;
3295 else
3296 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3297 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003298
Sebastian Redlbd150f42008-11-21 19:14:01 +00003299 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003300 // can go ahead and build the (technically ill-formed)
3301 // declarator: reference collapsing will take care of it.
3302 }
3303 }
3304
Bill Wendling3708c182007-05-27 10:15:43 +00003305 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003306 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003307 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003308 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003309 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003310 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003311}
3312
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003313/// ParseDirectDeclarator
3314/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003315/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003316/// '(' declarator ')'
3317/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003318/// [C90] direct-declarator '[' constant-expression[opt] ']'
3319/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3320/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3321/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3322/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003323/// direct-declarator '(' parameter-type-list ')'
3324/// direct-declarator '(' identifier-list[opt] ')'
3325/// [GNU] direct-declarator '(' parameter-forward-declarations
3326/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003327/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3328/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003329/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003330///
3331/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003332/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003333/// '::'[opt] nested-name-specifier[opt] type-name
3334///
3335/// id-expression: [C++ 5.1]
3336/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003337/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003338///
3339/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003340/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003341/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003342/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003343/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003344/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003345///
Chris Lattneracd58a32006-08-06 17:24:14 +00003346void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003347 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003348
Douglas Gregor7861a802009-11-03 01:35:08 +00003349 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3350 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003351 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003352 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003353 }
3354
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003355 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003356 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003357 // Change the declaration context for name lookup, until this function
3358 // is exited (and the declarator has been parsed).
3359 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003360 }
3361
Douglas Gregor27b4c162010-12-23 22:44:42 +00003362 // C++0x [dcl.fct]p14:
3363 // There is a syntactic ambiguity when an ellipsis occurs at the end
3364 // of a parameter-declaration-clause without a preceding comma. In
3365 // this case, the ellipsis is parsed as part of the
3366 // abstract-declarator if the type of the parameter names a template
3367 // parameter pack that has not been expanded; otherwise, it is parsed
3368 // as part of the parameter-declaration-clause.
3369 if (Tok.is(tok::ellipsis) &&
3370 !((D.getContext() == Declarator::PrototypeContext ||
3371 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003372 NextToken().is(tok::r_paren) &&
3373 !Actions.containsUnexpandedParameterPacks(D)))
3374 D.setEllipsisLoc(ConsumeToken());
3375
Douglas Gregor7861a802009-11-03 01:35:08 +00003376 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3377 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3378 // We found something that indicates the start of an unqualified-id.
3379 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003380 bool AllowConstructorName;
3381 if (D.getDeclSpec().hasTypeSpecifier())
3382 AllowConstructorName = false;
3383 else if (D.getCXXScopeSpec().isSet())
3384 AllowConstructorName =
3385 (D.getContext() == Declarator::FileContext ||
3386 (D.getContext() == Declarator::MemberContext &&
3387 D.getDeclSpec().isFriendSpecified()));
3388 else
3389 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3390
Douglas Gregor7861a802009-11-03 01:35:08 +00003391 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3392 /*EnteringContext=*/true,
3393 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003394 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003395 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003396 D.getName()) ||
3397 // Once we're past the identifier, if the scope was bad, mark the
3398 // whole declarator bad.
3399 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003400 D.SetIdentifier(0, Tok.getLocation());
3401 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003402 } else {
3403 // Parsed the unqualified-id; update range information and move along.
3404 if (D.getSourceRange().getBegin().isInvalid())
3405 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3406 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003407 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003408 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003409 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003410 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003411 assert(!getLang().CPlusPlus &&
3412 "There's a C++-specific check for tok::identifier above");
3413 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3414 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3415 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003416 goto PastIdentifier;
3417 }
3418
3419 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003420 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003421 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003422 // Example: 'char (*X)' or 'int (*XX)(void)'
3423 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003424
3425 // If the declarator was parenthesized, we entered the declarator
3426 // scope when parsing the parenthesized declarator, then exited
3427 // the scope already. Re-enter the scope, if we need to.
3428 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003429 // If there was an error parsing parenthesized declarator, declarator
3430 // scope may have been enterred before. Don't do it again.
3431 if (!D.isInvalidType() &&
3432 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003433 // Change the declaration context for name lookup, until this function
3434 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003435 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003436 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003437 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003438 // This could be something simple like "int" (in which case the declarator
3439 // portion is empty), if an abstract-declarator is allowed.
3440 D.SetIdentifier(0, Tok.getLocation());
3441 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003442 if (D.getContext() == Declarator::MemberContext)
3443 Diag(Tok, diag::err_expected_member_name_or_semi)
3444 << D.getDeclSpec().getSourceRange();
3445 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003446 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003447 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003448 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003449 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003450 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003451 }
Mike Stump11289f42009-09-09 15:08:12 +00003452
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003453 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003454 assert(D.isPastIdentifier() &&
3455 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003456
Alexis Hunt96d5c762009-11-21 08:43:09 +00003457 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003458 if (D.getIdentifier())
3459 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003460
Chris Lattneracd58a32006-08-06 17:24:14 +00003461 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003462 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003463 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3464 // In such a case, check if we actually have a function declarator; if it
3465 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003466 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3467 // When not in file scope, warn for ambiguous function declarators, just
3468 // in case the author intended it as a variable definition.
3469 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3470 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3471 break;
3472 }
John McCall084e83d2011-03-24 11:26:52 +00003473 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003474 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003475 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003476 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003477 } else {
3478 break;
3479 }
3480 }
3481}
3482
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003483/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3484/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003485/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003486/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3487///
3488/// direct-declarator:
3489/// '(' declarator ')'
3490/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003491/// direct-declarator '(' parameter-type-list ')'
3492/// direct-declarator '(' identifier-list[opt] ')'
3493/// [GNU] direct-declarator '(' parameter-forward-declarations
3494/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003495///
3496void Parser::ParseParenDeclarator(Declarator &D) {
3497 SourceLocation StartLoc = ConsumeParen();
3498 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003499
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003500 // Eat any attributes before we look at whether this is a grouping or function
3501 // declarator paren. If this is a grouping paren, the attribute applies to
3502 // the type being built up, for example:
3503 // int (__attribute__(()) *x)(long y)
3504 // If this ends up not being a grouping paren, the attribute applies to the
3505 // first argument, for example:
3506 // int (__attribute__(()) int x)
3507 // In either case, we need to eat any attributes to be able to determine what
3508 // sort of paren this is.
3509 //
John McCall084e83d2011-03-24 11:26:52 +00003510 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003511 bool RequiresArg = false;
3512 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003513 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003514
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003515 // We require that the argument list (if this is a non-grouping paren) be
3516 // present even if the attribute list was empty.
3517 RequiresArg = true;
3518 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003519 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003520 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003521 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3522 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall53fa7142010-12-24 02:08:15 +00003523 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003524 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003525 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003526 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003527 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003528
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003529 // If we haven't past the identifier yet (or where the identifier would be
3530 // stored, if this is an abstract declarator), then this is probably just
3531 // grouping parens. However, if this could be an abstract-declarator, then
3532 // this could also be the start of function arguments (consider 'void()').
3533 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003534
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003535 if (!D.mayOmitIdentifier()) {
3536 // If this can't be an abstract-declarator, this *must* be a grouping
3537 // paren, because we haven't seen the identifier yet.
3538 isGrouping = true;
3539 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003540 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003541 isDeclarationSpecifier()) { // 'int(int)' is a function.
3542 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3543 // considered to be a type, not a K&R identifier-list.
3544 isGrouping = false;
3545 } else {
3546 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3547 isGrouping = true;
3548 }
Mike Stump11289f42009-09-09 15:08:12 +00003549
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003550 // If this is a grouping paren, handle:
3551 // direct-declarator: '(' declarator ')'
3552 // direct-declarator: '(' attributes declarator ')'
3553 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003554 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003555 D.setGroupingParens(true);
3556
Sebastian Redlbd150f42008-11-21 19:14:01 +00003557 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003558 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003559 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003560 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3561 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003562
3563 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003564 return;
3565 }
Mike Stump11289f42009-09-09 15:08:12 +00003566
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003567 // Okay, if this wasn't a grouping paren, it must be the start of a function
3568 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003569 // identifier (and remember where it would have been), then call into
3570 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003571 D.SetIdentifier(0, Tok.getLocation());
3572
John McCall53fa7142010-12-24 02:08:15 +00003573 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003574}
3575
3576/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3577/// declarator D up to a paren, which indicates that we are parsing function
3578/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003579///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003580/// If AttrList is non-null, then the caller parsed those arguments immediately
3581/// after the open paren - they should be considered to be the first argument of
3582/// a parameter. If RequiresArg is true, then the first argument of the
3583/// function is required to be present and required to not be an identifier
3584/// list.
3585///
Chris Lattneracd58a32006-08-06 17:24:14 +00003586/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003587/// parameter-type-list: [C99 6.7.5]
3588/// parameter-list
3589/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003590/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003591///
3592/// parameter-list: [C99 6.7.5]
3593/// parameter-declaration
3594/// parameter-list ',' parameter-declaration
3595///
3596/// parameter-declaration: [C99 6.7.5]
3597/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003598/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003599/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003600/// declaration-specifiers abstract-declarator[opt]
3601/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003602/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003603/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003604///
Douglas Gregor54992352011-01-26 03:43:54 +00003605/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3606/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003607///
Sebastian Redl965b0e32011-03-05 14:45:16 +00003608/// [C++0x] exception-specification:
3609/// dynamic-exception-specification
3610/// noexcept-specification
3611///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003612void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall53fa7142010-12-24 02:08:15 +00003613 ParsedAttributes &attrs,
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003614 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003615 // lparen is already consumed!
3616 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003617
Douglas Gregor7fb25412010-10-01 18:44:50 +00003618 ParsedType TrailingReturnType;
3619
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003620 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003621 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003622 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003623 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003624
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003625 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003626
3627 // cv-qualifier-seq[opt].
John McCall084e83d2011-03-24 11:26:52 +00003628 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003629 SourceLocation RefQualifierLoc;
3630 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003631 ExceptionSpecificationType ESpecType = EST_None;
3632 SourceRange ESpecRange;
3633 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3634 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3635 ExprResult NoexceptExpr;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003636 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003637 MaybeParseCXX0XAttributes(attrs);
3638
Chris Lattnercf0bab22008-12-18 07:02:59 +00003639 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003640 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003641 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003642
Douglas Gregor54992352011-01-26 03:43:54 +00003643 // Parse ref-qualifier[opt]
3644 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3645 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003646 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003647
Douglas Gregor54992352011-01-26 03:43:54 +00003648 RefQualifierIsLValueRef = Tok.is(tok::amp);
3649 RefQualifierLoc = ConsumeToken();
3650 EndLoc = RefQualifierLoc;
3651 }
Sebastian Redl965b0e32011-03-05 14:45:16 +00003652
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003653 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003654 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3655 DynamicExceptions,
3656 DynamicExceptionRanges,
3657 NoexceptExpr);
3658 if (ESpecType != EST_None)
3659 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003660
3661 // Parse trailing-return-type.
3662 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3663 TrailingReturnType = ParseTrailingReturnType().get();
3664 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003665 }
3666
Chris Lattner371ed4e2008-04-06 06:57:35 +00003667 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003668 // int() -> no prototype, no '...'.
John McCall084e83d2011-03-24 11:26:52 +00003669 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003670 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003671 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003672 /*arglist*/ 0, 0,
3673 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003674 RefQualifierIsLValueRef,
3675 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003676 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003677 DynamicExceptions.data(),
3678 DynamicExceptionRanges.data(),
3679 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003680 NoexceptExpr.isUsable() ?
3681 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003682 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003683 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003684 attrs, EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003685 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003686 }
3687
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003688 // Alternatively, this parameter list may be an identifier list form for a
3689 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003690 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3691 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003692 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003693 // K&R identifier lists can't have typedefs as identifiers, per
3694 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003695 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003696 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003697
Steve Naroffb0486722009-01-28 19:16:40 +00003698 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003699 // normal declarators, not for abstract-declarators. Get the first
3700 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003701 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003702 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003703
3704 // Identifier lists follow a really simple grammar: the identifiers can
3705 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3706 // identifier lists are really rare in the brave new modern world, and it
3707 // is very common for someone to typo a type in a non-k&r style list. If
3708 // we are presented with something like: "void foo(intptr x, float y)",
3709 // we don't want to start parsing the function declarator as though it is
3710 // a K&R style declarator just because intptr is an invalid type.
3711 //
3712 // To handle this, we check to see if the token after the first identifier
3713 // is a "," or ")". Only if so, do we parse it as an identifier list.
3714 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3715 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3716 FirstTok.getIdentifierInfo(),
3717 FirstTok.getLocation(), D);
3718
3719 // If we get here, the code is invalid. Push the first identifier back
3720 // into the token stream and parse the first argument as an (invalid)
3721 // normal argument declarator.
3722 PP.EnterToken(Tok);
3723 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003724 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003725 }
Mike Stump11289f42009-09-09 15:08:12 +00003726
Chris Lattner371ed4e2008-04-06 06:57:35 +00003727 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003728
Chris Lattner371ed4e2008-04-06 06:57:35 +00003729 // Build up an array of information about the parsed arguments.
3730 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003731
3732 // Enter function-declaration scope, limiting any declarators to the
3733 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003734 ParseScope PrototypeScope(this,
3735 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003736
Chris Lattner371ed4e2008-04-06 06:57:35 +00003737 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003738 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003739 while (1) {
3740 if (Tok.is(tok::ellipsis)) {
3741 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003742 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003743 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003744 }
Mike Stump11289f42009-09-09 15:08:12 +00003745
Chris Lattner371ed4e2008-04-06 06:57:35 +00003746 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003747 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00003748 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003749
3750 // Skip any Microsoft attributes before a param.
3751 if (getLang().Microsoft && Tok.is(tok::l_square))
3752 ParseMicrosoftAttributes(DS.getAttributes());
3753
3754 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003755
3756 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00003757 // Take them so that we only apply the attributes to the first parameter.
3758 DS.takeAttributesFrom(attrs);
3759
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003760 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003761
Chris Lattner371ed4e2008-04-06 06:57:35 +00003762 // Parse the declarator. This is "PrototypeContext", because we must
3763 // accept either 'declarator' or 'abstract-declarator' here.
3764 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3765 ParseDeclarator(ParmDecl);
3766
3767 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00003768 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003769
Chris Lattner371ed4e2008-04-06 06:57:35 +00003770 // Remember this parsed parameter in ParamInfo.
3771 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003772
Douglas Gregor4d87df52008-12-16 21:30:33 +00003773 // DefArgToks is used when the parsing of default arguments needs
3774 // to be delayed.
3775 CachedTokens *DefArgToks = 0;
3776
Chris Lattner371ed4e2008-04-06 06:57:35 +00003777 // If no parameter was specified, verify that *something* was specified,
3778 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003779 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3780 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003781 // Completely missing, emit error.
3782 Diag(DSStart, diag::err_missing_param);
3783 } else {
3784 // Otherwise, we have something. Add it and let semantic analysis try
3785 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003786
Chris Lattner371ed4e2008-04-06 06:57:35 +00003787 // Inform the actions module about the parameter declarator, so it gets
3788 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003789 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003790
3791 // Parse the default argument, if any. We parse the default
3792 // arguments in all dialects; the semantic analysis in
3793 // ActOnParamDefaultArgument will reject the default argument in
3794 // C.
3795 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003796 SourceLocation EqualLoc = Tok.getLocation();
3797
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003798 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003799 if (D.getContext() == Declarator::MemberContext) {
3800 // If we're inside a class definition, cache the tokens
3801 // corresponding to the default argument. We'll actually parse
3802 // them when we see the end of the class definition.
3803 // FIXME: Templates will require something similar.
3804 // FIXME: Can we use a smart pointer for Toks?
3805 DefArgToks = new CachedTokens;
3806
Mike Stump11289f42009-09-09 15:08:12 +00003807 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003808 /*StopAtSemi=*/true,
3809 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003810 delete DefArgToks;
3811 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003812 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003813 } else {
3814 // Mark the end of the default argument so that we know when to
3815 // stop when we parse it later on.
3816 Token DefArgEnd;
3817 DefArgEnd.startToken();
3818 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3819 DefArgEnd.setLocation(Tok.getLocation());
3820 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003821 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003822 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003823 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003824 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003825 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003826 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003827
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003828 // The argument isn't actually potentially evaluated unless it is
3829 // used.
3830 EnterExpressionEvaluationContext Eval(Actions,
3831 Sema::PotentiallyEvaluatedIfUsed);
3832
John McCalldadc5752010-08-24 06:29:42 +00003833 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003834 if (DefArgResult.isInvalid()) {
3835 Actions.ActOnParamDefaultArgumentError(Param);
3836 SkipUntil(tok::comma, tok::r_paren, true, true);
3837 } else {
3838 // Inform the actions module about the default argument
3839 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003840 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003841 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003842 }
3843 }
Mike Stump11289f42009-09-09 15:08:12 +00003844
3845 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3846 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003847 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003848 }
3849
3850 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003851 if (Tok.isNot(tok::comma)) {
3852 if (Tok.is(tok::ellipsis)) {
3853 IsVariadic = true;
3854 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3855
3856 if (!getLang().CPlusPlus) {
3857 // We have ellipsis without a preceding ',', which is ill-formed
3858 // in C. Complain and provide the fix.
3859 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003860 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003861 }
3862 }
3863
3864 break;
3865 }
Mike Stump11289f42009-09-09 15:08:12 +00003866
Chris Lattner371ed4e2008-04-06 06:57:35 +00003867 // Consume the comma.
3868 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003869 }
Mike Stump11289f42009-09-09 15:08:12 +00003870
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003871 // If we have the closing ')', eat it.
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003872 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003873
John McCall084e83d2011-03-24 11:26:52 +00003874 DeclSpec DS(AttrFactory);
Douglas Gregor54992352011-01-26 03:43:54 +00003875 SourceLocation RefQualifierLoc;
3876 bool RefQualifierIsLValueRef = true;
Sebastian Redl965b0e32011-03-05 14:45:16 +00003877 ExceptionSpecificationType ESpecType = EST_None;
3878 SourceRange ESpecRange;
3879 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3880 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3881 ExprResult NoexceptExpr;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003882
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003883 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003884 MaybeParseCXX0XAttributes(attrs);
3885
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003886 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003887 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003888 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003889 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003890
Douglas Gregor54992352011-01-26 03:43:54 +00003891 // Parse ref-qualifier[opt]
3892 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3893 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003894 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003895
3896 RefQualifierIsLValueRef = Tok.is(tok::amp);
3897 RefQualifierLoc = ConsumeToken();
3898 EndLoc = RefQualifierLoc;
3899 }
3900
Sebastian Redl965b0e32011-03-05 14:45:16 +00003901 // FIXME: We should leave the prototype scope before parsing the exception
3902 // specification, and then reenter it when parsing the trailing return type.
3903 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3904
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003905 // Parse exception-specification[opt].
Sebastian Redl965b0e32011-03-05 14:45:16 +00003906 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3907 DynamicExceptions,
3908 DynamicExceptionRanges,
3909 NoexceptExpr);
3910 if (ESpecType != EST_None)
3911 EndLoc = ESpecRange.getEnd();
Douglas Gregor7fb25412010-10-01 18:44:50 +00003912
3913 // Parse trailing-return-type.
3914 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3915 TrailingReturnType = ParseTrailingReturnType().get();
3916 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003917 }
3918
Douglas Gregor7fb25412010-10-01 18:44:50 +00003919 // Leave prototype scope.
3920 PrototypeScope.Exit();
3921
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003922 // Remember that we parsed a function type, and remember the attributes.
John McCall084e83d2011-03-24 11:26:52 +00003923 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003924 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003925 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003926 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003927 RefQualifierIsLValueRef,
3928 RefQualifierLoc,
Sebastian Redl802a4532011-03-05 22:42:13 +00003929 ESpecType, ESpecRange.getBegin(),
Sebastian Redl965b0e32011-03-05 14:45:16 +00003930 DynamicExceptions.data(),
3931 DynamicExceptionRanges.data(),
3932 DynamicExceptions.size(),
Sebastian Redl802a4532011-03-05 22:42:13 +00003933 NoexceptExpr.isUsable() ?
3934 NoexceptExpr.get() : 0,
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003935 LParenLoc, EndLoc, D,
Douglas Gregor7fb25412010-10-01 18:44:50 +00003936 TrailingReturnType),
John McCall084e83d2011-03-24 11:26:52 +00003937 attrs, EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003938}
Chris Lattneracd58a32006-08-06 17:24:14 +00003939
Chris Lattner6c940e62008-04-06 06:34:08 +00003940/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3941/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003942/// first identifier has already been consumed, and the current token is the
3943/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003944///
3945/// identifier-list: [C99 6.7.5]
3946/// identifier
3947/// identifier-list ',' identifier
3948///
3949void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003950 IdentifierInfo *FirstIdent,
3951 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003952 Declarator &D) {
3953 // Build up an array of information about the parsed arguments.
3954 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3955 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003956
Chris Lattner6c940e62008-04-06 06:34:08 +00003957 // If there was no identifier specified for the declarator, either we are in
3958 // an abstract-declarator, or we are in a parameter declarator which was found
3959 // to be abstract. In abstract-declarators, identifier lists are not valid:
3960 // diagnose this.
3961 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003962 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003963
Chris Lattner9453ab82010-05-14 17:23:36 +00003964 // The first identifier was already read, and is known to be the first
3965 // identifier in the list. Remember this identifier in ParamInfo.
3966 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003967 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003968
Chris Lattner6c940e62008-04-06 06:34:08 +00003969 while (Tok.is(tok::comma)) {
3970 // Eat the comma.
3971 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003972
Chris Lattner9186f552008-04-06 06:39:19 +00003973 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003974 if (Tok.isNot(tok::identifier)) {
3975 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003976 SkipUntil(tok::r_paren);
3977 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003978 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003979
Chris Lattner6c940e62008-04-06 06:34:08 +00003980 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003981
3982 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003983 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003984 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003985
Chris Lattner6c940e62008-04-06 06:34:08 +00003986 // Verify that the argument identifier has not already been mentioned.
3987 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003988 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003989 } else {
3990 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003991 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003992 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003993 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003994 }
Mike Stump11289f42009-09-09 15:08:12 +00003995
Chris Lattner6c940e62008-04-06 06:34:08 +00003996 // Eat the identifier.
3997 ConsumeToken();
3998 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003999
4000 // If we have the closing ')', eat it and we're done.
4001 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
4002
Chris Lattner9186f552008-04-06 06:39:19 +00004003 // Remember that we parsed a function type, and remember the attributes. This
4004 // function type is always a K&R style function type, which is not varargs and
4005 // has no prototype.
John McCall084e83d2011-03-24 11:26:52 +00004006 ParsedAttributes attrs(AttrFactory);
4007 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00004008 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00004009 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00004010 /*TypeQuals*/0,
Douglas Gregor54992352011-01-26 03:43:54 +00004011 true, SourceLocation(),
Sebastian Redl802a4532011-03-05 22:42:13 +00004012 EST_None, SourceLocation(), 0, 0,
4013 0, 0, LParenLoc, RLoc, D),
John McCall084e83d2011-03-24 11:26:52 +00004014 attrs, RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00004015}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004016
Chris Lattnere8074e62006-08-06 18:30:15 +00004017/// [C90] direct-declarator '[' constant-expression[opt] ']'
4018/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4019/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4020/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4021/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4022void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00004023 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00004024
Chris Lattner84a11622008-12-18 07:27:21 +00004025 // C array syntax has many features, but by-far the most common is [] and [4].
4026 // This code does a fast path to handle some of the most obvious cases.
4027 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004028 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004029 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004030 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004031
Chris Lattner84a11622008-12-18 07:27:21 +00004032 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004033 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004034 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00004035 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004036 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004037 return;
4038 } else if (Tok.getKind() == tok::numeric_constant &&
4039 GetLookAheadToken(1).is(tok::r_square)) {
4040 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00004041 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00004042 ConsumeToken();
4043
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004044 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004045 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004046 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004047
Chris Lattner84a11622008-12-18 07:27:21 +00004048 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004049 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004050 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00004051 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004052 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004053 return;
4054 }
Mike Stump11289f42009-09-09 15:08:12 +00004055
Chris Lattnere8074e62006-08-06 18:30:15 +00004056 // If valid, this location is the position where we read the 'static' keyword.
4057 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004058 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004059 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004060
Chris Lattnere8074e62006-08-06 18:30:15 +00004061 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004062 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004063 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004064 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004065
Chris Lattnere8074e62006-08-06 18:30:15 +00004066 // If we haven't already read 'static', check to see if there is one after the
4067 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004068 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004069 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004070
Chris Lattnere8074e62006-08-06 18:30:15 +00004071 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004072 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004073 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004074
Chris Lattner521ff2b2008-04-06 05:26:30 +00004075 // Handle the case where we have '[*]' as the array size. However, a leading
4076 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4077 // the the token after the star is a ']'. Since stars in arrays are
4078 // infrequent, use of lookahead is not costly here.
4079 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004080 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004081
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004082 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004083 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004084 StaticLoc = SourceLocation(); // Drop the static.
4085 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004086 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004087 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004088 // Note, in C89, this production uses the constant-expr production instead
4089 // of assignment-expr. The only difference is that assignment-expr allows
4090 // things like '=' and '*='. Sema rejects these in C89 mode because they
4091 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004092
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004093 // Parse the constant-expression or assignment-expression now (depending
4094 // on dialect).
4095 if (getLang().CPlusPlus)
4096 NumElements = ParseConstantExpression();
4097 else
4098 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004099 }
Mike Stump11289f42009-09-09 15:08:12 +00004100
Chris Lattner62591722006-08-12 18:40:58 +00004101 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004102 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004103 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004104 // If the expression was invalid, skip it.
4105 SkipUntil(tok::r_square);
4106 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004107 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004108
4109 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4110
John McCall084e83d2011-03-24 11:26:52 +00004111 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004112 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004113
Chris Lattner84a11622008-12-18 07:27:21 +00004114 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004115 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004116 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004117 NumElements.release(),
4118 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004119 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004120}
4121
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004122/// [GNU] typeof-specifier:
4123/// typeof ( expressions )
4124/// typeof ( type-name )
4125/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004126///
4127void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004128 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004129 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004130 SourceLocation StartLoc = ConsumeToken();
4131
John McCalle8595032010-01-13 20:03:27 +00004132 const bool hasParens = Tok.is(tok::l_paren);
4133
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004134 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004135 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004136 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004137 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4138 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004139 if (hasParens)
4140 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004141
4142 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004143 // FIXME: Not accurate, the range gets one token more than it should.
4144 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004145 else
4146 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004147
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004148 if (isCastExpr) {
4149 if (!CastTy) {
4150 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004151 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004152 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004153
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004154 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004155 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004156 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4157 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004158 DiagID, CastTy))
4159 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004160 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004161 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004162
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004163 // If we get here, the operand to the typeof was an expresion.
4164 if (Operand.isInvalid()) {
4165 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004166 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004167 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004168
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004169 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004170 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004171 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4172 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004173 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004174 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004175}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004176
4177
4178/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4179/// from TryAltiVecVectorToken.
4180bool Parser::TryAltiVecVectorTokenOutOfLine() {
4181 Token Next = NextToken();
4182 switch (Next.getKind()) {
4183 default: return false;
4184 case tok::kw_short:
4185 case tok::kw_long:
4186 case tok::kw_signed:
4187 case tok::kw_unsigned:
4188 case tok::kw_void:
4189 case tok::kw_char:
4190 case tok::kw_int:
4191 case tok::kw_float:
4192 case tok::kw_double:
4193 case tok::kw_bool:
4194 case tok::kw___pixel:
4195 Tok.setKind(tok::kw___vector);
4196 return true;
4197 case tok::identifier:
4198 if (Next.getIdentifierInfo() == Ident_pixel) {
4199 Tok.setKind(tok::kw___vector);
4200 return true;
4201 }
4202 return false;
4203 }
4204}
4205
4206bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4207 const char *&PrevSpec, unsigned &DiagID,
4208 bool &isInvalid) {
4209 if (Tok.getIdentifierInfo() == Ident_vector) {
4210 Token Next = NextToken();
4211 switch (Next.getKind()) {
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 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4224 return true;
4225 case tok::identifier:
4226 if (Next.getIdentifierInfo() == Ident_pixel) {
4227 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4228 return true;
4229 }
4230 break;
4231 default:
4232 break;
4233 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004234 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004235 DS.isTypeAltiVecVector()) {
4236 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4237 return true;
4238 }
4239 return false;
4240}