blob: 9023b34fa9c9ba9a7cb40673842aa66ea6554f60 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/Scope.h"
18#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000019#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000020#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000021#include "llvm/ADT/SmallSet.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000022#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000023using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// C99 6.7: Declarations.
27//===----------------------------------------------------------------------===//
28
Chris Lattnerf5fbd792006-08-10 23:56:11 +000029/// ParseTypeName
30/// type-name: [C99 6.7.6]
31/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000032///
33/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000034TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000035 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000036 ObjCDeclSpec *objcQuals,
37 AccessSpecifier AS,
38 Decl **OwnedType) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000039 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000040 DeclSpec DS(AttrFactory);
John McCall31168b02011-06-15 23:02:42 +000041 DS.setObjCQualifiers(objcQuals);
Richard Smithcd1c0552011-07-01 19:46:12 +000042 ParseSpecifierQualifierList(DS, AS);
43 if (OwnedType)
44 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000045
Chris Lattnerf5fbd792006-08-10 23:56:11 +000046 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000047 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000048 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000049 if (Range)
50 *Range = DeclaratorInfo.getSourceRange();
51
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000052 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000053 return true;
54
Douglas Gregor0be31a22010-07-02 17:43:08 +000055 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000056}
57
Alexis Hunt96d5c762009-11-21 08:43:09 +000058/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000059///
60/// [GNU] attributes:
61/// attribute
62/// attributes attribute
63///
64/// [GNU] attribute:
65/// '__attribute__' '(' '(' attribute-list ')' ')'
66///
67/// [GNU] attribute-list:
68/// attrib
69/// attribute_list ',' attrib
70///
71/// [GNU] attrib:
72/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000073/// attrib-name
74/// attrib-name '(' identifier ')'
75/// attrib-name '(' identifier ',' nonempty-expr-list ')'
76/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000077///
Steve Naroff0f2fe172007-06-01 17:11:19 +000078/// [GNU] attrib-name:
79/// identifier
80/// typespec
81/// typequal
82/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000083///
Steve Naroff0f2fe172007-06-01 17:11:19 +000084/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000085/// token lookahead. Comment from gcc: "If they start with an identifier
86/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +000087/// start with that identifier; otherwise they are an expression list."
88///
89/// At the moment, I am not doing 2 token lookahead. I am also unaware of
90/// any attributes that don't work (based on my limited testing). Most
91/// attributes are very simple in practice. Until we find a bug, I don't see
92/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000093
John McCall53fa7142010-12-24 02:08:15 +000094void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
95 SourceLocation *endLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +000096 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000097
Chris Lattner76c72282007-10-09 17:33:22 +000098 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000099 ConsumeToken();
100 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
101 "attribute")) {
102 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000103 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000104 }
105 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
106 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000107 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000108 }
109 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000110 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
111 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000112
113 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000114 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
115 ConsumeToken();
116 continue;
117 }
118 // we have an identifier or declaration specifier (const, int, etc.)
119 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
120 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000121
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000122 // Availability attributes have their own grammar.
123 if (AttrName->isStr("availability"))
124 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000125 // Thread safety attributes fit into the FIXME case above, so we
126 // just parse the arguments as a list of expressions
127 else if (IsThreadSafetyAttribute(AttrName->getName()))
128 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, attrs, endLoc);
Douglas Gregora2f49452010-03-16 19:09:18 +0000129 // check if we have a "parameterized" attribute
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000130 else if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000131 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000132
Chris Lattner76c72282007-10-09 17:33:22 +0000133 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000134 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
135 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000136
137 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000138 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000139 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000140 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
141 ParmName, ParmLoc, 0, 0);
Chris Lattner76c72282007-10-09 17:33:22 +0000142 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000143 ConsumeToken();
144 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000145 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000146 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000147
Steve Naroff0f2fe172007-06-01 17:11:19 +0000148 // now parse the non-empty comma separated list of expressions
149 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000150 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000151 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000152 ArgExprsOk = false;
153 SkipUntil(tok::r_paren);
154 break;
155 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000156 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000157 }
Chris Lattner76c72282007-10-09 17:33:22 +0000158 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000159 break;
160 ConsumeToken(); // Eat the comma, move to the next argument
161 }
Chris Lattner76c72282007-10-09 17:33:22 +0000162 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000163 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000164 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
165 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000166 }
167 }
168 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000169 switch (Tok.getKind()) {
170 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000171 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000172 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000173 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000174 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
175 0, SourceLocation(), 0, 0);
Nate Begemanf2758702009-06-26 06:32:41 +0000176 break;
177 case tok::kw_char:
178 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000179 case tok::kw_char16_t:
180 case tok::kw_char32_t:
Nate Begemanf2758702009-06-26 06:32:41 +0000181 case tok::kw_bool:
182 case tok::kw_short:
183 case tok::kw_int:
184 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +0000185 case tok::kw___int64:
Nate Begemanf2758702009-06-26 06:32:41 +0000186 case tok::kw_signed:
187 case tok::kw_unsigned:
188 case tok::kw_float:
189 case tok::kw_double:
190 case tok::kw_void:
John McCall53fa7142010-12-24 02:08:15 +0000191 case tok::kw_typeof: {
192 AttributeList *attr
John McCall084e83d2011-03-24 11:26:52 +0000193 = attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
194 0, SourceLocation(), 0, 0);
John McCall53fa7142010-12-24 02:08:15 +0000195 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian9d7d3d82010-08-17 23:19:16 +0000196 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begemanf2758702009-06-26 06:32:41 +0000197 // If it's a builtin type name, eat it and expect a rparen
198 // __attribute__(( vec_type_hint(char) ))
199 ConsumeToken();
Nate Begemanf2758702009-06-26 06:32:41 +0000200 if (Tok.is(tok::r_paren))
201 ConsumeParen();
202 break;
John McCall53fa7142010-12-24 02:08:15 +0000203 }
Nate Begemanf2758702009-06-26 06:32:41 +0000204 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000205 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000206 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000207 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000208
Steve Naroff0f2fe172007-06-01 17:11:19 +0000209 // now parse the list of expressions
210 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000211 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000212 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000213 ArgExprsOk = false;
214 SkipUntil(tok::r_paren);
215 break;
216 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000217 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000218 }
Chris Lattner76c72282007-10-09 17:33:22 +0000219 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000220 break;
221 ConsumeToken(); // Eat the comma, move to the next argument
222 }
223 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000224 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000225 ConsumeParen(); // ignore the right paren loc for now
John McCall084e83d2011-03-24 11:26:52 +0000226 attrs.addNew(AttrName, AttrNameLoc, 0,
227 AttrNameLoc, 0, SourceLocation(),
228 ArgExprs.take(), ArgExprs.size());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000229 }
Nate Begemanf2758702009-06-26 06:32:41 +0000230 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000231 }
232 }
233 } else {
John McCall084e83d2011-03-24 11:26:52 +0000234 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
235 0, SourceLocation(), 0, 0);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000236 }
237 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000238 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000239 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000240 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000241 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
242 SkipUntil(tok::r_paren, false);
243 }
John McCall53fa7142010-12-24 02:08:15 +0000244 if (endLoc)
245 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000246 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000247}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000248
Eli Friedman06de2b52009-06-08 07:21:15 +0000249/// ParseMicrosoftDeclSpec - Parse an __declspec construct
250///
251/// [MS] decl-specifier:
252/// __declspec ( extended-decl-modifier-seq )
253///
254/// [MS] extended-decl-modifier-seq:
255/// extended-decl-modifier[opt]
256/// extended-decl-modifier extended-decl-modifier-seq
257
John McCall53fa7142010-12-24 02:08:15 +0000258void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000259 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000260
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000261 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000262 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
263 "declspec")) {
264 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000265 return;
Eli Friedman06de2b52009-06-08 07:21:15 +0000266 }
Francois Pichetdcf88932011-05-07 19:04:49 +0000267
Eli Friedman53339e02009-06-08 23:27:34 +0000268 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000269 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
270 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichetdcf88932011-05-07 19:04:49 +0000271
272 // FIXME: Remove this when we have proper __declspec(property()) support.
273 // Just skip everything inside property().
274 if (AttrName->getName() == "property") {
275 ConsumeParen();
276 SkipUntil(tok::r_paren);
277 }
Eli Friedman06de2b52009-06-08 07:21:15 +0000278 if (Tok.is(tok::l_paren)) {
279 ConsumeParen();
280 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
281 // correctly.
John McCalldadc5752010-08-24 06:29:42 +0000282 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000283 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000284 Expr *ExprList = ArgExpr.take();
John McCall084e83d2011-03-24 11:26:52 +0000285 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
286 SourceLocation(), &ExprList, 1, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000287 }
288 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
289 SkipUntil(tok::r_paren, false);
290 } else {
John McCall084e83d2011-03-24 11:26:52 +0000291 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
292 0, SourceLocation(), 0, 0, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000293 }
294 }
295 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
296 SkipUntil(tok::r_paren, false);
John McCall53fa7142010-12-24 02:08:15 +0000297 return;
Eli Friedman53339e02009-06-08 23:27:34 +0000298}
299
John McCall53fa7142010-12-24 02:08:15 +0000300void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000301 // Treat these like attributes
302 // FIXME: Allow Sema to distinguish between these and real attributes!
303 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000304 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000305 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
306 Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000307 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
308 SourceLocation AttrNameLoc = ConsumeToken();
309 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
310 // FIXME: Support these properly!
311 continue;
John McCall084e83d2011-03-24 11:26:52 +0000312 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
313 SourceLocation(), 0, 0, true);
Eli Friedman53339e02009-06-08 23:27:34 +0000314 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000315}
316
John McCall53fa7142010-12-24 02:08:15 +0000317void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000318 // Treat these like attributes
319 while (Tok.is(tok::kw___pascal)) {
320 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
321 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000322 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
323 SourceLocation(), 0, 0, true);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000324 }
John McCall53fa7142010-12-24 02:08:15 +0000325}
326
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000327void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
328 // Treat these like attributes
329 while (Tok.is(tok::kw___kernel)) {
330 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000331 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
332 AttrNameLoc, 0, AttrNameLoc, 0,
333 SourceLocation(), 0, 0, false);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000334 }
335}
336
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000337void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
338 SourceLocation Loc = Tok.getLocation();
339 switch(Tok.getKind()) {
340 // OpenCL qualifiers:
341 case tok::kw___private:
342 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000343 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000344 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000345 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000346 break;
347
348 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000349 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000350 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000351 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000352 break;
353
354 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000355 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000356 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000357 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000358 break;
359
360 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000361 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000362 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000363 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000364 break;
365
366 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000367 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000368 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000369 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000370 break;
371
372 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000373 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000374 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000375 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000376 break;
377
378 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000379 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000380 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000381 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000382 break;
383 default: break;
384 }
385}
386
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000387/// \brief Parse a version number.
388///
389/// version:
390/// simple-integer
391/// simple-integer ',' simple-integer
392/// simple-integer ',' simple-integer ',' simple-integer
393VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
394 Range = Tok.getLocation();
395
396 if (!Tok.is(tok::numeric_constant)) {
397 Diag(Tok, diag::err_expected_version);
398 SkipUntil(tok::comma, tok::r_paren, true, true, true);
399 return VersionTuple();
400 }
401
402 // Parse the major (and possibly minor and subminor) versions, which
403 // are stored in the numeric constant. We utilize a quirk of the
404 // lexer, which is that it handles something like 1.2.3 as a single
405 // numeric constant, rather than two separate tokens.
406 llvm::SmallString<512> Buffer;
407 Buffer.resize(Tok.getLength()+1);
408 const char *ThisTokBegin = &Buffer[0];
409
410 // Get the spelling of the token, which eliminates trigraphs, etc.
411 bool Invalid = false;
412 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
413 if (Invalid)
414 return VersionTuple();
415
416 // Parse the major version.
417 unsigned AfterMajor = 0;
418 unsigned Major = 0;
419 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
420 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
421 ++AfterMajor;
422 }
423
424 if (AfterMajor == 0) {
425 Diag(Tok, diag::err_expected_version);
426 SkipUntil(tok::comma, tok::r_paren, true, true, true);
427 return VersionTuple();
428 }
429
430 if (AfterMajor == ActualLength) {
431 ConsumeToken();
432
433 // We only had a single version component.
434 if (Major == 0) {
435 Diag(Tok, diag::err_zero_version);
436 return VersionTuple();
437 }
438
439 return VersionTuple(Major);
440 }
441
442 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
443 Diag(Tok, diag::err_expected_version);
444 SkipUntil(tok::comma, tok::r_paren, true, true, true);
445 return VersionTuple();
446 }
447
448 // Parse the minor version.
449 unsigned AfterMinor = AfterMajor + 1;
450 unsigned Minor = 0;
451 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
452 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
453 ++AfterMinor;
454 }
455
456 if (AfterMinor == ActualLength) {
457 ConsumeToken();
458
459 // We had major.minor.
460 if (Major == 0 && Minor == 0) {
461 Diag(Tok, diag::err_zero_version);
462 return VersionTuple();
463 }
464
465 return VersionTuple(Major, Minor);
466 }
467
468 // If what follows is not a '.', we have a problem.
469 if (ThisTokBegin[AfterMinor] != '.') {
470 Diag(Tok, diag::err_expected_version);
471 SkipUntil(tok::comma, tok::r_paren, true, true, true);
472 return VersionTuple();
473 }
474
475 // Parse the subminor version.
476 unsigned AfterSubminor = AfterMinor + 1;
477 unsigned Subminor = 0;
478 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
479 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
480 ++AfterSubminor;
481 }
482
483 if (AfterSubminor != ActualLength) {
484 Diag(Tok, diag::err_expected_version);
485 SkipUntil(tok::comma, tok::r_paren, true, true, true);
486 return VersionTuple();
487 }
488 ConsumeToken();
489 return VersionTuple(Major, Minor, Subminor);
490}
491
492/// \brief Parse the contents of the "availability" attribute.
493///
494/// availability-attribute:
495/// 'availability' '(' platform ',' version-arg-list ')'
496///
497/// platform:
498/// identifier
499///
500/// version-arg-list:
501/// version-arg
502/// version-arg ',' version-arg-list
503///
504/// version-arg:
505/// 'introduced' '=' version
506/// 'deprecated' '=' version
507/// 'removed' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000508/// 'unavailable'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000509void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
510 SourceLocation AvailabilityLoc,
511 ParsedAttributes &attrs,
512 SourceLocation *endLoc) {
513 SourceLocation PlatformLoc;
514 IdentifierInfo *Platform = 0;
515
516 enum { Introduced, Deprecated, Obsoleted, Unknown };
517 AvailabilityChange Changes[Unknown];
518
519 // Opening '('.
520 SourceLocation LParenLoc;
521 if (!Tok.is(tok::l_paren)) {
522 Diag(Tok, diag::err_expected_lparen);
523 return;
524 }
525 LParenLoc = ConsumeParen();
526
527 // Parse the platform name,
528 if (Tok.isNot(tok::identifier)) {
529 Diag(Tok, diag::err_availability_expected_platform);
530 SkipUntil(tok::r_paren);
531 return;
532 }
533 Platform = Tok.getIdentifierInfo();
534 PlatformLoc = ConsumeToken();
535
536 // Parse the ',' following the platform name.
537 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
538 return;
539
540 // If we haven't grabbed the pointers for the identifiers
541 // "introduced", "deprecated", and "obsoleted", do so now.
542 if (!Ident_introduced) {
543 Ident_introduced = PP.getIdentifierInfo("introduced");
544 Ident_deprecated = PP.getIdentifierInfo("deprecated");
545 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000546 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000547 }
548
549 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000550 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000551 do {
552 if (Tok.isNot(tok::identifier)) {
553 Diag(Tok, diag::err_availability_expected_change);
554 SkipUntil(tok::r_paren);
555 return;
556 }
557 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
558 SourceLocation KeywordLoc = ConsumeToken();
559
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000560 if (Keyword == Ident_unavailable) {
561 if (UnavailableLoc.isValid()) {
562 Diag(KeywordLoc, diag::err_availability_redundant)
563 << Keyword << SourceRange(UnavailableLoc);
564 }
565 UnavailableLoc = KeywordLoc;
566
567 if (Tok.isNot(tok::comma))
568 break;
569
570 ConsumeToken();
571 continue;
572 }
573
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000574 if (Tok.isNot(tok::equal)) {
575 Diag(Tok, diag::err_expected_equal_after)
576 << Keyword;
577 SkipUntil(tok::r_paren);
578 return;
579 }
580 ConsumeToken();
581
582 SourceRange VersionRange;
583 VersionTuple Version = ParseVersionTuple(VersionRange);
584
585 if (Version.empty()) {
586 SkipUntil(tok::r_paren);
587 return;
588 }
589
590 unsigned Index;
591 if (Keyword == Ident_introduced)
592 Index = Introduced;
593 else if (Keyword == Ident_deprecated)
594 Index = Deprecated;
595 else if (Keyword == Ident_obsoleted)
596 Index = Obsoleted;
597 else
598 Index = Unknown;
599
600 if (Index < Unknown) {
601 if (!Changes[Index].KeywordLoc.isInvalid()) {
602 Diag(KeywordLoc, diag::err_availability_redundant)
603 << Keyword
604 << SourceRange(Changes[Index].KeywordLoc,
605 Changes[Index].VersionRange.getEnd());
606 }
607
608 Changes[Index].KeywordLoc = KeywordLoc;
609 Changes[Index].Version = Version;
610 Changes[Index].VersionRange = VersionRange;
611 } else {
612 Diag(KeywordLoc, diag::err_availability_unknown_change)
613 << Keyword << VersionRange;
614 }
615
616 if (Tok.isNot(tok::comma))
617 break;
618
619 ConsumeToken();
620 } while (true);
621
622 // Closing ')'.
623 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
624 if (RParenLoc.isInvalid())
625 return;
626
627 if (endLoc)
628 *endLoc = RParenLoc;
629
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000630 // The 'unavailable' availability cannot be combined with any other
631 // availability changes. Make sure that hasn't happened.
632 if (UnavailableLoc.isValid()) {
633 bool Complained = false;
634 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
635 if (Changes[Index].KeywordLoc.isValid()) {
636 if (!Complained) {
637 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
638 << SourceRange(Changes[Index].KeywordLoc,
639 Changes[Index].VersionRange.getEnd());
640 Complained = true;
641 }
642
643 // Clear out the availability.
644 Changes[Index] = AvailabilityChange();
645 }
646 }
647 }
648
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000649 // Record this attribute
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000650 attrs.addNew(&Availability, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000651 0, SourceLocation(),
652 Platform, PlatformLoc,
653 Changes[Introduced],
654 Changes[Deprecated],
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000655 Changes[Obsoleted],
656 UnavailableLoc, false, false);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000657}
658
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000659/// \brief Wrapper around a case statement checking if AttrName is
660/// one of the thread safety attributes
661bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
662 return llvm::StringSwitch<bool>(AttrName)
663 .Case("guarded_by", true)
664 .Case("guarded_var", true)
665 .Case("pt_guarded_by", true)
666 .Case("pt_guarded_var", true)
667 .Case("lockable", true)
668 .Case("scoped_lockable", true)
669 .Case("no_thread_safety_analysis", true)
670 .Case("acquired_after", true)
671 .Case("acquired_before", true)
672 .Case("exclusive_lock_function", true)
673 .Case("shared_lock_function", true)
674 .Case("exclusive_trylock_function", true)
675 .Case("shared_trylock_function", true)
676 .Case("unlock_function", true)
677 .Case("lock_returned", true)
678 .Case("locks_excluded", true)
679 .Case("exclusive_locks_required", true)
680 .Case("shared_locks_required", true)
681 .Default(false);
682}
683
684/// \brief Parse the contents of thread safety attributes. These
685/// should always be parsed as an expression list.
686///
687/// We need to special case the parsing due to the fact that if the first token
688/// of the first argument is an identifier, the main parse loop will store
689/// that token as a "parameter" and the rest of
690/// the arguments will be added to a list of "arguments". However,
691/// subsequent tokens in the first argument are lost. We instead parse each
692/// argument as an expression and add all arguments to the list of "arguments".
693/// In future, we will take advantage of this special case to also
694/// deal with some argument scoping issues here (for example, referring to a
695/// function parameter in the attribute on that function).
696void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
697 SourceLocation AttrNameLoc,
698 ParsedAttributes &Attrs,
699 SourceLocation *EndLoc) {
700
701 if (Tok.is(tok::l_paren)) {
702 SourceLocation LeftParenLoc = Tok.getLocation();
703 ConsumeParen(); // ignore the left paren loc for now
704
705 ExprVector ArgExprs(Actions);
706 bool ArgExprsOk = true;
707
708 // now parse the list of expressions
709 while (1) {
710 ExprResult ArgExpr(ParseAssignmentExpression());
711 if (ArgExpr.isInvalid()) {
712 ArgExprsOk = false;
713 MatchRHSPunctuation(tok::r_paren, LeftParenLoc);
714 break;
715 } else {
716 ArgExprs.push_back(ArgExpr.release());
717 }
718 if (Tok.isNot(tok::comma))
719 break;
720 ConsumeToken(); // Eat the comma, move to the next argument
721 }
722 // Match the ')'.
723 if (ArgExprsOk && Tok.is(tok::r_paren)) {
724 ConsumeParen(); // ignore the right paren loc for now
725 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
726 ArgExprs.take(), ArgExprs.size());
727 }
728 } else {
729 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc,
730 0, SourceLocation(), 0, 0);
731 }
732}
733
John McCall53fa7142010-12-24 02:08:15 +0000734void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
735 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
736 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000737}
738
Chris Lattner53361ac2006-08-10 05:19:57 +0000739/// ParseDeclaration - Parse a full 'declaration', which consists of
740/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000741/// 'Context' should be a Declarator::TheContext value. This returns the
742/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000743///
744/// declaration: [C99 6.7]
745/// block-declaration ->
746/// simple-declaration
747/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000748/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000749/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000750/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000751/// [C++] using-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000752/// [C++0x/C1X] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000753/// others... [FIXME]
754///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000755Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
756 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000757 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000758 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000759 ParenBraceBracketBalancer BalancerRAIIObj(*this);
760
John McCall48871652010-08-21 09:40:31 +0000761 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +0000762 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000763 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000764 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000765 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000766 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000767 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000768 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000769 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000770 // Could be the start of an inline namespace. Allowed as an ext in C++03.
771 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +0000772 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000773 SourceLocation InlineLoc = ConsumeToken();
774 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
775 break;
776 }
John McCall53fa7142010-12-24 02:08:15 +0000777 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000778 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000779 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000780 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000781 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000782 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000783 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000784 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +0000785 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000786 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000787 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000788 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000789 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000790 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000791 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000792 default:
John McCall53fa7142010-12-24 02:08:15 +0000793 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000794 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000795
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000796 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +0000797 // single decl, convert it now. Alias declarations can also declare a type;
798 // include that too if it is present.
799 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +0000800}
801
802/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
803/// declaration-specifiers init-declarator-list[opt] ';'
804///[C90/C++]init-declarator-list ';' [TODO]
805/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000806///
Richard Smith02e85f32011-04-14 22:09:26 +0000807/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
808/// attribute-specifier-seq[opt] type-specifier-seq declarator
809///
Chris Lattner32dc41c2009-03-29 17:27:48 +0000810/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000811/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +0000812///
813/// If FRI is non-null, we might be parsing a for-range-declaration instead
814/// of a simple-declaration. If we find that we are, we also parse the
815/// for-range-initializer, and place it here.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000816Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
817 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000818 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000819 ParsedAttributes &attrs,
Richard Smith02e85f32011-04-14 22:09:26 +0000820 bool RequireSemi,
821 ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000822 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000823 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000824 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000825
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000826 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000827 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000828 StmtResult R = Actions.ActOnVlaStmt(DS);
829 if (R.isUsable())
830 Stmts.push_back(R.release());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000831
Chris Lattner0e894622006-08-13 19:58:17 +0000832 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
833 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000834 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000835 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000836 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000837 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000838 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000839 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000840 }
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000841
842 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +0000843}
Mike Stump11289f42009-09-09 15:08:12 +0000844
John McCalld5a36322009-11-03 19:26:08 +0000845/// ParseDeclGroup - Having concluded that this is either a function
846/// definition or a group of object declarations, actually parse the
847/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000848Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
849 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000850 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +0000851 SourceLocation *DeclEnd,
852 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +0000853 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000854 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000855 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000856
John McCalld5a36322009-11-03 19:26:08 +0000857 // Bail out if the first declarator didn't seem well-formed.
858 if (!D.hasName() && !D.mayOmitIdentifier()) {
859 // Skip until ; or }.
860 SkipUntil(tok::r_brace, true, true);
861 if (Tok.is(tok::semi))
862 ConsumeToken();
863 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000864 }
Mike Stump11289f42009-09-09 15:08:12 +0000865
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000866 // Check to see if we have a function *definition* which must have a body.
867 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
868 // Look at the next token to make sure that this isn't a function
869 // declaration. We have to check this because __attribute__ might be the
870 // start of a function definition in GCC-extended K&R C.
871 !isDeclarationAfterDeclarator()) {
872
Chris Lattner13901342010-07-11 22:42:07 +0000873 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000874 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
875 Diag(Tok, diag::err_function_declared_typedef);
876
877 // Recover by treating the 'typedef' as spurious.
878 DS.ClearStorageClassSpecs();
879 }
880
John McCall48871652010-08-21 09:40:31 +0000881 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000882 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000883 }
884
885 if (isDeclarationSpecifier()) {
886 // If there is an invalid declaration specifier right after the function
887 // prototype, then we must be in a missing semicolon case where this isn't
888 // actually a body. Just fall through into the code that handles it as a
889 // prototype, and let the top-level code handle the erroneous declspec
890 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000891 } else {
892 Diag(Tok, diag::err_expected_fn_body);
893 SkipUntil(tok::semi);
894 return DeclGroupPtrTy();
895 }
896 }
897
Richard Smith02e85f32011-04-14 22:09:26 +0000898 if (ParseAttributesAfterDeclarator(D))
899 return DeclGroupPtrTy();
900
901 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
902 // must parse and analyze the for-range-initializer before the declaration is
903 // analyzed.
904 if (FRI && Tok.is(tok::colon)) {
905 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +0000906 if (Tok.is(tok::l_brace))
907 FRI->RangeExpr = ParseBraceInitializer();
908 else
909 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +0000910 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
911 Actions.ActOnCXXForRangeDecl(ThisDecl);
912 Actions.FinalizeDeclaration(ThisDecl);
913 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
914 }
915
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000916 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +0000917 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall28a6aea2009-11-04 02:18:39 +0000918 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000919 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000920 DeclsInGroup.push_back(FirstDecl);
921
922 // If we don't have a comma, it is either the end of the list (a ';') or an
923 // error, bail out.
924 while (Tok.is(tok::comma)) {
925 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000926 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000927
928 // Parse the next declarator.
929 D.clear();
930
931 // Accept attributes in an init-declarator. In the first declarator in a
932 // declaration, these would be part of the declspec. In subsequent
933 // declarators, they become part of the declarator itself, so that they
934 // don't apply to declarators after *this* one. Examples:
935 // short __attribute__((common)) var; -> declspec
936 // short var __attribute__((common)); -> declarator
937 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +0000938 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +0000939
940 ParseDeclarator(D);
941
John McCall48871652010-08-21 09:40:31 +0000942 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000943 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000944 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000945 DeclsInGroup.push_back(ThisDecl);
946 }
947
948 if (DeclEnd)
949 *DeclEnd = Tok.getLocation();
950
951 if (Context != Declarator::ForContext &&
952 ExpectAndConsume(tok::semi,
953 Context == Declarator::FileContext
954 ? diag::err_invalid_token_after_toplevel_declarator
955 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000956 // Okay, there was no semicolon and one was expected. If we see a
957 // declaration specifier, just assume it was missing and continue parsing.
958 // Otherwise things are very confused and we skip to recover.
959 if (!isDeclarationSpecifier()) {
960 SkipUntil(tok::r_brace, true, true);
961 if (Tok.is(tok::semi))
962 ConsumeToken();
963 }
John McCalld5a36322009-11-03 19:26:08 +0000964 }
965
Douglas Gregor0be31a22010-07-02 17:43:08 +0000966 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000967 DeclsInGroup.data(),
968 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000969}
970
Richard Smith02e85f32011-04-14 22:09:26 +0000971/// Parse an optional simple-asm-expr and attributes, and attach them to a
972/// declarator. Returns true on an error.
973bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
974 // If a simple-asm-expr is present, parse it.
975 if (Tok.is(tok::kw_asm)) {
976 SourceLocation Loc;
977 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
978 if (AsmLabel.isInvalid()) {
979 SkipUntil(tok::semi, true, true);
980 return true;
981 }
982
983 D.setAsmLabel(AsmLabel.release());
984 D.SetRangeEnd(Loc);
985 }
986
987 MaybeParseGNUAttributes(D);
988 return false;
989}
990
Douglas Gregor23996282009-05-12 21:31:51 +0000991/// \brief Parse 'declaration' after parsing 'declaration-specifiers
992/// declarator'. This method parses the remainder of the declaration
993/// (including any attributes or initializer, among other things) and
994/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000995///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000996/// init-declarator: [C99 6.7]
997/// declarator
998/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000999/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1000/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001001/// [C++] declarator initializer[opt]
1002///
1003/// [C++] initializer:
1004/// [C++] '=' initializer-clause
1005/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001006/// [C++0x] '=' 'default' [TODO]
1007/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001008/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001009///
1010/// According to the standard grammar, =default and =delete are function
1011/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001012///
John McCall48871652010-08-21 09:40:31 +00001013Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001014 const ParsedTemplateInfo &TemplateInfo) {
Richard Smith02e85f32011-04-14 22:09:26 +00001015 if (ParseAttributesAfterDeclarator(D))
1016 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001017
Richard Smith02e85f32011-04-14 22:09:26 +00001018 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1019}
Mike Stump11289f42009-09-09 15:08:12 +00001020
Richard Smith02e85f32011-04-14 22:09:26 +00001021Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1022 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001023 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001024 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001025 switch (TemplateInfo.Kind) {
1026 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001027 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001028 break;
1029
1030 case ParsedTemplateInfo::Template:
1031 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001032 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001033 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001034 TemplateInfo.TemplateParams->data(),
1035 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +00001036 D);
1037 break;
1038
1039 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +00001040 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001041 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001042 TemplateInfo.ExternLoc,
1043 TemplateInfo.TemplateLoc,
1044 D);
1045 if (ThisRes.isInvalid()) {
1046 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001047 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001048 }
1049
1050 ThisDecl = ThisRes.get();
1051 break;
1052 }
1053 }
Mike Stump11289f42009-09-09 15:08:12 +00001054
Richard Smith30482bc2011-02-20 03:19:35 +00001055 bool TypeContainsAuto =
1056 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1057
Douglas Gregor23996282009-05-12 21:31:51 +00001058 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001059 if (isTokenEqualOrMistypedEqualEqual(
1060 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +00001061 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001062 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001063 if (D.isFunctionDeclarator())
1064 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1065 << 1 /* delete */;
1066 else
1067 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001068 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001069 if (D.isFunctionDeclarator())
1070 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1071 << 1 /* delete */;
1072 else
1073 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001074 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +00001075 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1076 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001077 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001078 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001079
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001080 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001081 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001082 ConsumeCodeCompletionToken();
1083 SkipUntil(tok::comma, true, true);
1084 return ThisDecl;
1085 }
1086
John McCalldadc5752010-08-24 06:29:42 +00001087 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001088
John McCall1f4ee7b2009-12-19 09:28:58 +00001089 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001090 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001091 ExitScope();
1092 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001093
Douglas Gregor23996282009-05-12 21:31:51 +00001094 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001095 SkipUntil(tok::comma, true, true);
1096 Actions.ActOnInitializerError(ThisDecl);
1097 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001098 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1099 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001100 }
1101 } else if (Tok.is(tok::l_paren)) {
1102 // Parse C++ direct initializer: '(' expression-list ')'
1103 SourceLocation LParenLoc = ConsumeParen();
1104 ExprVector Exprs(Actions);
1105 CommaLocsTy CommaLocs;
1106
Douglas Gregor613bf102009-12-22 17:47:17 +00001107 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1108 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001109 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001110 }
1111
Douglas Gregor23996282009-05-12 21:31:51 +00001112 if (ParseExpressionList(Exprs, CommaLocs)) {
1113 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001114
1115 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001116 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001117 ExitScope();
1118 }
Douglas Gregor23996282009-05-12 21:31:51 +00001119 } else {
1120 // Match the ')'.
1121 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1122
1123 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1124 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001125
1126 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001127 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001128 ExitScope();
1129 }
1130
Douglas Gregor23996282009-05-12 21:31:51 +00001131 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1132 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +00001133 RParenLoc,
1134 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001135 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001136 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1137 // Parse C++0x braced-init-list.
1138 if (D.getCXXScopeSpec().isSet()) {
1139 EnterScope(0);
1140 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1141 }
1142
1143 ExprResult Init(ParseBraceInitializer());
1144
1145 if (D.getCXXScopeSpec().isSet()) {
1146 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1147 ExitScope();
1148 }
1149
1150 if (Init.isInvalid()) {
1151 Actions.ActOnInitializerError(ThisDecl);
1152 } else
1153 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1154 /*DirectInit=*/true, TypeContainsAuto);
1155
Douglas Gregor23996282009-05-12 21:31:51 +00001156 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001157 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001158 }
1159
Richard Smithb2bc2e62011-02-21 20:05:19 +00001160 Actions.FinalizeDeclaration(ThisDecl);
1161
Douglas Gregor23996282009-05-12 21:31:51 +00001162 return ThisDecl;
1163}
1164
Chris Lattner1890ac82006-08-13 01:16:23 +00001165/// ParseSpecifierQualifierList
1166/// specifier-qualifier-list:
1167/// type-specifier specifier-qualifier-list[opt]
1168/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001169/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001170///
Richard Smithcd1c0552011-07-01 19:46:12 +00001171void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001172 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1173 /// parse declaration-specifiers and complain about extra stuff.
Richard Smithcd1c0552011-07-01 19:46:12 +00001174 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump11289f42009-09-09 15:08:12 +00001175
Chris Lattner1890ac82006-08-13 01:16:23 +00001176 // Validate declspec for type-name.
1177 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +00001178 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +00001179 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +00001180 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +00001181
Chris Lattner1b22eed2006-11-28 05:12:07 +00001182 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001183 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001184 if (DS.getStorageClassSpecLoc().isValid())
1185 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1186 else
1187 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001188 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001189 }
Mike Stump11289f42009-09-09 15:08:12 +00001190
Chris Lattner1b22eed2006-11-28 05:12:07 +00001191 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001192 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001193 if (DS.isInlineSpecified())
1194 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1195 if (DS.isVirtualSpecified())
1196 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1197 if (DS.isExplicitSpecified())
1198 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001199 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001200 }
1201}
Chris Lattner53361ac2006-08-10 05:19:57 +00001202
Chris Lattner6cc055a2009-04-12 20:42:31 +00001203/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1204/// specified token is valid after the identifier in a declarator which
1205/// immediately follows the declspec. For example, these things are valid:
1206///
1207/// int x [ 4]; // direct-declarator
1208/// int x ( int y); // direct-declarator
1209/// int(int x ) // direct-declarator
1210/// int x ; // simple-declaration
1211/// int x = 17; // init-declarator-list
1212/// int x , y; // init-declarator-list
1213/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001214/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001215/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001216///
1217/// This is not, because 'x' does not immediately follow the declspec (though
1218/// ')' happens to be valid anyway).
1219/// int (x)
1220///
1221static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1222 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1223 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001224 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001225}
1226
Chris Lattner20a0c612009-04-14 21:34:55 +00001227
1228/// ParseImplicitInt - This method is called when we have an non-typename
1229/// identifier in a declspec (which normally terminates the decl spec) when
1230/// the declspec has no type specifier. In this case, the declspec is either
1231/// malformed or is "implicit int" (in K&R and C89).
1232///
1233/// This method handles diagnosing this prettily and returns false if the
1234/// declspec is done being processed. If it recovers and thinks there may be
1235/// other pieces of declspec after it, it returns true.
1236///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001237bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001238 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001239 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001240 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001241
Chris Lattner20a0c612009-04-14 21:34:55 +00001242 SourceLocation Loc = Tok.getLocation();
1243 // If we see an identifier that is not a type name, we normally would
1244 // parse it as the identifer being declared. However, when a typename
1245 // is typo'd or the definition is not included, this will incorrectly
1246 // parse the typename as the identifier name and fall over misparsing
1247 // later parts of the diagnostic.
1248 //
1249 // As such, we try to do some look-ahead in cases where this would
1250 // otherwise be an "implicit-int" case to see if this is invalid. For
1251 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1252 // an identifier with implicit int, we'd get a parse error because the
1253 // next token is obviously invalid for a type. Parse these as a case
1254 // with an invalid type specifier.
1255 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001256
Chris Lattner20a0c612009-04-14 21:34:55 +00001257 // Since we know that this either implicit int (which is rare) or an
1258 // error, we'd do lookahead to try to do better recovery.
1259 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1260 // If this token is valid for implicit int, e.g. "static x = 4", then
1261 // we just avoid eating the identifier, so it will be parsed as the
1262 // identifier in the declarator.
1263 return false;
1264 }
Mike Stump11289f42009-09-09 15:08:12 +00001265
Chris Lattner20a0c612009-04-14 21:34:55 +00001266 // Otherwise, if we don't consume this token, we are going to emit an
1267 // error anyway. Try to recover from various common problems. Check
1268 // to see if this was a reference to a tag name without a tag specified.
1269 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001270 //
1271 // C++ doesn't need this, and isTagName doesn't take SS.
1272 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001273 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001274 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001275
Douglas Gregor0be31a22010-07-02 17:43:08 +00001276 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001277 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001278 case DeclSpec::TST_enum:
1279 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1280 case DeclSpec::TST_union:
1281 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1282 case DeclSpec::TST_struct:
1283 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1284 case DeclSpec::TST_class:
1285 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001286 }
Mike Stump11289f42009-09-09 15:08:12 +00001287
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001288 if (TagName) {
1289 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001290 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001291 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump11289f42009-09-09 15:08:12 +00001292
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001293 // Parse this as a tag as if the missing tag were present.
1294 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001295 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001296 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001297 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001298 return true;
1299 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001300 }
Mike Stump11289f42009-09-09 15:08:12 +00001301
Douglas Gregor15e56022009-10-13 23:27:22 +00001302 // This is almost certainly an invalid type name. Let the action emit a
1303 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001304 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001305 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001306 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001307 // The action emitted a diagnostic, so we don't have to.
1308 if (T) {
1309 // The action has suggested that the type T could be used. Set that as
1310 // the type in the declaration specifiers, consume the would-be type
1311 // name token, and we're done.
1312 const char *PrevSpec;
1313 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001314 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001315 DS.SetRangeEnd(Tok.getLocation());
1316 ConsumeToken();
1317
1318 // There may be other declaration specifiers after this.
1319 return true;
1320 }
1321
1322 // Fall through; the action had no suggestion for us.
1323 } else {
1324 // The action did not emit a diagnostic, so emit one now.
1325 SourceRange R;
1326 if (SS) R = SS->getRange();
1327 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1328 }
Mike Stump11289f42009-09-09 15:08:12 +00001329
Douglas Gregor15e56022009-10-13 23:27:22 +00001330 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001331 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001332 unsigned DiagID;
1333 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001334 DS.SetRangeEnd(Tok.getLocation());
1335 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001336
Chris Lattner20a0c612009-04-14 21:34:55 +00001337 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1338 // avoid rippling error messages on subsequent uses of the same type,
1339 // could be useful if #include was forgotten.
1340 return false;
1341}
1342
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001343/// \brief Determine the declaration specifier context from the declarator
1344/// context.
1345///
1346/// \param Context the declarator context, which is one of the
1347/// Declarator::TheContext enumerator values.
1348Parser::DeclSpecContext
1349Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1350 if (Context == Declarator::MemberContext)
1351 return DSC_class;
1352 if (Context == Declarator::FileContext)
1353 return DSC_top_level;
1354 return DSC_normal;
1355}
1356
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001357/// ParseDeclarationSpecifiers
1358/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001359/// storage-class-specifier declaration-specifiers[opt]
1360/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001361/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001362/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001363///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001364/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001365/// 'typedef'
1366/// 'extern'
1367/// 'static'
1368/// 'auto'
1369/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001370/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001371/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001372/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001373/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001374/// [C++] 'virtual'
1375/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001376/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001377/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001378/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001379
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001380///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001381void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001382 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001383 AccessSpecifier AS,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001384 DeclSpecContext DSContext) {
1385 if (DS.getSourceRange().isInvalid()) {
1386 DS.SetRangeStart(Tok.getLocation());
1387 DS.SetRangeEnd(Tok.getLocation());
1388 }
1389
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001390 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001391 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001392 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001393 unsigned DiagID = 0;
1394
Chris Lattner4d8f8732006-11-28 05:05:08 +00001395 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001396
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001397 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001398 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001399 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001400 // If this is not a declaration specifier token, we're done reading decl
1401 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001402 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001403 return;
Mike Stump11289f42009-09-09 15:08:12 +00001404
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001405 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001406 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001407 if (DS.hasTypeSpecifier()) {
1408 bool AllowNonIdentifiers
1409 = (getCurScope()->getFlags() & (Scope::ControlScope |
1410 Scope::BlockScope |
1411 Scope::TemplateParamScope |
1412 Scope::FunctionPrototypeScope |
1413 Scope::AtCatchScope)) == 0;
1414 bool AllowNestedNameSpecifiers
1415 = DSContext == DSC_top_level ||
1416 (DSContext == DSC_class && DS.isFriendSpecified());
1417
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001418 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1419 AllowNonIdentifiers,
1420 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001421 ConsumeCodeCompletionToken();
1422 return;
1423 }
1424
Douglas Gregor80039242011-02-15 20:33:25 +00001425 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1426 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1427 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001428 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1429 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001430 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001431 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001432 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001433 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001434
1435 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1436 ConsumeCodeCompletionToken();
1437 return;
1438 }
1439
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001440 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001441 // C++ scope specifier. Annotate and loop, or bail out on error.
1442 if (TryAnnotateCXXScopeToken(true)) {
1443 if (!DS.hasTypeSpecifier())
1444 DS.SetTypeSpecError();
1445 goto DoneWithDeclSpec;
1446 }
John McCall8bc2a702010-03-01 18:20:46 +00001447 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1448 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001449 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001450
1451 case tok::annot_cxxscope: {
1452 if (DS.hasTypeSpecifier())
1453 goto DoneWithDeclSpec;
1454
John McCall9dab4e62009-12-12 11:40:51 +00001455 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001456 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1457 Tok.getAnnotationRange(),
1458 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001459
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001460 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001461 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001462 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001463 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001464 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001465 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001466
1467 // C++ [class.qual]p2:
1468 // In a lookup in which the constructor is an acceptable lookup
1469 // result and the nested-name-specifier nominates a class C:
1470 //
1471 // - if the name specified after the
1472 // nested-name-specifier, when looked up in C, is the
1473 // injected-class-name of C (Clause 9), or
1474 //
1475 // - if the name specified after the nested-name-specifier
1476 // is the same as the identifier or the
1477 // simple-template-id's template-name in the last
1478 // component of the nested-name-specifier,
1479 //
1480 // the name is instead considered to name the constructor of
1481 // class C.
1482 //
1483 // Thus, if the template-name is actually the constructor
1484 // name, then the code is ill-formed; this interpretation is
1485 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001486 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00001487 if ((DSContext == DSC_top_level ||
1488 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1489 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001490 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001491 if (isConstructorDeclarator()) {
1492 // The user meant this to be an out-of-line constructor
1493 // definition, but template arguments are not allowed
1494 // there. Just allow this as a constructor; we'll
1495 // complain about it later.
1496 goto DoneWithDeclSpec;
1497 }
1498
1499 // The user meant this to name a type, but it actually names
1500 // a constructor with some extraneous template
1501 // arguments. Complain, then parse it as a type as the user
1502 // intended.
1503 Diag(TemplateId->TemplateNameLoc,
1504 diag::err_out_of_line_template_id_names_constructor)
1505 << TemplateId->Name;
1506 }
1507
John McCall9dab4e62009-12-12 11:40:51 +00001508 DS.getTypeSpecScope() = SS;
1509 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001510 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001511 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001512 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001513 continue;
1514 }
1515
Douglas Gregorc5790df2009-09-28 07:26:33 +00001516 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001517 DS.getTypeSpecScope() = SS;
1518 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001519 if (Tok.getAnnotationValue()) {
1520 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001521 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1522 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001523 PrevSpec, DiagID, T);
1524 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001525 else
1526 DS.SetTypeSpecError();
1527 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1528 ConsumeToken(); // The typename
1529 }
1530
Douglas Gregor167fa622009-03-25 15:40:00 +00001531 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001532 goto DoneWithDeclSpec;
1533
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001534 // If we're in a context where the identifier could be a class name,
1535 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001536 if ((DSContext == DSC_top_level ||
1537 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001538 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001539 &SS)) {
1540 if (isConstructorDeclarator())
1541 goto DoneWithDeclSpec;
1542
1543 // As noted in C++ [class.qual]p2 (cited above), when the name
1544 // of the class is qualified in a context where it could name
1545 // a constructor, its a constructor name. However, we've
1546 // looked at the declarator, and the user probably meant this
1547 // to be a type. Complain that it isn't supposed to be treated
1548 // as a type, then proceed to parse it as a type.
1549 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1550 << Next.getIdentifierInfo();
1551 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001552
John McCallba7bf592010-08-24 05:47:05 +00001553 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1554 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001555 getCurScope(), &SS,
1556 false, false, ParsedType(),
1557 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001558
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001559 // If the referenced identifier is not a type, then this declspec is
1560 // erroneous: We already checked about that it has no type specifier, and
1561 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001562 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001563 if (TypeRep == 0) {
1564 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001565 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001566 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001567 }
Mike Stump11289f42009-09-09 15:08:12 +00001568
John McCall9dab4e62009-12-12 11:40:51 +00001569 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001570 ConsumeToken(); // The C++ scope.
1571
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001572 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001573 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001574 if (isInvalid)
1575 break;
Mike Stump11289f42009-09-09 15:08:12 +00001576
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001577 DS.SetRangeEnd(Tok.getLocation());
1578 ConsumeToken(); // The typename.
1579
1580 continue;
1581 }
Mike Stump11289f42009-09-09 15:08:12 +00001582
Chris Lattnere387d9e2009-01-21 19:48:37 +00001583 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001584 if (Tok.getAnnotationValue()) {
1585 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001586 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001587 DiagID, T);
1588 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001589 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001590
1591 if (isInvalid)
1592 break;
1593
Chris Lattnere387d9e2009-01-21 19:48:37 +00001594 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1595 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001596
Chris Lattnere387d9e2009-01-21 19:48:37 +00001597 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1598 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001599 // Objective-C interface.
1600 if (Tok.is(tok::less) && getLang().ObjC1)
1601 ParseObjCProtocolQualifiers(DS);
1602
Chris Lattnere387d9e2009-01-21 19:48:37 +00001603 continue;
1604 }
Mike Stump11289f42009-09-09 15:08:12 +00001605
Douglas Gregor06873092011-04-28 15:48:45 +00001606 case tok::kw___is_signed:
1607 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1608 // typically treats it as a trait. If we see __is_signed as it appears
1609 // in libstdc++, e.g.,
1610 //
1611 // static const bool __is_signed;
1612 //
1613 // then treat __is_signed as an identifier rather than as a keyword.
1614 if (DS.getTypeSpecType() == TST_bool &&
1615 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1616 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1617 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1618 Tok.setKind(tok::identifier);
1619 }
1620
1621 // We're done with the declaration-specifiers.
1622 goto DoneWithDeclSpec;
1623
Chris Lattner16fac4f2008-07-26 01:18:38 +00001624 // typedef-name
1625 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001626 // In C++, check to see if this is a scope specifier like foo::bar::, if
1627 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001628 if (getLang().CPlusPlus) {
1629 if (TryAnnotateCXXScopeToken(true)) {
1630 if (!DS.hasTypeSpecifier())
1631 DS.SetTypeSpecError();
1632 goto DoneWithDeclSpec;
1633 }
1634 if (!Tok.is(tok::identifier))
1635 continue;
1636 }
Mike Stump11289f42009-09-09 15:08:12 +00001637
Chris Lattner16fac4f2008-07-26 01:18:38 +00001638 // This identifier can only be a typedef name if we haven't already seen
1639 // a type-specifier. Without this check we misparse:
1640 // typedef int X; struct Y { short X; }; as 'short int'.
1641 if (DS.hasTypeSpecifier())
1642 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001643
John Thompson22334602010-02-05 00:12:22 +00001644 // Check for need to substitute AltiVec keyword tokens.
1645 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1646 break;
1647
Chris Lattner16fac4f2008-07-26 01:18:38 +00001648 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001649 ParsedType TypeRep =
1650 Actions.getTypeName(*Tok.getIdentifierInfo(),
1651 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001652
Chris Lattner6cc055a2009-04-12 20:42:31 +00001653 // If this is not a typedef name, don't parse it as part of the declspec,
1654 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001655 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001656 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001657 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001658 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001659
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001660 // If we're in a context where the identifier could be a class name,
1661 // check whether this is a constructor declaration.
1662 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001663 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001664 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001665 goto DoneWithDeclSpec;
1666
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001667 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001668 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001669 if (isInvalid)
1670 break;
Mike Stump11289f42009-09-09 15:08:12 +00001671
Chris Lattner16fac4f2008-07-26 01:18:38 +00001672 DS.SetRangeEnd(Tok.getLocation());
1673 ConsumeToken(); // The identifier
1674
1675 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1676 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001677 // Objective-C interface.
1678 if (Tok.is(tok::less) && getLang().ObjC1)
1679 ParseObjCProtocolQualifiers(DS);
1680
Steve Naroffcd5e7822008-09-22 10:28:57 +00001681 // Need to support trailing type qualifiers (e.g. "id<p> const").
1682 // If a type specifier follows, it will be diagnosed elsewhere.
1683 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001684 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001685
1686 // type-name
1687 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001688 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00001689 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001690 // This template-id does not refer to a type name, so we're
1691 // done with the type-specifiers.
1692 goto DoneWithDeclSpec;
1693 }
1694
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001695 // If we're in a context where the template-id could be a
1696 // constructor name or specialization, check whether this is a
1697 // constructor declaration.
1698 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001699 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001700 isConstructorDeclarator())
1701 goto DoneWithDeclSpec;
1702
Douglas Gregor7f741122009-02-25 19:37:18 +00001703 // Turn the template-id annotation token into a type annotation
1704 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001705 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001706 continue;
1707 }
1708
Chris Lattnere37e2332006-08-15 04:50:22 +00001709 // GNU attributes support.
1710 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001711 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001712 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001713
1714 // Microsoft declspec support.
1715 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001716 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001717 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001718
Steve Naroff44ac7772008-12-25 14:16:32 +00001719 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001720 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001721 // FIXME: Add handling here!
1722 break;
1723
1724 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001725 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001726 case tok::kw___cdecl:
1727 case tok::kw___stdcall:
1728 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001729 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00001730 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00001731 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001732 continue;
1733
Dawn Perchik335e16b2010-09-03 01:29:35 +00001734 // Borland single token adornments.
1735 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001736 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001737 continue;
1738
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001739 // OpenCL single token adornments.
1740 case tok::kw___kernel:
1741 ParseOpenCLAttributes(DS.getAttributes());
1742 continue;
1743
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001744 // storage-class-specifier
1745 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001746 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001747 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001748 break;
1749 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001750 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001751 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001752 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001753 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001754 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001755 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001756 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001757 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001758 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001759 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001760 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001761 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001762 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001763 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001764 break;
1765 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00001766 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001767 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1768 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1769 DiagID, getLang());
1770 if (!isInvalid)
1771 Diag(Tok, diag::auto_storage_class)
1772 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1773 }
1774 else
1775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1776 DiagID);
1777 }
Anders Carlsson082acde2009-06-26 18:41:36 +00001778 else
John McCall49bfce42009-08-03 20:12:06 +00001779 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001780 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001781 break;
1782 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001783 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001784 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001785 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001786 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001787 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001788 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001789 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001790 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001791 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001792 break;
Mike Stump11289f42009-09-09 15:08:12 +00001793
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001794 // function-specifier
1795 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001796 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001797 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001798 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001799 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001800 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001801 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001802 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001803 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001804
Anders Carlssoncd8db412009-05-06 04:46:28 +00001805 // friend
1806 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001807 if (DSContext == DSC_class)
1808 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1809 else {
1810 PrevSpec = ""; // not actually used by the diagnostic
1811 DiagID = diag::err_friend_invalid_in_context;
1812 isInvalid = true;
1813 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001814 break;
Mike Stump11289f42009-09-09 15:08:12 +00001815
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001816 // constexpr
1817 case tok::kw_constexpr:
1818 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1819 break;
1820
Chris Lattnere387d9e2009-01-21 19:48:37 +00001821 // type-specifier
1822 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001823 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1824 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001825 break;
1826 case tok::kw_long:
1827 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001828 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1829 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001830 else
John McCall49bfce42009-08-03 20:12:06 +00001831 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1832 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001833 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001834 case tok::kw___int64:
1835 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1836 DiagID);
1837 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001838 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001839 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1840 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001841 break;
1842 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001843 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1844 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001845 break;
1846 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001847 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1848 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001849 break;
1850 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001851 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1852 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001853 break;
1854 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001855 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1856 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001857 break;
1858 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001859 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1860 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001861 break;
1862 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001863 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1864 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001865 break;
1866 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001867 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1868 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001869 break;
1870 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001871 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1872 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001873 break;
1874 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001875 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1876 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001877 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001878 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001879 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1880 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001881 break;
1882 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001883 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1884 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001885 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001886 case tok::kw_bool:
1887 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001888 if (Tok.is(tok::kw_bool) &&
1889 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1890 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1891 PrevSpec = ""; // Not used by the diagnostic.
1892 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00001893 // For better error recovery.
1894 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001895 isInvalid = true;
1896 } else {
1897 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1898 DiagID);
1899 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001900 break;
1901 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001902 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1903 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001904 break;
1905 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001906 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1907 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001908 break;
1909 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001910 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1911 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001912 break;
John Thompson22334602010-02-05 00:12:22 +00001913 case tok::kw___vector:
1914 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1915 break;
1916 case tok::kw___pixel:
1917 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1918 break;
John McCall39439732011-04-09 22:50:59 +00001919 case tok::kw___unknown_anytype:
1920 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1921 PrevSpec, DiagID);
1922 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001923
1924 // class-specifier:
1925 case tok::kw_class:
1926 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001927 case tok::kw_union: {
1928 tok::TokenKind Kind = Tok.getKind();
1929 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001930 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001931 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001932 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001933
1934 // enum-specifier:
1935 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001936 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001937 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001938 continue;
1939
1940 // cv-qualifier:
1941 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001942 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1943 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001944 break;
1945 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001946 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1947 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001948 break;
1949 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001950 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1951 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001952 break;
1953
Douglas Gregor333489b2009-03-27 23:10:48 +00001954 // C++ typename-specifier:
1955 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001956 if (TryAnnotateTypeOrScopeToken()) {
1957 DS.SetTypeSpecError();
1958 goto DoneWithDeclSpec;
1959 }
1960 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001961 continue;
1962 break;
1963
Chris Lattnere387d9e2009-01-21 19:48:37 +00001964 // GNU typeof support.
1965 case tok::kw_typeof:
1966 ParseTypeofSpecifier(DS);
1967 continue;
1968
Anders Carlsson74948d02009-06-24 17:47:40 +00001969 case tok::kw_decltype:
1970 ParseDecltypeSpecifier(DS);
1971 continue;
1972
Alexis Hunt4a257072011-05-19 05:37:45 +00001973 case tok::kw___underlying_type:
1974 ParseUnderlyingTypeSpecifier(DS);
1975
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00001976 // OpenCL qualifiers:
1977 case tok::kw_private:
1978 if (!getLang().OpenCL)
1979 goto DoneWithDeclSpec;
1980 case tok::kw___private:
1981 case tok::kw___global:
1982 case tok::kw___local:
1983 case tok::kw___constant:
1984 case tok::kw___read_only:
1985 case tok::kw___write_only:
1986 case tok::kw___read_write:
1987 ParseOpenCLQualifiers(DS);
1988 break;
1989
Steve Naroffcfdf6162008-06-05 00:02:44 +00001990 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001991 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001992 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1993 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001994 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001995 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001996
Douglas Gregor3a001f42010-11-19 17:10:50 +00001997 if (!ParseObjCProtocolQualifiers(DS))
1998 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1999 << FixItHint::CreateInsertion(Loc, "id")
2000 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002001
2002 // Need to support trailing type qualifiers (e.g. "id<p> const").
2003 // If a type specifier follows, it will be diagnosed elsewhere.
2004 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002005 }
John McCall49bfce42009-08-03 20:12:06 +00002006 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002007 if (isInvalid) {
2008 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002009 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002010
2011 if (DiagID == diag::ext_duplicate_declspec)
2012 Diag(Tok, DiagID)
2013 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2014 else
2015 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002016 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002017
Chris Lattner2e232092008-03-13 06:29:04 +00002018 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002019 if (DiagID != diag::err_bool_redeclaration)
2020 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002021 }
2022}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002023
Chris Lattnera448d752009-01-06 06:59:53 +00002024/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00002025/// primarily follow the C++ grammar with additions for C99 and GNU,
2026/// which together subsume the C grammar. Note that the C++
2027/// type-specifier also includes the C type-qualifier (for const,
2028/// volatile, and C99 restrict). Returns true if a type-specifier was
2029/// found (and parsed), false otherwise.
2030///
2031/// type-specifier: [C++ 7.1.5]
2032/// simple-type-specifier
2033/// class-specifier
2034/// enum-specifier
2035/// elaborated-type-specifier [TODO]
2036/// cv-qualifier
2037///
2038/// cv-qualifier: [C++ 7.1.5.1]
2039/// 'const'
2040/// 'volatile'
2041/// [C99] 'restrict'
2042///
2043/// simple-type-specifier: [ C++ 7.1.5.2]
2044/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2045/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2046/// 'char'
2047/// 'wchar_t'
2048/// 'bool'
2049/// 'short'
2050/// 'int'
2051/// 'long'
2052/// 'signed'
2053/// 'unsigned'
2054/// 'float'
2055/// 'double'
2056/// 'void'
2057/// [C99] '_Bool'
2058/// [C99] '_Complex'
2059/// [C99] '_Imaginary' // Removed in TC2?
2060/// [GNU] '_Decimal32'
2061/// [GNU] '_Decimal64'
2062/// [GNU] '_Decimal128'
2063/// [GNU] typeof-specifier
2064/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2065/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00002066/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00002067/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00002068bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00002069 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002070 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00002071 const ParsedTemplateInfo &TemplateInfo,
2072 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00002073 SourceLocation Loc = Tok.getLocation();
2074
2075 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00002076 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00002077 // If we already have a type specifier, this identifier is not a type.
2078 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2079 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2080 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2081 return false;
John Thompson22334602010-02-05 00:12:22 +00002082 // Check for need to substitute AltiVec keyword tokens.
2083 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2084 break;
2085 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002086 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00002087 // Annotate typenames and C++ scope specifiers. If we get one, just
2088 // recurse to handle whatever we get.
2089 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002090 return true;
2091 if (Tok.is(tok::identifier))
2092 return false;
2093 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2094 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00002095 case tok::coloncolon: // ::foo::bar
2096 if (NextToken().is(tok::kw_new) || // ::new
2097 NextToken().is(tok::kw_delete)) // ::delete
2098 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002099
Chris Lattner020bab92009-01-04 23:41:41 +00002100 // Annotate typenames and C++ scope specifiers. If we get one, just
2101 // recurse to handle whatever we get.
2102 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002103 return true;
2104 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2105 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00002106
Douglas Gregor450c75a2008-11-07 15:42:26 +00002107 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00002108 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002109 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00002110 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2111 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002112 DiagID, T);
2113 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002114 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002115 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2116 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002117
Douglas Gregor450c75a2008-11-07 15:42:26 +00002118 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2119 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2120 // Objective-C interface. If we don't have Objective-C or a '<', this is
2121 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002122 if (Tok.is(tok::less) && getLang().ObjC1)
2123 ParseObjCProtocolQualifiers(DS);
2124
Douglas Gregor450c75a2008-11-07 15:42:26 +00002125 return true;
2126 }
2127
2128 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002129 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002130 break;
2131 case tok::kw_long:
2132 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002133 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2134 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002135 else
John McCall49bfce42009-08-03 20:12:06 +00002136 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2137 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002138 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002139 case tok::kw___int64:
2140 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2141 DiagID);
2142 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002143 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002144 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002145 break;
2146 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002147 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2148 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002149 break;
2150 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002151 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2152 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002153 break;
2154 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002155 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2156 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002157 break;
2158 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002159 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002160 break;
2161 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002162 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002163 break;
2164 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002165 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002166 break;
2167 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002168 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002169 break;
2170 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002171 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002172 break;
2173 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002174 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002175 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002176 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002177 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002178 break;
2179 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002180 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002181 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002182 case tok::kw_bool:
2183 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00002184 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002185 break;
2186 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002187 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2188 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002189 break;
2190 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002191 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2192 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002193 break;
2194 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002195 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2196 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002197 break;
John Thompson22334602010-02-05 00:12:22 +00002198 case tok::kw___vector:
2199 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2200 break;
2201 case tok::kw___pixel:
2202 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2203 break;
2204
Douglas Gregor450c75a2008-11-07 15:42:26 +00002205 // class-specifier:
2206 case tok::kw_class:
2207 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002208 case tok::kw_union: {
2209 tok::TokenKind Kind = Tok.getKind();
2210 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00002211 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2212 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002213 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002214 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00002215
2216 // enum-specifier:
2217 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002218 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002219 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002220 return true;
2221
2222 // cv-qualifier:
2223 case tok::kw_const:
2224 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002225 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002226 break;
2227 case tok::kw_volatile:
2228 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002229 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002230 break;
2231 case tok::kw_restrict:
2232 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002233 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002234 break;
2235
2236 // GNU typeof support.
2237 case tok::kw_typeof:
2238 ParseTypeofSpecifier(DS);
2239 return true;
2240
Anders Carlsson74948d02009-06-24 17:47:40 +00002241 // C++0x decltype support.
2242 case tok::kw_decltype:
2243 ParseDecltypeSpecifier(DS);
2244 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002245
Alexis Hunt4a257072011-05-19 05:37:45 +00002246 // C++0x type traits support.
2247 case tok::kw___underlying_type:
2248 ParseUnderlyingTypeSpecifier(DS);
2249 return true;
2250
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002251 // OpenCL qualifiers:
2252 case tok::kw_private:
2253 if (!getLang().OpenCL)
2254 return false;
2255 case tok::kw___private:
2256 case tok::kw___global:
2257 case tok::kw___local:
2258 case tok::kw___constant:
2259 case tok::kw___read_only:
2260 case tok::kw___write_only:
2261 case tok::kw___read_write:
2262 ParseOpenCLQualifiers(DS);
2263 break;
2264
Anders Carlssonbae27372009-06-26 23:44:14 +00002265 // C++0x auto support.
2266 case tok::kw_auto:
2267 if (!getLang().CPlusPlus0x)
2268 return false;
2269
John McCall49bfce42009-08-03 20:12:06 +00002270 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002271 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002272
Eli Friedman53339e02009-06-08 23:27:34 +00002273 case tok::kw___ptr64:
2274 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002275 case tok::kw___cdecl:
2276 case tok::kw___stdcall:
2277 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002278 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002279 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002280 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002281 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002282
Dawn Perchik335e16b2010-09-03 01:29:35 +00002283 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002284 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002285 return true;
2286
Douglas Gregor450c75a2008-11-07 15:42:26 +00002287 default:
2288 // Not a type-specifier; do nothing.
2289 return false;
2290 }
2291
2292 // If the specifier combination wasn't legal, issue a diagnostic.
2293 if (isInvalid) {
2294 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002295 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002296 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002297 }
2298 DS.SetRangeEnd(Tok.getLocation());
2299 ConsumeToken(); // whatever we parsed above.
2300 return true;
2301}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002302
Chris Lattner70ae4912007-10-29 04:42:53 +00002303/// ParseStructDeclaration - Parse a struct declaration without the terminating
2304/// semicolon.
2305///
Chris Lattner90a26b02007-01-23 04:38:16 +00002306/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002307/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002308/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002309/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002310/// struct-declarator-list:
2311/// struct-declarator
2312/// struct-declarator-list ',' struct-declarator
2313/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2314/// struct-declarator:
2315/// declarator
2316/// [GNU] declarator attributes[opt]
2317/// declarator[opt] ':' constant-expression
2318/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2319///
Chris Lattnera12405b2008-04-10 06:46:29 +00002320void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002321ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002322 if (Tok.is(tok::kw___extension__)) {
2323 // __extension__ silences extension warnings in the subexpression.
2324 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002325 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002326 return ParseStructDeclaration(DS, Fields);
2327 }
Mike Stump11289f42009-09-09 15:08:12 +00002328
Steve Naroff97170802007-08-20 22:28:22 +00002329 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002330 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002331
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002332 // If there are no declarators, this is a free-standing declaration
2333 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002334 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002335 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002336 return;
2337 }
2338
2339 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002340 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002341 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002342 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002343 FieldDeclarator DeclaratorInfo(DS);
2344
2345 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002346 if (!FirstDeclarator)
2347 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002348
Steve Naroff97170802007-08-20 22:28:22 +00002349 /// struct-declarator: declarator
2350 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002351 if (Tok.isNot(tok::colon)) {
2352 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2353 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002354 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002355 }
Mike Stump11289f42009-09-09 15:08:12 +00002356
Chris Lattner76c72282007-10-09 17:33:22 +00002357 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002358 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002359 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002360 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002361 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002362 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002363 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002364 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002365
Steve Naroff97170802007-08-20 22:28:22 +00002366 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002367 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002368
John McCallcfefb6d2009-11-03 02:38:08 +00002369 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002370 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002371 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002372
Steve Naroff97170802007-08-20 22:28:22 +00002373 // If we don't have a comma, it is either the end of the list (a ';')
2374 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002375 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002376 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002377
Steve Naroff97170802007-08-20 22:28:22 +00002378 // Consume the comma.
2379 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002380
John McCallcfefb6d2009-11-03 02:38:08 +00002381 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002382 }
Steve Naroff97170802007-08-20 22:28:22 +00002383}
2384
2385/// ParseStructUnionBody
2386/// struct-contents:
2387/// struct-declaration-list
2388/// [EXT] empty
2389/// [GNU] "struct-declaration-list" without terminatoring ';'
2390/// struct-declaration-list:
2391/// struct-declaration
2392/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002393/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002394///
Chris Lattner1300fb92007-01-23 23:42:53 +00002395void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002396 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002397 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2398 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002399
Chris Lattner90a26b02007-01-23 04:38:16 +00002400 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002401
Douglas Gregor658b9552009-01-09 22:42:13 +00002402 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002403 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002404
Chris Lattner7b9ace62007-01-23 20:11:08 +00002405 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2406 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002407 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002408 Diag(Tok, diag::ext_empty_struct_union)
2409 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002410
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002411 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002412
Chris Lattner7b9ace62007-01-23 20:11:08 +00002413 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002414 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002415 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002416
Chris Lattner736ed5d2007-06-09 05:59:07 +00002417 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002418 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002419 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002420 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002421 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002422 ConsumeToken();
2423 continue;
2424 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002425
2426 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002427 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002428
John McCallcfefb6d2009-11-03 02:38:08 +00002429 if (!Tok.is(tok::at)) {
2430 struct CFieldCallback : FieldCallback {
2431 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002432 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002433 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002434
John McCall48871652010-08-21 09:40:31 +00002435 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002436 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002437 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2438
John McCall48871652010-08-21 09:40:31 +00002439 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002440 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002441 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002442 FD.D.getDeclSpec().getSourceRange().getBegin(),
2443 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002444 FieldDecls.push_back(Field);
2445 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002446 }
John McCallcfefb6d2009-11-03 02:38:08 +00002447 } Callback(*this, TagDecl, FieldDecls);
2448
2449 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002450 } else { // Handle @defs
2451 ConsumeToken();
2452 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2453 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002454 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002455 continue;
2456 }
2457 ConsumeToken();
2458 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2459 if (!Tok.is(tok::identifier)) {
2460 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002461 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002462 continue;
2463 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002464 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002465 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002466 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002467 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2468 ConsumeToken();
2469 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002470 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002471
Chris Lattner76c72282007-10-09 17:33:22 +00002472 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002473 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002474 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002475 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002476 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002477 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002478 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2479 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002480 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002481 // If we stopped at a ';', eat it.
2482 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002483 }
2484 }
Mike Stump11289f42009-09-09 15:08:12 +00002485
Steve Naroff33a1e802007-10-29 21:38:07 +00002486 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002487
John McCall084e83d2011-03-24 11:26:52 +00002488 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002489 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002490 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002491
Douglas Gregor0be31a22010-07-02 17:43:08 +00002492 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00002493 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00002494 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002495 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002496 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002497 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002498}
2499
Chris Lattner3b561a32006-08-13 00:12:11 +00002500/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002501/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002502/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002503///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002504/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2505/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002506/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002507/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002508///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002509/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2510/// [C++0x] enum-head '{' enumerator-list ',' '}'
2511///
2512/// enum-head: [C++0x]
2513/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2514/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2515///
2516/// enum-key: [C++0x]
2517/// 'enum'
2518/// 'enum' 'class'
2519/// 'enum' 'struct'
2520///
2521/// enum-base: [C++0x]
2522/// ':' type-specifier-seq
2523///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002524/// [C++] elaborated-type-specifier:
2525/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2526///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002527void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002528 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002529 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002530 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002531 if (Tok.is(tok::code_completion)) {
2532 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002533 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002534 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002535 }
John McCallcb432fa2011-07-06 05:58:41 +00002536
2537 bool IsScopedEnum = false;
2538 bool IsScopedUsingClassTag = false;
2539
2540 if (getLang().CPlusPlus0x &&
2541 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
2542 IsScopedEnum = true;
2543 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2544 ConsumeToken();
2545 }
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002546
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002547 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002548 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002549 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002550
John McCallcb432fa2011-07-06 05:58:41 +00002551 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
2552
Abramo Bagnarad7548482010-05-19 21:37:53 +00002553 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002554 if (getLang().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00002555 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2556 // if a fixed underlying type is allowed.
2557 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2558
John McCallba7bf592010-08-24 05:47:05 +00002559 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002560 return;
2561
2562 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002563 Diag(Tok, diag::err_expected_ident);
2564 if (Tok.isNot(tok::l_brace)) {
2565 // Has no name and is not a definition.
2566 // Skip the rest of this declarator, up until the comma or semicolon.
2567 SkipUntil(tok::comma, true);
2568 return;
2569 }
2570 }
2571 }
Mike Stump11289f42009-09-09 15:08:12 +00002572
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002573 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002574 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2575 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002576 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002577
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002578 // Skip the rest of this declarator, up until the comma or semicolon.
2579 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002580 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002581 }
Mike Stump11289f42009-09-09 15:08:12 +00002582
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002583 // If an identifier is present, consume and remember it.
2584 IdentifierInfo *Name = 0;
2585 SourceLocation NameLoc;
2586 if (Tok.is(tok::identifier)) {
2587 Name = Tok.getIdentifierInfo();
2588 NameLoc = ConsumeToken();
2589 }
Mike Stump11289f42009-09-09 15:08:12 +00002590
Douglas Gregor0bf31402010-10-08 23:50:27 +00002591 if (!Name && IsScopedEnum) {
2592 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2593 // declaration of a scoped enumeration.
2594 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2595 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002596 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002597 }
2598
2599 TypeResult BaseType;
2600
Douglas Gregord1f69f62010-12-01 17:42:47 +00002601 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002602 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002603 bool PossibleBitfield = false;
2604 if (getCurScope()->getFlags() & Scope::ClassScope) {
2605 // If we're in class scope, this can either be an enum declaration with
2606 // an underlying type, or a declaration of a bitfield member. We try to
2607 // use a simple disambiguation scheme first to catch the common cases
2608 // (integer literal, sizeof); if it's still ambiguous, we then consider
2609 // anything that's a simple-type-specifier followed by '(' as an
2610 // expression. This suffices because function types are not valid
2611 // underlying types anyway.
2612 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2613 // If the next token starts an expression, we know we're parsing a
2614 // bit-field. This is the common case.
2615 if (TPR == TPResult::True())
2616 PossibleBitfield = true;
2617 // If the next token starts a type-specifier-seq, it may be either a
2618 // a fixed underlying type or the start of a function-style cast in C++;
2619 // lookahead one more token to see if it's obvious that we have a
2620 // fixed underlying type.
2621 else if (TPR == TPResult::False() &&
2622 GetLookAheadToken(2).getKind() == tok::semi) {
2623 // Consume the ':'.
2624 ConsumeToken();
2625 } else {
2626 // We have the start of a type-specifier-seq, so we have to perform
2627 // tentative parsing to determine whether we have an expression or a
2628 // type.
2629 TentativeParsingAction TPA(*this);
2630
2631 // Consume the ':'.
2632 ConsumeToken();
2633
Douglas Gregora1aec292011-02-22 20:32:04 +00002634 if ((getLang().CPlusPlus &&
2635 isCXXDeclarationSpecifier() != TPResult::True()) ||
2636 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002637 // We'll parse this as a bitfield later.
2638 PossibleBitfield = true;
2639 TPA.Revert();
2640 } else {
2641 // We have a type-specifier-seq.
2642 TPA.Commit();
2643 }
2644 }
2645 } else {
2646 // Consume the ':'.
2647 ConsumeToken();
2648 }
2649
2650 if (!PossibleBitfield) {
2651 SourceRange Range;
2652 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002653
2654 if (!getLang().CPlusPlus0x)
2655 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2656 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002657 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002658 }
2659
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002660 // There are three options here. If we have 'enum foo;', then this is a
2661 // forward declaration. If we have 'enum foo {...' then this is a
2662 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2663 //
2664 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2665 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2666 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2667 //
John McCallfaf5fb42010-08-26 23:41:50 +00002668 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002669 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002670 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002671 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002672 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002673 else
John McCallfaf5fb42010-08-26 23:41:50 +00002674 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002675
2676 // enums cannot be templates, although they can be referenced from a
2677 // template.
2678 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002679 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002680 Diag(Tok, diag::err_enum_template);
2681
2682 // Skip the rest of this declarator, up until the comma or semicolon.
2683 SkipUntil(tok::comma, true);
2684 return;
2685 }
2686
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002687 if (!Name && TUK != Sema::TUK_Definition) {
2688 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2689
2690 // Skip the rest of this declarator, up until the comma or semicolon.
2691 SkipUntil(tok::comma, true);
2692 return;
2693 }
2694
Douglas Gregord6ab8742009-05-28 23:31:59 +00002695 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002696 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002697 const char *PrevSpec = 0;
2698 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002699 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002700 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002701 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002702 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002703 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002704 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002705
Douglas Gregorba41d012010-04-24 16:38:41 +00002706 if (IsDependent) {
2707 // This enum has a dependent nested-name-specifier. Handle it as a
2708 // dependent tag.
2709 if (!Name) {
2710 DS.SetTypeSpecError();
2711 Diag(Tok, diag::err_expected_type_name_after_typename);
2712 return;
2713 }
2714
Douglas Gregor0be31a22010-07-02 17:43:08 +00002715 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002716 TUK, SS, Name, StartLoc,
2717 NameLoc);
2718 if (Type.isInvalid()) {
2719 DS.SetTypeSpecError();
2720 return;
2721 }
2722
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002723 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2724 NameLoc.isValid() ? NameLoc : StartLoc,
2725 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002726 Diag(StartLoc, DiagID) << PrevSpec;
2727
2728 return;
2729 }
Mike Stump11289f42009-09-09 15:08:12 +00002730
John McCall48871652010-08-21 09:40:31 +00002731 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002732 // The action failed to produce an enumeration tag. If this is a
2733 // definition, consume the entire definition.
2734 if (Tok.is(tok::l_brace)) {
2735 ConsumeBrace();
2736 SkipUntil(tok::r_brace);
2737 }
2738
2739 DS.SetTypeSpecError();
2740 return;
2741 }
2742
Chris Lattner76c72282007-10-09 17:33:22 +00002743 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002744 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002745
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002746 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2747 NameLoc.isValid() ? NameLoc : StartLoc,
2748 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002749 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002750}
2751
Chris Lattnerc1915e22007-01-25 07:29:02 +00002752/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2753/// enumerator-list:
2754/// enumerator
2755/// enumerator-list ',' enumerator
2756/// enumerator:
2757/// enumeration-constant
2758/// enumeration-constant '=' constant-expression
2759/// enumeration-constant:
2760/// identifier
2761///
John McCall48871652010-08-21 09:40:31 +00002762void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002763 // Enter the scope of the enum body and start the definition.
2764 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002765 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002766
Chris Lattnerc1915e22007-01-25 07:29:02 +00002767 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002768
Chris Lattner37256fb2007-08-27 17:24:30 +00002769 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002770 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002771 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002772
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002773 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002774
John McCall48871652010-08-21 09:40:31 +00002775 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002776
Chris Lattnerc1915e22007-01-25 07:29:02 +00002777 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002778 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002779 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2780 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002781
John McCall811a0f52010-10-22 23:36:17 +00002782 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002783 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002784 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002785
Chris Lattnerc1915e22007-01-25 07:29:02 +00002786 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002787 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002788 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002789 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002790 AssignedVal = ParseConstantExpression();
2791 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002792 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002793 }
Mike Stump11289f42009-09-09 15:08:12 +00002794
Chris Lattnerc1915e22007-01-25 07:29:02 +00002795 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002796 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2797 LastEnumConstDecl,
2798 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002799 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002800 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002801 EnumConstantDecls.push_back(EnumConstDecl);
2802 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002803
Douglas Gregorce66d022010-09-07 14:51:08 +00002804 if (Tok.is(tok::identifier)) {
2805 // We're missing a comma between enumerators.
2806 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2807 Diag(Loc, diag::err_enumerator_list_missing_comma)
2808 << FixItHint::CreateInsertion(Loc, ", ");
2809 continue;
2810 }
2811
Chris Lattner76c72282007-10-09 17:33:22 +00002812 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002813 break;
2814 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002815
2816 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002817 !(getLang().C99 || getLang().CPlusPlus0x))
2818 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2819 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002820 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002821 }
Mike Stump11289f42009-09-09 15:08:12 +00002822
Chris Lattnerc1915e22007-01-25 07:29:02 +00002823 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002824 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002825
Chris Lattnerc1915e22007-01-25 07:29:02 +00002826 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002827 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002828 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002829
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002830 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2831 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002832 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002833
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002834 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002835 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002836}
Chris Lattner3b561a32006-08-13 00:12:11 +00002837
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002838/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002839/// start of a type-qualifier-list.
2840bool Parser::isTypeQualifier() const {
2841 switch (Tok.getKind()) {
2842 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002843
2844 // type-qualifier only in OpenCL
2845 case tok::kw_private:
2846 return getLang().OpenCL;
2847
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002848 // type-qualifier
2849 case tok::kw_const:
2850 case tok::kw_volatile:
2851 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002852 case tok::kw___private:
2853 case tok::kw___local:
2854 case tok::kw___global:
2855 case tok::kw___constant:
2856 case tok::kw___read_only:
2857 case tok::kw___read_write:
2858 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002859 return true;
2860 }
2861}
2862
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002863/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2864/// is definitely a type-specifier. Return false if it isn't part of a type
2865/// specifier or if we're not sure.
2866bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2867 switch (Tok.getKind()) {
2868 default: return false;
2869 // type-specifiers
2870 case tok::kw_short:
2871 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002872 case tok::kw___int64:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002873 case tok::kw_signed:
2874 case tok::kw_unsigned:
2875 case tok::kw__Complex:
2876 case tok::kw__Imaginary:
2877 case tok::kw_void:
2878 case tok::kw_char:
2879 case tok::kw_wchar_t:
2880 case tok::kw_char16_t:
2881 case tok::kw_char32_t:
2882 case tok::kw_int:
2883 case tok::kw_float:
2884 case tok::kw_double:
2885 case tok::kw_bool:
2886 case tok::kw__Bool:
2887 case tok::kw__Decimal32:
2888 case tok::kw__Decimal64:
2889 case tok::kw__Decimal128:
2890 case tok::kw___vector:
2891
2892 // struct-or-union-specifier (C99) or class-specifier (C++)
2893 case tok::kw_class:
2894 case tok::kw_struct:
2895 case tok::kw_union:
2896 // enum-specifier
2897 case tok::kw_enum:
2898
2899 // typedef-name
2900 case tok::annot_typename:
2901 return true;
2902 }
2903}
2904
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002905/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002906/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002907bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002908 switch (Tok.getKind()) {
2909 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002910
Chris Lattner020bab92009-01-04 23:41:41 +00002911 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002912 if (TryAltiVecVectorToken())
2913 return true;
2914 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002915 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002916 // Annotate typenames and C++ scope specifiers. If we get one, just
2917 // recurse to handle whatever we get.
2918 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002919 return true;
2920 if (Tok.is(tok::identifier))
2921 return false;
2922 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002923
Chris Lattner020bab92009-01-04 23:41:41 +00002924 case tok::coloncolon: // ::foo::bar
2925 if (NextToken().is(tok::kw_new) || // ::new
2926 NextToken().is(tok::kw_delete)) // ::delete
2927 return false;
2928
Chris Lattner020bab92009-01-04 23:41:41 +00002929 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002930 return true;
2931 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002932
Chris Lattnere37e2332006-08-15 04:50:22 +00002933 // GNU attributes support.
2934 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002935 // GNU typeof support.
2936 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002937
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002938 // type-specifiers
2939 case tok::kw_short:
2940 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002941 case tok::kw___int64:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002942 case tok::kw_signed:
2943 case tok::kw_unsigned:
2944 case tok::kw__Complex:
2945 case tok::kw__Imaginary:
2946 case tok::kw_void:
2947 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002948 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002949 case tok::kw_char16_t:
2950 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002951 case tok::kw_int:
2952 case tok::kw_float:
2953 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002954 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002955 case tok::kw__Bool:
2956 case tok::kw__Decimal32:
2957 case tok::kw__Decimal64:
2958 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002959 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002960
Chris Lattner861a2262008-04-13 18:59:07 +00002961 // struct-or-union-specifier (C99) or class-specifier (C++)
2962 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002963 case tok::kw_struct:
2964 case tok::kw_union:
2965 // enum-specifier
2966 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002967
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002968 // type-qualifier
2969 case tok::kw_const:
2970 case tok::kw_volatile:
2971 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002972
2973 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002974 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002975 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002976
Chris Lattner409bf7d2008-10-20 00:25:30 +00002977 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2978 case tok::less:
2979 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002980
Steve Naroff44ac7772008-12-25 14:16:32 +00002981 case tok::kw___cdecl:
2982 case tok::kw___stdcall:
2983 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002984 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002985 case tok::kw___w64:
2986 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002987 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00002988 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002989
2990 case tok::kw___private:
2991 case tok::kw___local:
2992 case tok::kw___global:
2993 case tok::kw___constant:
2994 case tok::kw___read_only:
2995 case tok::kw___read_write:
2996 case tok::kw___write_only:
2997
Eli Friedman53339e02009-06-08 23:27:34 +00002998 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002999
3000 case tok::kw_private:
3001 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003002 }
3003}
3004
Chris Lattneracd58a32006-08-06 17:24:14 +00003005/// isDeclarationSpecifier() - Return true if the current token is part of a
3006/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003007///
3008/// \param DisambiguatingWithExpression True to indicate that the purpose of
3009/// this check is to disambiguate between an expression and a declaration.
3010bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003011 switch (Tok.getKind()) {
3012 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003013
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003014 case tok::kw_private:
3015 return getLang().OpenCL;
3016
Chris Lattner020bab92009-01-04 23:41:41 +00003017 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003018 // Unfortunate hack to support "Class.factoryMethod" notation.
3019 if (getLang().ObjC1 && NextToken().is(tok::period))
3020 return false;
John Thompson22334602010-02-05 00:12:22 +00003021 if (TryAltiVecVectorToken())
3022 return true;
3023 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003024 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003025 // Annotate typenames and C++ scope specifiers. If we get one, just
3026 // recurse to handle whatever we get.
3027 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003028 return true;
3029 if (Tok.is(tok::identifier))
3030 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003031
3032 // If we're in Objective-C and we have an Objective-C class type followed
3033 // by an identifier and then either ':' or ']', in a place where an
3034 // expression is permitted, then this is probably a class message send
3035 // missing the initial '['. In this case, we won't consider this to be
3036 // the start of a declaration.
3037 if (DisambiguatingWithExpression &&
3038 isStartOfObjCClassMessageMissingOpenBracket())
3039 return false;
3040
John McCall1f476a12010-02-26 08:45:28 +00003041 return isDeclarationSpecifier();
3042
Chris Lattner020bab92009-01-04 23:41:41 +00003043 case tok::coloncolon: // ::foo::bar
3044 if (NextToken().is(tok::kw_new) || // ::new
3045 NextToken().is(tok::kw_delete)) // ::delete
3046 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003047
Chris Lattner020bab92009-01-04 23:41:41 +00003048 // Annotate typenames and C++ scope specifiers. If we get one, just
3049 // recurse to handle whatever we get.
3050 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003051 return true;
3052 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003053
Chris Lattneracd58a32006-08-06 17:24:14 +00003054 // storage-class-specifier
3055 case tok::kw_typedef:
3056 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003057 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003058 case tok::kw_static:
3059 case tok::kw_auto:
3060 case tok::kw_register:
3061 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003062
Chris Lattneracd58a32006-08-06 17:24:14 +00003063 // type-specifiers
3064 case tok::kw_short:
3065 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003066 case tok::kw___int64:
Chris Lattneracd58a32006-08-06 17:24:14 +00003067 case tok::kw_signed:
3068 case tok::kw_unsigned:
3069 case tok::kw__Complex:
3070 case tok::kw__Imaginary:
3071 case tok::kw_void:
3072 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003073 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003074 case tok::kw_char16_t:
3075 case tok::kw_char32_t:
3076
Chris Lattneracd58a32006-08-06 17:24:14 +00003077 case tok::kw_int:
3078 case tok::kw_float:
3079 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003080 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003081 case tok::kw__Bool:
3082 case tok::kw__Decimal32:
3083 case tok::kw__Decimal64:
3084 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003085 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003086
Chris Lattner861a2262008-04-13 18:59:07 +00003087 // struct-or-union-specifier (C99) or class-specifier (C++)
3088 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003089 case tok::kw_struct:
3090 case tok::kw_union:
3091 // enum-specifier
3092 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003093
Chris Lattneracd58a32006-08-06 17:24:14 +00003094 // type-qualifier
3095 case tok::kw_const:
3096 case tok::kw_volatile:
3097 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003098
Chris Lattneracd58a32006-08-06 17:24:14 +00003099 // function-specifier
3100 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003101 case tok::kw_virtual:
3102 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003103
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003104 // static_assert-declaration
3105 case tok::kw__Static_assert:
3106
Chris Lattner599e47e2007-08-09 17:01:07 +00003107 // GNU typeof support.
3108 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003109
Chris Lattner599e47e2007-08-09 17:01:07 +00003110 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003111 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00003112 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003113
Francois Pichete878cb62011-06-19 08:02:06 +00003114 // C++0x decltype.
3115 case tok::kw_decltype:
3116 return true;
3117
Chris Lattner8b2ec162008-07-26 03:38:44 +00003118 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3119 case tok::less:
3120 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003121
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003122 // typedef-name
3123 case tok::annot_typename:
3124 return !DisambiguatingWithExpression ||
3125 !isStartOfObjCClassMessageMissingOpenBracket();
3126
Steve Narofff192fab2009-01-06 19:34:12 +00003127 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003128 case tok::kw___cdecl:
3129 case tok::kw___stdcall:
3130 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003131 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003132 case tok::kw___w64:
3133 case tok::kw___ptr64:
3134 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003135 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003136 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003137
3138 case tok::kw___private:
3139 case tok::kw___local:
3140 case tok::kw___global:
3141 case tok::kw___constant:
3142 case tok::kw___read_only:
3143 case tok::kw___read_write:
3144 case tok::kw___write_only:
3145
Eli Friedman53339e02009-06-08 23:27:34 +00003146 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003147 }
3148}
3149
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003150bool Parser::isConstructorDeclarator() {
3151 TentativeParsingAction TPA(*this);
3152
3153 // Parse the C++ scope specifier.
3154 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003155 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00003156 TPA.Revert();
3157 return false;
3158 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003159
3160 // Parse the constructor name.
3161 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3162 // We already know that we have a constructor name; just consume
3163 // the token.
3164 ConsumeToken();
3165 } else {
3166 TPA.Revert();
3167 return false;
3168 }
3169
3170 // Current class name must be followed by a left parentheses.
3171 if (Tok.isNot(tok::l_paren)) {
3172 TPA.Revert();
3173 return false;
3174 }
3175 ConsumeParen();
3176
3177 // A right parentheses or ellipsis signals that we have a constructor.
3178 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3179 TPA.Revert();
3180 return true;
3181 }
3182
3183 // If we need to, enter the specified scope.
3184 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003185 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003186 DeclScopeObj.EnterDeclaratorScope();
3187
Francois Pichet79f3a872011-01-31 04:54:32 +00003188 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003189 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003190 MaybeParseMicrosoftAttributes(Attrs);
3191
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003192 // Check whether the next token(s) are part of a declaration
3193 // specifier, in which case we have the start of a parameter and,
3194 // therefore, we know that this is a constructor.
3195 bool IsConstructor = isDeclarationSpecifier();
3196 TPA.Revert();
3197 return IsConstructor;
3198}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003199
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003200/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003201/// type-qualifier-list: [C99 6.7.5]
3202/// type-qualifier
3203/// [vendor] attributes
3204/// [ only if VendorAttributesAllowed=true ]
3205/// type-qualifier-list type-qualifier
3206/// [vendor] type-qualifier-list attributes
3207/// [ only if VendorAttributesAllowed=true ]
3208/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3209/// [ only if CXX0XAttributesAllowed=true ]
3210/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003211///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003212void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3213 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00003214 bool CXX0XAttributesAllowed) {
3215 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3216 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00003217 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003218 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003219 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00003220 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003221 else
3222 Diag(Loc, diag::err_attributes_not_allowed);
3223 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003224
3225 SourceLocation EndLoc;
3226
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003227 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003228 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003229 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003230 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003231 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003232
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003233 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003234 case tok::code_completion:
3235 Actions.CodeCompleteTypeQualifiers(DS);
3236 ConsumeCodeCompletionToken();
3237 break;
3238
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003239 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003240 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3241 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003242 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003243 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003244 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3245 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003246 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003247 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003248 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3249 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003250 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003251
3252 // OpenCL qualifiers:
3253 case tok::kw_private:
3254 if (!getLang().OpenCL)
3255 goto DoneWithTypeQuals;
3256 case tok::kw___private:
3257 case tok::kw___global:
3258 case tok::kw___local:
3259 case tok::kw___constant:
3260 case tok::kw___read_only:
3261 case tok::kw___write_only:
3262 case tok::kw___read_write:
3263 ParseOpenCLQualifiers(DS);
3264 break;
3265
Eli Friedman53339e02009-06-08 23:27:34 +00003266 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003267 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00003268 case tok::kw___cdecl:
3269 case tok::kw___stdcall:
3270 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003271 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00003272 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003273 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003274 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003275 continue;
3276 }
3277 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003278 case tok::kw___pascal:
3279 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003280 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003281 continue;
3282 }
3283 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003284 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003285 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003286 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003287 continue; // do *not* consume the next token!
3288 }
3289 // otherwise, FALL THROUGH!
3290 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003291 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003292 // If this is not a type-qualifier token, we're done reading type
3293 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003294 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003295 if (EndLoc.isValid())
3296 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003297 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003298 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003299
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003300 // If the specifier combination wasn't legal, issue a diagnostic.
3301 if (isInvalid) {
3302 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003303 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003304 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003305 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003306 }
3307}
3308
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003309
3310/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3311///
3312void Parser::ParseDeclarator(Declarator &D) {
3313 /// This implements the 'declarator' production in the C grammar, then checks
3314 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003315 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003316}
3317
Sebastian Redlbd150f42008-11-21 19:14:01 +00003318/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3319/// is parsed by the function passed to it. Pass null, and the direct-declarator
3320/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003321/// ptr-operator production.
3322///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003323/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3324/// [C] pointer[opt] direct-declarator
3325/// [C++] direct-declarator
3326/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003327///
3328/// pointer: [C99 6.7.5]
3329/// '*' type-qualifier-list[opt]
3330/// '*' type-qualifier-list[opt] pointer
3331///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003332/// ptr-operator:
3333/// '*' cv-qualifier-seq[opt]
3334/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003335/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003336/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003337/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003338/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003339void Parser::ParseDeclaratorInternal(Declarator &D,
3340 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003341 if (Diags.hasAllExtensionsSilenced())
3342 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003343
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003344 // C++ member pointers start with a '::' or a nested-name.
3345 // Member pointers get special handling, since there's no place for the
3346 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003347 if (getLang().CPlusPlus &&
3348 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3349 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003350 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003351 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003352
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003353 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003354 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003355 // The scope spec really belongs to the direct-declarator.
3356 D.getCXXScopeSpec() = SS;
3357 if (DirectDeclParser)
3358 (this->*DirectDeclParser)(D);
3359 return;
3360 }
3361
3362 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003363 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003364 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003365 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003366 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003367
3368 // Recurse to parse whatever is left.
3369 ParseDeclaratorInternal(D, DirectDeclParser);
3370
3371 // Sema will have to catch (syntactically invalid) pointers into global
3372 // scope. It has to catch pointers into namespace scope anyway.
3373 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003374 Loc),
3375 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003376 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003377 return;
3378 }
3379 }
3380
3381 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003382 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003383 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003384 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003385 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003386 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003387 if (DirectDeclParser)
3388 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003389 return;
3390 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003391
Sebastian Redled0f3b02009-03-15 22:02:01 +00003392 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3393 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003394 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003395 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003396
Chris Lattner9eac9312009-03-27 04:18:06 +00003397 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003398 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003399 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003400
Bill Wendling3708c182007-05-27 10:15:43 +00003401 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003402 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003403
Bill Wendling3708c182007-05-27 10:15:43 +00003404 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003405 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003406 if (Kind == tok::star)
3407 // Remember that we parsed a pointer type, and remember the type-quals.
3408 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003409 DS.getConstSpecLoc(),
3410 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003411 DS.getRestrictSpecLoc()),
3412 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003413 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003414 else
3415 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003416 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003417 Loc),
3418 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003419 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003420 } else {
3421 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003422 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003423
Sebastian Redl3b27be62009-03-23 00:00:23 +00003424 // Complain about rvalue references in C++03, but then go on and build
3425 // the declarator.
3426 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003427 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003428
Bill Wendling93efb222007-06-02 23:28:54 +00003429 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3430 // cv-qualifiers are introduced through the use of a typedef or of a
3431 // template type argument, in which case the cv-qualifiers are ignored.
3432 //
3433 // [GNU] Retricted references are allowed.
3434 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003435 // [C++0x] Attributes on references are not allowed.
3436 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003437 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003438
3439 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3440 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3441 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003442 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003443 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3444 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003445 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003446 }
Bill Wendling3708c182007-05-27 10:15:43 +00003447
3448 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003449 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003450
Douglas Gregor66583c52008-11-03 15:51:28 +00003451 if (D.getNumTypeObjects() > 0) {
3452 // C++ [dcl.ref]p4: There shall be no references to references.
3453 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3454 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003455 if (const IdentifierInfo *II = D.getIdentifier())
3456 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3457 << II;
3458 else
3459 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3460 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003461
Sebastian Redlbd150f42008-11-21 19:14:01 +00003462 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003463 // can go ahead and build the (technically ill-formed)
3464 // declarator: reference collapsing will take care of it.
3465 }
3466 }
3467
Bill Wendling3708c182007-05-27 10:15:43 +00003468 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003469 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003470 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003471 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003472 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003473 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003474}
3475
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003476/// ParseDirectDeclarator
3477/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003478/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003479/// '(' declarator ')'
3480/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003481/// [C90] direct-declarator '[' constant-expression[opt] ']'
3482/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3483/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3484/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3485/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003486/// direct-declarator '(' parameter-type-list ')'
3487/// direct-declarator '(' identifier-list[opt] ')'
3488/// [GNU] direct-declarator '(' parameter-forward-declarations
3489/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003490/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3491/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003492/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003493///
3494/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003495/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003496/// '::'[opt] nested-name-specifier[opt] type-name
3497///
3498/// id-expression: [C++ 5.1]
3499/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003500/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003501///
3502/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003503/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003504/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003505/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003506/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003507/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003508///
Chris Lattneracd58a32006-08-06 17:24:14 +00003509void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003510 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003511
Douglas Gregor7861a802009-11-03 01:35:08 +00003512 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3513 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003514 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003515 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003516 }
3517
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003518 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003519 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003520 // Change the declaration context for name lookup, until this function
3521 // is exited (and the declarator has been parsed).
3522 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003523 }
3524
Douglas Gregor27b4c162010-12-23 22:44:42 +00003525 // C++0x [dcl.fct]p14:
3526 // There is a syntactic ambiguity when an ellipsis occurs at the end
3527 // of a parameter-declaration-clause without a preceding comma. In
3528 // this case, the ellipsis is parsed as part of the
3529 // abstract-declarator if the type of the parameter names a template
3530 // parameter pack that has not been expanded; otherwise, it is parsed
3531 // as part of the parameter-declaration-clause.
3532 if (Tok.is(tok::ellipsis) &&
3533 !((D.getContext() == Declarator::PrototypeContext ||
3534 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003535 NextToken().is(tok::r_paren) &&
3536 !Actions.containsUnexpandedParameterPacks(D)))
3537 D.setEllipsisLoc(ConsumeToken());
3538
Douglas Gregor7861a802009-11-03 01:35:08 +00003539 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3540 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3541 // We found something that indicates the start of an unqualified-id.
3542 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003543 bool AllowConstructorName;
3544 if (D.getDeclSpec().hasTypeSpecifier())
3545 AllowConstructorName = false;
3546 else if (D.getCXXScopeSpec().isSet())
3547 AllowConstructorName =
3548 (D.getContext() == Declarator::FileContext ||
3549 (D.getContext() == Declarator::MemberContext &&
3550 D.getDeclSpec().isFriendSpecified()));
3551 else
3552 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3553
Douglas Gregor7861a802009-11-03 01:35:08 +00003554 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3555 /*EnteringContext=*/true,
3556 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003557 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003558 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003559 D.getName()) ||
3560 // Once we're past the identifier, if the scope was bad, mark the
3561 // whole declarator bad.
3562 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003563 D.SetIdentifier(0, Tok.getLocation());
3564 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003565 } else {
3566 // Parsed the unqualified-id; update range information and move along.
3567 if (D.getSourceRange().getBegin().isInvalid())
3568 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3569 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003570 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003571 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003572 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003573 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003574 assert(!getLang().CPlusPlus &&
3575 "There's a C++-specific check for tok::identifier above");
3576 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3577 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3578 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003579 goto PastIdentifier;
3580 }
3581
3582 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003583 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003584 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003585 // Example: 'char (*X)' or 'int (*XX)(void)'
3586 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003587
3588 // If the declarator was parenthesized, we entered the declarator
3589 // scope when parsing the parenthesized declarator, then exited
3590 // the scope already. Re-enter the scope, if we need to.
3591 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003592 // If there was an error parsing parenthesized declarator, declarator
3593 // scope may have been enterred before. Don't do it again.
3594 if (!D.isInvalidType() &&
3595 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003596 // Change the declaration context for name lookup, until this function
3597 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003598 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003599 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003600 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003601 // This could be something simple like "int" (in which case the declarator
3602 // portion is empty), if an abstract-declarator is allowed.
3603 D.SetIdentifier(0, Tok.getLocation());
3604 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003605 if (D.getContext() == Declarator::MemberContext)
3606 Diag(Tok, diag::err_expected_member_name_or_semi)
3607 << D.getDeclSpec().getSourceRange();
3608 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003609 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003610 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003611 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003612 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003613 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003614 }
Mike Stump11289f42009-09-09 15:08:12 +00003615
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003616 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003617 assert(D.isPastIdentifier() &&
3618 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003619
Alexis Hunt96d5c762009-11-21 08:43:09 +00003620 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003621 if (D.getIdentifier())
3622 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003623
Chris Lattneracd58a32006-08-06 17:24:14 +00003624 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003625 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003626 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3627 // In such a case, check if we actually have a function declarator; if it
3628 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003629 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3630 // When not in file scope, warn for ambiguous function declarators, just
3631 // in case the author intended it as a variable definition.
3632 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3633 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3634 break;
3635 }
John McCall084e83d2011-03-24 11:26:52 +00003636 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003637 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003638 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003639 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003640 } else {
3641 break;
3642 }
3643 }
3644}
3645
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003646/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3647/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003648/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003649/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3650///
3651/// direct-declarator:
3652/// '(' declarator ')'
3653/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003654/// direct-declarator '(' parameter-type-list ')'
3655/// direct-declarator '(' identifier-list[opt] ')'
3656/// [GNU] direct-declarator '(' parameter-forward-declarations
3657/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003658///
3659void Parser::ParseParenDeclarator(Declarator &D) {
3660 SourceLocation StartLoc = ConsumeParen();
3661 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003662
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003663 // Eat any attributes before we look at whether this is a grouping or function
3664 // declarator paren. If this is a grouping paren, the attribute applies to
3665 // the type being built up, for example:
3666 // int (__attribute__(()) *x)(long y)
3667 // If this ends up not being a grouping paren, the attribute applies to the
3668 // first argument, for example:
3669 // int (__attribute__(()) int x)
3670 // In either case, we need to eat any attributes to be able to determine what
3671 // sort of paren this is.
3672 //
John McCall084e83d2011-03-24 11:26:52 +00003673 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003674 bool RequiresArg = false;
3675 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003676 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003677
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003678 // We require that the argument list (if this is a non-grouping paren) be
3679 // present even if the attribute list was empty.
3680 RequiresArg = true;
3681 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003682 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003683 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003684 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00003685 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
3686 Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00003687 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003688 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003689 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003690 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003691 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003692
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003693 // If we haven't past the identifier yet (or where the identifier would be
3694 // stored, if this is an abstract declarator), then this is probably just
3695 // grouping parens. However, if this could be an abstract-declarator, then
3696 // this could also be the start of function arguments (consider 'void()').
3697 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003698
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003699 if (!D.mayOmitIdentifier()) {
3700 // If this can't be an abstract-declarator, this *must* be a grouping
3701 // paren, because we haven't seen the identifier yet.
3702 isGrouping = true;
3703 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003704 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003705 isDeclarationSpecifier()) { // 'int(int)' is a function.
3706 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3707 // considered to be a type, not a K&R identifier-list.
3708 isGrouping = false;
3709 } else {
3710 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3711 isGrouping = true;
3712 }
Mike Stump11289f42009-09-09 15:08:12 +00003713
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003714 // If this is a grouping paren, handle:
3715 // direct-declarator: '(' declarator ')'
3716 // direct-declarator: '(' attributes declarator ')'
3717 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003718 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003719 D.setGroupingParens(true);
3720
Sebastian Redlbd150f42008-11-21 19:14:01 +00003721 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003722 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003723 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003724 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3725 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003726
3727 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003728 return;
3729 }
Mike Stump11289f42009-09-09 15:08:12 +00003730
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003731 // Okay, if this wasn't a grouping paren, it must be the start of a function
3732 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003733 // identifier (and remember where it would have been), then call into
3734 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003735 D.SetIdentifier(0, Tok.getLocation());
3736
John McCall53fa7142010-12-24 02:08:15 +00003737 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003738}
3739
3740/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3741/// declarator D up to a paren, which indicates that we are parsing function
3742/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003743///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003744/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003745/// after the open paren - they should be considered to be the first argument of
3746/// a parameter. If RequiresArg is true, then the first argument of the
3747/// function is required to be present and required to not be an identifier
3748/// list.
3749///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003750/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3751/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3752/// (C++0x) trailing-return-type[opt].
3753///
3754/// [C++0x] exception-specification:
3755/// dynamic-exception-specification
3756/// noexcept-specification
3757///
3758void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3759 ParsedAttributes &attrs,
3760 bool RequiresArg) {
3761 // lparen is already consumed!
3762 assert(D.isPastIdentifier() && "Should not call before identifier!");
3763
3764 // This should be true when the function has typed arguments.
3765 // Otherwise, it is treated as a K&R-style function.
3766 bool HasProto = false;
3767 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003768 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00003769 // Remember where we see an ellipsis, if any.
3770 SourceLocation EllipsisLoc;
3771
3772 DeclSpec DS(AttrFactory);
3773 bool RefQualifierIsLValueRef = true;
3774 SourceLocation RefQualifierLoc;
3775 ExceptionSpecificationType ESpecType = EST_None;
3776 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003777 SmallVector<ParsedType, 2> DynamicExceptions;
3778 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00003779 ExprResult NoexceptExpr;
3780 ParsedType TrailingReturnType;
3781
3782 SourceLocation EndLoc;
3783
3784 if (isFunctionDeclaratorIdentifierList()) {
3785 if (RequiresArg)
3786 Diag(Tok, diag::err_argument_required_after_attribute);
3787
3788 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
3789
3790 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3791 } else {
3792 // Enter function-declaration scope, limiting any declarators to the
3793 // function prototype scope, including parameter declarators.
3794 ParseScope PrototypeScope(this,
3795 Scope::FunctionPrototypeScope|Scope::DeclScope);
3796
3797 if (Tok.isNot(tok::r_paren))
3798 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
3799 else if (RequiresArg)
3800 Diag(Tok, diag::err_argument_required_after_attribute);
3801
3802 HasProto = ParamInfo.size() || getLang().CPlusPlus;
3803
3804 // If we have the closing ')', eat it.
3805 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3806
3807 if (getLang().CPlusPlus) {
3808 MaybeParseCXX0XAttributes(attrs);
3809
3810 // Parse cv-qualifier-seq[opt].
3811 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
3812 if (!DS.getSourceRange().getEnd().isInvalid())
3813 EndLoc = DS.getSourceRange().getEnd();
3814
3815 // Parse ref-qualifier[opt].
3816 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3817 if (!getLang().CPlusPlus0x)
3818 Diag(Tok, diag::ext_ref_qualifier);
3819
3820 RefQualifierIsLValueRef = Tok.is(tok::amp);
3821 RefQualifierLoc = ConsumeToken();
3822 EndLoc = RefQualifierLoc;
3823 }
3824
3825 // Parse exception-specification[opt].
3826 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3827 DynamicExceptions,
3828 DynamicExceptionRanges,
3829 NoexceptExpr);
3830 if (ESpecType != EST_None)
3831 EndLoc = ESpecRange.getEnd();
3832
3833 // Parse trailing-return-type[opt].
3834 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003835 SourceRange Range;
3836 TrailingReturnType = ParseTrailingReturnType(Range).get();
3837 if (Range.getEnd().isValid())
3838 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00003839 }
3840 }
3841
3842 // Leave prototype scope.
3843 PrototypeScope.Exit();
3844 }
3845
3846 // Remember that we parsed a function type, and remember the attributes.
3847 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
3848 /*isVariadic=*/EllipsisLoc.isValid(),
3849 EllipsisLoc,
3850 ParamInfo.data(), ParamInfo.size(),
3851 DS.getTypeQualifiers(),
3852 RefQualifierIsLValueRef,
3853 RefQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00003854 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00003855 ESpecType, ESpecRange.getBegin(),
3856 DynamicExceptions.data(),
3857 DynamicExceptionRanges.data(),
3858 DynamicExceptions.size(),
3859 NoexceptExpr.isUsable() ?
3860 NoexceptExpr.get() : 0,
3861 LParenLoc, EndLoc, D,
3862 TrailingReturnType),
3863 attrs, EndLoc);
3864}
3865
3866/// isFunctionDeclaratorIdentifierList - This parameter list may have an
3867/// identifier list form for a K&R-style function: void foo(a,b,c)
3868///
3869/// Note that identifier-lists are only allowed for normal declarators, not for
3870/// abstract-declarators.
3871bool Parser::isFunctionDeclaratorIdentifierList() {
3872 return !getLang().CPlusPlus
3873 && Tok.is(tok::identifier)
3874 && !TryAltiVecVectorToken()
3875 // K&R identifier lists can't have typedefs as identifiers, per C99
3876 // 6.7.5.3p11.
3877 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
3878 // Identifier lists follow a really simple grammar: the identifiers can
3879 // be followed *only* by a ", identifier" or ")". However, K&R
3880 // identifier lists are really rare in the brave new modern world, and
3881 // it is very common for someone to typo a type in a non-K&R style
3882 // list. If we are presented with something like: "void foo(intptr x,
3883 // float y)", we don't want to start parsing the function declarator as
3884 // though it is a K&R style declarator just because intptr is an
3885 // invalid type.
3886 //
3887 // To handle this, we check to see if the token after the first
3888 // identifier is a "," or ")". Only then do we parse it as an
3889 // identifier list.
3890 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
3891}
3892
3893/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3894/// we found a K&R-style identifier list instead of a typed parameter list.
3895///
3896/// After returning, ParamInfo will hold the parsed parameters.
3897///
3898/// identifier-list: [C99 6.7.5]
3899/// identifier
3900/// identifier-list ',' identifier
3901///
3902void Parser::ParseFunctionDeclaratorIdentifierList(
3903 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003904 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00003905 // If there was no identifier specified for the declarator, either we are in
3906 // an abstract-declarator, or we are in a parameter declarator which was found
3907 // to be abstract. In abstract-declarators, identifier lists are not valid:
3908 // diagnose this.
3909 if (!D.getIdentifier())
3910 Diag(Tok, diag::ext_ident_list_in_param);
3911
3912 // Maintain an efficient lookup of params we have seen so far.
3913 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
3914
3915 while (1) {
3916 // If this isn't an identifier, report the error and skip until ')'.
3917 if (Tok.isNot(tok::identifier)) {
3918 Diag(Tok, diag::err_expected_ident);
3919 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
3920 // Forget we parsed anything.
3921 ParamInfo.clear();
3922 return;
3923 }
3924
3925 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
3926
3927 // Reject 'typedef int y; int test(x, y)', but continue parsing.
3928 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
3929 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
3930
3931 // Verify that the argument identifier has not already been mentioned.
3932 if (!ParamsSoFar.insert(ParmII)) {
3933 Diag(Tok, diag::err_param_redefinition) << ParmII;
3934 } else {
3935 // Remember this identifier in ParamInfo.
3936 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3937 Tok.getLocation(),
3938 0));
3939 }
3940
3941 // Eat the identifier.
3942 ConsumeToken();
3943
3944 // The list continues if we see a comma.
3945 if (Tok.isNot(tok::comma))
3946 break;
3947 ConsumeToken();
3948 }
3949}
3950
3951/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
3952/// after the opening parenthesis. This function will not parse a K&R-style
3953/// identifier list.
3954///
3955/// D is the declarator being parsed. If attrs is non-null, then the caller
3956/// parsed those arguments immediately after the open paren - they should be
3957/// considered to be the first argument of a parameter.
3958///
3959/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
3960/// be the location of the ellipsis, if any was parsed.
3961///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003962/// parameter-type-list: [C99 6.7.5]
3963/// parameter-list
3964/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003965/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003966///
3967/// parameter-list: [C99 6.7.5]
3968/// parameter-declaration
3969/// parameter-list ',' parameter-declaration
3970///
3971/// parameter-declaration: [C99 6.7.5]
3972/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003973/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003974/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003975/// declaration-specifiers abstract-declarator[opt]
3976/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003977/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003978/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003979///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003980void Parser::ParseParameterDeclarationClause(
3981 Declarator &D,
3982 ParsedAttributes &attrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003983 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00003984 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00003985
Chris Lattner371ed4e2008-04-06 06:57:35 +00003986 while (1) {
3987 if (Tok.is(tok::ellipsis)) {
Douglas Gregor94349fd2009-02-18 07:07:28 +00003988 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003989 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003990 }
Mike Stump11289f42009-09-09 15:08:12 +00003991
Chris Lattner371ed4e2008-04-06 06:57:35 +00003992 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003993 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00003994 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003995
3996 // Skip any Microsoft attributes before a param.
3997 if (getLang().Microsoft && Tok.is(tok::l_square))
3998 ParseMicrosoftAttributes(DS.getAttributes());
3999
4000 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004001
4002 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004003 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004004 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4005 // attributes lost? Should they even be allowed?
4006 // FIXME: If we can leave the attributes in the token stream somehow, we can
4007 // get rid of a parameter (attrs) and this statement. It might be too much
4008 // hassle.
John McCall53fa7142010-12-24 02:08:15 +00004009 DS.takeAttributesFrom(attrs);
4010
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004011 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004012
Chris Lattner371ed4e2008-04-06 06:57:35 +00004013 // Parse the declarator. This is "PrototypeContext", because we must
4014 // accept either 'declarator' or 'abstract-declarator' here.
4015 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4016 ParseDeclarator(ParmDecl);
4017
4018 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004019 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004020
Chris Lattner371ed4e2008-04-06 06:57:35 +00004021 // Remember this parsed parameter in ParamInfo.
4022 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004023
Douglas Gregor4d87df52008-12-16 21:30:33 +00004024 // DefArgToks is used when the parsing of default arguments needs
4025 // to be delayed.
4026 CachedTokens *DefArgToks = 0;
4027
Chris Lattner371ed4e2008-04-06 06:57:35 +00004028 // If no parameter was specified, verify that *something* was specified,
4029 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004030 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4031 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004032 // Completely missing, emit error.
4033 Diag(DSStart, diag::err_missing_param);
4034 } else {
4035 // Otherwise, we have something. Add it and let semantic analysis try
4036 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004037
Chris Lattner371ed4e2008-04-06 06:57:35 +00004038 // Inform the actions module about the parameter declarator, so it gets
4039 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004040 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004041
4042 // Parse the default argument, if any. We parse the default
4043 // arguments in all dialects; the semantic analysis in
4044 // ActOnParamDefaultArgument will reject the default argument in
4045 // C.
4046 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004047 SourceLocation EqualLoc = Tok.getLocation();
4048
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004049 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004050 if (D.getContext() == Declarator::MemberContext) {
4051 // If we're inside a class definition, cache the tokens
4052 // corresponding to the default argument. We'll actually parse
4053 // them when we see the end of the class definition.
4054 // FIXME: Templates will require something similar.
4055 // FIXME: Can we use a smart pointer for Toks?
4056 DefArgToks = new CachedTokens;
4057
Mike Stump11289f42009-09-09 15:08:12 +00004058 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004059 /*StopAtSemi=*/true,
4060 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004061 delete DefArgToks;
4062 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004063 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004064 } else {
4065 // Mark the end of the default argument so that we know when to
4066 // stop when we parse it later on.
4067 Token DefArgEnd;
4068 DefArgEnd.startToken();
4069 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4070 DefArgEnd.setLocation(Tok.getLocation());
4071 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004072 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004073 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004074 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004075 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004076 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004077 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004078
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004079 // The argument isn't actually potentially evaluated unless it is
4080 // used.
4081 EnterExpressionEvaluationContext Eval(Actions,
4082 Sema::PotentiallyEvaluatedIfUsed);
4083
John McCalldadc5752010-08-24 06:29:42 +00004084 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004085 if (DefArgResult.isInvalid()) {
4086 Actions.ActOnParamDefaultArgumentError(Param);
4087 SkipUntil(tok::comma, tok::r_paren, true, true);
4088 } else {
4089 // Inform the actions module about the default argument
4090 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004091 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004092 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004093 }
4094 }
Mike Stump11289f42009-09-09 15:08:12 +00004095
4096 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4097 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004098 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004099 }
4100
4101 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004102 if (Tok.isNot(tok::comma)) {
4103 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004104 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4105
4106 if (!getLang().CPlusPlus) {
4107 // We have ellipsis without a preceding ',', which is ill-formed
4108 // in C. Complain and provide the fix.
4109 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004110 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004111 }
4112 }
4113
4114 break;
4115 }
Mike Stump11289f42009-09-09 15:08:12 +00004116
Chris Lattner371ed4e2008-04-06 06:57:35 +00004117 // Consume the comma.
4118 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004119 }
Mike Stump11289f42009-09-09 15:08:12 +00004120
Chris Lattner6c940e62008-04-06 06:34:08 +00004121}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004122
Chris Lattnere8074e62006-08-06 18:30:15 +00004123/// [C90] direct-declarator '[' constant-expression[opt] ']'
4124/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4125/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4126/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4127/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4128void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00004129 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00004130
Chris Lattner84a11622008-12-18 07:27:21 +00004131 // C array syntax has many features, but by-far the most common is [] and [4].
4132 // This code does a fast path to handle some of the most obvious cases.
4133 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004134 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004135 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004136 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004137
Chris Lattner84a11622008-12-18 07:27:21 +00004138 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004139 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004140 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00004141 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004142 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004143 return;
4144 } else if (Tok.getKind() == tok::numeric_constant &&
4145 GetLookAheadToken(1).is(tok::r_square)) {
4146 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00004147 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00004148 ConsumeToken();
4149
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004150 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004151 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004152 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004153
Chris Lattner84a11622008-12-18 07:27:21 +00004154 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004155 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004156 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00004157 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004158 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004159 return;
4160 }
Mike Stump11289f42009-09-09 15:08:12 +00004161
Chris Lattnere8074e62006-08-06 18:30:15 +00004162 // If valid, this location is the position where we read the 'static' keyword.
4163 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004164 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004165 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004166
Chris Lattnere8074e62006-08-06 18:30:15 +00004167 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004168 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004169 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004170 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004171
Chris Lattnere8074e62006-08-06 18:30:15 +00004172 // If we haven't already read 'static', check to see if there is one after the
4173 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004174 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004175 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004176
Chris Lattnere8074e62006-08-06 18:30:15 +00004177 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004178 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004179 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004180
Chris Lattner521ff2b2008-04-06 05:26:30 +00004181 // Handle the case where we have '[*]' as the array size. However, a leading
4182 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4183 // the the token after the star is a ']'. Since stars in arrays are
4184 // infrequent, use of lookahead is not costly here.
4185 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004186 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004187
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004188 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004189 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004190 StaticLoc = SourceLocation(); // Drop the static.
4191 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004192 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004193 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004194 // Note, in C89, this production uses the constant-expr production instead
4195 // of assignment-expr. The only difference is that assignment-expr allows
4196 // things like '=' and '*='. Sema rejects these in C89 mode because they
4197 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004198
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004199 // Parse the constant-expression or assignment-expression now (depending
4200 // on dialect).
4201 if (getLang().CPlusPlus)
4202 NumElements = ParseConstantExpression();
4203 else
4204 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004205 }
Mike Stump11289f42009-09-09 15:08:12 +00004206
Chris Lattner62591722006-08-12 18:40:58 +00004207 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004208 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004209 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004210 // If the expression was invalid, skip it.
4211 SkipUntil(tok::r_square);
4212 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004213 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004214
4215 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4216
John McCall084e83d2011-03-24 11:26:52 +00004217 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004218 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004219
Chris Lattner84a11622008-12-18 07:27:21 +00004220 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004221 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004222 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004223 NumElements.release(),
4224 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004225 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004226}
4227
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004228/// [GNU] typeof-specifier:
4229/// typeof ( expressions )
4230/// typeof ( type-name )
4231/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004232///
4233void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004234 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004235 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004236 SourceLocation StartLoc = ConsumeToken();
4237
John McCalle8595032010-01-13 20:03:27 +00004238 const bool hasParens = Tok.is(tok::l_paren);
4239
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004240 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004241 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004242 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004243 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4244 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004245 if (hasParens)
4246 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004247
4248 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004249 // FIXME: Not accurate, the range gets one token more than it should.
4250 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004251 else
4252 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004253
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004254 if (isCastExpr) {
4255 if (!CastTy) {
4256 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004257 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004258 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004259
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004260 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004261 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004262 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4263 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004264 DiagID, CastTy))
4265 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004266 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004267 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004268
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004269 // If we get here, the operand to the typeof was an expresion.
4270 if (Operand.isInvalid()) {
4271 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004272 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004273 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004274
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004275 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004276 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004277 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4278 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004279 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004280 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004281}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004282
4283
4284/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4285/// from TryAltiVecVectorToken.
4286bool Parser::TryAltiVecVectorTokenOutOfLine() {
4287 Token Next = NextToken();
4288 switch (Next.getKind()) {
4289 default: return false;
4290 case tok::kw_short:
4291 case tok::kw_long:
4292 case tok::kw_signed:
4293 case tok::kw_unsigned:
4294 case tok::kw_void:
4295 case tok::kw_char:
4296 case tok::kw_int:
4297 case tok::kw_float:
4298 case tok::kw_double:
4299 case tok::kw_bool:
4300 case tok::kw___pixel:
4301 Tok.setKind(tok::kw___vector);
4302 return true;
4303 case tok::identifier:
4304 if (Next.getIdentifierInfo() == Ident_pixel) {
4305 Tok.setKind(tok::kw___vector);
4306 return true;
4307 }
4308 return false;
4309 }
4310}
4311
4312bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4313 const char *&PrevSpec, unsigned &DiagID,
4314 bool &isInvalid) {
4315 if (Tok.getIdentifierInfo() == Ident_vector) {
4316 Token Next = NextToken();
4317 switch (Next.getKind()) {
4318 case tok::kw_short:
4319 case tok::kw_long:
4320 case tok::kw_signed:
4321 case tok::kw_unsigned:
4322 case tok::kw_void:
4323 case tok::kw_char:
4324 case tok::kw_int:
4325 case tok::kw_float:
4326 case tok::kw_double:
4327 case tok::kw_bool:
4328 case tok::kw___pixel:
4329 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4330 return true;
4331 case tok::identifier:
4332 if (Next.getIdentifierInfo() == Ident_pixel) {
4333 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4334 return true;
4335 }
4336 break;
4337 default:
4338 break;
4339 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004340 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004341 DS.isTypeAltiVecVector()) {
4342 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4343 return true;
4344 }
4345 return false;
4346}