blob: 5fd95f17fc9fad29f4ca7e58bdddd0631b24f0c1 [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) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +0000306 Tok.is(tok::kw___ptr32) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000307 Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000308 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
309 SourceLocation AttrNameLoc = ConsumeToken();
Francois Pichetf2fb4112011-08-25 00:36:46 +0000310 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
311 Tok.is(tok::kw___ptr32))
Eli Friedman53339e02009-06-08 23:27:34 +0000312 // FIXME: Support these properly!
313 continue;
John McCall084e83d2011-03-24 11:26:52 +0000314 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
315 SourceLocation(), 0, 0, true);
Eli Friedman53339e02009-06-08 23:27:34 +0000316 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000317}
318
John McCall53fa7142010-12-24 02:08:15 +0000319void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000320 // Treat these like attributes
321 while (Tok.is(tok::kw___pascal)) {
322 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
323 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000324 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
325 SourceLocation(), 0, 0, true);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000326 }
John McCall53fa7142010-12-24 02:08:15 +0000327}
328
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000329void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
330 // Treat these like attributes
331 while (Tok.is(tok::kw___kernel)) {
332 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000333 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
334 AttrNameLoc, 0, AttrNameLoc, 0,
335 SourceLocation(), 0, 0, false);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000336 }
337}
338
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000339void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
340 SourceLocation Loc = Tok.getLocation();
341 switch(Tok.getKind()) {
342 // OpenCL qualifiers:
343 case tok::kw___private:
344 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000345 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000346 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000347 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000348 break;
349
350 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000351 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000352 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000353 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000354 break;
355
356 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000357 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000358 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000359 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000360 break;
361
362 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000363 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000364 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000365 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000366 break;
367
368 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000369 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000370 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000371 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000372 break;
373
374 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000375 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000376 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000377 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000378 break;
379
380 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000381 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000382 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000383 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000384 break;
385 default: break;
386 }
387}
388
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000389/// \brief Parse a version number.
390///
391/// version:
392/// simple-integer
393/// simple-integer ',' simple-integer
394/// simple-integer ',' simple-integer ',' simple-integer
395VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
396 Range = Tok.getLocation();
397
398 if (!Tok.is(tok::numeric_constant)) {
399 Diag(Tok, diag::err_expected_version);
400 SkipUntil(tok::comma, tok::r_paren, true, true, true);
401 return VersionTuple();
402 }
403
404 // Parse the major (and possibly minor and subminor) versions, which
405 // are stored in the numeric constant. We utilize a quirk of the
406 // lexer, which is that it handles something like 1.2.3 as a single
407 // numeric constant, rather than two separate tokens.
408 llvm::SmallString<512> Buffer;
409 Buffer.resize(Tok.getLength()+1);
410 const char *ThisTokBegin = &Buffer[0];
411
412 // Get the spelling of the token, which eliminates trigraphs, etc.
413 bool Invalid = false;
414 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
415 if (Invalid)
416 return VersionTuple();
417
418 // Parse the major version.
419 unsigned AfterMajor = 0;
420 unsigned Major = 0;
421 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
422 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
423 ++AfterMajor;
424 }
425
426 if (AfterMajor == 0) {
427 Diag(Tok, diag::err_expected_version);
428 SkipUntil(tok::comma, tok::r_paren, true, true, true);
429 return VersionTuple();
430 }
431
432 if (AfterMajor == ActualLength) {
433 ConsumeToken();
434
435 // We only had a single version component.
436 if (Major == 0) {
437 Diag(Tok, diag::err_zero_version);
438 return VersionTuple();
439 }
440
441 return VersionTuple(Major);
442 }
443
444 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
445 Diag(Tok, diag::err_expected_version);
446 SkipUntil(tok::comma, tok::r_paren, true, true, true);
447 return VersionTuple();
448 }
449
450 // Parse the minor version.
451 unsigned AfterMinor = AfterMajor + 1;
452 unsigned Minor = 0;
453 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
454 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
455 ++AfterMinor;
456 }
457
458 if (AfterMinor == ActualLength) {
459 ConsumeToken();
460
461 // We had major.minor.
462 if (Major == 0 && Minor == 0) {
463 Diag(Tok, diag::err_zero_version);
464 return VersionTuple();
465 }
466
467 return VersionTuple(Major, Minor);
468 }
469
470 // If what follows is not a '.', we have a problem.
471 if (ThisTokBegin[AfterMinor] != '.') {
472 Diag(Tok, diag::err_expected_version);
473 SkipUntil(tok::comma, tok::r_paren, true, true, true);
474 return VersionTuple();
475 }
476
477 // Parse the subminor version.
478 unsigned AfterSubminor = AfterMinor + 1;
479 unsigned Subminor = 0;
480 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
481 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
482 ++AfterSubminor;
483 }
484
485 if (AfterSubminor != ActualLength) {
486 Diag(Tok, diag::err_expected_version);
487 SkipUntil(tok::comma, tok::r_paren, true, true, true);
488 return VersionTuple();
489 }
490 ConsumeToken();
491 return VersionTuple(Major, Minor, Subminor);
492}
493
494/// \brief Parse the contents of the "availability" attribute.
495///
496/// availability-attribute:
497/// 'availability' '(' platform ',' version-arg-list ')'
498///
499/// platform:
500/// identifier
501///
502/// version-arg-list:
503/// version-arg
504/// version-arg ',' version-arg-list
505///
506/// version-arg:
507/// 'introduced' '=' version
508/// 'deprecated' '=' version
509/// 'removed' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000510/// 'unavailable'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000511void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
512 SourceLocation AvailabilityLoc,
513 ParsedAttributes &attrs,
514 SourceLocation *endLoc) {
515 SourceLocation PlatformLoc;
516 IdentifierInfo *Platform = 0;
517
518 enum { Introduced, Deprecated, Obsoleted, Unknown };
519 AvailabilityChange Changes[Unknown];
520
521 // Opening '('.
522 SourceLocation LParenLoc;
523 if (!Tok.is(tok::l_paren)) {
524 Diag(Tok, diag::err_expected_lparen);
525 return;
526 }
527 LParenLoc = ConsumeParen();
528
529 // Parse the platform name,
530 if (Tok.isNot(tok::identifier)) {
531 Diag(Tok, diag::err_availability_expected_platform);
532 SkipUntil(tok::r_paren);
533 return;
534 }
535 Platform = Tok.getIdentifierInfo();
536 PlatformLoc = ConsumeToken();
537
538 // Parse the ',' following the platform name.
539 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
540 return;
541
542 // If we haven't grabbed the pointers for the identifiers
543 // "introduced", "deprecated", and "obsoleted", do so now.
544 if (!Ident_introduced) {
545 Ident_introduced = PP.getIdentifierInfo("introduced");
546 Ident_deprecated = PP.getIdentifierInfo("deprecated");
547 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000548 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000549 }
550
551 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000552 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000553 do {
554 if (Tok.isNot(tok::identifier)) {
555 Diag(Tok, diag::err_availability_expected_change);
556 SkipUntil(tok::r_paren);
557 return;
558 }
559 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
560 SourceLocation KeywordLoc = ConsumeToken();
561
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000562 if (Keyword == Ident_unavailable) {
563 if (UnavailableLoc.isValid()) {
564 Diag(KeywordLoc, diag::err_availability_redundant)
565 << Keyword << SourceRange(UnavailableLoc);
566 }
567 UnavailableLoc = KeywordLoc;
568
569 if (Tok.isNot(tok::comma))
570 break;
571
572 ConsumeToken();
573 continue;
574 }
575
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000576 if (Tok.isNot(tok::equal)) {
577 Diag(Tok, diag::err_expected_equal_after)
578 << Keyword;
579 SkipUntil(tok::r_paren);
580 return;
581 }
582 ConsumeToken();
583
584 SourceRange VersionRange;
585 VersionTuple Version = ParseVersionTuple(VersionRange);
586
587 if (Version.empty()) {
588 SkipUntil(tok::r_paren);
589 return;
590 }
591
592 unsigned Index;
593 if (Keyword == Ident_introduced)
594 Index = Introduced;
595 else if (Keyword == Ident_deprecated)
596 Index = Deprecated;
597 else if (Keyword == Ident_obsoleted)
598 Index = Obsoleted;
599 else
600 Index = Unknown;
601
602 if (Index < Unknown) {
603 if (!Changes[Index].KeywordLoc.isInvalid()) {
604 Diag(KeywordLoc, diag::err_availability_redundant)
605 << Keyword
606 << SourceRange(Changes[Index].KeywordLoc,
607 Changes[Index].VersionRange.getEnd());
608 }
609
610 Changes[Index].KeywordLoc = KeywordLoc;
611 Changes[Index].Version = Version;
612 Changes[Index].VersionRange = VersionRange;
613 } else {
614 Diag(KeywordLoc, diag::err_availability_unknown_change)
615 << Keyword << VersionRange;
616 }
617
618 if (Tok.isNot(tok::comma))
619 break;
620
621 ConsumeToken();
622 } while (true);
623
624 // Closing ')'.
625 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
626 if (RParenLoc.isInvalid())
627 return;
628
629 if (endLoc)
630 *endLoc = RParenLoc;
631
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000632 // The 'unavailable' availability cannot be combined with any other
633 // availability changes. Make sure that hasn't happened.
634 if (UnavailableLoc.isValid()) {
635 bool Complained = false;
636 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
637 if (Changes[Index].KeywordLoc.isValid()) {
638 if (!Complained) {
639 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
640 << SourceRange(Changes[Index].KeywordLoc,
641 Changes[Index].VersionRange.getEnd());
642 Complained = true;
643 }
644
645 // Clear out the availability.
646 Changes[Index] = AvailabilityChange();
647 }
648 }
649 }
650
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000651 // Record this attribute
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000652 attrs.addNew(&Availability, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000653 0, SourceLocation(),
654 Platform, PlatformLoc,
655 Changes[Introduced],
656 Changes[Deprecated],
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000657 Changes[Obsoleted],
658 UnavailableLoc, false, false);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000659}
660
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000661/// \brief Wrapper around a case statement checking if AttrName is
662/// one of the thread safety attributes
663bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
664 return llvm::StringSwitch<bool>(AttrName)
665 .Case("guarded_by", true)
666 .Case("guarded_var", true)
667 .Case("pt_guarded_by", true)
668 .Case("pt_guarded_var", true)
669 .Case("lockable", true)
670 .Case("scoped_lockable", true)
671 .Case("no_thread_safety_analysis", true)
672 .Case("acquired_after", true)
673 .Case("acquired_before", true)
674 .Case("exclusive_lock_function", true)
675 .Case("shared_lock_function", true)
676 .Case("exclusive_trylock_function", true)
677 .Case("shared_trylock_function", true)
678 .Case("unlock_function", true)
679 .Case("lock_returned", true)
680 .Case("locks_excluded", true)
681 .Case("exclusive_locks_required", true)
682 .Case("shared_locks_required", true)
683 .Default(false);
684}
685
686/// \brief Parse the contents of thread safety attributes. These
687/// should always be parsed as an expression list.
688///
689/// We need to special case the parsing due to the fact that if the first token
690/// of the first argument is an identifier, the main parse loop will store
691/// that token as a "parameter" and the rest of
692/// the arguments will be added to a list of "arguments". However,
693/// subsequent tokens in the first argument are lost. We instead parse each
694/// argument as an expression and add all arguments to the list of "arguments".
695/// In future, we will take advantage of this special case to also
696/// deal with some argument scoping issues here (for example, referring to a
697/// function parameter in the attribute on that function).
698void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
699 SourceLocation AttrNameLoc,
700 ParsedAttributes &Attrs,
701 SourceLocation *EndLoc) {
702
703 if (Tok.is(tok::l_paren)) {
704 SourceLocation LeftParenLoc = Tok.getLocation();
705 ConsumeParen(); // ignore the left paren loc for now
706
707 ExprVector ArgExprs(Actions);
708 bool ArgExprsOk = true;
709
710 // now parse the list of expressions
711 while (1) {
712 ExprResult ArgExpr(ParseAssignmentExpression());
713 if (ArgExpr.isInvalid()) {
714 ArgExprsOk = false;
715 MatchRHSPunctuation(tok::r_paren, LeftParenLoc);
716 break;
717 } else {
718 ArgExprs.push_back(ArgExpr.release());
719 }
720 if (Tok.isNot(tok::comma))
721 break;
722 ConsumeToken(); // Eat the comma, move to the next argument
723 }
724 // Match the ')'.
725 if (ArgExprsOk && Tok.is(tok::r_paren)) {
726 ConsumeParen(); // ignore the right paren loc for now
727 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
728 ArgExprs.take(), ArgExprs.size());
729 }
730 } else {
731 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc,
732 0, SourceLocation(), 0, 0);
733 }
734}
735
John McCall53fa7142010-12-24 02:08:15 +0000736void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
737 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
738 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000739}
740
Chris Lattner53361ac2006-08-10 05:19:57 +0000741/// ParseDeclaration - Parse a full 'declaration', which consists of
742/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000743/// 'Context' should be a Declarator::TheContext value. This returns the
744/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000745///
746/// declaration: [C99 6.7]
747/// block-declaration ->
748/// simple-declaration
749/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000750/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000751/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000752/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000753/// [C++] using-declaration
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000754/// [C++0x/C1X] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000755/// others... [FIXME]
756///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000757Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
758 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000759 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000760 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000761 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +0000762 // Must temporarily exit the objective-c container scope for
763 // parsing c none objective-c decls.
764 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000765
John McCall48871652010-08-21 09:40:31 +0000766 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +0000767 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000768 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000769 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000770 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000771 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000772 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000773 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000774 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000775 // Could be the start of an inline namespace. Allowed as an ext in C++03.
776 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +0000777 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000778 SourceLocation InlineLoc = ConsumeToken();
779 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
780 break;
781 }
John McCall53fa7142010-12-24 02:08:15 +0000782 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000783 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000784 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000785 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000786 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000787 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000788 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000789 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +0000790 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000791 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000792 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +0000793 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000794 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000795 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000796 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000797 default:
John McCall53fa7142010-12-24 02:08:15 +0000798 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000799 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000800
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000801 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +0000802 // single decl, convert it now. Alias declarations can also declare a type;
803 // include that too if it is present.
804 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +0000805}
806
807/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
808/// declaration-specifiers init-declarator-list[opt] ';'
809///[C90/C++]init-declarator-list ';' [TODO]
810/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000811///
Richard Smith02e85f32011-04-14 22:09:26 +0000812/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
813/// attribute-specifier-seq[opt] type-specifier-seq declarator
814///
Chris Lattner32dc41c2009-03-29 17:27:48 +0000815/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000816/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +0000817///
818/// If FRI is non-null, we might be parsing a for-range-declaration instead
819/// of a simple-declaration. If we find that we are, we also parse the
820/// for-range-initializer, and place it here.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000821Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
822 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000823 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000824 ParsedAttributes &attrs,
Richard Smith02e85f32011-04-14 22:09:26 +0000825 bool RequireSemi,
826 ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000827 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000828 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000829 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000830
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000831 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000832 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000833 StmtResult R = Actions.ActOnVlaStmt(DS);
834 if (R.isUsable())
835 Stmts.push_back(R.release());
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000836
Chris Lattner0e894622006-08-13 19:58:17 +0000837 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
838 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000839 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000840 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000841 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000842 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000843 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000844 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000845 }
Douglas Gregor0e7dde52011-04-24 05:37:28 +0000846
847 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +0000848}
Mike Stump11289f42009-09-09 15:08:12 +0000849
John McCalld5a36322009-11-03 19:26:08 +0000850/// ParseDeclGroup - Having concluded that this is either a function
851/// definition or a group of object declarations, actually parse the
852/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000853Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
854 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000855 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +0000856 SourceLocation *DeclEnd,
857 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +0000858 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000859 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000860 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000861
John McCalld5a36322009-11-03 19:26:08 +0000862 // Bail out if the first declarator didn't seem well-formed.
863 if (!D.hasName() && !D.mayOmitIdentifier()) {
864 // Skip until ; or }.
865 SkipUntil(tok::r_brace, true, true);
866 if (Tok.is(tok::semi))
867 ConsumeToken();
868 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000869 }
Mike Stump11289f42009-09-09 15:08:12 +0000870
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000871 // Check to see if we have a function *definition* which must have a body.
872 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
873 // Look at the next token to make sure that this isn't a function
874 // declaration. We have to check this because __attribute__ might be the
875 // start of a function definition in GCC-extended K&R C.
876 !isDeclarationAfterDeclarator()) {
877
Chris Lattner13901342010-07-11 22:42:07 +0000878 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000879 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
880 Diag(Tok, diag::err_function_declared_typedef);
881
882 // Recover by treating the 'typedef' as spurious.
883 DS.ClearStorageClassSpecs();
884 }
885
John McCall48871652010-08-21 09:40:31 +0000886 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000887 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000888 }
889
890 if (isDeclarationSpecifier()) {
891 // If there is an invalid declaration specifier right after the function
892 // prototype, then we must be in a missing semicolon case where this isn't
893 // actually a body. Just fall through into the code that handles it as a
894 // prototype, and let the top-level code handle the erroneous declspec
895 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000896 } else {
897 Diag(Tok, diag::err_expected_fn_body);
898 SkipUntil(tok::semi);
899 return DeclGroupPtrTy();
900 }
901 }
902
Richard Smith02e85f32011-04-14 22:09:26 +0000903 if (ParseAttributesAfterDeclarator(D))
904 return DeclGroupPtrTy();
905
906 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
907 // must parse and analyze the for-range-initializer before the declaration is
908 // analyzed.
909 if (FRI && Tok.is(tok::colon)) {
910 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +0000911 if (Tok.is(tok::l_brace))
912 FRI->RangeExpr = ParseBraceInitializer();
913 else
914 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +0000915 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
916 Actions.ActOnCXXForRangeDecl(ThisDecl);
917 Actions.FinalizeDeclaration(ThisDecl);
918 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
919 }
920
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000921 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +0000922 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
John McCall28a6aea2009-11-04 02:18:39 +0000923 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000924 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000925 DeclsInGroup.push_back(FirstDecl);
926
927 // If we don't have a comma, it is either the end of the list (a ';') or an
928 // error, bail out.
929 while (Tok.is(tok::comma)) {
930 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000931 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000932
933 // Parse the next declarator.
934 D.clear();
935
936 // Accept attributes in an init-declarator. In the first declarator in a
937 // declaration, these would be part of the declspec. In subsequent
938 // declarators, they become part of the declarator itself, so that they
939 // don't apply to declarators after *this* one. Examples:
940 // short __attribute__((common)) var; -> declspec
941 // short var __attribute__((common)); -> declarator
942 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +0000943 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +0000944
945 ParseDeclarator(D);
946
John McCall48871652010-08-21 09:40:31 +0000947 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000948 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000949 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000950 DeclsInGroup.push_back(ThisDecl);
951 }
952
953 if (DeclEnd)
954 *DeclEnd = Tok.getLocation();
955
956 if (Context != Declarator::ForContext &&
957 ExpectAndConsume(tok::semi,
958 Context == Declarator::FileContext
959 ? diag::err_invalid_token_after_toplevel_declarator
960 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000961 // Okay, there was no semicolon and one was expected. If we see a
962 // declaration specifier, just assume it was missing and continue parsing.
963 // Otherwise things are very confused and we skip to recover.
964 if (!isDeclarationSpecifier()) {
965 SkipUntil(tok::r_brace, true, true);
966 if (Tok.is(tok::semi))
967 ConsumeToken();
968 }
John McCalld5a36322009-11-03 19:26:08 +0000969 }
970
Douglas Gregor0be31a22010-07-02 17:43:08 +0000971 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000972 DeclsInGroup.data(),
973 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000974}
975
Richard Smith02e85f32011-04-14 22:09:26 +0000976/// Parse an optional simple-asm-expr and attributes, and attach them to a
977/// declarator. Returns true on an error.
978bool Parser::ParseAttributesAfterDeclarator(Declarator &D) {
979 // If a simple-asm-expr is present, parse it.
980 if (Tok.is(tok::kw_asm)) {
981 SourceLocation Loc;
982 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
983 if (AsmLabel.isInvalid()) {
984 SkipUntil(tok::semi, true, true);
985 return true;
986 }
987
988 D.setAsmLabel(AsmLabel.release());
989 D.SetRangeEnd(Loc);
990 }
991
992 MaybeParseGNUAttributes(D);
993 return false;
994}
995
Douglas Gregor23996282009-05-12 21:31:51 +0000996/// \brief Parse 'declaration' after parsing 'declaration-specifiers
997/// declarator'. This method parses the remainder of the declaration
998/// (including any attributes or initializer, among other things) and
999/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001000///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001001/// init-declarator: [C99 6.7]
1002/// declarator
1003/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001004/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1005/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001006/// [C++] declarator initializer[opt]
1007///
1008/// [C++] initializer:
1009/// [C++] '=' initializer-clause
1010/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001011/// [C++0x] '=' 'default' [TODO]
1012/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001013/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001014///
1015/// According to the standard grammar, =default and =delete are function
1016/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001017///
John McCall48871652010-08-21 09:40:31 +00001018Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001019 const ParsedTemplateInfo &TemplateInfo) {
Richard Smith02e85f32011-04-14 22:09:26 +00001020 if (ParseAttributesAfterDeclarator(D))
1021 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001022
Richard Smith02e85f32011-04-14 22:09:26 +00001023 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1024}
Mike Stump11289f42009-09-09 15:08:12 +00001025
Richard Smith02e85f32011-04-14 22:09:26 +00001026Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1027 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001028 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001029 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001030 switch (TemplateInfo.Kind) {
1031 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001032 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001033 break;
1034
1035 case ParsedTemplateInfo::Template:
1036 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001037 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001038 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001039 TemplateInfo.TemplateParams->data(),
1040 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +00001041 D);
1042 break;
1043
1044 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +00001045 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001046 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001047 TemplateInfo.ExternLoc,
1048 TemplateInfo.TemplateLoc,
1049 D);
1050 if (ThisRes.isInvalid()) {
1051 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001052 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001053 }
1054
1055 ThisDecl = ThisRes.get();
1056 break;
1057 }
1058 }
Mike Stump11289f42009-09-09 15:08:12 +00001059
Richard Smith30482bc2011-02-20 03:19:35 +00001060 bool TypeContainsAuto =
1061 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1062
Douglas Gregor23996282009-05-12 21:31:51 +00001063 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +00001064 if (isTokenEqualOrMistypedEqualEqual(
1065 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +00001066 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001067 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001068 if (D.isFunctionDeclarator())
1069 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1070 << 1 /* delete */;
1071 else
1072 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001073 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001074 if (D.isFunctionDeclarator())
1075 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
1076 << 1 /* delete */;
1077 else
1078 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001079 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +00001080 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1081 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001082 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001083 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001084
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001085 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001086 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001087 ConsumeCodeCompletionToken();
1088 SkipUntil(tok::comma, true, true);
1089 return ThisDecl;
1090 }
1091
John McCalldadc5752010-08-24 06:29:42 +00001092 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001093
John McCall1f4ee7b2009-12-19 09:28:58 +00001094 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001095 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001096 ExitScope();
1097 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001098
Douglas Gregor23996282009-05-12 21:31:51 +00001099 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001100 SkipUntil(tok::comma, true, true);
1101 Actions.ActOnInitializerError(ThisDecl);
1102 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001103 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1104 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001105 }
1106 } else if (Tok.is(tok::l_paren)) {
1107 // Parse C++ direct initializer: '(' expression-list ')'
1108 SourceLocation LParenLoc = ConsumeParen();
1109 ExprVector Exprs(Actions);
1110 CommaLocsTy CommaLocs;
1111
Douglas Gregor613bf102009-12-22 17:47:17 +00001112 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
1113 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001114 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001115 }
1116
Douglas Gregor23996282009-05-12 21:31:51 +00001117 if (ParseExpressionList(Exprs, CommaLocs)) {
1118 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001119
1120 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001121 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001122 ExitScope();
1123 }
Douglas Gregor23996282009-05-12 21:31:51 +00001124 } else {
1125 // Match the ')'.
1126 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1127
1128 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1129 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001130
1131 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001132 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001133 ExitScope();
1134 }
1135
Douglas Gregor23996282009-05-12 21:31:51 +00001136 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
1137 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +00001138 RParenLoc,
1139 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001140 }
Sebastian Redl3da34892011-06-05 12:23:16 +00001141 } else if (getLang().CPlusPlus0x && Tok.is(tok::l_brace)) {
1142 // Parse C++0x braced-init-list.
1143 if (D.getCXXScopeSpec().isSet()) {
1144 EnterScope(0);
1145 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1146 }
1147
1148 ExprResult Init(ParseBraceInitializer());
1149
1150 if (D.getCXXScopeSpec().isSet()) {
1151 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1152 ExitScope();
1153 }
1154
1155 if (Init.isInvalid()) {
1156 Actions.ActOnInitializerError(ThisDecl);
1157 } else
1158 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1159 /*DirectInit=*/true, TypeContainsAuto);
1160
Douglas Gregor23996282009-05-12 21:31:51 +00001161 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001162 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001163 }
1164
Richard Smithb2bc2e62011-02-21 20:05:19 +00001165 Actions.FinalizeDeclaration(ThisDecl);
1166
Douglas Gregor23996282009-05-12 21:31:51 +00001167 return ThisDecl;
1168}
1169
Chris Lattner1890ac82006-08-13 01:16:23 +00001170/// ParseSpecifierQualifierList
1171/// specifier-qualifier-list:
1172/// type-specifier specifier-qualifier-list[opt]
1173/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001174/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001175///
Richard Smithcd1c0552011-07-01 19:46:12 +00001176void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001177 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1178 /// parse declaration-specifiers and complain about extra stuff.
Richard Smithcd1c0552011-07-01 19:46:12 +00001179 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS);
Mike Stump11289f42009-09-09 15:08:12 +00001180
Chris Lattner1890ac82006-08-13 01:16:23 +00001181 // Validate declspec for type-name.
1182 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +00001183 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +00001184 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +00001185 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +00001186
Chris Lattner1b22eed2006-11-28 05:12:07 +00001187 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001188 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001189 if (DS.getStorageClassSpecLoc().isValid())
1190 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1191 else
1192 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001193 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001194 }
Mike Stump11289f42009-09-09 15:08:12 +00001195
Chris Lattner1b22eed2006-11-28 05:12:07 +00001196 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001197 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001198 if (DS.isInlineSpecified())
1199 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1200 if (DS.isVirtualSpecified())
1201 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1202 if (DS.isExplicitSpecified())
1203 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001204 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001205 }
1206}
Chris Lattner53361ac2006-08-10 05:19:57 +00001207
Chris Lattner6cc055a2009-04-12 20:42:31 +00001208/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1209/// specified token is valid after the identifier in a declarator which
1210/// immediately follows the declspec. For example, these things are valid:
1211///
1212/// int x [ 4]; // direct-declarator
1213/// int x ( int y); // direct-declarator
1214/// int(int x ) // direct-declarator
1215/// int x ; // simple-declaration
1216/// int x = 17; // init-declarator-list
1217/// int x , y; // init-declarator-list
1218/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001219/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001220/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001221///
1222/// This is not, because 'x' does not immediately follow the declspec (though
1223/// ')' happens to be valid anyway).
1224/// int (x)
1225///
1226static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1227 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1228 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001229 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001230}
1231
Chris Lattner20a0c612009-04-14 21:34:55 +00001232
1233/// ParseImplicitInt - This method is called when we have an non-typename
1234/// identifier in a declspec (which normally terminates the decl spec) when
1235/// the declspec has no type specifier. In this case, the declspec is either
1236/// malformed or is "implicit int" (in K&R and C89).
1237///
1238/// This method handles diagnosing this prettily and returns false if the
1239/// declspec is done being processed. If it recovers and thinks there may be
1240/// other pieces of declspec after it, it returns true.
1241///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001242bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001243 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +00001244 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001245 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001246
Chris Lattner20a0c612009-04-14 21:34:55 +00001247 SourceLocation Loc = Tok.getLocation();
1248 // If we see an identifier that is not a type name, we normally would
1249 // parse it as the identifer being declared. However, when a typename
1250 // is typo'd or the definition is not included, this will incorrectly
1251 // parse the typename as the identifier name and fall over misparsing
1252 // later parts of the diagnostic.
1253 //
1254 // As such, we try to do some look-ahead in cases where this would
1255 // otherwise be an "implicit-int" case to see if this is invalid. For
1256 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1257 // an identifier with implicit int, we'd get a parse error because the
1258 // next token is obviously invalid for a type. Parse these as a case
1259 // with an invalid type specifier.
1260 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001261
Chris Lattner20a0c612009-04-14 21:34:55 +00001262 // Since we know that this either implicit int (which is rare) or an
1263 // error, we'd do lookahead to try to do better recovery.
1264 if (isValidAfterIdentifierInDeclarator(NextToken())) {
1265 // If this token is valid for implicit int, e.g. "static x = 4", then
1266 // we just avoid eating the identifier, so it will be parsed as the
1267 // identifier in the declarator.
1268 return false;
1269 }
Mike Stump11289f42009-09-09 15:08:12 +00001270
Chris Lattner20a0c612009-04-14 21:34:55 +00001271 // Otherwise, if we don't consume this token, we are going to emit an
1272 // error anyway. Try to recover from various common problems. Check
1273 // to see if this was a reference to a tag name without a tag specified.
1274 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001275 //
1276 // C++ doesn't need this, and isTagName doesn't take SS.
1277 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001278 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001279 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001280
Douglas Gregor0be31a22010-07-02 17:43:08 +00001281 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001282 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001283 case DeclSpec::TST_enum:
1284 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1285 case DeclSpec::TST_union:
1286 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1287 case DeclSpec::TST_struct:
1288 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1289 case DeclSpec::TST_class:
1290 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001291 }
Mike Stump11289f42009-09-09 15:08:12 +00001292
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001293 if (TagName) {
1294 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +00001295 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001296 << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName);
Mike Stump11289f42009-09-09 15:08:12 +00001297
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001298 // Parse this as a tag as if the missing tag were present.
1299 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001300 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001301 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001302 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001303 return true;
1304 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001305 }
Mike Stump11289f42009-09-09 15:08:12 +00001306
Douglas Gregor15e56022009-10-13 23:27:22 +00001307 // This is almost certainly an invalid type name. Let the action emit a
1308 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001309 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +00001310 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +00001311 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001312 // The action emitted a diagnostic, so we don't have to.
1313 if (T) {
1314 // The action has suggested that the type T could be used. Set that as
1315 // the type in the declaration specifiers, consume the would-be type
1316 // name token, and we're done.
1317 const char *PrevSpec;
1318 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001319 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001320 DS.SetRangeEnd(Tok.getLocation());
1321 ConsumeToken();
1322
1323 // There may be other declaration specifiers after this.
1324 return true;
1325 }
1326
1327 // Fall through; the action had no suggestion for us.
1328 } else {
1329 // The action did not emit a diagnostic, so emit one now.
1330 SourceRange R;
1331 if (SS) R = SS->getRange();
1332 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1333 }
Mike Stump11289f42009-09-09 15:08:12 +00001334
Douglas Gregor15e56022009-10-13 23:27:22 +00001335 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +00001336 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +00001337 unsigned DiagID;
1338 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +00001339 DS.SetRangeEnd(Tok.getLocation());
1340 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001341
Chris Lattner20a0c612009-04-14 21:34:55 +00001342 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1343 // avoid rippling error messages on subsequent uses of the same type,
1344 // could be useful if #include was forgotten.
1345 return false;
1346}
1347
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001348/// \brief Determine the declaration specifier context from the declarator
1349/// context.
1350///
1351/// \param Context the declarator context, which is one of the
1352/// Declarator::TheContext enumerator values.
1353Parser::DeclSpecContext
1354Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1355 if (Context == Declarator::MemberContext)
1356 return DSC_class;
1357 if (Context == Declarator::FileContext)
1358 return DSC_top_level;
1359 return DSC_normal;
1360}
1361
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001362/// ParseDeclarationSpecifiers
1363/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00001364/// storage-class-specifier declaration-specifiers[opt]
1365/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001366/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001367/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001368///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001369/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001370/// 'typedef'
1371/// 'extern'
1372/// 'static'
1373/// 'auto'
1374/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001375/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00001376/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001377/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00001378/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00001379/// [C++] 'virtual'
1380/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001381/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00001382/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001383/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00001384
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001385///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00001386void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001387 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00001388 AccessSpecifier AS,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001389 DeclSpecContext DSContext) {
1390 if (DS.getSourceRange().isInvalid()) {
1391 DS.SetRangeStart(Tok.getLocation());
1392 DS.SetRangeEnd(Tok.getLocation());
1393 }
1394
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001395 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001396 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001397 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001398 unsigned DiagID = 0;
1399
Chris Lattner4d8f8732006-11-28 05:05:08 +00001400 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00001401
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001402 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00001403 default:
Chris Lattner0974b232008-07-26 00:20:22 +00001404 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001405 // If this is not a declaration specifier token, we're done reading decl
1406 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001407 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001408 return;
Mike Stump11289f42009-09-09 15:08:12 +00001409
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001410 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00001411 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001412 if (DS.hasTypeSpecifier()) {
1413 bool AllowNonIdentifiers
1414 = (getCurScope()->getFlags() & (Scope::ControlScope |
1415 Scope::BlockScope |
1416 Scope::TemplateParamScope |
1417 Scope::FunctionPrototypeScope |
1418 Scope::AtCatchScope)) == 0;
1419 bool AllowNestedNameSpecifiers
1420 = DSContext == DSC_top_level ||
1421 (DSContext == DSC_class && DS.isFriendSpecified());
1422
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00001423 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
1424 AllowNonIdentifiers,
1425 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001426 ConsumeCodeCompletionToken();
1427 return;
1428 }
1429
Douglas Gregor80039242011-02-15 20:33:25 +00001430 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
1431 CCC = Sema::PCC_LocalDeclarationSpecifiers;
1432 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +00001433 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
1434 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001435 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00001436 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001437 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +00001438 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00001439
1440 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
1441 ConsumeCodeCompletionToken();
1442 return;
1443 }
1444
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001445 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00001446 // C++ scope specifier. Annotate and loop, or bail out on error.
1447 if (TryAnnotateCXXScopeToken(true)) {
1448 if (!DS.hasTypeSpecifier())
1449 DS.SetTypeSpecError();
1450 goto DoneWithDeclSpec;
1451 }
John McCall8bc2a702010-03-01 18:20:46 +00001452 if (Tok.is(tok::coloncolon)) // ::new or ::delete
1453 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00001454 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001455
1456 case tok::annot_cxxscope: {
1457 if (DS.hasTypeSpecifier())
1458 goto DoneWithDeclSpec;
1459
John McCall9dab4e62009-12-12 11:40:51 +00001460 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00001461 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
1462 Tok.getAnnotationRange(),
1463 SS);
John McCall9dab4e62009-12-12 11:40:51 +00001464
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001465 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00001466 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00001467 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001468 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00001469 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00001470 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001471
1472 // C++ [class.qual]p2:
1473 // In a lookup in which the constructor is an acceptable lookup
1474 // result and the nested-name-specifier nominates a class C:
1475 //
1476 // - if the name specified after the
1477 // nested-name-specifier, when looked up in C, is the
1478 // injected-class-name of C (Clause 9), or
1479 //
1480 // - if the name specified after the nested-name-specifier
1481 // is the same as the identifier or the
1482 // simple-template-id's template-name in the last
1483 // component of the nested-name-specifier,
1484 //
1485 // the name is instead considered to name the constructor of
1486 // class C.
1487 //
1488 // Thus, if the template-name is actually the constructor
1489 // name, then the code is ill-formed; this interpretation is
1490 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001491 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00001492 if ((DSContext == DSC_top_level ||
1493 (DSContext == DSC_class && DS.isFriendSpecified())) &&
1494 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001495 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001496 if (isConstructorDeclarator()) {
1497 // The user meant this to be an out-of-line constructor
1498 // definition, but template arguments are not allowed
1499 // there. Just allow this as a constructor; we'll
1500 // complain about it later.
1501 goto DoneWithDeclSpec;
1502 }
1503
1504 // The user meant this to name a type, but it actually names
1505 // a constructor with some extraneous template
1506 // arguments. Complain, then parse it as a type as the user
1507 // intended.
1508 Diag(TemplateId->TemplateNameLoc,
1509 diag::err_out_of_line_template_id_names_constructor)
1510 << TemplateId->Name;
1511 }
1512
John McCall9dab4e62009-12-12 11:40:51 +00001513 DS.getTypeSpecScope() = SS;
1514 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001515 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001516 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00001517 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00001518 continue;
1519 }
1520
Douglas Gregorc5790df2009-09-28 07:26:33 +00001521 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001522 DS.getTypeSpecScope() = SS;
1523 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001524 if (Tok.getAnnotationValue()) {
1525 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001526 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1527 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001528 PrevSpec, DiagID, T);
1529 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001530 else
1531 DS.SetTypeSpecError();
1532 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1533 ConsumeToken(); // The typename
1534 }
1535
Douglas Gregor167fa622009-03-25 15:40:00 +00001536 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001537 goto DoneWithDeclSpec;
1538
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001539 // If we're in a context where the identifier could be a class name,
1540 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001541 if ((DSContext == DSC_top_level ||
1542 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001543 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001544 &SS)) {
1545 if (isConstructorDeclarator())
1546 goto DoneWithDeclSpec;
1547
1548 // As noted in C++ [class.qual]p2 (cited above), when the name
1549 // of the class is qualified in a context where it could name
1550 // a constructor, its a constructor name. However, we've
1551 // looked at the declarator, and the user probably meant this
1552 // to be a type. Complain that it isn't supposed to be treated
1553 // as a type, then proceed to parse it as a type.
1554 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1555 << Next.getIdentifierInfo();
1556 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001557
John McCallba7bf592010-08-24 05:47:05 +00001558 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1559 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00001560 getCurScope(), &SS,
1561 false, false, ParsedType(),
1562 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001563
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001564 // If the referenced identifier is not a type, then this declspec is
1565 // erroneous: We already checked about that it has no type specifier, and
1566 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001567 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001568 if (TypeRep == 0) {
1569 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001570 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001571 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001572 }
Mike Stump11289f42009-09-09 15:08:12 +00001573
John McCall9dab4e62009-12-12 11:40:51 +00001574 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001575 ConsumeToken(); // The C++ scope.
1576
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001577 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001578 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001579 if (isInvalid)
1580 break;
Mike Stump11289f42009-09-09 15:08:12 +00001581
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001582 DS.SetRangeEnd(Tok.getLocation());
1583 ConsumeToken(); // The typename.
1584
1585 continue;
1586 }
Mike Stump11289f42009-09-09 15:08:12 +00001587
Chris Lattnere387d9e2009-01-21 19:48:37 +00001588 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001589 if (Tok.getAnnotationValue()) {
1590 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001591 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001592 DiagID, T);
1593 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001594 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001595
1596 if (isInvalid)
1597 break;
1598
Chris Lattnere387d9e2009-01-21 19:48:37 +00001599 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1600 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001601
Chris Lattnere387d9e2009-01-21 19:48:37 +00001602 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1603 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001604 // Objective-C interface.
1605 if (Tok.is(tok::less) && getLang().ObjC1)
1606 ParseObjCProtocolQualifiers(DS);
1607
Chris Lattnere387d9e2009-01-21 19:48:37 +00001608 continue;
1609 }
Mike Stump11289f42009-09-09 15:08:12 +00001610
Douglas Gregor06873092011-04-28 15:48:45 +00001611 case tok::kw___is_signed:
1612 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
1613 // typically treats it as a trait. If we see __is_signed as it appears
1614 // in libstdc++, e.g.,
1615 //
1616 // static const bool __is_signed;
1617 //
1618 // then treat __is_signed as an identifier rather than as a keyword.
1619 if (DS.getTypeSpecType() == TST_bool &&
1620 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
1621 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
1622 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
1623 Tok.setKind(tok::identifier);
1624 }
1625
1626 // We're done with the declaration-specifiers.
1627 goto DoneWithDeclSpec;
1628
Chris Lattner16fac4f2008-07-26 01:18:38 +00001629 // typedef-name
1630 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001631 // In C++, check to see if this is a scope specifier like foo::bar::, if
1632 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001633 if (getLang().CPlusPlus) {
1634 if (TryAnnotateCXXScopeToken(true)) {
1635 if (!DS.hasTypeSpecifier())
1636 DS.SetTypeSpecError();
1637 goto DoneWithDeclSpec;
1638 }
1639 if (!Tok.is(tok::identifier))
1640 continue;
1641 }
Mike Stump11289f42009-09-09 15:08:12 +00001642
Chris Lattner16fac4f2008-07-26 01:18:38 +00001643 // This identifier can only be a typedef name if we haven't already seen
1644 // a type-specifier. Without this check we misparse:
1645 // typedef int X; struct Y { short X; }; as 'short int'.
1646 if (DS.hasTypeSpecifier())
1647 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001648
John Thompson22334602010-02-05 00:12:22 +00001649 // Check for need to substitute AltiVec keyword tokens.
1650 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1651 break;
1652
Chris Lattner16fac4f2008-07-26 01:18:38 +00001653 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001654 ParsedType TypeRep =
1655 Actions.getTypeName(*Tok.getIdentifierInfo(),
1656 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001657
Chris Lattner6cc055a2009-04-12 20:42:31 +00001658 // If this is not a typedef name, don't parse it as part of the declspec,
1659 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001660 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001661 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001662 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001663 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001664
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001665 // If we're in a context where the identifier could be a class name,
1666 // check whether this is a constructor declaration.
1667 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001668 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001669 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001670 goto DoneWithDeclSpec;
1671
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001672 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001673 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001674 if (isInvalid)
1675 break;
Mike Stump11289f42009-09-09 15:08:12 +00001676
Chris Lattner16fac4f2008-07-26 01:18:38 +00001677 DS.SetRangeEnd(Tok.getLocation());
1678 ConsumeToken(); // The identifier
1679
1680 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1681 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001682 // Objective-C interface.
1683 if (Tok.is(tok::less) && getLang().ObjC1)
1684 ParseObjCProtocolQualifiers(DS);
1685
Steve Naroffcd5e7822008-09-22 10:28:57 +00001686 // Need to support trailing type qualifiers (e.g. "id<p> const").
1687 // If a type specifier follows, it will be diagnosed elsewhere.
1688 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001689 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001690
1691 // type-name
1692 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00001693 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00001694 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001695 // This template-id does not refer to a type name, so we're
1696 // done with the type-specifiers.
1697 goto DoneWithDeclSpec;
1698 }
1699
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001700 // If we're in a context where the template-id could be a
1701 // constructor name or specialization, check whether this is a
1702 // constructor declaration.
1703 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001704 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001705 isConstructorDeclarator())
1706 goto DoneWithDeclSpec;
1707
Douglas Gregor7f741122009-02-25 19:37:18 +00001708 // Turn the template-id annotation token into a type annotation
1709 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001710 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001711 continue;
1712 }
1713
Chris Lattnere37e2332006-08-15 04:50:22 +00001714 // GNU attributes support.
1715 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001716 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001717 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001718
1719 // Microsoft declspec support.
1720 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001721 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001722 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001723
Steve Naroff44ac7772008-12-25 14:16:32 +00001724 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001725 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001726 // FIXME: Add handling here!
1727 break;
1728
1729 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00001730 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00001731 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001732 case tok::kw___cdecl:
1733 case tok::kw___stdcall:
1734 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001735 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00001736 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00001737 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001738 continue;
1739
Dawn Perchik335e16b2010-09-03 01:29:35 +00001740 // Borland single token adornments.
1741 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001742 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001743 continue;
1744
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001745 // OpenCL single token adornments.
1746 case tok::kw___kernel:
1747 ParseOpenCLAttributes(DS.getAttributes());
1748 continue;
1749
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001750 // storage-class-specifier
1751 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001752 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001753 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001754 break;
1755 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001756 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001757 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001758 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001759 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001760 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001761 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001762 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001763 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001764 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001765 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001766 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001767 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001768 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001769 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001770 break;
1771 case tok::kw_auto:
Douglas Gregor1e989862011-03-14 21:43:30 +00001772 if (getLang().CPlusPlus0x) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00001773 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1774 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1775 DiagID, getLang());
1776 if (!isInvalid)
1777 Diag(Tok, diag::auto_storage_class)
1778 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1779 }
1780 else
1781 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1782 DiagID);
1783 }
Anders Carlsson082acde2009-06-26 18:41:36 +00001784 else
John McCall49bfce42009-08-03 20:12:06 +00001785 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001786 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001787 break;
1788 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001789 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001790 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001791 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001792 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001793 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001794 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001795 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001796 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001797 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001798 break;
Mike Stump11289f42009-09-09 15:08:12 +00001799
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001800 // function-specifier
1801 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001802 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001803 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001804 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001805 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001806 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001807 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001808 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001809 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001810
Anders Carlssoncd8db412009-05-06 04:46:28 +00001811 // friend
1812 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001813 if (DSContext == DSC_class)
1814 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1815 else {
1816 PrevSpec = ""; // not actually used by the diagnostic
1817 DiagID = diag::err_friend_invalid_in_context;
1818 isInvalid = true;
1819 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001820 break;
Mike Stump11289f42009-09-09 15:08:12 +00001821
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001822 // constexpr
1823 case tok::kw_constexpr:
1824 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1825 break;
1826
Chris Lattnere387d9e2009-01-21 19:48:37 +00001827 // type-specifier
1828 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001829 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1830 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001831 break;
1832 case tok::kw_long:
1833 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001834 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1835 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001836 else
John McCall49bfce42009-08-03 20:12:06 +00001837 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1838 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001839 break;
Francois Pichet84133e42011-04-28 01:59:37 +00001840 case tok::kw___int64:
1841 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1842 DiagID);
1843 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001844 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001845 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1846 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001847 break;
1848 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001849 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1850 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001851 break;
1852 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001853 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1854 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001855 break;
1856 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001857 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1858 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001859 break;
1860 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001861 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1862 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001863 break;
1864 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001865 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1866 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001867 break;
1868 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001869 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1870 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001871 break;
1872 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001873 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1874 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001875 break;
1876 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001877 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1878 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001879 break;
1880 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001881 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1882 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001883 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001884 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001885 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1886 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001887 break;
1888 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001889 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1890 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001891 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001892 case tok::kw_bool:
1893 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001894 if (Tok.is(tok::kw_bool) &&
1895 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1896 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1897 PrevSpec = ""; // Not used by the diagnostic.
1898 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00001899 // For better error recovery.
1900 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001901 isInvalid = true;
1902 } else {
1903 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1904 DiagID);
1905 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001906 break;
1907 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001908 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1909 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001910 break;
1911 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001912 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1913 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001914 break;
1915 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001916 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1917 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001918 break;
John Thompson22334602010-02-05 00:12:22 +00001919 case tok::kw___vector:
1920 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1921 break;
1922 case tok::kw___pixel:
1923 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1924 break;
John McCall39439732011-04-09 22:50:59 +00001925 case tok::kw___unknown_anytype:
1926 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
1927 PrevSpec, DiagID);
1928 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001929
1930 // class-specifier:
1931 case tok::kw_class:
1932 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001933 case tok::kw_union: {
1934 tok::TokenKind Kind = Tok.getKind();
1935 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001936 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001937 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001938 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001939
1940 // enum-specifier:
1941 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001942 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001943 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001944 continue;
1945
1946 // cv-qualifier:
1947 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001948 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1949 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001950 break;
1951 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001952 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1953 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001954 break;
1955 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001956 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1957 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001958 break;
1959
Douglas Gregor333489b2009-03-27 23:10:48 +00001960 // C++ typename-specifier:
1961 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001962 if (TryAnnotateTypeOrScopeToken()) {
1963 DS.SetTypeSpecError();
1964 goto DoneWithDeclSpec;
1965 }
1966 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001967 continue;
1968 break;
1969
Chris Lattnere387d9e2009-01-21 19:48:37 +00001970 // GNU typeof support.
1971 case tok::kw_typeof:
1972 ParseTypeofSpecifier(DS);
1973 continue;
1974
Anders Carlsson74948d02009-06-24 17:47:40 +00001975 case tok::kw_decltype:
1976 ParseDecltypeSpecifier(DS);
1977 continue;
1978
Alexis Hunt4a257072011-05-19 05:37:45 +00001979 case tok::kw___underlying_type:
1980 ParseUnderlyingTypeSpecifier(DS);
1981
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00001982 // OpenCL qualifiers:
1983 case tok::kw_private:
1984 if (!getLang().OpenCL)
1985 goto DoneWithDeclSpec;
1986 case tok::kw___private:
1987 case tok::kw___global:
1988 case tok::kw___local:
1989 case tok::kw___constant:
1990 case tok::kw___read_only:
1991 case tok::kw___write_only:
1992 case tok::kw___read_write:
1993 ParseOpenCLQualifiers(DS);
1994 break;
1995
Steve Naroffcfdf6162008-06-05 00:02:44 +00001996 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001997 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001998 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1999 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00002000 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002001 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002002
Douglas Gregor3a001f42010-11-19 17:10:50 +00002003 if (!ParseObjCProtocolQualifiers(DS))
2004 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2005 << FixItHint::CreateInsertion(Loc, "id")
2006 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002007
2008 // Need to support trailing type qualifiers (e.g. "id<p> const").
2009 // If a type specifier follows, it will be diagnosed elsewhere.
2010 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002011 }
John McCall49bfce42009-08-03 20:12:06 +00002012 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002013 if (isInvalid) {
2014 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002015 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002016
2017 if (DiagID == diag::ext_duplicate_declspec)
2018 Diag(Tok, DiagID)
2019 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2020 else
2021 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002022 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002023
Chris Lattner2e232092008-03-13 06:29:04 +00002024 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002025 if (DiagID != diag::err_bool_redeclaration)
2026 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002027 }
2028}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002029
Chris Lattnera448d752009-01-06 06:59:53 +00002030/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00002031/// primarily follow the C++ grammar with additions for C99 and GNU,
2032/// which together subsume the C grammar. Note that the C++
2033/// type-specifier also includes the C type-qualifier (for const,
2034/// volatile, and C99 restrict). Returns true if a type-specifier was
2035/// found (and parsed), false otherwise.
2036///
2037/// type-specifier: [C++ 7.1.5]
2038/// simple-type-specifier
2039/// class-specifier
2040/// enum-specifier
2041/// elaborated-type-specifier [TODO]
2042/// cv-qualifier
2043///
2044/// cv-qualifier: [C++ 7.1.5.1]
2045/// 'const'
2046/// 'volatile'
2047/// [C99] 'restrict'
2048///
2049/// simple-type-specifier: [ C++ 7.1.5.2]
2050/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
2051/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
2052/// 'char'
2053/// 'wchar_t'
2054/// 'bool'
2055/// 'short'
2056/// 'int'
2057/// 'long'
2058/// 'signed'
2059/// 'unsigned'
2060/// 'float'
2061/// 'double'
2062/// 'void'
2063/// [C99] '_Bool'
2064/// [C99] '_Complex'
2065/// [C99] '_Imaginary' // Removed in TC2?
2066/// [GNU] '_Decimal32'
2067/// [GNU] '_Decimal64'
2068/// [GNU] '_Decimal128'
2069/// [GNU] typeof-specifier
2070/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
2071/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00002072/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00002073/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00002074bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00002075 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002076 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00002077 const ParsedTemplateInfo &TemplateInfo,
2078 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00002079 SourceLocation Loc = Tok.getLocation();
2080
2081 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00002082 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00002083 // If we already have a type specifier, this identifier is not a type.
2084 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
2085 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
2086 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
2087 return false;
John Thompson22334602010-02-05 00:12:22 +00002088 // Check for need to substitute AltiVec keyword tokens.
2089 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2090 break;
2091 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002092 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00002093 // Annotate typenames and C++ scope specifiers. If we get one, just
2094 // recurse to handle whatever we get.
2095 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002096 return true;
2097 if (Tok.is(tok::identifier))
2098 return false;
2099 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2100 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00002101 case tok::coloncolon: // ::foo::bar
2102 if (NextToken().is(tok::kw_new) || // ::new
2103 NextToken().is(tok::kw_delete)) // ::delete
2104 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002105
Chris Lattner020bab92009-01-04 23:41:41 +00002106 // Annotate typenames and C++ scope specifiers. If we get one, just
2107 // recurse to handle whatever we get.
2108 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002109 return true;
2110 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
2111 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00002112
Douglas Gregor450c75a2008-11-07 15:42:26 +00002113 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00002114 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002115 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00002116 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2117 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002118 DiagID, T);
2119 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002120 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002121 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2122 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002123
Douglas Gregor450c75a2008-11-07 15:42:26 +00002124 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2125 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
2126 // Objective-C interface. If we don't have Objective-C or a '<', this is
2127 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002128 if (Tok.is(tok::less) && getLang().ObjC1)
2129 ParseObjCProtocolQualifiers(DS);
2130
Douglas Gregor450c75a2008-11-07 15:42:26 +00002131 return true;
2132 }
2133
2134 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002135 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002136 break;
2137 case tok::kw_long:
2138 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002139 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2140 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002141 else
John McCall49bfce42009-08-03 20:12:06 +00002142 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2143 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002144 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002145 case tok::kw___int64:
2146 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2147 DiagID);
2148 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002149 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002150 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002151 break;
2152 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002153 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2154 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002155 break;
2156 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002157 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2158 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002159 break;
2160 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002161 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2162 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002163 break;
2164 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002165 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002166 break;
2167 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002168 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002169 break;
2170 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002171 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002172 break;
2173 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002174 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002175 break;
2176 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002177 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002178 break;
2179 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002180 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002181 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002182 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002183 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002184 break;
2185 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002186 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002187 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002188 case tok::kw_bool:
2189 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00002190 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002191 break;
2192 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002193 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2194 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002195 break;
2196 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002197 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2198 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002199 break;
2200 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002201 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2202 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002203 break;
John Thompson22334602010-02-05 00:12:22 +00002204 case tok::kw___vector:
2205 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2206 break;
2207 case tok::kw___pixel:
2208 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2209 break;
2210
Douglas Gregor450c75a2008-11-07 15:42:26 +00002211 // class-specifier:
2212 case tok::kw_class:
2213 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002214 case tok::kw_union: {
2215 tok::TokenKind Kind = Tok.getKind();
2216 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00002217 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
2218 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002219 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002220 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00002221
2222 // enum-specifier:
2223 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002224 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002225 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00002226 return true;
2227
2228 // cv-qualifier:
2229 case tok::kw_const:
2230 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002231 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002232 break;
2233 case tok::kw_volatile:
2234 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002235 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002236 break;
2237 case tok::kw_restrict:
2238 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002239 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00002240 break;
2241
2242 // GNU typeof support.
2243 case tok::kw_typeof:
2244 ParseTypeofSpecifier(DS);
2245 return true;
2246
Anders Carlsson74948d02009-06-24 17:47:40 +00002247 // C++0x decltype support.
2248 case tok::kw_decltype:
2249 ParseDecltypeSpecifier(DS);
2250 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002251
Alexis Hunt4a257072011-05-19 05:37:45 +00002252 // C++0x type traits support.
2253 case tok::kw___underlying_type:
2254 ParseUnderlyingTypeSpecifier(DS);
2255 return true;
2256
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002257 // OpenCL qualifiers:
2258 case tok::kw_private:
2259 if (!getLang().OpenCL)
2260 return false;
2261 case tok::kw___private:
2262 case tok::kw___global:
2263 case tok::kw___local:
2264 case tok::kw___constant:
2265 case tok::kw___read_only:
2266 case tok::kw___write_only:
2267 case tok::kw___read_write:
2268 ParseOpenCLQualifiers(DS);
2269 break;
2270
Anders Carlssonbae27372009-06-26 23:44:14 +00002271 // C++0x auto support.
2272 case tok::kw_auto:
2273 if (!getLang().CPlusPlus0x)
2274 return false;
2275
John McCall49bfce42009-08-03 20:12:06 +00002276 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00002277 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002278
Eli Friedman53339e02009-06-08 23:27:34 +00002279 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002280 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00002281 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002282 case tok::kw___cdecl:
2283 case tok::kw___stdcall:
2284 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002285 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002286 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002287 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00002288 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00002289
Dawn Perchik335e16b2010-09-03 01:29:35 +00002290 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002291 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002292 return true;
2293
Douglas Gregor450c75a2008-11-07 15:42:26 +00002294 default:
2295 // Not a type-specifier; do nothing.
2296 return false;
2297 }
2298
2299 // If the specifier combination wasn't legal, issue a diagnostic.
2300 if (isInvalid) {
2301 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002302 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00002303 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00002304 }
2305 DS.SetRangeEnd(Tok.getLocation());
2306 ConsumeToken(); // whatever we parsed above.
2307 return true;
2308}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002309
Chris Lattner70ae4912007-10-29 04:42:53 +00002310/// ParseStructDeclaration - Parse a struct declaration without the terminating
2311/// semicolon.
2312///
Chris Lattner90a26b02007-01-23 04:38:16 +00002313/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002314/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002315/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002316/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002317/// struct-declarator-list:
2318/// struct-declarator
2319/// struct-declarator-list ',' struct-declarator
2320/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2321/// struct-declarator:
2322/// declarator
2323/// [GNU] declarator attributes[opt]
2324/// declarator[opt] ':' constant-expression
2325/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2326///
Chris Lattnera12405b2008-04-10 06:46:29 +00002327void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00002328ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002329
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002330 if (Tok.is(tok::kw___extension__)) {
2331 // __extension__ silences extension warnings in the subexpression.
2332 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002333 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002334 return ParseStructDeclaration(DS, Fields);
2335 }
Mike Stump11289f42009-09-09 15:08:12 +00002336
Steve Naroff97170802007-08-20 22:28:22 +00002337 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002338 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002339
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002340 // If there are no declarators, this is a free-standing declaration
2341 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002342 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002343 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00002344 return;
2345 }
2346
2347 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002348 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00002349 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00002350 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00002351 FieldDeclarator DeclaratorInfo(DS);
2352
2353 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002354 if (!FirstDeclarator)
2355 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002356
Steve Naroff97170802007-08-20 22:28:22 +00002357 /// struct-declarator: declarator
2358 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002359 if (Tok.isNot(tok::colon)) {
2360 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2361 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002362 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002363 }
Mike Stump11289f42009-09-09 15:08:12 +00002364
Chris Lattner76c72282007-10-09 17:33:22 +00002365 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002366 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002367 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002368 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002369 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002370 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002371 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002372 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002373
Steve Naroff97170802007-08-20 22:28:22 +00002374 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002375 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002376
John McCallcfefb6d2009-11-03 02:38:08 +00002377 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00002378 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00002379 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00002380
Steve Naroff97170802007-08-20 22:28:22 +00002381 // If we don't have a comma, it is either the end of the list (a ';')
2382 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002383 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002384 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002385
Steve Naroff97170802007-08-20 22:28:22 +00002386 // Consume the comma.
2387 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002388
John McCallcfefb6d2009-11-03 02:38:08 +00002389 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00002390 }
Steve Naroff97170802007-08-20 22:28:22 +00002391}
2392
2393/// ParseStructUnionBody
2394/// struct-contents:
2395/// struct-declaration-list
2396/// [EXT] empty
2397/// [GNU] "struct-declaration-list" without terminatoring ';'
2398/// struct-declaration-list:
2399/// struct-declaration
2400/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00002401/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00002402///
Chris Lattner1300fb92007-01-23 23:42:53 +00002403void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00002404 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00002405 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2406 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00002407
Chris Lattner90a26b02007-01-23 04:38:16 +00002408 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002409
Douglas Gregor658b9552009-01-09 22:42:13 +00002410 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002411 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002412
Chris Lattner7b9ace62007-01-23 20:11:08 +00002413 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2414 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00002415 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00002416 Diag(Tok, diag::ext_empty_struct_union)
2417 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00002418
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002419 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00002420
Chris Lattner7b9ace62007-01-23 20:11:08 +00002421 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00002422 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002423 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00002424
Chris Lattner736ed5d2007-06-09 05:59:07 +00002425 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00002426 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00002427 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00002428 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00002429 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00002430 ConsumeToken();
2431 continue;
2432 }
Chris Lattnera12405b2008-04-10 06:46:29 +00002433
2434 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00002435 DeclSpec DS(AttrFactory);
Mike Stump11289f42009-09-09 15:08:12 +00002436
John McCallcfefb6d2009-11-03 02:38:08 +00002437 if (!Tok.is(tok::at)) {
2438 struct CFieldCallback : FieldCallback {
2439 Parser &P;
John McCall48871652010-08-21 09:40:31 +00002440 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002441 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00002442
John McCall48871652010-08-21 09:40:31 +00002443 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002444 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00002445 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2446
John McCall48871652010-08-21 09:40:31 +00002447 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00002448 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00002449 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00002450 FD.D.getDeclSpec().getSourceRange().getBegin(),
2451 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00002452 FieldDecls.push_back(Field);
2453 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00002454 }
John McCallcfefb6d2009-11-03 02:38:08 +00002455 } Callback(*this, TagDecl, FieldDecls);
2456
2457 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00002458 } else { // Handle @defs
2459 ConsumeToken();
2460 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2461 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00002462 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002463 continue;
2464 }
2465 ConsumeToken();
2466 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2467 if (!Tok.is(tok::identifier)) {
2468 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00002469 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00002470 continue;
2471 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002472 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002473 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00002474 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00002475 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2476 ConsumeToken();
2477 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00002478 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00002479
Chris Lattner76c72282007-10-09 17:33:22 +00002480 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00002481 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00002482 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00002483 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00002484 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00002485 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00002486 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2487 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00002488 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00002489 // If we stopped at a ';', eat it.
2490 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00002491 }
2492 }
Mike Stump11289f42009-09-09 15:08:12 +00002493
Steve Naroff33a1e802007-10-29 21:38:07 +00002494 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00002495
John McCall084e83d2011-03-24 11:26:52 +00002496 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00002497 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002498 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00002499
Douglas Gregor0be31a22010-07-02 17:43:08 +00002500 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00002501 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00002502 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00002503 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002504 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002505 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00002506}
2507
Chris Lattner3b561a32006-08-13 00:12:11 +00002508/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00002509/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00002510/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002511///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00002512/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2513/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002514/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00002515/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002516///
Douglas Gregor0bf31402010-10-08 23:50:27 +00002517/// [C++0x] enum-head '{' enumerator-list[opt] '}'
2518/// [C++0x] enum-head '{' enumerator-list ',' '}'
2519///
2520/// enum-head: [C++0x]
2521/// enum-key attributes[opt] identifier[opt] enum-base[opt]
2522/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
2523///
2524/// enum-key: [C++0x]
2525/// 'enum'
2526/// 'enum' 'class'
2527/// 'enum' 'struct'
2528///
2529/// enum-base: [C++0x]
2530/// ':' type-specifier-seq
2531///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002532/// [C++] elaborated-type-specifier:
2533/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2534///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002535void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00002536 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002537 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00002538 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002539 if (Tok.is(tok::code_completion)) {
2540 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00002541 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002542 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002543 }
John McCallcb432fa2011-07-06 05:58:41 +00002544
2545 bool IsScopedEnum = false;
2546 bool IsScopedUsingClassTag = false;
2547
2548 if (getLang().CPlusPlus0x &&
2549 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
2550 IsScopedEnum = true;
2551 IsScopedUsingClassTag = Tok.is(tok::kw_class);
2552 ConsumeToken();
2553 }
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00002554
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002555 // If attributes exist after tag, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002556 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002557 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002558
John McCallcb432fa2011-07-06 05:58:41 +00002559 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
2560
Abramo Bagnarad7548482010-05-19 21:37:53 +00002561 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00002562 if (getLang().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00002563 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2564 // if a fixed underlying type is allowed.
2565 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2566
John McCallba7bf592010-08-24 05:47:05 +00002567 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00002568 return;
2569
2570 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002571 Diag(Tok, diag::err_expected_ident);
2572 if (Tok.isNot(tok::l_brace)) {
2573 // Has no name and is not a definition.
2574 // Skip the rest of this declarator, up until the comma or semicolon.
2575 SkipUntil(tok::comma, true);
2576 return;
2577 }
2578 }
2579 }
Mike Stump11289f42009-09-09 15:08:12 +00002580
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002581 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002582 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2583 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002584 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00002585
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002586 // Skip the rest of this declarator, up until the comma or semicolon.
2587 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00002588 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002589 }
Mike Stump11289f42009-09-09 15:08:12 +00002590
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002591 // If an identifier is present, consume and remember it.
2592 IdentifierInfo *Name = 0;
2593 SourceLocation NameLoc;
2594 if (Tok.is(tok::identifier)) {
2595 Name = Tok.getIdentifierInfo();
2596 NameLoc = ConsumeToken();
2597 }
Mike Stump11289f42009-09-09 15:08:12 +00002598
Douglas Gregor0bf31402010-10-08 23:50:27 +00002599 if (!Name && IsScopedEnum) {
2600 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2601 // declaration of a scoped enumeration.
2602 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2603 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002604 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002605 }
2606
2607 TypeResult BaseType;
2608
Douglas Gregord1f69f62010-12-01 17:42:47 +00002609 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002610 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002611 bool PossibleBitfield = false;
2612 if (getCurScope()->getFlags() & Scope::ClassScope) {
2613 // If we're in class scope, this can either be an enum declaration with
2614 // an underlying type, or a declaration of a bitfield member. We try to
2615 // use a simple disambiguation scheme first to catch the common cases
2616 // (integer literal, sizeof); if it's still ambiguous, we then consider
2617 // anything that's a simple-type-specifier followed by '(' as an
2618 // expression. This suffices because function types are not valid
2619 // underlying types anyway.
2620 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2621 // If the next token starts an expression, we know we're parsing a
2622 // bit-field. This is the common case.
2623 if (TPR == TPResult::True())
2624 PossibleBitfield = true;
2625 // If the next token starts a type-specifier-seq, it may be either a
2626 // a fixed underlying type or the start of a function-style cast in C++;
2627 // lookahead one more token to see if it's obvious that we have a
2628 // fixed underlying type.
2629 else if (TPR == TPResult::False() &&
2630 GetLookAheadToken(2).getKind() == tok::semi) {
2631 // Consume the ':'.
2632 ConsumeToken();
2633 } else {
2634 // We have the start of a type-specifier-seq, so we have to perform
2635 // tentative parsing to determine whether we have an expression or a
2636 // type.
2637 TentativeParsingAction TPA(*this);
2638
2639 // Consume the ':'.
2640 ConsumeToken();
2641
Douglas Gregora1aec292011-02-22 20:32:04 +00002642 if ((getLang().CPlusPlus &&
2643 isCXXDeclarationSpecifier() != TPResult::True()) ||
2644 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002645 // We'll parse this as a bitfield later.
2646 PossibleBitfield = true;
2647 TPA.Revert();
2648 } else {
2649 // We have a type-specifier-seq.
2650 TPA.Commit();
2651 }
2652 }
2653 } else {
2654 // Consume the ':'.
2655 ConsumeToken();
2656 }
2657
2658 if (!PossibleBitfield) {
2659 SourceRange Range;
2660 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002661
2662 if (!getLang().CPlusPlus0x)
2663 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2664 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002665 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002666 }
2667
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002668 // There are three options here. If we have 'enum foo;', then this is a
2669 // forward declaration. If we have 'enum foo {...' then this is a
2670 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2671 //
2672 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2673 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2674 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2675 //
John McCallfaf5fb42010-08-26 23:41:50 +00002676 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002677 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002678 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002679 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002680 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002681 else
John McCallfaf5fb42010-08-26 23:41:50 +00002682 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002683
2684 // enums cannot be templates, although they can be referenced from a
2685 // template.
2686 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002687 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002688 Diag(Tok, diag::err_enum_template);
2689
2690 // Skip the rest of this declarator, up until the comma or semicolon.
2691 SkipUntil(tok::comma, true);
2692 return;
2693 }
2694
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002695 if (!Name && TUK != Sema::TUK_Definition) {
2696 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2697
2698 // Skip the rest of this declarator, up until the comma or semicolon.
2699 SkipUntil(tok::comma, true);
2700 return;
2701 }
2702
Douglas Gregord6ab8742009-05-28 23:31:59 +00002703 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002704 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002705 const char *PrevSpec = 0;
2706 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002707 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002708 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002709 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002710 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002711 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002712 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002713
Douglas Gregorba41d012010-04-24 16:38:41 +00002714 if (IsDependent) {
2715 // This enum has a dependent nested-name-specifier. Handle it as a
2716 // dependent tag.
2717 if (!Name) {
2718 DS.SetTypeSpecError();
2719 Diag(Tok, diag::err_expected_type_name_after_typename);
2720 return;
2721 }
2722
Douglas Gregor0be31a22010-07-02 17:43:08 +00002723 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002724 TUK, SS, Name, StartLoc,
2725 NameLoc);
2726 if (Type.isInvalid()) {
2727 DS.SetTypeSpecError();
2728 return;
2729 }
2730
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002731 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2732 NameLoc.isValid() ? NameLoc : StartLoc,
2733 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002734 Diag(StartLoc, DiagID) << PrevSpec;
2735
2736 return;
2737 }
Mike Stump11289f42009-09-09 15:08:12 +00002738
John McCall48871652010-08-21 09:40:31 +00002739 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002740 // The action failed to produce an enumeration tag. If this is a
2741 // definition, consume the entire definition.
2742 if (Tok.is(tok::l_brace)) {
2743 ConsumeBrace();
2744 SkipUntil(tok::r_brace);
2745 }
2746
2747 DS.SetTypeSpecError();
2748 return;
2749 }
2750
Chris Lattner76c72282007-10-09 17:33:22 +00002751 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002752 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002753
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00002754 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2755 NameLoc.isValid() ? NameLoc : StartLoc,
2756 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002757 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002758}
2759
Chris Lattnerc1915e22007-01-25 07:29:02 +00002760/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2761/// enumerator-list:
2762/// enumerator
2763/// enumerator-list ',' enumerator
2764/// enumerator:
2765/// enumeration-constant
2766/// enumeration-constant '=' constant-expression
2767/// enumeration-constant:
2768/// identifier
2769///
John McCall48871652010-08-21 09:40:31 +00002770void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002771 // Enter the scope of the enum body and start the definition.
2772 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002773 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002774
Chris Lattnerc1915e22007-01-25 07:29:02 +00002775 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002776
Chris Lattner37256fb2007-08-27 17:24:30 +00002777 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002778 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002779 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002780
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002781 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002782
John McCall48871652010-08-21 09:40:31 +00002783 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002784
Chris Lattnerc1915e22007-01-25 07:29:02 +00002785 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002786 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002787 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2788 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002789
John McCall811a0f52010-10-22 23:36:17 +00002790 // If attributes exist after the enumerator, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002791 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002792 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002793
Chris Lattnerc1915e22007-01-25 07:29:02 +00002794 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002795 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002796 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002797 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002798 AssignedVal = ParseConstantExpression();
2799 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002800 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002801 }
Mike Stump11289f42009-09-09 15:08:12 +00002802
Chris Lattnerc1915e22007-01-25 07:29:02 +00002803 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002804 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2805 LastEnumConstDecl,
2806 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002807 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002808 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002809 EnumConstantDecls.push_back(EnumConstDecl);
2810 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002811
Douglas Gregorce66d022010-09-07 14:51:08 +00002812 if (Tok.is(tok::identifier)) {
2813 // We're missing a comma between enumerators.
2814 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2815 Diag(Loc, diag::err_enumerator_list_missing_comma)
2816 << FixItHint::CreateInsertion(Loc, ", ");
2817 continue;
2818 }
2819
Chris Lattner76c72282007-10-09 17:33:22 +00002820 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002821 break;
2822 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002823
2824 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002825 !(getLang().C99 || getLang().CPlusPlus0x))
2826 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2827 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002828 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002829 }
Mike Stump11289f42009-09-09 15:08:12 +00002830
Chris Lattnerc1915e22007-01-25 07:29:02 +00002831 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002832 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002833
Chris Lattnerc1915e22007-01-25 07:29:02 +00002834 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00002835 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00002836 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002837
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002838 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2839 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002840 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002841
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002842 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002843 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002844}
Chris Lattner3b561a32006-08-13 00:12:11 +00002845
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002846/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002847/// start of a type-qualifier-list.
2848bool Parser::isTypeQualifier() const {
2849 switch (Tok.getKind()) {
2850 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002851
2852 // type-qualifier only in OpenCL
2853 case tok::kw_private:
2854 return getLang().OpenCL;
2855
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002856 // type-qualifier
2857 case tok::kw_const:
2858 case tok::kw_volatile:
2859 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002860 case tok::kw___private:
2861 case tok::kw___local:
2862 case tok::kw___global:
2863 case tok::kw___constant:
2864 case tok::kw___read_only:
2865 case tok::kw___read_write:
2866 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002867 return true;
2868 }
2869}
2870
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002871/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2872/// is definitely a type-specifier. Return false if it isn't part of a type
2873/// specifier or if we're not sure.
2874bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2875 switch (Tok.getKind()) {
2876 default: return false;
2877 // type-specifiers
2878 case tok::kw_short:
2879 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002880 case tok::kw___int64:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002881 case tok::kw_signed:
2882 case tok::kw_unsigned:
2883 case tok::kw__Complex:
2884 case tok::kw__Imaginary:
2885 case tok::kw_void:
2886 case tok::kw_char:
2887 case tok::kw_wchar_t:
2888 case tok::kw_char16_t:
2889 case tok::kw_char32_t:
2890 case tok::kw_int:
2891 case tok::kw_float:
2892 case tok::kw_double:
2893 case tok::kw_bool:
2894 case tok::kw__Bool:
2895 case tok::kw__Decimal32:
2896 case tok::kw__Decimal64:
2897 case tok::kw__Decimal128:
2898 case tok::kw___vector:
2899
2900 // struct-or-union-specifier (C99) or class-specifier (C++)
2901 case tok::kw_class:
2902 case tok::kw_struct:
2903 case tok::kw_union:
2904 // enum-specifier
2905 case tok::kw_enum:
2906
2907 // typedef-name
2908 case tok::annot_typename:
2909 return true;
2910 }
2911}
2912
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002913/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002914/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002915bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002916 switch (Tok.getKind()) {
2917 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002918
Chris Lattner020bab92009-01-04 23:41:41 +00002919 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002920 if (TryAltiVecVectorToken())
2921 return true;
2922 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002923 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002924 // Annotate typenames and C++ scope specifiers. If we get one, just
2925 // recurse to handle whatever we get.
2926 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002927 return true;
2928 if (Tok.is(tok::identifier))
2929 return false;
2930 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002931
Chris Lattner020bab92009-01-04 23:41:41 +00002932 case tok::coloncolon: // ::foo::bar
2933 if (NextToken().is(tok::kw_new) || // ::new
2934 NextToken().is(tok::kw_delete)) // ::delete
2935 return false;
2936
Chris Lattner020bab92009-01-04 23:41:41 +00002937 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002938 return true;
2939 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002940
Chris Lattnere37e2332006-08-15 04:50:22 +00002941 // GNU attributes support.
2942 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002943 // GNU typeof support.
2944 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002945
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002946 // type-specifiers
2947 case tok::kw_short:
2948 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00002949 case tok::kw___int64:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002950 case tok::kw_signed:
2951 case tok::kw_unsigned:
2952 case tok::kw__Complex:
2953 case tok::kw__Imaginary:
2954 case tok::kw_void:
2955 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002956 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002957 case tok::kw_char16_t:
2958 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002959 case tok::kw_int:
2960 case tok::kw_float:
2961 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002962 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002963 case tok::kw__Bool:
2964 case tok::kw__Decimal32:
2965 case tok::kw__Decimal64:
2966 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002967 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002968
Chris Lattner861a2262008-04-13 18:59:07 +00002969 // struct-or-union-specifier (C99) or class-specifier (C++)
2970 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002971 case tok::kw_struct:
2972 case tok::kw_union:
2973 // enum-specifier
2974 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002975
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002976 // type-qualifier
2977 case tok::kw_const:
2978 case tok::kw_volatile:
2979 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002980
2981 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002982 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002983 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002984
Chris Lattner409bf7d2008-10-20 00:25:30 +00002985 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2986 case tok::less:
2987 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002988
Steve Naroff44ac7772008-12-25 14:16:32 +00002989 case tok::kw___cdecl:
2990 case tok::kw___stdcall:
2991 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002992 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002993 case tok::kw___w64:
2994 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002995 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002996 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00002997 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002998
2999 case tok::kw___private:
3000 case tok::kw___local:
3001 case tok::kw___global:
3002 case tok::kw___constant:
3003 case tok::kw___read_only:
3004 case tok::kw___read_write:
3005 case tok::kw___write_only:
3006
Eli Friedman53339e02009-06-08 23:27:34 +00003007 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003008
3009 case tok::kw_private:
3010 return getLang().OpenCL;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003011 }
3012}
3013
Chris Lattneracd58a32006-08-06 17:24:14 +00003014/// isDeclarationSpecifier() - Return true if the current token is part of a
3015/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003016///
3017/// \param DisambiguatingWithExpression True to indicate that the purpose of
3018/// this check is to disambiguate between an expression and a declaration.
3019bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003020 switch (Tok.getKind()) {
3021 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003022
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003023 case tok::kw_private:
3024 return getLang().OpenCL;
3025
Chris Lattner020bab92009-01-04 23:41:41 +00003026 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003027 // Unfortunate hack to support "Class.factoryMethod" notation.
3028 if (getLang().ObjC1 && NextToken().is(tok::period))
3029 return false;
John Thompson22334602010-02-05 00:12:22 +00003030 if (TryAltiVecVectorToken())
3031 return true;
3032 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003033 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003034 // Annotate typenames and C++ scope specifiers. If we get one, just
3035 // recurse to handle whatever we get.
3036 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003037 return true;
3038 if (Tok.is(tok::identifier))
3039 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003040
3041 // If we're in Objective-C and we have an Objective-C class type followed
3042 // by an identifier and then either ':' or ']', in a place where an
3043 // expression is permitted, then this is probably a class message send
3044 // missing the initial '['. In this case, we won't consider this to be
3045 // the start of a declaration.
3046 if (DisambiguatingWithExpression &&
3047 isStartOfObjCClassMessageMissingOpenBracket())
3048 return false;
3049
John McCall1f476a12010-02-26 08:45:28 +00003050 return isDeclarationSpecifier();
3051
Chris Lattner020bab92009-01-04 23:41:41 +00003052 case tok::coloncolon: // ::foo::bar
3053 if (NextToken().is(tok::kw_new) || // ::new
3054 NextToken().is(tok::kw_delete)) // ::delete
3055 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003056
Chris Lattner020bab92009-01-04 23:41:41 +00003057 // Annotate typenames and C++ scope specifiers. If we get one, just
3058 // recurse to handle whatever we get.
3059 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003060 return true;
3061 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003062
Chris Lattneracd58a32006-08-06 17:24:14 +00003063 // storage-class-specifier
3064 case tok::kw_typedef:
3065 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003066 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003067 case tok::kw_static:
3068 case tok::kw_auto:
3069 case tok::kw_register:
3070 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003071
Chris Lattneracd58a32006-08-06 17:24:14 +00003072 // type-specifiers
3073 case tok::kw_short:
3074 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003075 case tok::kw___int64:
Chris Lattneracd58a32006-08-06 17:24:14 +00003076 case tok::kw_signed:
3077 case tok::kw_unsigned:
3078 case tok::kw__Complex:
3079 case tok::kw__Imaginary:
3080 case tok::kw_void:
3081 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003082 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003083 case tok::kw_char16_t:
3084 case tok::kw_char32_t:
3085
Chris Lattneracd58a32006-08-06 17:24:14 +00003086 case tok::kw_int:
3087 case tok::kw_float:
3088 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003089 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003090 case tok::kw__Bool:
3091 case tok::kw__Decimal32:
3092 case tok::kw__Decimal64:
3093 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003094 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003095
Chris Lattner861a2262008-04-13 18:59:07 +00003096 // struct-or-union-specifier (C99) or class-specifier (C++)
3097 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003098 case tok::kw_struct:
3099 case tok::kw_union:
3100 // enum-specifier
3101 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003102
Chris Lattneracd58a32006-08-06 17:24:14 +00003103 // type-qualifier
3104 case tok::kw_const:
3105 case tok::kw_volatile:
3106 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003107
Chris Lattneracd58a32006-08-06 17:24:14 +00003108 // function-specifier
3109 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003110 case tok::kw_virtual:
3111 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003112
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003113 // static_assert-declaration
3114 case tok::kw__Static_assert:
3115
Chris Lattner599e47e2007-08-09 17:01:07 +00003116 // GNU typeof support.
3117 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003118
Chris Lattner599e47e2007-08-09 17:01:07 +00003119 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003120 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00003121 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003122
Francois Pichete878cb62011-06-19 08:02:06 +00003123 // C++0x decltype.
3124 case tok::kw_decltype:
3125 return true;
3126
Chris Lattner8b2ec162008-07-26 03:38:44 +00003127 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3128 case tok::less:
3129 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003130
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003131 // typedef-name
3132 case tok::annot_typename:
3133 return !DisambiguatingWithExpression ||
3134 !isStartOfObjCClassMessageMissingOpenBracket();
3135
Steve Narofff192fab2009-01-06 19:34:12 +00003136 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003137 case tok::kw___cdecl:
3138 case tok::kw___stdcall:
3139 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003140 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003141 case tok::kw___w64:
3142 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003143 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003144 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003145 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003146 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003147
3148 case tok::kw___private:
3149 case tok::kw___local:
3150 case tok::kw___global:
3151 case tok::kw___constant:
3152 case tok::kw___read_only:
3153 case tok::kw___read_write:
3154 case tok::kw___write_only:
3155
Eli Friedman53339e02009-06-08 23:27:34 +00003156 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003157 }
3158}
3159
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003160bool Parser::isConstructorDeclarator() {
3161 TentativeParsingAction TPA(*this);
3162
3163 // Parse the C++ scope specifier.
3164 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003165 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00003166 TPA.Revert();
3167 return false;
3168 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003169
3170 // Parse the constructor name.
3171 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3172 // We already know that we have a constructor name; just consume
3173 // the token.
3174 ConsumeToken();
3175 } else {
3176 TPA.Revert();
3177 return false;
3178 }
3179
3180 // Current class name must be followed by a left parentheses.
3181 if (Tok.isNot(tok::l_paren)) {
3182 TPA.Revert();
3183 return false;
3184 }
3185 ConsumeParen();
3186
3187 // A right parentheses or ellipsis signals that we have a constructor.
3188 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
3189 TPA.Revert();
3190 return true;
3191 }
3192
3193 // If we need to, enter the specified scope.
3194 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003195 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003196 DeclScopeObj.EnterDeclaratorScope();
3197
Francois Pichet79f3a872011-01-31 04:54:32 +00003198 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003199 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00003200 MaybeParseMicrosoftAttributes(Attrs);
3201
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003202 // Check whether the next token(s) are part of a declaration
3203 // specifier, in which case we have the start of a parameter and,
3204 // therefore, we know that this is a constructor.
3205 bool IsConstructor = isDeclarationSpecifier();
3206 TPA.Revert();
3207 return IsConstructor;
3208}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003209
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003210/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00003211/// type-qualifier-list: [C99 6.7.5]
3212/// type-qualifier
3213/// [vendor] attributes
3214/// [ only if VendorAttributesAllowed=true ]
3215/// type-qualifier-list type-qualifier
3216/// [vendor] type-qualifier-list attributes
3217/// [ only if VendorAttributesAllowed=true ]
3218/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3219/// [ only if CXX0XAttributesAllowed=true ]
3220/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003221///
Dawn Perchik335e16b2010-09-03 01:29:35 +00003222void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3223 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00003224 bool CXX0XAttributesAllowed) {
3225 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3226 SourceLocation Loc = Tok.getLocation();
John McCall084e83d2011-03-24 11:26:52 +00003227 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003228 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003229 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00003230 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003231 else
3232 Diag(Loc, diag::err_attributes_not_allowed);
3233 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003234
3235 SourceLocation EndLoc;
3236
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003237 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00003238 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003239 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003240 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00003241 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003242
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003243 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00003244 case tok::code_completion:
3245 Actions.CodeCompleteTypeQualifiers(DS);
3246 ConsumeCodeCompletionToken();
3247 break;
3248
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003249 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003250 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
3251 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003252 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003253 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003254 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3255 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003256 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003257 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003258 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3259 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003260 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003261
3262 // OpenCL qualifiers:
3263 case tok::kw_private:
3264 if (!getLang().OpenCL)
3265 goto DoneWithTypeQuals;
3266 case tok::kw___private:
3267 case tok::kw___global:
3268 case tok::kw___local:
3269 case tok::kw___constant:
3270 case tok::kw___read_only:
3271 case tok::kw___write_only:
3272 case tok::kw___read_write:
3273 ParseOpenCLQualifiers(DS);
3274 break;
3275
Eli Friedman53339e02009-06-08 23:27:34 +00003276 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00003277 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003278 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00003279 case tok::kw___cdecl:
3280 case tok::kw___stdcall:
3281 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003282 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00003283 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003284 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003285 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00003286 continue;
3287 }
3288 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00003289 case tok::kw___pascal:
3290 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003291 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00003292 continue;
3293 }
3294 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00003295 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003296 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00003297 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00003298 continue; // do *not* consume the next token!
3299 }
3300 // otherwise, FALL THROUGH!
3301 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00003302 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00003303 // If this is not a type-qualifier token, we're done reading type
3304 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00003305 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003306 if (EndLoc.isValid())
3307 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00003308 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003309 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003310
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003311 // If the specifier combination wasn't legal, issue a diagnostic.
3312 if (isInvalid) {
3313 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00003314 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00003315 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00003316 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003317 }
3318}
3319
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003320
3321/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3322///
3323void Parser::ParseDeclarator(Declarator &D) {
3324 /// This implements the 'declarator' production in the C grammar, then checks
3325 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003326 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003327}
3328
Sebastian Redlbd150f42008-11-21 19:14:01 +00003329/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3330/// is parsed by the function passed to it. Pass null, and the direct-declarator
3331/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003332/// ptr-operator production.
3333///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003334/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3335/// [C] pointer[opt] direct-declarator
3336/// [C++] direct-declarator
3337/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00003338///
3339/// pointer: [C99 6.7.5]
3340/// '*' type-qualifier-list[opt]
3341/// '*' type-qualifier-list[opt] pointer
3342///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003343/// ptr-operator:
3344/// '*' cv-qualifier-seq[opt]
3345/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00003346/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003347/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00003348/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003349/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00003350void Parser::ParseDeclaratorInternal(Declarator &D,
3351 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00003352 if (Diags.hasAllExtensionsSilenced())
3353 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00003354
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003355 // C++ member pointers start with a '::' or a nested-name.
3356 // Member pointers get special handling, since there's no place for the
3357 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00003358 if (getLang().CPlusPlus &&
3359 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3360 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003361 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00003362 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00003363
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00003364 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00003365 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003366 // The scope spec really belongs to the direct-declarator.
3367 D.getCXXScopeSpec() = SS;
3368 if (DirectDeclParser)
3369 (this->*DirectDeclParser)(D);
3370 return;
3371 }
3372
3373 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003374 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00003375 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003376 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003377 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003378
3379 // Recurse to parse whatever is left.
3380 ParseDeclaratorInternal(D, DirectDeclParser);
3381
3382 // Sema will have to catch (syntactically invalid) pointers into global
3383 // scope. It has to catch pointers into namespace scope anyway.
3384 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003385 Loc),
3386 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003387 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003388 return;
3389 }
3390 }
3391
3392 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00003393 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00003394 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00003395 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00003396 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00003397 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00003398 if (DirectDeclParser)
3399 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003400 return;
3401 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003402
Sebastian Redled0f3b02009-03-15 22:02:01 +00003403 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3404 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00003405 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003406 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00003407
Chris Lattner9eac9312009-03-27 04:18:06 +00003408 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00003409 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00003410 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003411
Bill Wendling3708c182007-05-27 10:15:43 +00003412 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003413 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00003414
Bill Wendling3708c182007-05-27 10:15:43 +00003415 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003416 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00003417 if (Kind == tok::star)
3418 // Remember that we parsed a pointer type, and remember the type-quals.
3419 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00003420 DS.getConstSpecLoc(),
3421 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00003422 DS.getRestrictSpecLoc()),
3423 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003424 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00003425 else
3426 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00003427 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00003428 Loc),
3429 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003430 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003431 } else {
3432 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00003433 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00003434
Sebastian Redl3b27be62009-03-23 00:00:23 +00003435 // Complain about rvalue references in C++03, but then go on and build
3436 // the declarator.
3437 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00003438 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00003439
Bill Wendling93efb222007-06-02 23:28:54 +00003440 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3441 // cv-qualifiers are introduced through the use of a typedef or of a
3442 // template type argument, in which case the cv-qualifiers are ignored.
3443 //
3444 // [GNU] Retricted references are allowed.
3445 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00003446 // [C++0x] Attributes on references are not allowed.
3447 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003448 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00003449
3450 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3451 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3452 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003453 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00003454 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3455 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00003456 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00003457 }
Bill Wendling3708c182007-05-27 10:15:43 +00003458
3459 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00003460 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00003461
Douglas Gregor66583c52008-11-03 15:51:28 +00003462 if (D.getNumTypeObjects() > 0) {
3463 // C++ [dcl.ref]p4: There shall be no references to references.
3464 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
3465 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003466 if (const IdentifierInfo *II = D.getIdentifier())
3467 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3468 << II;
3469 else
3470 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
3471 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00003472
Sebastian Redlbd150f42008-11-21 19:14:01 +00003473 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00003474 // can go ahead and build the (technically ill-formed)
3475 // declarator: reference collapsing will take care of it.
3476 }
3477 }
3478
Bill Wendling3708c182007-05-27 10:15:43 +00003479 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00003480 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00003481 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00003482 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003483 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00003484 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00003485}
3486
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003487/// ParseDirectDeclarator
3488/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00003489/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003490/// '(' declarator ')'
3491/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00003492/// [C90] direct-declarator '[' constant-expression[opt] ']'
3493/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3494/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3495/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3496/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003497/// direct-declarator '(' parameter-type-list ')'
3498/// direct-declarator '(' identifier-list[opt] ')'
3499/// [GNU] direct-declarator '(' parameter-forward-declarations
3500/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003501/// [C++] direct-declarator '(' parameter-declaration-clause ')'
3502/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00003503/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003504///
3505/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00003506/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00003507/// '::'[opt] nested-name-specifier[opt] type-name
3508///
3509/// id-expression: [C++ 5.1]
3510/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003511/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00003512///
3513/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00003514/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003515/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00003516/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00003517/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00003518/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00003519///
Chris Lattneracd58a32006-08-06 17:24:14 +00003520void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003521 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003522
Douglas Gregor7861a802009-11-03 01:35:08 +00003523 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
3524 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003525 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00003526 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00003527 }
3528
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003529 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00003530 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00003531 // Change the declaration context for name lookup, until this function
3532 // is exited (and the declarator has been parsed).
3533 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003534 }
3535
Douglas Gregor27b4c162010-12-23 22:44:42 +00003536 // C++0x [dcl.fct]p14:
3537 // There is a syntactic ambiguity when an ellipsis occurs at the end
3538 // of a parameter-declaration-clause without a preceding comma. In
3539 // this case, the ellipsis is parsed as part of the
3540 // abstract-declarator if the type of the parameter names a template
3541 // parameter pack that has not been expanded; otherwise, it is parsed
3542 // as part of the parameter-declaration-clause.
3543 if (Tok.is(tok::ellipsis) &&
3544 !((D.getContext() == Declarator::PrototypeContext ||
3545 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00003546 NextToken().is(tok::r_paren) &&
3547 !Actions.containsUnexpandedParameterPacks(D)))
3548 D.setEllipsisLoc(ConsumeToken());
3549
Douglas Gregor7861a802009-11-03 01:35:08 +00003550 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
3551 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
3552 // We found something that indicates the start of an unqualified-id.
3553 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00003554 bool AllowConstructorName;
3555 if (D.getDeclSpec().hasTypeSpecifier())
3556 AllowConstructorName = false;
3557 else if (D.getCXXScopeSpec().isSet())
3558 AllowConstructorName =
3559 (D.getContext() == Declarator::FileContext ||
3560 (D.getContext() == Declarator::MemberContext &&
3561 D.getDeclSpec().isFriendSpecified()));
3562 else
3563 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
3564
Douglas Gregor7861a802009-11-03 01:35:08 +00003565 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
3566 /*EnteringContext=*/true,
3567 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003568 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00003569 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00003570 D.getName()) ||
3571 // Once we're past the identifier, if the scope was bad, mark the
3572 // whole declarator bad.
3573 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003574 D.SetIdentifier(0, Tok.getLocation());
3575 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00003576 } else {
3577 // Parsed the unqualified-id; update range information and move along.
3578 if (D.getSourceRange().getBegin().isInvalid())
3579 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
3580 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00003581 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003582 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00003583 }
Douglas Gregor7861a802009-11-03 01:35:08 +00003584 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003585 assert(!getLang().CPlusPlus &&
3586 "There's a C++-specific check for tok::identifier above");
3587 assert(Tok.getIdentifierInfo() && "Not an identifier?");
3588 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
3589 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00003590 goto PastIdentifier;
3591 }
3592
3593 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003594 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00003595 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00003596 // Example: 'char (*X)' or 'int (*XX)(void)'
3597 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003598
3599 // If the declarator was parenthesized, we entered the declarator
3600 // scope when parsing the parenthesized declarator, then exited
3601 // the scope already. Re-enter the scope, if we need to.
3602 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003603 // If there was an error parsing parenthesized declarator, declarator
3604 // scope may have been enterred before. Don't do it again.
3605 if (!D.isInvalidType() &&
3606 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003607 // Change the declaration context for name lookup, until this function
3608 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00003609 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003610 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003611 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003612 // This could be something simple like "int" (in which case the declarator
3613 // portion is empty), if an abstract-declarator is allowed.
3614 D.SetIdentifier(0, Tok.getLocation());
3615 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00003616 if (D.getContext() == Declarator::MemberContext)
3617 Diag(Tok, diag::err_expected_member_name_or_semi)
3618 << D.getDeclSpec().getSourceRange();
3619 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00003620 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003621 else
Chris Lattner6d29c102008-11-18 07:48:38 +00003622 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00003623 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00003624 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00003625 }
Mike Stump11289f42009-09-09 15:08:12 +00003626
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00003627 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00003628 assert(D.isPastIdentifier() &&
3629 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00003630
Alexis Hunt96d5c762009-11-21 08:43:09 +00003631 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00003632 if (D.getIdentifier())
3633 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003634
Chris Lattneracd58a32006-08-06 17:24:14 +00003635 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00003636 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003637 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
3638 // In such a case, check if we actually have a function declarator; if it
3639 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003640 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
3641 // When not in file scope, warn for ambiguous function declarators, just
3642 // in case the author intended it as a variable definition.
3643 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
3644 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
3645 break;
3646 }
John McCall084e83d2011-03-24 11:26:52 +00003647 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003648 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00003649 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00003650 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00003651 } else {
3652 break;
3653 }
3654 }
3655}
3656
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003657/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3658/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00003659/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003660/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3661///
3662/// direct-declarator:
3663/// '(' declarator ')'
3664/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003665/// direct-declarator '(' parameter-type-list ')'
3666/// direct-declarator '(' identifier-list[opt] ')'
3667/// [GNU] direct-declarator '(' parameter-forward-declarations
3668/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003669///
3670void Parser::ParseParenDeclarator(Declarator &D) {
3671 SourceLocation StartLoc = ConsumeParen();
3672 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003673
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003674 // Eat any attributes before we look at whether this is a grouping or function
3675 // declarator paren. If this is a grouping paren, the attribute applies to
3676 // the type being built up, for example:
3677 // int (__attribute__(()) *x)(long y)
3678 // If this ends up not being a grouping paren, the attribute applies to the
3679 // first argument, for example:
3680 // int (__attribute__(()) int x)
3681 // In either case, we need to eat any attributes to be able to determine what
3682 // sort of paren this is.
3683 //
John McCall084e83d2011-03-24 11:26:52 +00003684 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003685 bool RequiresArg = false;
3686 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003687 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003688
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003689 // We require that the argument list (if this is a non-grouping paren) be
3690 // present even if the attribute list was empty.
3691 RequiresArg = true;
3692 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003693 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003694 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003695 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet17ed0202011-08-18 09:59:55 +00003696 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichetf2fb4112011-08-25 00:36:46 +00003697 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall53fa7142010-12-24 02:08:15 +00003698 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003699 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003700 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003701 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003702 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003703
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003704 // If we haven't past the identifier yet (or where the identifier would be
3705 // stored, if this is an abstract declarator), then this is probably just
3706 // grouping parens. However, if this could be an abstract-declarator, then
3707 // this could also be the start of function arguments (consider 'void()').
3708 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003709
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003710 if (!D.mayOmitIdentifier()) {
3711 // If this can't be an abstract-declarator, this *must* be a grouping
3712 // paren, because we haven't seen the identifier yet.
3713 isGrouping = true;
3714 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003715 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003716 isDeclarationSpecifier()) { // 'int(int)' is a function.
3717 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3718 // considered to be a type, not a K&R identifier-list.
3719 isGrouping = false;
3720 } else {
3721 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3722 isGrouping = true;
3723 }
Mike Stump11289f42009-09-09 15:08:12 +00003724
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003725 // If this is a grouping paren, handle:
3726 // direct-declarator: '(' declarator ')'
3727 // direct-declarator: '(' attributes declarator ')'
3728 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003729 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003730 D.setGroupingParens(true);
3731
Sebastian Redlbd150f42008-11-21 19:14:01 +00003732 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003733 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003734 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00003735 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc),
3736 attrs, EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003737
3738 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003739 return;
3740 }
Mike Stump11289f42009-09-09 15:08:12 +00003741
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003742 // Okay, if this wasn't a grouping paren, it must be the start of a function
3743 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003744 // identifier (and remember where it would have been), then call into
3745 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003746 D.SetIdentifier(0, Tok.getLocation());
3747
John McCall53fa7142010-12-24 02:08:15 +00003748 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003749}
3750
3751/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3752/// declarator D up to a paren, which indicates that we are parsing function
3753/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003754///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003755/// If attrs is non-null, then the caller parsed those arguments immediately
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003756/// after the open paren - they should be considered to be the first argument of
3757/// a parameter. If RequiresArg is true, then the first argument of the
3758/// function is required to be present and required to not be an identifier
3759/// list.
3760///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003761/// For C++, after the parameter-list, it also parses cv-qualifier-seq[opt],
3762/// (C++0x) ref-qualifier[opt], exception-specification[opt], and
3763/// (C++0x) trailing-return-type[opt].
3764///
3765/// [C++0x] exception-specification:
3766/// dynamic-exception-specification
3767/// noexcept-specification
3768///
3769void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3770 ParsedAttributes &attrs,
3771 bool RequiresArg) {
3772 // lparen is already consumed!
3773 assert(D.isPastIdentifier() && "Should not call before identifier!");
3774
3775 // This should be true when the function has typed arguments.
3776 // Otherwise, it is treated as a K&R-style function.
3777 bool HasProto = false;
3778 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003779 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00003780 // Remember where we see an ellipsis, if any.
3781 SourceLocation EllipsisLoc;
3782
3783 DeclSpec DS(AttrFactory);
3784 bool RefQualifierIsLValueRef = true;
3785 SourceLocation RefQualifierLoc;
3786 ExceptionSpecificationType ESpecType = EST_None;
3787 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003788 SmallVector<ParsedType, 2> DynamicExceptions;
3789 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00003790 ExprResult NoexceptExpr;
3791 ParsedType TrailingReturnType;
3792
3793 SourceLocation EndLoc;
3794
3795 if (isFunctionDeclaratorIdentifierList()) {
3796 if (RequiresArg)
3797 Diag(Tok, diag::err_argument_required_after_attribute);
3798
3799 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
3800
3801 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3802 } else {
3803 // Enter function-declaration scope, limiting any declarators to the
3804 // function prototype scope, including parameter declarators.
3805 ParseScope PrototypeScope(this,
3806 Scope::FunctionPrototypeScope|Scope::DeclScope);
3807
3808 if (Tok.isNot(tok::r_paren))
3809 ParseParameterDeclarationClause(D, attrs, ParamInfo, EllipsisLoc);
3810 else if (RequiresArg)
3811 Diag(Tok, diag::err_argument_required_after_attribute);
3812
3813 HasProto = ParamInfo.size() || getLang().CPlusPlus;
3814
3815 // If we have the closing ')', eat it.
3816 EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3817
3818 if (getLang().CPlusPlus) {
3819 MaybeParseCXX0XAttributes(attrs);
3820
3821 // Parse cv-qualifier-seq[opt].
3822 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
3823 if (!DS.getSourceRange().getEnd().isInvalid())
3824 EndLoc = DS.getSourceRange().getEnd();
3825
3826 // Parse ref-qualifier[opt].
3827 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3828 if (!getLang().CPlusPlus0x)
3829 Diag(Tok, diag::ext_ref_qualifier);
3830
3831 RefQualifierIsLValueRef = Tok.is(tok::amp);
3832 RefQualifierLoc = ConsumeToken();
3833 EndLoc = RefQualifierLoc;
3834 }
3835
3836 // Parse exception-specification[opt].
3837 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3838 DynamicExceptions,
3839 DynamicExceptionRanges,
3840 NoexceptExpr);
3841 if (ESpecType != EST_None)
3842 EndLoc = ESpecRange.getEnd();
3843
3844 // Parse trailing-return-type[opt].
3845 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
Douglas Gregordb0b9f12011-08-04 15:30:47 +00003846 SourceRange Range;
3847 TrailingReturnType = ParseTrailingReturnType(Range).get();
3848 if (Range.getEnd().isValid())
3849 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00003850 }
3851 }
3852
3853 // Leave prototype scope.
3854 PrototypeScope.Exit();
3855 }
3856
3857 // Remember that we parsed a function type, and remember the attributes.
3858 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
3859 /*isVariadic=*/EllipsisLoc.isValid(),
3860 EllipsisLoc,
3861 ParamInfo.data(), ParamInfo.size(),
3862 DS.getTypeQualifiers(),
3863 RefQualifierIsLValueRef,
3864 RefQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00003865 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00003866 ESpecType, ESpecRange.getBegin(),
3867 DynamicExceptions.data(),
3868 DynamicExceptionRanges.data(),
3869 DynamicExceptions.size(),
3870 NoexceptExpr.isUsable() ?
3871 NoexceptExpr.get() : 0,
3872 LParenLoc, EndLoc, D,
3873 TrailingReturnType),
3874 attrs, EndLoc);
3875}
3876
3877/// isFunctionDeclaratorIdentifierList - This parameter list may have an
3878/// identifier list form for a K&R-style function: void foo(a,b,c)
3879///
3880/// Note that identifier-lists are only allowed for normal declarators, not for
3881/// abstract-declarators.
3882bool Parser::isFunctionDeclaratorIdentifierList() {
3883 return !getLang().CPlusPlus
3884 && Tok.is(tok::identifier)
3885 && !TryAltiVecVectorToken()
3886 // K&R identifier lists can't have typedefs as identifiers, per C99
3887 // 6.7.5.3p11.
3888 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
3889 // Identifier lists follow a really simple grammar: the identifiers can
3890 // be followed *only* by a ", identifier" or ")". However, K&R
3891 // identifier lists are really rare in the brave new modern world, and
3892 // it is very common for someone to typo a type in a non-K&R style
3893 // list. If we are presented with something like: "void foo(intptr x,
3894 // float y)", we don't want to start parsing the function declarator as
3895 // though it is a K&R style declarator just because intptr is an
3896 // invalid type.
3897 //
3898 // To handle this, we check to see if the token after the first
3899 // identifier is a "," or ")". Only then do we parse it as an
3900 // identifier list.
3901 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
3902}
3903
3904/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3905/// we found a K&R-style identifier list instead of a typed parameter list.
3906///
3907/// After returning, ParamInfo will hold the parsed parameters.
3908///
3909/// identifier-list: [C99 6.7.5]
3910/// identifier
3911/// identifier-list ',' identifier
3912///
3913void Parser::ParseFunctionDeclaratorIdentifierList(
3914 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003915 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00003916 // If there was no identifier specified for the declarator, either we are in
3917 // an abstract-declarator, or we are in a parameter declarator which was found
3918 // to be abstract. In abstract-declarators, identifier lists are not valid:
3919 // diagnose this.
3920 if (!D.getIdentifier())
3921 Diag(Tok, diag::ext_ident_list_in_param);
3922
3923 // Maintain an efficient lookup of params we have seen so far.
3924 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
3925
3926 while (1) {
3927 // If this isn't an identifier, report the error and skip until ')'.
3928 if (Tok.isNot(tok::identifier)) {
3929 Diag(Tok, diag::err_expected_ident);
3930 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
3931 // Forget we parsed anything.
3932 ParamInfo.clear();
3933 return;
3934 }
3935
3936 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
3937
3938 // Reject 'typedef int y; int test(x, y)', but continue parsing.
3939 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
3940 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
3941
3942 // Verify that the argument identifier has not already been mentioned.
3943 if (!ParamsSoFar.insert(ParmII)) {
3944 Diag(Tok, diag::err_param_redefinition) << ParmII;
3945 } else {
3946 // Remember this identifier in ParamInfo.
3947 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3948 Tok.getLocation(),
3949 0));
3950 }
3951
3952 // Eat the identifier.
3953 ConsumeToken();
3954
3955 // The list continues if we see a comma.
3956 if (Tok.isNot(tok::comma))
3957 break;
3958 ConsumeToken();
3959 }
3960}
3961
3962/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
3963/// after the opening parenthesis. This function will not parse a K&R-style
3964/// identifier list.
3965///
3966/// D is the declarator being parsed. If attrs is non-null, then the caller
3967/// parsed those arguments immediately after the open paren - they should be
3968/// considered to be the first argument of a parameter.
3969///
3970/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
3971/// be the location of the ellipsis, if any was parsed.
3972///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003973/// parameter-type-list: [C99 6.7.5]
3974/// parameter-list
3975/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003976/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003977///
3978/// parameter-list: [C99 6.7.5]
3979/// parameter-declaration
3980/// parameter-list ',' parameter-declaration
3981///
3982/// parameter-declaration: [C99 6.7.5]
3983/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003984/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003985/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003986/// declaration-specifiers abstract-declarator[opt]
3987/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003988/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003989/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003990///
Douglas Gregor9e66af42011-07-05 16:44:18 +00003991void Parser::ParseParameterDeclarationClause(
3992 Declarator &D,
3993 ParsedAttributes &attrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003994 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00003995 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00003996
Chris Lattner371ed4e2008-04-06 06:57:35 +00003997 while (1) {
3998 if (Tok.is(tok::ellipsis)) {
Douglas Gregor94349fd2009-02-18 07:07:28 +00003999 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004000 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004001 }
Mike Stump11289f42009-09-09 15:08:12 +00004002
Chris Lattner371ed4e2008-04-06 06:57:35 +00004003 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004004 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004005 DeclSpec DS(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004006
4007 // Skip any Microsoft attributes before a param.
4008 if (getLang().Microsoft && Tok.is(tok::l_square))
4009 ParseMicrosoftAttributes(DS.getAttributes());
4010
4011 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004012
4013 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004014 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004015 // FIXME: If we saw an ellipsis first, this code is not reached. Are the
4016 // attributes lost? Should they even be allowed?
4017 // FIXME: If we can leave the attributes in the token stream somehow, we can
4018 // get rid of a parameter (attrs) and this statement. It might be too much
4019 // hassle.
John McCall53fa7142010-12-24 02:08:15 +00004020 DS.takeAttributesFrom(attrs);
4021
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004022 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00004023
Chris Lattner371ed4e2008-04-06 06:57:35 +00004024 // Parse the declarator. This is "PrototypeContext", because we must
4025 // accept either 'declarator' or 'abstract-declarator' here.
4026 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4027 ParseDeclarator(ParmDecl);
4028
4029 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00004030 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00004031
Chris Lattner371ed4e2008-04-06 06:57:35 +00004032 // Remember this parsed parameter in ParamInfo.
4033 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00004034
Douglas Gregor4d87df52008-12-16 21:30:33 +00004035 // DefArgToks is used when the parsing of default arguments needs
4036 // to be delayed.
4037 CachedTokens *DefArgToks = 0;
4038
Chris Lattner371ed4e2008-04-06 06:57:35 +00004039 // If no parameter was specified, verify that *something* was specified,
4040 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00004041 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4042 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00004043 // Completely missing, emit error.
4044 Diag(DSStart, diag::err_missing_param);
4045 } else {
4046 // Otherwise, we have something. Add it and let semantic analysis try
4047 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00004048
Chris Lattner371ed4e2008-04-06 06:57:35 +00004049 // Inform the actions module about the parameter declarator, so it gets
4050 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00004051 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004052
4053 // Parse the default argument, if any. We parse the default
4054 // arguments in all dialects; the semantic analysis in
4055 // ActOnParamDefaultArgument will reject the default argument in
4056 // C.
4057 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00004058 SourceLocation EqualLoc = Tok.getLocation();
4059
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004060 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00004061 if (D.getContext() == Declarator::MemberContext) {
4062 // If we're inside a class definition, cache the tokens
4063 // corresponding to the default argument. We'll actually parse
4064 // them when we see the end of the class definition.
4065 // FIXME: Templates will require something similar.
4066 // FIXME: Can we use a smart pointer for Toks?
4067 DefArgToks = new CachedTokens;
4068
Mike Stump11289f42009-09-09 15:08:12 +00004069 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00004070 /*StopAtSemi=*/true,
4071 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004072 delete DefArgToks;
4073 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00004074 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004075 } else {
4076 // Mark the end of the default argument so that we know when to
4077 // stop when we parse it later on.
4078 Token DefArgEnd;
4079 DefArgEnd.startToken();
4080 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4081 DefArgEnd.setLocation(Tok.getLocation());
4082 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00004083 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00004084 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00004085 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004086 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00004087 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00004088 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004089
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00004090 // The argument isn't actually potentially evaluated unless it is
4091 // used.
4092 EnterExpressionEvaluationContext Eval(Actions,
4093 Sema::PotentiallyEvaluatedIfUsed);
4094
John McCalldadc5752010-08-24 06:29:42 +00004095 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004096 if (DefArgResult.isInvalid()) {
4097 Actions.ActOnParamDefaultArgumentError(Param);
4098 SkipUntil(tok::comma, tok::r_paren, true, true);
4099 } else {
4100 // Inform the actions module about the default argument
4101 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00004102 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00004103 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004104 }
4105 }
Mike Stump11289f42009-09-09 15:08:12 +00004106
4107 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4108 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00004109 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00004110 }
4111
4112 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004113 if (Tok.isNot(tok::comma)) {
4114 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004115 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4116
4117 if (!getLang().CPlusPlus) {
4118 // We have ellipsis without a preceding ',', which is ill-formed
4119 // in C. Complain and provide the fix.
4120 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00004121 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004122 }
4123 }
4124
4125 break;
4126 }
Mike Stump11289f42009-09-09 15:08:12 +00004127
Chris Lattner371ed4e2008-04-06 06:57:35 +00004128 // Consume the comma.
4129 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00004130 }
Mike Stump11289f42009-09-09 15:08:12 +00004131
Chris Lattner6c940e62008-04-06 06:34:08 +00004132}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004133
Chris Lattnere8074e62006-08-06 18:30:15 +00004134/// [C90] direct-declarator '[' constant-expression[opt] ']'
4135/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4136/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4137/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4138/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
4139void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00004140 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00004141
Chris Lattner84a11622008-12-18 07:27:21 +00004142 // C array syntax has many features, but by-far the most common is [] and [4].
4143 // This code does a fast path to handle some of the most obvious cases.
4144 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004145 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004146 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004147 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004148
Chris Lattner84a11622008-12-18 07:27:21 +00004149 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00004150 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00004151 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00004152 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004153 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004154 return;
4155 } else if (Tok.getKind() == tok::numeric_constant &&
4156 GetLookAheadToken(1).is(tok::r_square)) {
4157 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00004158 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00004159 ConsumeToken();
4160
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004161 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall084e83d2011-03-24 11:26:52 +00004162 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004163 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004164
Chris Lattner84a11622008-12-18 07:27:21 +00004165 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004166 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall53fa7142010-12-24 02:08:15 +00004167 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00004168 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004169 attrs, EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00004170 return;
4171 }
Mike Stump11289f42009-09-09 15:08:12 +00004172
Chris Lattnere8074e62006-08-06 18:30:15 +00004173 // If valid, this location is the position where we read the 'static' keyword.
4174 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00004175 if (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 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004179 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00004180 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004181 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00004182
Chris Lattnere8074e62006-08-06 18:30:15 +00004183 // If we haven't already read 'static', check to see if there is one after the
4184 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00004185 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00004186 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00004187
Chris Lattnere8074e62006-08-06 18:30:15 +00004188 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00004189 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00004190 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00004191
Chris Lattner521ff2b2008-04-06 05:26:30 +00004192 // Handle the case where we have '[*]' as the array size. However, a leading
4193 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4194 // the the token after the star is a ']'. Since stars in arrays are
4195 // infrequent, use of lookahead is not costly here.
4196 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00004197 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00004198
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004199 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00004200 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004201 StaticLoc = SourceLocation(); // Drop the static.
4202 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00004203 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00004204 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00004205 // Note, in C89, this production uses the constant-expr production instead
4206 // of assignment-expr. The only difference is that assignment-expr allows
4207 // things like '=' and '*='. Sema rejects these in C89 mode because they
4208 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00004209
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00004210 // Parse the constant-expression or assignment-expression now (depending
4211 // on dialect).
4212 if (getLang().CPlusPlus)
4213 NumElements = ParseConstantExpression();
4214 else
4215 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00004216 }
Mike Stump11289f42009-09-09 15:08:12 +00004217
Chris Lattner62591722006-08-12 18:40:58 +00004218 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00004219 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00004220 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00004221 // If the expression was invalid, skip it.
4222 SkipUntil(tok::r_square);
4223 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00004224 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004225
4226 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
4227
John McCall084e83d2011-03-24 11:26:52 +00004228 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00004229 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004230
Chris Lattner84a11622008-12-18 07:27:21 +00004231 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00004232 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00004233 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00004234 NumElements.release(),
4235 StartLoc, EndLoc),
John McCall084e83d2011-03-24 11:26:52 +00004236 attrs, EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00004237}
4238
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004239/// [GNU] typeof-specifier:
4240/// typeof ( expressions )
4241/// typeof ( type-name )
4242/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00004243///
4244void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00004245 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004246 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00004247 SourceLocation StartLoc = ConsumeToken();
4248
John McCalle8595032010-01-13 20:03:27 +00004249 const bool hasParens = Tok.is(tok::l_paren);
4250
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004251 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00004252 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004253 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00004254 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4255 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00004256 if (hasParens)
4257 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004258
4259 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004260 // FIXME: Not accurate, the range gets one token more than it should.
4261 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004262 else
4263 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00004264
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004265 if (isCastExpr) {
4266 if (!CastTy) {
4267 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004268 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00004269 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004270
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004271 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004272 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004273 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4274 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00004275 DiagID, CastTy))
4276 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00004277 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004278 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004279
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004280 // If we get here, the operand to the typeof was an expresion.
4281 if (Operand.isInvalid()) {
4282 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00004283 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00004284 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00004285
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004286 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004287 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00004288 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4289 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00004290 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00004291 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00004292}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004293
4294
4295/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4296/// from TryAltiVecVectorToken.
4297bool Parser::TryAltiVecVectorTokenOutOfLine() {
4298 Token Next = NextToken();
4299 switch (Next.getKind()) {
4300 default: return false;
4301 case tok::kw_short:
4302 case tok::kw_long:
4303 case tok::kw_signed:
4304 case tok::kw_unsigned:
4305 case tok::kw_void:
4306 case tok::kw_char:
4307 case tok::kw_int:
4308 case tok::kw_float:
4309 case tok::kw_double:
4310 case tok::kw_bool:
4311 case tok::kw___pixel:
4312 Tok.setKind(tok::kw___vector);
4313 return true;
4314 case tok::identifier:
4315 if (Next.getIdentifierInfo() == Ident_pixel) {
4316 Tok.setKind(tok::kw___vector);
4317 return true;
4318 }
4319 return false;
4320 }
4321}
4322
4323bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
4324 const char *&PrevSpec, unsigned &DiagID,
4325 bool &isInvalid) {
4326 if (Tok.getIdentifierInfo() == Ident_vector) {
4327 Token Next = NextToken();
4328 switch (Next.getKind()) {
4329 case tok::kw_short:
4330 case tok::kw_long:
4331 case tok::kw_signed:
4332 case tok::kw_unsigned:
4333 case tok::kw_void:
4334 case tok::kw_char:
4335 case tok::kw_int:
4336 case tok::kw_float:
4337 case tok::kw_double:
4338 case tok::kw_bool:
4339 case tok::kw___pixel:
4340 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4341 return true;
4342 case tok::identifier:
4343 if (Next.getIdentifierInfo() == Ident_pixel) {
4344 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
4345 return true;
4346 }
4347 break;
4348 default:
4349 break;
4350 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00004351 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00004352 DS.isTypeAltiVecVector()) {
4353 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
4354 return true;
4355 }
4356 return false;
4357}