blob: 2316590afe67de9bec71cb40ed18238cd5de7f9d [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) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002322
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002323 if (Tok.is(tok::kw___extension__)) {
2324 // __extension__ silences extension warnings in the subexpression.
2325 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002326 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002327 return ParseStructDeclaration(DS, Fields);
2328 }
Mike Stump11289f42009-09-09 15:08:12 +00002329
Steve Naroff97170802007-08-20 22:28:22 +00002330 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002331 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002332
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002333 // If there are no declarators, this is a free-standing declaration
2334 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002335 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002336 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002337 return;
2338 }
2339
2340 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002341 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002342 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002343 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002344 FieldDeclarator DeclaratorInfo(DS);
2345
2346 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002347 if (!FirstDeclarator)
2348 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002349
Steve Naroff97170802007-08-20 22:28:22 +00002350 /// struct-declarator: declarator
2351 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002352 if (Tok.isNot(tok::colon)) {
2353 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2354 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002355 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002356 }
Mike Stump11289f42009-09-09 15:08:12 +00002357
Chris Lattner76c72282007-10-09 17:33:22 +00002358 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002359 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002360 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002361 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002362 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002363 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002364 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002365 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002366
Steve Naroff97170802007-08-20 22:28:22 +00002367 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002368 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002369
John McCallcfefb6d2009-11-03 02:38:08 +00002370 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002371 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002372 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002373
Steve Naroff97170802007-08-20 22:28:22 +00002374 // If we don't have a comma, it is either the end of the list (a ';')
2375 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002376 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002377 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002378
Steve Naroff97170802007-08-20 22:28:22 +00002379 // Consume the comma.
2380 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002381
John McCallcfefb6d2009-11-03 02:38:08 +00002382 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002383 }
Steve Naroff97170802007-08-20 22:28:22 +00002384}
2385
2386/// ParseStructUnionBody
2387/// struct-contents:
2388/// struct-declaration-list
2389/// [EXT] empty
2390/// [GNU] "struct-declaration-list" without terminatoring ';'
2391/// struct-declaration-list:
2392/// struct-declaration
2393/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002394/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002395///
Chris Lattner1300fb92007-01-23 23:42:53 +00002396void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002397 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002398 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2399 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002400
Chris Lattner90a26b02007-01-23 04:38:16 +00002401 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002402
Douglas Gregor658b9552009-01-09 22:42:13 +00002403 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002404 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002405
Chris Lattner7b9ace62007-01-23 20:11:08 +00002406 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2407 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002408 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002409 Diag(Tok, diag::ext_empty_struct_union)
2410 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002411
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002412 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002413
Chris Lattner7b9ace62007-01-23 20:11:08 +00002414 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002415 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002416 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002417
Chris Lattner736ed5d2007-06-09 05:59:07 +00002418 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002419 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002420 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002421 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002422 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002423 ConsumeToken();
2424 continue;
2425 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002426
2427 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002428 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002429
John McCallcfefb6d2009-11-03 02:38:08 +00002430 if (!Tok.is(tok::at)) {
2431 struct CFieldCallback : FieldCallback {
2432 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002433 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002434 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002435
John McCall48871652010-08-21 09:40:31 +00002436 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002437 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002438 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2439
John McCall48871652010-08-21 09:40:31 +00002440 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002441 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002442 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002443 FD.D.getDeclSpec().getSourceRange().getBegin(),
2444 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002445 FieldDecls.push_back(Field);
2446 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002447 }
John McCallcfefb6d2009-11-03 02:38:08 +00002448 } Callback(*this, TagDecl, FieldDecls);
2449
2450 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002451 } else { // Handle @defs
2452 ConsumeToken();
2453 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2454 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002455 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002456 continue;
2457 }
2458 ConsumeToken();
2459 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2460 if (!Tok.is(tok::identifier)) {
2461 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002462 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002463 continue;
2464 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002465 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002466 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002467 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002468 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2469 ConsumeToken();
2470 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002471 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002472
Chris Lattner76c72282007-10-09 17:33:22 +00002473 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002474 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002475 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002476 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002477 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002478 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002479 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2480 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002481 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002482 // If we stopped at a ';', eat it.
2483 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002484 }
2485 }
Mike Stump11289f42009-09-09 15:08:12 +00002486
Steve Naroff33a1e802007-10-29 21:38:07 +00002487 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002488
John McCall084e83d2011-03-24 11:26:52 +00002489 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002490 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002491 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002492
Douglas Gregor0be31a22010-07-02 17:43:08 +00002493 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00002494 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00002495 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002496 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002497 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002498 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002499}
2500
Chris Lattner3b561a32006-08-13 00:12:11 +00002501/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002502/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002503/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002504///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002505/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2506/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002507/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002508/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002509///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002510/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2511/// [C++0x] enum-head '{' enumerator-list ',' '}'
2512///
2513/// enum-head: [C++0x]
2514/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2515/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2516///
2517/// enum-key: [C++0x]
2518/// 'enum'
2519/// 'enum' 'class'
2520/// 'enum' 'struct'
2521///
2522/// enum-base: [C++0x]
2523/// ':' type-specifier-seq
2524///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002525/// [C++] elaborated-type-specifier:
2526/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2527///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002528void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002529 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002530 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002531 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002532 if (Tok.is(tok::code_completion)) {
2533 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002534 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002535 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002536 }
John McCallcb432fa2011-07-06 05:58:41 +00002537
2538 bool IsScopedEnum = false;
2539 bool IsScopedUsingClassTag = false;
2540
2541 if (getLang().CPlusPlus0x &&
2542 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
2543 IsScopedEnum = true;
2544 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2545 ConsumeToken();
2546 }
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002547
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002548 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002549 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002550 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002551
John McCallcb432fa2011-07-06 05:58:41 +00002552 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
2553
Abramo Bagnarad7548482010-05-19 21:37:53 +00002554 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002555 if (getLang().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00002556 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2557 // if a fixed underlying type is allowed.
2558 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2559
John McCallba7bf592010-08-24 05:47:05 +00002560 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002561 return;
2562
2563 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002564 Diag(Tok, diag::err_expected_ident);
2565 if (Tok.isNot(tok::l_brace)) {
2566 // Has no name and is not a definition.
2567 // Skip the rest of this declarator, up until the comma or semicolon.
2568 SkipUntil(tok::comma, true);
2569 return;
2570 }
2571 }
2572 }
Mike Stump11289f42009-09-09 15:08:12 +00002573
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002574 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002575 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2576 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002577 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002578
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002579 // Skip the rest of this declarator, up until the comma or semicolon.
2580 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002581 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002582 }
Mike Stump11289f42009-09-09 15:08:12 +00002583
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002584 // If an identifier is present, consume and remember it.
2585 IdentifierInfo *Name = 0;
2586 SourceLocation NameLoc;
2587 if (Tok.is(tok::identifier)) {
2588 Name = Tok.getIdentifierInfo();
2589 NameLoc = ConsumeToken();
2590 }
Mike Stump11289f42009-09-09 15:08:12 +00002591
Douglas Gregor0bf31402010-10-08 23:50:27 +00002592 if (!Name && IsScopedEnum) {
2593 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2594 // declaration of a scoped enumeration.
2595 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2596 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002597 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002598 }
2599
2600 TypeResult BaseType;
2601
Douglas Gregord1f69f62010-12-01 17:42:47 +00002602 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002603 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002604 bool PossibleBitfield = false;
2605 if (getCurScope()->getFlags() & Scope::ClassScope) {
2606 // If we're in class scope, this can either be an enum declaration with
2607 // an underlying type, or a declaration of a bitfield member. We try to
2608 // use a simple disambiguation scheme first to catch the common cases
2609 // (integer literal, sizeof); if it's still ambiguous, we then consider
2610 // anything that's a simple-type-specifier followed by '(' as an
2611 // expression. This suffices because function types are not valid
2612 // underlying types anyway.
2613 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2614 // If the next token starts an expression, we know we're parsing a
2615 // bit-field. This is the common case.
2616 if (TPR == TPResult::True())
2617 PossibleBitfield = true;
2618 // If the next token starts a type-specifier-seq, it may be either a
2619 // a fixed underlying type or the start of a function-style cast in C++;
2620 // lookahead one more token to see if it's obvious that we have a
2621 // fixed underlying type.
2622 else if (TPR == TPResult::False() &&
2623 GetLookAheadToken(2).getKind() == tok::semi) {
2624 // Consume the ':'.
2625 ConsumeToken();
2626 } else {
2627 // We have the start of a type-specifier-seq, so we have to perform
2628 // tentative parsing to determine whether we have an expression or a
2629 // type.
2630 TentativeParsingAction TPA(*this);
2631
2632 // Consume the ':'.
2633 ConsumeToken();
2634
Douglas Gregora1aec292011-02-22 20:32:04 +00002635 if ((getLang().CPlusPlus &&
2636 isCXXDeclarationSpecifier() != TPResult::True()) ||
2637 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002638 // We'll parse this as a bitfield later.
2639 PossibleBitfield = true;
2640 TPA.Revert();
2641 } else {
2642 // We have a type-specifier-seq.
2643 TPA.Commit();
2644 }
2645 }
2646 } else {
2647 // Consume the ':'.
2648 ConsumeToken();
2649 }
2650
2651 if (!PossibleBitfield) {
2652 SourceRange Range;
2653 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002654
2655 if (!getLang().CPlusPlus0x)
2656 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2657 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002658 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002659 }
2660
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002661 // There are three options here. If we have 'enum foo;', then this is a
2662 // forward declaration. If we have 'enum foo {...' then this is a
2663 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2664 //
2665 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2666 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2667 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2668 //
John McCallfaf5fb42010-08-26 23:41:50 +00002669 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002670 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002671 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002672 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002673 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002674 else
John McCallfaf5fb42010-08-26 23:41:50 +00002675 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002676
2677 // enums cannot be templates, although they can be referenced from a
2678 // template.
2679 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002680 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002681 Diag(Tok, diag::err_enum_template);
2682
2683 // Skip the rest of this declarator, up until the comma or semicolon.
2684 SkipUntil(tok::comma, true);
2685 return;
2686 }
2687
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002688 if (!Name && TUK != Sema::TUK_Definition) {
2689 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2690
2691 // Skip the rest of this declarator, up until the comma or semicolon.
2692 SkipUntil(tok::comma, true);
2693 return;
2694 }
2695
Douglas Gregord6ab8742009-05-28 23:31:59 +00002696 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002697 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002698 const char *PrevSpec = 0;
2699 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002700 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002701 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002702 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002703 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002704 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002705 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002706
Douglas Gregorba41d012010-04-24 16:38:41 +00002707 if (IsDependent) {
2708 // This enum has a dependent nested-name-specifier. Handle it as a
2709 // dependent tag.
2710 if (!Name) {
2711 DS.SetTypeSpecError();
2712 Diag(Tok, diag::err_expected_type_name_after_typename);
2713 return;
2714 }
2715
Douglas Gregor0be31a22010-07-02 17:43:08 +00002716 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002717 TUK, SS, Name, StartLoc,
2718 NameLoc);
2719 if (Type.isInvalid()) {
2720 DS.SetTypeSpecError();
2721 return;
2722 }
2723
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002724 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2725 NameLoc.isValid() ? NameLoc : StartLoc,
2726 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002727 Diag(StartLoc, DiagID) << PrevSpec;
2728
2729 return;
2730 }
Mike Stump11289f42009-09-09 15:08:12 +00002731
John McCall48871652010-08-21 09:40:31 +00002732 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002733 // The action failed to produce an enumeration tag. If this is a
2734 // definition, consume the entire definition.
2735 if (Tok.is(tok::l_brace)) {
2736 ConsumeBrace();
2737 SkipUntil(tok::r_brace);
2738 }
2739
2740 DS.SetTypeSpecError();
2741 return;
2742 }
2743
Chris Lattner76c72282007-10-09 17:33:22 +00002744 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002745 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002746
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002747 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2748 NameLoc.isValid() ? NameLoc : StartLoc,
2749 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002750 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002751}
2752
Chris Lattnerc1915e22007-01-25 07:29:02 +00002753/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2754/// enumerator-list:
2755/// enumerator
2756/// enumerator-list ',' enumerator
2757/// enumerator:
2758/// enumeration-constant
2759/// enumeration-constant '=' constant-expression
2760/// enumeration-constant:
2761/// identifier
2762///
John McCall48871652010-08-21 09:40:31 +00002763void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002764 // Enter the scope of the enum body and start the definition.
2765 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002766 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002767
Chris Lattnerc1915e22007-01-25 07:29:02 +00002768 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002769
Chris Lattner37256fb2007-08-27 17:24:30 +00002770 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002771 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002772 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002773
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002774 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002775
John McCall48871652010-08-21 09:40:31 +00002776 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002777
Chris Lattnerc1915e22007-01-25 07:29:02 +00002778 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002779 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002780 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2781 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002782
John McCall811a0f52010-10-22 23:36:17 +00002783 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002784 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002785 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002786
Chris Lattnerc1915e22007-01-25 07:29:02 +00002787 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002788 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002789 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002790 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002791 AssignedVal = ParseConstantExpression();
2792 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002793 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002794 }
Mike Stump11289f42009-09-09 15:08:12 +00002795
Chris Lattnerc1915e22007-01-25 07:29:02 +00002796 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002797 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2798 LastEnumConstDecl,
2799 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002800 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002801 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002802 EnumConstantDecls.push_back(EnumConstDecl);
2803 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002804
Douglas Gregorce66d022010-09-07 14:51:08 +00002805 if (Tok.is(tok::identifier)) {
2806 // We're missing a comma between enumerators.
2807 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2808 Diag(Loc, diag::err_enumerator_list_missing_comma)
2809 << FixItHint::CreateInsertion(Loc, ", ");
2810 continue;
2811 }
2812
Chris Lattner76c72282007-10-09 17:33:22 +00002813 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002814 break;
2815 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002816
2817 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002818 !(getLang().C99 || getLang().CPlusPlus0x))
2819 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2820 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002821 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002822 }
Mike Stump11289f42009-09-09 15:08:12 +00002823
Chris Lattnerc1915e22007-01-25 07:29:02 +00002824 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002825 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002826
Chris Lattnerc1915e22007-01-25 07:29:02 +00002827 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002828 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002829 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002830
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002831 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2832 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002833 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002834
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002835 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002836 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002837}
Chris Lattner3b561a32006-08-13 00:12:11 +00002838
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002839/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002840/// start of a type-qualifier-list.
2841bool Parser::isTypeQualifier() const {
2842 switch (Tok.getKind()) {
2843 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002844
2845 // type-qualifier only in OpenCL
2846 case tok::kw_private:
2847 return getLang().OpenCL;
2848
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002849 // type-qualifier
2850 case tok::kw_const:
2851 case tok::kw_volatile:
2852 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002853 case tok::kw___private:
2854 case tok::kw___local:
2855 case tok::kw___global:
2856 case tok::kw___constant:
2857 case tok::kw___read_only:
2858 case tok::kw___read_write:
2859 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002860 return true;
2861 }
2862}
2863
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002864/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2865/// is definitely a type-specifier. Return false if it isn't part of a type
2866/// specifier or if we're not sure.
2867bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2868 switch (Tok.getKind()) {
2869 default: return false;
2870 // type-specifiers
2871 case tok::kw_short:
2872 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002873 case tok::kw___int64:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002874 case tok::kw_signed:
2875 case tok::kw_unsigned:
2876 case tok::kw__Complex:
2877 case tok::kw__Imaginary:
2878 case tok::kw_void:
2879 case tok::kw_char:
2880 case tok::kw_wchar_t:
2881 case tok::kw_char16_t:
2882 case tok::kw_char32_t:
2883 case tok::kw_int:
2884 case tok::kw_float:
2885 case tok::kw_double:
2886 case tok::kw_bool:
2887 case tok::kw__Bool:
2888 case tok::kw__Decimal32:
2889 case tok::kw__Decimal64:
2890 case tok::kw__Decimal128:
2891 case tok::kw___vector:
2892
2893 // struct-or-union-specifier (C99) or class-specifier (C++)
2894 case tok::kw_class:
2895 case tok::kw_struct:
2896 case tok::kw_union:
2897 // enum-specifier
2898 case tok::kw_enum:
2899
2900 // typedef-name
2901 case tok::annot_typename:
2902 return true;
2903 }
2904}
2905
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002906/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002907/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002908bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002909 switch (Tok.getKind()) {
2910 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002911
Chris Lattner020bab92009-01-04 23:41:41 +00002912 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002913 if (TryAltiVecVectorToken())
2914 return true;
2915 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002916 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002917 // Annotate typenames and C++ scope specifiers. If we get one, just
2918 // recurse to handle whatever we get.
2919 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002920 return true;
2921 if (Tok.is(tok::identifier))
2922 return false;
2923 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002924
Chris Lattner020bab92009-01-04 23:41:41 +00002925 case tok::coloncolon: // ::foo::bar
2926 if (NextToken().is(tok::kw_new) || // ::new
2927 NextToken().is(tok::kw_delete)) // ::delete
2928 return false;
2929
Chris Lattner020bab92009-01-04 23:41:41 +00002930 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002931 return true;
2932 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002933
Chris Lattnere37e2332006-08-15 04:50:22 +00002934 // GNU attributes support.
2935 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002936 // GNU typeof support.
2937 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002938
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002939 // type-specifiers
2940 case tok::kw_short:
2941 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002942 case tok::kw___int64:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002943 case tok::kw_signed:
2944 case tok::kw_unsigned:
2945 case tok::kw__Complex:
2946 case tok::kw__Imaginary:
2947 case tok::kw_void:
2948 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002949 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002950 case tok::kw_char16_t:
2951 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002952 case tok::kw_int:
2953 case tok::kw_float:
2954 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002955 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002956 case tok::kw__Bool:
2957 case tok::kw__Decimal32:
2958 case tok::kw__Decimal64:
2959 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002960 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002961
Chris Lattner861a2262008-04-13 18:59:07 +00002962 // struct-or-union-specifier (C99) or class-specifier (C++)
2963 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002964 case tok::kw_struct:
2965 case tok::kw_union:
2966 // enum-specifier
2967 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002968
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002969 // type-qualifier
2970 case tok::kw_const:
2971 case tok::kw_volatile:
2972 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002973
2974 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002975 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002976 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002977
Chris Lattner409bf7d2008-10-20 00:25:30 +00002978 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2979 case tok::less:
2980 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002981
Steve Naroff44ac7772008-12-25 14:16:32 +00002982 case tok::kw___cdecl:
2983 case tok::kw___stdcall:
2984 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002985 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002986 case tok::kw___w64:
2987 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002988 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00002989 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002990
2991 case tok::kw___private:
2992 case tok::kw___local:
2993 case tok::kw___global:
2994 case tok::kw___constant:
2995 case tok::kw___read_only:
2996 case tok::kw___read_write:
2997 case tok::kw___write_only:
2998
Eli Friedman53339e02009-06-08 23:27:34 +00002999 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003000
3001 case tok::kw_private:
3002 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003003 }
3004}
3005
Chris Lattneracd58a32006-08-06 17:24:14 +00003006/// isDeclarationSpecifier() - Return true if the current token is part of a
3007/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003008///
3009/// \param DisambiguatingWithExpression True to indicate that the purpose of
3010/// this check is to disambiguate between an expression and a declaration.
3011bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003012 switch (Tok.getKind()) {
3013 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003014
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003015 case tok::kw_private:
3016 return getLang().OpenCL;
3017
Chris Lattner020bab92009-01-04 23:41:41 +00003018 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003019 // Unfortunate hack to support "Class.factoryMethod" notation.
3020 if (getLang().ObjC1 && NextToken().is(tok::period))
3021 return false;
John Thompson22334602010-02-05 00:12:22 +00003022 if (TryAltiVecVectorToken())
3023 return true;
3024 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003025 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003026 // Annotate typenames and C++ scope specifiers. If we get one, just
3027 // recurse to handle whatever we get.
3028 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003029 return true;
3030 if (Tok.is(tok::identifier))
3031 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003032
3033 // If we're in Objective-C and we have an Objective-C class type followed
3034 // by an identifier and then either ':' or ']', in a place where an
3035 // expression is permitted, then this is probably a class message send
3036 // missing the initial '['. In this case, we won't consider this to be
3037 // the start of a declaration.
3038 if (DisambiguatingWithExpression &&
3039 isStartOfObjCClassMessageMissingOpenBracket())
3040 return false;
3041
John McCall1f476a12010-02-26 08:45:28 +00003042 return isDeclarationSpecifier();
3043
Chris Lattner020bab92009-01-04 23:41:41 +00003044 case tok::coloncolon: // ::foo::bar
3045 if (NextToken().is(tok::kw_new) || // ::new
3046 NextToken().is(tok::kw_delete)) // ::delete
3047 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003048
Chris Lattner020bab92009-01-04 23:41:41 +00003049 // Annotate typenames and C++ scope specifiers. If we get one, just
3050 // recurse to handle whatever we get.
3051 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003052 return true;
3053 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003054
Chris Lattneracd58a32006-08-06 17:24:14 +00003055 // storage-class-specifier
3056 case tok::kw_typedef:
3057 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003058 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003059 case tok::kw_static:
3060 case tok::kw_auto:
3061 case tok::kw_register:
3062 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003063
Chris Lattneracd58a32006-08-06 17:24:14 +00003064 // type-specifiers
3065 case tok::kw_short:
3066 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003067 case tok::kw___int64:
Chris Lattneracd58a32006-08-06 17:24:14 +00003068 case tok::kw_signed:
3069 case tok::kw_unsigned:
3070 case tok::kw__Complex:
3071 case tok::kw__Imaginary:
3072 case tok::kw_void:
3073 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003074 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003075 case tok::kw_char16_t:
3076 case tok::kw_char32_t:
3077
Chris Lattneracd58a32006-08-06 17:24:14 +00003078 case tok::kw_int:
3079 case tok::kw_float:
3080 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003081 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003082 case tok::kw__Bool:
3083 case tok::kw__Decimal32:
3084 case tok::kw__Decimal64:
3085 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003086 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003087
Chris Lattner861a2262008-04-13 18:59:07 +00003088 // struct-or-union-specifier (C99) or class-specifier (C++)
3089 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003090 case tok::kw_struct:
3091 case tok::kw_union:
3092 // enum-specifier
3093 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003094
Chris Lattneracd58a32006-08-06 17:24:14 +00003095 // type-qualifier
3096 case tok::kw_const:
3097 case tok::kw_volatile:
3098 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003099
Chris Lattneracd58a32006-08-06 17:24:14 +00003100 // function-specifier
3101 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003102 case tok::kw_virtual:
3103 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003104
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003105 // static_assert-declaration
3106 case tok::kw__Static_assert:
3107
Chris Lattner599e47e2007-08-09 17:01:07 +00003108 // GNU typeof support.
3109 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003110
Chris Lattner599e47e2007-08-09 17:01:07 +00003111 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003112 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00003113 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003114
Francois Pichete878cb62011-06-19 08:02:06 +00003115 // C++0x decltype.
3116 case tok::kw_decltype:
3117 return true;
3118
Chris Lattner8b2ec162008-07-26 03:38:44 +00003119 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3120 case tok::less:
3121 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003122
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003123 // typedef-name
3124 case tok::annot_typename:
3125 return !DisambiguatingWithExpression ||
3126 !isStartOfObjCClassMessageMissingOpenBracket();
3127
Steve Narofff192fab2009-01-06 19:34:12 +00003128 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003129 case tok::kw___cdecl:
3130 case tok::kw___stdcall:
3131 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003132 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003133 case tok::kw___w64:
3134 case tok::kw___ptr64:
3135 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003136 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003137 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003138
3139 case tok::kw___private:
3140 case tok::kw___local:
3141 case tok::kw___global:
3142 case tok::kw___constant:
3143 case tok::kw___read_only:
3144 case tok::kw___read_write:
3145 case tok::kw___write_only:
3146
Eli Friedman53339e02009-06-08 23:27:34 +00003147 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003148 }
3149}
3150
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003151bool Parser::isConstructorDeclarator() {
3152 TentativeParsingAction TPA(*this);
3153
3154 // Parse the C++ scope specifier.
3155 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003156 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00003157 TPA.Revert();
3158 return false;
3159 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003160
3161 // Parse the constructor name.
3162 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3163 // We already know that we have a constructor name; just consume
3164 // the token.
3165 ConsumeToken();
3166 } else {
3167 TPA.Revert();
3168 return false;
3169 }
3170
3171 // Current class name must be followed by a left parentheses.
3172 if (Tok.isNot(tok::l_paren)) {
3173 TPA.Revert();
3174 return false;
3175 }
3176 ConsumeParen();
3177
3178 // A right parentheses or ellipsis signals that we have a constructor.
3179 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3180 TPA.Revert();
3181 return true;
3182 }
3183
3184 // If we need to, enter the specified scope.
3185 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003186 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003187 DeclScopeObj.EnterDeclaratorScope();
3188
Francois Pichet79f3a872011-01-31 04:54:32 +00003189 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003190 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003191 MaybeParseMicrosoftAttributes(Attrs);
3192
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003193 // Check whether the next token(s) are part of a declaration
3194 // specifier, in which case we have the start of a parameter and,
3195 // therefore, we know that this is a constructor.
3196 bool IsConstructor = isDeclarationSpecifier();
3197 TPA.Revert();
3198 return IsConstructor;
3199}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003200
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003201/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003202/// type-qualifier-list: [C99 6.7.5]
3203/// type-qualifier
3204/// [vendor] attributes
3205/// [ only if VendorAttributesAllowed=true ]
3206/// type-qualifier-list type-qualifier
3207/// [vendor] type-qualifier-list attributes
3208/// [ only if VendorAttributesAllowed=true ]
3209/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3210/// [ only if CXX0XAttributesAllowed=true ]
3211/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003212///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003213void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3214 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00003215 bool CXX0XAttributesAllowed) {
3216 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3217 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00003218 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003219 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003220 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00003221 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003222 else
3223 Diag(Loc, diag::err_attributes_not_allowed);
3224 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003225
3226 SourceLocation EndLoc;
3227
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003228 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003229 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003230 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003231 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003232 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003233
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003234 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003235 case tok::code_completion:
3236 Actions.CodeCompleteTypeQualifiers(DS);
3237 ConsumeCodeCompletionToken();
3238 break;
3239
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003240 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003241 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3242 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003243 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003244 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003245 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3246 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003247 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003248 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003249 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3250 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003251 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003252
3253 // OpenCL qualifiers:
3254 case tok::kw_private:
3255 if (!getLang().OpenCL)
3256 goto DoneWithTypeQuals;
3257 case tok::kw___private:
3258 case tok::kw___global:
3259 case tok::kw___local:
3260 case tok::kw___constant:
3261 case tok::kw___read_only:
3262 case tok::kw___write_only:
3263 case tok::kw___read_write:
3264 ParseOpenCLQualifiers(DS);
3265 break;
3266
Eli Friedman53339e02009-06-08 23:27:34 +00003267 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003268 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00003269 case tok::kw___cdecl:
3270 case tok::kw___stdcall:
3271 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003272 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00003273 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003274 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003275 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003276 continue;
3277 }
3278 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003279 case tok::kw___pascal:
3280 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003281 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003282 continue;
3283 }
3284 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003285 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003286 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003287 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003288 continue; // do *not* consume the next token!
3289 }
3290 // otherwise, FALL THROUGH!
3291 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003292 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003293 // If this is not a type-qualifier token, we're done reading type
3294 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003295 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003296 if (EndLoc.isValid())
3297 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003298 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003299 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003300
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003301 // If the specifier combination wasn't legal, issue a diagnostic.
3302 if (isInvalid) {
3303 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003304 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003305 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003306 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003307 }
3308}
3309
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003310
3311/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3312///
3313void Parser::ParseDeclarator(Declarator &D) {
3314 /// This implements the 'declarator' production in the C grammar, then checks
3315 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003316 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003317}
3318
Sebastian Redlbd150f42008-11-21 19:14:01 +00003319/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3320/// is parsed by the function passed to it. Pass null, and the direct-declarator
3321/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003322/// ptr-operator production.
3323///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003324/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3325/// [C] pointer[opt] direct-declarator
3326/// [C++] direct-declarator
3327/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003328///
3329/// pointer: [C99 6.7.5]
3330/// '*' type-qualifier-list[opt]
3331/// '*' type-qualifier-list[opt] pointer
3332///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003333/// ptr-operator:
3334/// '*' cv-qualifier-seq[opt]
3335/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003336/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003337/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003338/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003339/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003340void Parser::ParseDeclaratorInternal(Declarator &D,
3341 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003342 if (Diags.hasAllExtensionsSilenced())
3343 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003344
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003345 // C++ member pointers start with a '::' or a nested-name.
3346 // Member pointers get special handling, since there's no place for the
3347 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003348 if (getLang().CPlusPlus &&
3349 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3350 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003351 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003352 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003353
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003354 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003355 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003356 // The scope spec really belongs to the direct-declarator.
3357 D.getCXXScopeSpec() = SS;
3358 if (DirectDeclParser)
3359 (this->*DirectDeclParser)(D);
3360 return;
3361 }
3362
3363 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003364 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003365 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003366 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003367 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003368
3369 // Recurse to parse whatever is left.
3370 ParseDeclaratorInternal(D, DirectDeclParser);
3371
3372 // Sema will have to catch (syntactically invalid) pointers into global
3373 // scope. It has to catch pointers into namespace scope anyway.
3374 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003375 Loc),
3376 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003377 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003378 return;
3379 }
3380 }
3381
3382 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003383 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003384 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003385 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003386 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003387 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003388 if (DirectDeclParser)
3389 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003390 return;
3391 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003392
Sebastian Redled0f3b02009-03-15 22:02:01 +00003393 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3394 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003395 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003396 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003397
Chris Lattner9eac9312009-03-27 04:18:06 +00003398 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003399 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003400 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003401
Bill Wendling3708c182007-05-27 10:15:43 +00003402 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003403 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003404
Bill Wendling3708c182007-05-27 10:15:43 +00003405 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003406 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003407 if (Kind == tok::star)
3408 // Remember that we parsed a pointer type, and remember the type-quals.
3409 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003410 DS.getConstSpecLoc(),
3411 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003412 DS.getRestrictSpecLoc()),
3413 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003414 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003415 else
3416 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003417 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003418 Loc),
3419 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003420 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003421 } else {
3422 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003423 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003424
Sebastian Redl3b27be62009-03-23 00:00:23 +00003425 // Complain about rvalue references in C++03, but then go on and build
3426 // the declarator.
3427 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003428 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003429
Bill Wendling93efb222007-06-02 23:28:54 +00003430 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3431 // cv-qualifiers are introduced through the use of a typedef or of a
3432 // template type argument, in which case the cv-qualifiers are ignored.
3433 //
3434 // [GNU] Retricted references are allowed.
3435 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003436 // [C++0x] Attributes on references are not allowed.
3437 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003438 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003439
3440 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3441 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3442 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003443 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003444 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3445 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003446 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003447 }
Bill Wendling3708c182007-05-27 10:15:43 +00003448
3449 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003450 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003451
Douglas Gregor66583c52008-11-03 15:51:28 +00003452 if (D.getNumTypeObjects() > 0) {
3453 // C++ [dcl.ref]p4: There shall be no references to references.
3454 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3455 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003456 if (const IdentifierInfo *II = D.getIdentifier())
3457 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3458 << II;
3459 else
3460 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3461 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003462
Sebastian Redlbd150f42008-11-21 19:14:01 +00003463 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003464 // can go ahead and build the (technically ill-formed)
3465 // declarator: reference collapsing will take care of it.
3466 }
3467 }
3468
Bill Wendling3708c182007-05-27 10:15:43 +00003469 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003470 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003471 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003472 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003473 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003474 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003475}
3476
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003477/// ParseDirectDeclarator
3478/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003479/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003480/// '(' declarator ')'
3481/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003482/// [C90] direct-declarator '[' constant-expression[opt] ']'
3483/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3484/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3485/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3486/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003487/// direct-declarator '(' parameter-type-list ')'
3488/// direct-declarator '(' identifier-list[opt] ')'
3489/// [GNU] direct-declarator '(' parameter-forward-declarations
3490/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003491/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3492/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003493/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003494///
3495/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003496/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003497/// '::'[opt] nested-name-specifier[opt] type-name
3498///
3499/// id-expression: [C++ 5.1]
3500/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003501/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003502///
3503/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003504/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003505/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003506/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003507/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003508/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003509///
Chris Lattneracd58a32006-08-06 17:24:14 +00003510void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003511 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003512
Douglas Gregor7861a802009-11-03 01:35:08 +00003513 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3514 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003515 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003516 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003517 }
3518
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003519 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003520 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003521 // Change the declaration context for name lookup, until this function
3522 // is exited (and the declarator has been parsed).
3523 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003524 }
3525
Douglas Gregor27b4c162010-12-23 22:44:42 +00003526 // C++0x [dcl.fct]p14:
3527 // There is a syntactic ambiguity when an ellipsis occurs at the end
3528 // of a parameter-declaration-clause without a preceding comma. In
3529 // this case, the ellipsis is parsed as part of the
3530 // abstract-declarator if the type of the parameter names a template
3531 // parameter pack that has not been expanded; otherwise, it is parsed
3532 // as part of the parameter-declaration-clause.
3533 if (Tok.is(tok::ellipsis) &&
3534 !((D.getContext() == Declarator::PrototypeContext ||
3535 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003536 NextToken().is(tok::r_paren) &&
3537 !Actions.containsUnexpandedParameterPacks(D)))
3538 D.setEllipsisLoc(ConsumeToken());
3539
Douglas Gregor7861a802009-11-03 01:35:08 +00003540 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3541 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3542 // We found something that indicates the start of an unqualified-id.
3543 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003544 bool AllowConstructorName;
3545 if (D.getDeclSpec().hasTypeSpecifier())
3546 AllowConstructorName = false;
3547 else if (D.getCXXScopeSpec().isSet())
3548 AllowConstructorName =
3549 (D.getContext() == Declarator::FileContext ||
3550 (D.getContext() == Declarator::MemberContext &&
3551 D.getDeclSpec().isFriendSpecified()));
3552 else
3553 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3554
Douglas Gregor7861a802009-11-03 01:35:08 +00003555 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3556 /*EnteringContext=*/true,
3557 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003558 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003559 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003560 D.getName()) ||
3561 // Once we're past the identifier, if the scope was bad, mark the
3562 // whole declarator bad.
3563 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003564 D.SetIdentifier(0, Tok.getLocation());
3565 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003566 } else {
3567 // Parsed the unqualified-id; update range information and move along.
3568 if (D.getSourceRange().getBegin().isInvalid())
3569 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3570 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003571 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003572 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003573 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003574 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003575 assert(!getLang().CPlusPlus &&
3576 "There's a C++-specific check for tok::identifier above");
3577 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3578 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3579 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003580 goto PastIdentifier;
3581 }
3582
3583 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003584 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003585 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003586 // Example: 'char (*X)' or 'int (*XX)(void)'
3587 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003588
3589 // If the declarator was parenthesized, we entered the declarator
3590 // scope when parsing the parenthesized declarator, then exited
3591 // the scope already. Re-enter the scope, if we need to.
3592 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003593 // If there was an error parsing parenthesized declarator, declarator
3594 // scope may have been enterred before. Don't do it again.
3595 if (!D.isInvalidType() &&
3596 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003597 // Change the declaration context for name lookup, until this function
3598 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003599 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003600 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003601 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003602 // This could be something simple like "int" (in which case the declarator
3603 // portion is empty), if an abstract-declarator is allowed.
3604 D.SetIdentifier(0, Tok.getLocation());
3605 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003606 if (D.getContext() == Declarator::MemberContext)
3607 Diag(Tok, diag::err_expected_member_name_or_semi)
3608 << D.getDeclSpec().getSourceRange();
3609 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003610 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003611 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003612 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003613 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003614 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003615 }
Mike Stump11289f42009-09-09 15:08:12 +00003616
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003617 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003618 assert(D.isPastIdentifier() &&
3619 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003620
Alexis Hunt96d5c762009-11-21 08:43:09 +00003621 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003622 if (D.getIdentifier())
3623 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003624
Chris Lattneracd58a32006-08-06 17:24:14 +00003625 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003626 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003627 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3628 // In such a case, check if we actually have a function declarator; if it
3629 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003630 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3631 // When not in file scope, warn for ambiguous function declarators, just
3632 // in case the author intended it as a variable definition.
3633 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3634 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3635 break;
3636 }
John McCall084e83d2011-03-24 11:26:52 +00003637 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003638 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003639 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003640 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003641 } else {
3642 break;
3643 }
3644 }
3645}
3646
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003647/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3648/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003649/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003650/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3651///
3652/// direct-declarator:
3653/// '(' declarator ')'
3654/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003655/// direct-declarator '(' parameter-type-list ')'
3656/// direct-declarator '(' identifier-list[opt] ')'
3657/// [GNU] direct-declarator '(' parameter-forward-declarations
3658/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003659///
3660void Parser::ParseParenDeclarator(Declarator &D) {
3661 SourceLocation StartLoc = ConsumeParen();
3662 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003663
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003664 // Eat any attributes before we look at whether this is a grouping or function
3665 // declarator paren. If this is a grouping paren, the attribute applies to
3666 // the type being built up, for example:
3667 // int (__attribute__(()) *x)(long y)
3668 // If this ends up not being a grouping paren, the attribute applies to the
3669 // first argument, for example:
3670 // int (__attribute__(()) int x)
3671 // In either case, we need to eat any attributes to be able to determine what
3672 // sort of paren this is.
3673 //
John McCall084e83d2011-03-24 11:26:52 +00003674 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003675 bool RequiresArg = false;
3676 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003677 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003678
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003679 // We require that the argument list (if this is a non-grouping paren) be
3680 // present even if the attribute list was empty.
3681 RequiresArg = true;
3682 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003683 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003684 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003685 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00003686 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
3687 Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00003688 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003689 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003690 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003691 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003692 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003693
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003694 // If we haven't past the identifier yet (or where the identifier would be
3695 // stored, if this is an abstract declarator), then this is probably just
3696 // grouping parens. However, if this could be an abstract-declarator, then
3697 // this could also be the start of function arguments (consider 'void()').
3698 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003699
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003700 if (!D.mayOmitIdentifier()) {
3701 // If this can't be an abstract-declarator, this *must* be a grouping
3702 // paren, because we haven't seen the identifier yet.
3703 isGrouping = true;
3704 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003705 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003706 isDeclarationSpecifier()) { // 'int(int)' is a function.
3707 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3708 // considered to be a type, not a K&R identifier-list.
3709 isGrouping = false;
3710 } else {
3711 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3712 isGrouping = true;
3713 }
Mike Stump11289f42009-09-09 15:08:12 +00003714
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003715 // If this is a grouping paren, handle:
3716 // direct-declarator: '(' declarator ')'
3717 // direct-declarator: '(' attributes declarator ')'
3718 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003719 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003720 D.setGroupingParens(true);
3721
Sebastian Redlbd150f42008-11-21 19:14:01 +00003722 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003723 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003724 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003725 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3726 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003727
3728 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003729 return;
3730 }
Mike Stump11289f42009-09-09 15:08:12 +00003731
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003732 // Okay, if this wasn't a grouping paren, it must be the start of a function
3733 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003734 // identifier (and remember where it would have been), then call into
3735 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003736 D.SetIdentifier(0, Tok.getLocation());
3737
John McCall53fa7142010-12-24 02:08:15 +00003738 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003739}
3740
3741/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3742/// declarator D up to a paren, which indicates that we are parsing function
3743/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003744///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003745/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003746/// after the open paren - they should be considered to be the first argument of
3747/// a parameter. If RequiresArg is true, then the first argument of the
3748/// function is required to be present and required to not be an identifier
3749/// list.
3750///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003751/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3752/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3753/// (C++0x) trailing-return-type[opt].
3754///
3755/// [C++0x] exception-specification:
3756/// dynamic-exception-specification
3757/// noexcept-specification
3758///
3759void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3760 ParsedAttributes &attrs,
3761 bool RequiresArg) {
3762 // lparen is already consumed!
3763 assert(D.isPastIdentifier() && "Should not call before identifier!");
3764
3765 // This should be true when the function has typed arguments.
3766 // Otherwise, it is treated as a K&R-style function.
3767 bool HasProto = false;
3768 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003769 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00003770 // Remember where we see an ellipsis, if any.
3771 SourceLocation EllipsisLoc;
3772
3773 DeclSpec DS(AttrFactory);
3774 bool RefQualifierIsLValueRef = true;
3775 SourceLocation RefQualifierLoc;
3776 ExceptionSpecificationType ESpecType = EST_None;
3777 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003778 SmallVector<ParsedType, 2> DynamicExceptions;
3779 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00003780 ExprResult NoexceptExpr;
3781 ParsedType TrailingReturnType;
3782
3783 SourceLocation EndLoc;
3784
3785 if (isFunctionDeclaratorIdentifierList()) {
3786 if (RequiresArg)
3787 Diag(Tok, diag::err_argument_required_after_attribute);
3788
3789 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
3790
3791 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3792 } else {
3793 // Enter function-declaration scope, limiting any declarators to the
3794 // function prototype scope, including parameter declarators.
3795 ParseScope PrototypeScope(this,
3796 Scope::FunctionPrototypeScope|Scope::DeclScope);
3797
3798 if (Tok.isNot(tok::r_paren))
3799 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
3800 else if (RequiresArg)
3801 Diag(Tok, diag::err_argument_required_after_attribute);
3802
3803 HasProto = ParamInfo.size() || getLang().CPlusPlus;
3804
3805 // If we have the closing ')', eat it.
3806 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3807
3808 if (getLang().CPlusPlus) {
3809 MaybeParseCXX0XAttributes(attrs);
3810
3811 // Parse cv-qualifier-seq[opt].
3812 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
3813 if (!DS.getSourceRange().getEnd().isInvalid())
3814 EndLoc = DS.getSourceRange().getEnd();
3815
3816 // Parse ref-qualifier[opt].
3817 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3818 if (!getLang().CPlusPlus0x)
3819 Diag(Tok, diag::ext_ref_qualifier);
3820
3821 RefQualifierIsLValueRef = Tok.is(tok::amp);
3822 RefQualifierLoc = ConsumeToken();
3823 EndLoc = RefQualifierLoc;
3824 }
3825
3826 // Parse exception-specification[opt].
3827 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3828 DynamicExceptions,
3829 DynamicExceptionRanges,
3830 NoexceptExpr);
3831 if (ESpecType != EST_None)
3832 EndLoc = ESpecRange.getEnd();
3833
3834 // Parse trailing-return-type[opt].
3835 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003836 SourceRange Range;
3837 TrailingReturnType = ParseTrailingReturnType(Range).get();
3838 if (Range.getEnd().isValid())
3839 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00003840 }
3841 }
3842
3843 // Leave prototype scope.
3844 PrototypeScope.Exit();
3845 }
3846
3847 // Remember that we parsed a function type, and remember the attributes.
3848 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
3849 /*isVariadic=*/EllipsisLoc.isValid(),
3850 EllipsisLoc,
3851 ParamInfo.data(), ParamInfo.size(),
3852 DS.getTypeQualifiers(),
3853 RefQualifierIsLValueRef,
3854 RefQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00003855 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00003856 ESpecType, ESpecRange.getBegin(),
3857 DynamicExceptions.data(),
3858 DynamicExceptionRanges.data(),
3859 DynamicExceptions.size(),
3860 NoexceptExpr.isUsable() ?
3861 NoexceptExpr.get() : 0,
3862 LParenLoc, EndLoc, D,
3863 TrailingReturnType),
3864 attrs, EndLoc);
3865}
3866
3867/// isFunctionDeclaratorIdentifierList - This parameter list may have an
3868/// identifier list form for a K&R-style function: void foo(a,b,c)
3869///
3870/// Note that identifier-lists are only allowed for normal declarators, not for
3871/// abstract-declarators.
3872bool Parser::isFunctionDeclaratorIdentifierList() {
3873 return !getLang().CPlusPlus
3874 && Tok.is(tok::identifier)
3875 && !TryAltiVecVectorToken()
3876 // K&R identifier lists can't have typedefs as identifiers, per C99
3877 // 6.7.5.3p11.
3878 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
3879 // Identifier lists follow a really simple grammar: the identifiers can
3880 // be followed *only* by a ", identifier" or ")". However, K&R
3881 // identifier lists are really rare in the brave new modern world, and
3882 // it is very common for someone to typo a type in a non-K&R style
3883 // list. If we are presented with something like: "void foo(intptr x,
3884 // float y)", we don't want to start parsing the function declarator as
3885 // though it is a K&R style declarator just because intptr is an
3886 // invalid type.
3887 //
3888 // To handle this, we check to see if the token after the first
3889 // identifier is a "," or ")". Only then do we parse it as an
3890 // identifier list.
3891 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
3892}
3893
3894/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3895/// we found a K&R-style identifier list instead of a typed parameter list.
3896///
3897/// After returning, ParamInfo will hold the parsed parameters.
3898///
3899/// identifier-list: [C99 6.7.5]
3900/// identifier
3901/// identifier-list ',' identifier
3902///
3903void Parser::ParseFunctionDeclaratorIdentifierList(
3904 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003905 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00003906 // If there was no identifier specified for the declarator, either we are in
3907 // an abstract-declarator, or we are in a parameter declarator which was found
3908 // to be abstract. In abstract-declarators, identifier lists are not valid:
3909 // diagnose this.
3910 if (!D.getIdentifier())
3911 Diag(Tok, diag::ext_ident_list_in_param);
3912
3913 // Maintain an efficient lookup of params we have seen so far.
3914 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
3915
3916 while (1) {
3917 // If this isn't an identifier, report the error and skip until ')'.
3918 if (Tok.isNot(tok::identifier)) {
3919 Diag(Tok, diag::err_expected_ident);
3920 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
3921 // Forget we parsed anything.
3922 ParamInfo.clear();
3923 return;
3924 }
3925
3926 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
3927
3928 // Reject 'typedef int y; int test(x, y)', but continue parsing.
3929 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
3930 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
3931
3932 // Verify that the argument identifier has not already been mentioned.
3933 if (!ParamsSoFar.insert(ParmII)) {
3934 Diag(Tok, diag::err_param_redefinition) << ParmII;
3935 } else {
3936 // Remember this identifier in ParamInfo.
3937 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3938 Tok.getLocation(),
3939 0));
3940 }
3941
3942 // Eat the identifier.
3943 ConsumeToken();
3944
3945 // The list continues if we see a comma.
3946 if (Tok.isNot(tok::comma))
3947 break;
3948 ConsumeToken();
3949 }
3950}
3951
3952/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
3953/// after the opening parenthesis. This function will not parse a K&R-style
3954/// identifier list.
3955///
3956/// D is the declarator being parsed. If attrs is non-null, then the caller
3957/// parsed those arguments immediately after the open paren - they should be
3958/// considered to be the first argument of a parameter.
3959///
3960/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
3961/// be the location of the ellipsis, if any was parsed.
3962///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003963/// parameter-type-list: [C99 6.7.5]
3964/// parameter-list
3965/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003966/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003967///
3968/// parameter-list: [C99 6.7.5]
3969/// parameter-declaration
3970/// parameter-list ',' parameter-declaration
3971///
3972/// parameter-declaration: [C99 6.7.5]
3973/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003974/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003975/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003976/// declaration-specifiers abstract-declarator[opt]
3977/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003978/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003979/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003980///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003981void Parser::ParseParameterDeclarationClause(
3982 Declarator &D,
3983 ParsedAttributes &attrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003984 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00003985 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00003986
Chris Lattner371ed4e2008-04-06 06:57:35 +00003987 while (1) {
3988 if (Tok.is(tok::ellipsis)) {
Douglas Gregor94349fd2009-02-18 07:07:28 +00003989 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003990 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003991 }
Mike Stump11289f42009-09-09 15:08:12 +00003992
Chris Lattner371ed4e2008-04-06 06:57:35 +00003993 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003994 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00003995 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003996
3997 // Skip any Microsoft attributes before a param.
3998 if (getLang().Microsoft && Tok.is(tok::l_square))
3999 ParseMicrosoftAttributes(DS.getAttributes());
4000
4001 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004002
4003 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004004 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004005 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4006 // attributes lost? Should they even be allowed?
4007 // FIXME: If we can leave the attributes in the token stream somehow, we can
4008 // get rid of a parameter (attrs) and this statement. It might be too much
4009 // hassle.
John McCall53fa7142010-12-24 02:08:15 +00004010 DS.takeAttributesFrom(attrs);
4011
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004012 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004013
Chris Lattner371ed4e2008-04-06 06:57:35 +00004014 // Parse the declarator. This is "PrototypeContext", because we must
4015 // accept either 'declarator' or 'abstract-declarator' here.
4016 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4017 ParseDeclarator(ParmDecl);
4018
4019 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004020 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004021
Chris Lattner371ed4e2008-04-06 06:57:35 +00004022 // Remember this parsed parameter in ParamInfo.
4023 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004024
Douglas Gregor4d87df52008-12-16 21:30:33 +00004025 // DefArgToks is used when the parsing of default arguments needs
4026 // to be delayed.
4027 CachedTokens *DefArgToks = 0;
4028
Chris Lattner371ed4e2008-04-06 06:57:35 +00004029 // If no parameter was specified, verify that *something* was specified,
4030 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004031 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4032 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004033 // Completely missing, emit error.
4034 Diag(DSStart, diag::err_missing_param);
4035 } else {
4036 // Otherwise, we have something. Add it and let semantic analysis try
4037 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004038
Chris Lattner371ed4e2008-04-06 06:57:35 +00004039 // Inform the actions module about the parameter declarator, so it gets
4040 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004041 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004042
4043 // Parse the default argument, if any. We parse the default
4044 // arguments in all dialects; the semantic analysis in
4045 // ActOnParamDefaultArgument will reject the default argument in
4046 // C.
4047 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004048 SourceLocation EqualLoc = Tok.getLocation();
4049
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004050 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004051 if (D.getContext() == Declarator::MemberContext) {
4052 // If we're inside a class definition, cache the tokens
4053 // corresponding to the default argument. We'll actually parse
4054 // them when we see the end of the class definition.
4055 // FIXME: Templates will require something similar.
4056 // FIXME: Can we use a smart pointer for Toks?
4057 DefArgToks = new CachedTokens;
4058
Mike Stump11289f42009-09-09 15:08:12 +00004059 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004060 /*StopAtSemi=*/true,
4061 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004062 delete DefArgToks;
4063 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004064 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004065 } else {
4066 // Mark the end of the default argument so that we know when to
4067 // stop when we parse it later on.
4068 Token DefArgEnd;
4069 DefArgEnd.startToken();
4070 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4071 DefArgEnd.setLocation(Tok.getLocation());
4072 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004073 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004074 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004075 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004076 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004077 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004078 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004079
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004080 // The argument isn't actually potentially evaluated unless it is
4081 // used.
4082 EnterExpressionEvaluationContext Eval(Actions,
4083 Sema::PotentiallyEvaluatedIfUsed);
4084
John McCalldadc5752010-08-24 06:29:42 +00004085 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004086 if (DefArgResult.isInvalid()) {
4087 Actions.ActOnParamDefaultArgumentError(Param);
4088 SkipUntil(tok::comma, tok::r_paren, true, true);
4089 } else {
4090 // Inform the actions module about the default argument
4091 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004092 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004093 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004094 }
4095 }
Mike Stump11289f42009-09-09 15:08:12 +00004096
4097 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4098 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004099 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004100 }
4101
4102 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004103 if (Tok.isNot(tok::comma)) {
4104 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004105 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4106
4107 if (!getLang().CPlusPlus) {
4108 // We have ellipsis without a preceding ',', which is ill-formed
4109 // in C. Complain and provide the fix.
4110 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004111 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004112 }
4113 }
4114
4115 break;
4116 }
Mike Stump11289f42009-09-09 15:08:12 +00004117
Chris Lattner371ed4e2008-04-06 06:57:35 +00004118 // Consume the comma.
4119 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004120 }
Mike Stump11289f42009-09-09 15:08:12 +00004121
Chris Lattner6c940e62008-04-06 06:34:08 +00004122}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004123
Chris Lattnere8074e62006-08-06 18:30:15 +00004124/// [C90] direct-declarator '[' constant-expression[opt] ']'
4125/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4126/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4127/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4128/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4129void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00004130 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00004131
Chris Lattner84a11622008-12-18 07:27:21 +00004132 // C array syntax has many features, but by-far the most common is [] and [4].
4133 // This code does a fast path to handle some of the most obvious cases.
4134 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004135 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004136 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004137 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004138
Chris Lattner84a11622008-12-18 07:27:21 +00004139 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004140 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004141 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00004142 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004143 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004144 return;
4145 } else if (Tok.getKind() == tok::numeric_constant &&
4146 GetLookAheadToken(1).is(tok::r_square)) {
4147 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00004148 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00004149 ConsumeToken();
4150
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004151 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004152 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004153 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004154
Chris Lattner84a11622008-12-18 07:27:21 +00004155 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004156 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004157 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00004158 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004159 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004160 return;
4161 }
Mike Stump11289f42009-09-09 15:08:12 +00004162
Chris Lattnere8074e62006-08-06 18:30:15 +00004163 // If valid, this location is the position where we read the 'static' keyword.
4164 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004165 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004166 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004167
Chris Lattnere8074e62006-08-06 18:30:15 +00004168 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004169 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004170 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004171 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004172
Chris Lattnere8074e62006-08-06 18:30:15 +00004173 // If we haven't already read 'static', check to see if there is one after the
4174 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004175 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004176 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004177
Chris Lattnere8074e62006-08-06 18:30:15 +00004178 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004179 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004180 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004181
Chris Lattner521ff2b2008-04-06 05:26:30 +00004182 // Handle the case where we have '[*]' as the array size. However, a leading
4183 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4184 // the the token after the star is a ']'. Since stars in arrays are
4185 // infrequent, use of lookahead is not costly here.
4186 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004187 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004188
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004189 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004190 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004191 StaticLoc = SourceLocation(); // Drop the static.
4192 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004193 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004194 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004195 // Note, in C89, this production uses the constant-expr production instead
4196 // of assignment-expr. The only difference is that assignment-expr allows
4197 // things like '=' and '*='. Sema rejects these in C89 mode because they
4198 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004199
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004200 // Parse the constant-expression or assignment-expression now (depending
4201 // on dialect).
4202 if (getLang().CPlusPlus)
4203 NumElements = ParseConstantExpression();
4204 else
4205 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004206 }
Mike Stump11289f42009-09-09 15:08:12 +00004207
Chris Lattner62591722006-08-12 18:40:58 +00004208 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004209 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004210 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004211 // If the expression was invalid, skip it.
4212 SkipUntil(tok::r_square);
4213 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004214 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004215
4216 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4217
John McCall084e83d2011-03-24 11:26:52 +00004218 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004219 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004220
Chris Lattner84a11622008-12-18 07:27:21 +00004221 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004222 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004223 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004224 NumElements.release(),
4225 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004226 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004227}
4228
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004229/// [GNU] typeof-specifier:
4230/// typeof ( expressions )
4231/// typeof ( type-name )
4232/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004233///
4234void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004235 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004236 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004237 SourceLocation StartLoc = ConsumeToken();
4238
John McCalle8595032010-01-13 20:03:27 +00004239 const bool hasParens = Tok.is(tok::l_paren);
4240
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004241 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004242 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004243 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004244 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4245 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004246 if (hasParens)
4247 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004248
4249 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004250 // FIXME: Not accurate, the range gets one token more than it should.
4251 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004252 else
4253 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004254
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004255 if (isCastExpr) {
4256 if (!CastTy) {
4257 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004258 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004259 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004260
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004261 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004262 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004263 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4264 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004265 DiagID, CastTy))
4266 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004267 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004268 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004269
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004270 // If we get here, the operand to the typeof was an expresion.
4271 if (Operand.isInvalid()) {
4272 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004273 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004274 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004275
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004276 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004277 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004278 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4279 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004280 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004281 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004282}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004283
4284
4285/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4286/// from TryAltiVecVectorToken.
4287bool Parser::TryAltiVecVectorTokenOutOfLine() {
4288 Token Next = NextToken();
4289 switch (Next.getKind()) {
4290 default: return false;
4291 case tok::kw_short:
4292 case tok::kw_long:
4293 case tok::kw_signed:
4294 case tok::kw_unsigned:
4295 case tok::kw_void:
4296 case tok::kw_char:
4297 case tok::kw_int:
4298 case tok::kw_float:
4299 case tok::kw_double:
4300 case tok::kw_bool:
4301 case tok::kw___pixel:
4302 Tok.setKind(tok::kw___vector);
4303 return true;
4304 case tok::identifier:
4305 if (Next.getIdentifierInfo() == Ident_pixel) {
4306 Tok.setKind(tok::kw___vector);
4307 return true;
4308 }
4309 return false;
4310 }
4311}
4312
4313bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4314 const char *&PrevSpec, unsigned &DiagID,
4315 bool &isInvalid) {
4316 if (Tok.getIdentifierInfo() == Ident_vector) {
4317 Token Next = NextToken();
4318 switch (Next.getKind()) {
4319 case tok::kw_short:
4320 case tok::kw_long:
4321 case tok::kw_signed:
4322 case tok::kw_unsigned:
4323 case tok::kw_void:
4324 case tok::kw_char:
4325 case tok::kw_int:
4326 case tok::kw_float:
4327 case tok::kw_double:
4328 case tok::kw_bool:
4329 case tok::kw___pixel:
4330 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4331 return true;
4332 case tok::identifier:
4333 if (Next.getIdentifierInfo() == Ident_pixel) {
4334 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4335 return true;
4336 }
4337 break;
4338 default:
4339 break;
4340 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004341 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004342 DS.isTypeAltiVecVector()) {
4343 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4344 return true;
4345 }
4346 return false;
4347}