blob: 5a7fc7e72d06d0df46103578f227f7ec15ca921e [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"
John McCall8b0666c2010-08-20 18:27:03 +000016#include "clang/Sema/Scope.h"
17#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000019#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000020#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// C99 6.7: Declarations.
25//===----------------------------------------------------------------------===//
26
Chris Lattnerf5fbd792006-08-10 23:56:11 +000027/// ParseTypeName
28/// type-name: [C99 6.7.6]
29/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000030///
31/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000032TypeResult Parser::ParseTypeName(SourceRange *Range,
33 Declarator::TheContext Context) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000034 // Parse the common declaration-specifiers piece.
35 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000036 ParseSpecifierQualifierList(DS);
Sebastian Redld6434562009-05-29 18:02:33 +000037
Chris Lattnerf5fbd792006-08-10 23:56:11 +000038 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000039 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000040 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000041 if (Range)
42 *Range = DeclaratorInfo.getSourceRange();
43
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000044 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000045 return true;
46
Douglas Gregor0be31a22010-07-02 17:43:08 +000047 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000048}
49
Alexis Hunt96d5c762009-11-21 08:43:09 +000050/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000051///
52/// [GNU] attributes:
53/// attribute
54/// attributes attribute
55///
56/// [GNU] attribute:
57/// '__attribute__' '(' '(' attribute-list ')' ')'
58///
59/// [GNU] attribute-list:
60/// attrib
61/// attribute_list ',' attrib
62///
63/// [GNU] attrib:
64/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000065/// attrib-name
66/// attrib-name '(' identifier ')'
67/// attrib-name '(' identifier ',' nonempty-expr-list ')'
68/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000069///
Steve Naroff0f2fe172007-06-01 17:11:19 +000070/// [GNU] attrib-name:
71/// identifier
72/// typespec
73/// typequal
74/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000075///
Steve Naroff0f2fe172007-06-01 17:11:19 +000076/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000077/// token lookahead. Comment from gcc: "If they start with an identifier
78/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +000079/// start with that identifier; otherwise they are an expression list."
80///
81/// At the moment, I am not doing 2 token lookahead. I am also unaware of
82/// any attributes that don't work (based on my limited testing). Most
83/// attributes are very simple in practice. Until we find a bug, I don't see
84/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000085
John McCall53fa7142010-12-24 02:08:15 +000086void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
87 SourceLocation *endLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +000088 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattner76c72282007-10-09 17:33:22 +000090 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000091 ConsumeToken();
92 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
93 "attribute")) {
94 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +000095 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +000096 }
97 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
98 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +000099 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000100 }
101 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000102 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
103 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000104
105 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000106 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
107 ConsumeToken();
108 continue;
109 }
110 // we have an identifier or declaration specifier (const, int, etc.)
111 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
112 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000113
Douglas Gregora2f49452010-03-16 19:09:18 +0000114 // check if we have a "parameterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000115 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000116 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000117
Chris Lattner76c72282007-10-09 17:33:22 +0000118 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000119 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
120 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000121
122 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000123 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000124 ConsumeParen(); // ignore the right paren loc for now
John McCall53fa7142010-12-24 02:08:15 +0000125 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
126 ParmName, ParmLoc, 0, 0));
Chris Lattner76c72282007-10-09 17:33:22 +0000127 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000128 ConsumeToken();
129 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000130 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000131 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000132
Steve Naroff0f2fe172007-06-01 17:11:19 +0000133 // now parse the non-empty comma separated list of expressions
134 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000135 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000136 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000137 ArgExprsOk = false;
138 SkipUntil(tok::r_paren);
139 break;
140 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000141 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000142 }
Chris Lattner76c72282007-10-09 17:33:22 +0000143 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000144 break;
145 ConsumeToken(); // Eat the comma, move to the next argument
146 }
Chris Lattner76c72282007-10-09 17:33:22 +0000147 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000148 ConsumeParen(); // ignore the right paren loc for now
John McCall53fa7142010-12-24 02:08:15 +0000149 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0,
150 AttrNameLoc, ParmName, ParmLoc,
151 ArgExprs.take(), ArgExprs.size()));
Steve Naroff0f2fe172007-06-01 17:11:19 +0000152 }
153 }
154 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000155 switch (Tok.getKind()) {
156 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000157 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000158 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000159 ConsumeParen(); // ignore the right paren loc for now
John McCall53fa7142010-12-24 02:08:15 +0000160 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
161 0, SourceLocation(), 0, 0));
Nate Begemanf2758702009-06-26 06:32:41 +0000162 break;
163 case tok::kw_char:
164 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000165 case tok::kw_char16_t:
166 case tok::kw_char32_t:
Nate Begemanf2758702009-06-26 06:32:41 +0000167 case tok::kw_bool:
168 case tok::kw_short:
169 case tok::kw_int:
170 case tok::kw_long:
171 case tok::kw_signed:
172 case tok::kw_unsigned:
173 case tok::kw_float:
174 case tok::kw_double:
175 case tok::kw_void:
John McCall53fa7142010-12-24 02:08:15 +0000176 case tok::kw_typeof: {
177 AttributeList *attr
178 = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
179 0, SourceLocation(), 0, 0);
180 attrs.add(attr);
181 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian9d7d3d82010-08-17 23:19:16 +0000182 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begemanf2758702009-06-26 06:32:41 +0000183 // If it's a builtin type name, eat it and expect a rparen
184 // __attribute__(( vec_type_hint(char) ))
185 ConsumeToken();
Nate Begemanf2758702009-06-26 06:32:41 +0000186 if (Tok.is(tok::r_paren))
187 ConsumeParen();
188 break;
John McCall53fa7142010-12-24 02:08:15 +0000189 }
Nate Begemanf2758702009-06-26 06:32:41 +0000190 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000191 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000192 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000193 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000194
Steve Naroff0f2fe172007-06-01 17:11:19 +0000195 // now parse the list of expressions
196 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000197 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000198 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000199 ArgExprsOk = false;
200 SkipUntil(tok::r_paren);
201 break;
202 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000203 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000204 }
Chris Lattner76c72282007-10-09 17:33:22 +0000205 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000206 break;
207 ConsumeToken(); // Eat the comma, move to the next argument
208 }
209 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000210 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000211 ConsumeParen(); // ignore the right paren loc for now
John McCall53fa7142010-12-24 02:08:15 +0000212 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0,
213 AttrNameLoc, 0, SourceLocation(),
214 ArgExprs.take(), ArgExprs.size()));
Steve Naroff0f2fe172007-06-01 17:11:19 +0000215 }
Nate Begemanf2758702009-06-26 06:32:41 +0000216 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000217 }
218 }
219 } else {
John McCall53fa7142010-12-24 02:08:15 +0000220 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
221 0, SourceLocation(), 0, 0));
Steve Naroff0f2fe172007-06-01 17:11:19 +0000222 }
223 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000224 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000225 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000226 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000227 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
228 SkipUntil(tok::r_paren, false);
229 }
John McCall53fa7142010-12-24 02:08:15 +0000230 if (endLoc)
231 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000232 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000233}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000234
Eli Friedman06de2b52009-06-08 07:21:15 +0000235/// ParseMicrosoftDeclSpec - Parse an __declspec construct
236///
237/// [MS] decl-specifier:
238/// __declspec ( extended-decl-modifier-seq )
239///
240/// [MS] extended-decl-modifier-seq:
241/// extended-decl-modifier[opt]
242/// extended-decl-modifier extended-decl-modifier-seq
243
John McCall53fa7142010-12-24 02:08:15 +0000244void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000245 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000246
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000247 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000248 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
249 "declspec")) {
250 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000251 return;
Eli Friedman06de2b52009-06-08 07:21:15 +0000252 }
Eli Friedman53339e02009-06-08 23:27:34 +0000253 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000254 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
255 SourceLocation AttrNameLoc = ConsumeToken();
256 if (Tok.is(tok::l_paren)) {
257 ConsumeParen();
258 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
259 // correctly.
John McCalldadc5752010-08-24 06:29:42 +0000260 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000261 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000262 Expr *ExprList = ArgExpr.take();
John McCall53fa7142010-12-24 02:08:15 +0000263 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
264 SourceLocation(), &ExprList, 1, true));
Eli Friedman06de2b52009-06-08 07:21:15 +0000265 }
266 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
267 SkipUntil(tok::r_paren, false);
268 } else {
John McCall53fa7142010-12-24 02:08:15 +0000269 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
270 0, SourceLocation(), 0, 0, true));
Eli Friedman06de2b52009-06-08 07:21:15 +0000271 }
272 }
273 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
274 SkipUntil(tok::r_paren, false);
John McCall53fa7142010-12-24 02:08:15 +0000275 return;
Eli Friedman53339e02009-06-08 23:27:34 +0000276}
277
John McCall53fa7142010-12-24 02:08:15 +0000278void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000279 // Treat these like attributes
280 // FIXME: Allow Sema to distinguish between these and real attributes!
281 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000282 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
283 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000284 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
285 SourceLocation AttrNameLoc = ConsumeToken();
286 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
287 // FIXME: Support these properly!
288 continue;
John McCall53fa7142010-12-24 02:08:15 +0000289 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
290 SourceLocation(), 0, 0, true));
Eli Friedman53339e02009-06-08 23:27:34 +0000291 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000292}
293
John McCall53fa7142010-12-24 02:08:15 +0000294void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000295 // Treat these like attributes
296 while (Tok.is(tok::kw___pascal)) {
297 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
298 SourceLocation AttrNameLoc = ConsumeToken();
John McCall53fa7142010-12-24 02:08:15 +0000299 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
300 SourceLocation(), 0, 0, true));
Dawn Perchik335e16b2010-09-03 01:29:35 +0000301 }
John McCall53fa7142010-12-24 02:08:15 +0000302}
303
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000304void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
305 // Treat these like attributes
306 while (Tok.is(tok::kw___kernel)) {
307 SourceLocation AttrNameLoc = ConsumeToken();
308 attrs.add(AttrFactory.Create(PP.getIdentifierInfo("opencl_kernel_function"),
309 AttrNameLoc, 0, AttrNameLoc, 0,
310 SourceLocation(), 0, 0, false));
311 }
312}
313
John McCall53fa7142010-12-24 02:08:15 +0000314void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
315 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
316 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000317}
318
Chris Lattner53361ac2006-08-10 05:19:57 +0000319/// ParseDeclaration - Parse a full 'declaration', which consists of
320/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000321/// 'Context' should be a Declarator::TheContext value. This returns the
322/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000323///
324/// declaration: [C99 6.7]
325/// block-declaration ->
326/// simple-declaration
327/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000328/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000329/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000330/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000331/// [C++] using-declaration
Sebastian Redlf769df52009-03-24 22:27:57 +0000332/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000333/// others... [FIXME]
334///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000335Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
336 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000337 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000338 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000339 ParenBraceBracketBalancer BalancerRAIIObj(*this);
340
John McCall48871652010-08-21 09:40:31 +0000341 Decl *SingleDecl = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000342 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000343 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000344 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000345 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000346 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000347 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000348 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000349 // Could be the start of an inline namespace. Allowed as an ext in C++03.
350 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +0000351 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000352 SourceLocation InlineLoc = ConsumeToken();
353 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
354 break;
355 }
John McCall53fa7142010-12-24 02:08:15 +0000356 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000357 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000358 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000359 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000360 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000361 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000362 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000363 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall53fa7142010-12-24 02:08:15 +0000364 DeclEnd, attrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000365 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000366 case tok::kw_static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000367 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000368 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000369 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000370 default:
John McCall53fa7142010-12-24 02:08:15 +0000371 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000372 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000373
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000374 // This routine returns a DeclGroup, if the thing we parsed only contains a
375 // single decl, convert it now.
376 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000377}
378
379/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
380/// declaration-specifiers init-declarator-list[opt] ';'
381///[C90/C++]init-declarator-list ';' [TODO]
382/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000383///
384/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000385/// declaration. If it is true, it checks for and eats it.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000386Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
387 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000388 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000389 ParsedAttributes &attrs,
Chris Lattner005fc1b2010-04-05 18:18:31 +0000390 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000391 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000392 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000393 DS.takeAttributesFrom(attrs);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000394 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000395 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000396 StmtResult R = Actions.ActOnVlaStmt(DS);
397 if (R.isUsable())
398 Stmts.push_back(R.release());
Mike Stump11289f42009-09-09 15:08:12 +0000399
Chris Lattner0e894622006-08-13 19:58:17 +0000400 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
401 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000402 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000403 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000404 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallb54367d2010-05-21 20:45:30 +0000405 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000406 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000407 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000408 }
Mike Stump11289f42009-09-09 15:08:12 +0000409
Chris Lattner005fc1b2010-04-05 18:18:31 +0000410 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld5a36322009-11-03 19:26:08 +0000411}
Mike Stump11289f42009-09-09 15:08:12 +0000412
John McCalld5a36322009-11-03 19:26:08 +0000413/// ParseDeclGroup - Having concluded that this is either a function
414/// definition or a group of object declarations, actually parse the
415/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000416Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
417 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000418 bool AllowFunctionDefinitions,
419 SourceLocation *DeclEnd) {
420 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000421 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000422 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000423
John McCalld5a36322009-11-03 19:26:08 +0000424 // Bail out if the first declarator didn't seem well-formed.
425 if (!D.hasName() && !D.mayOmitIdentifier()) {
426 // Skip until ; or }.
427 SkipUntil(tok::r_brace, true, true);
428 if (Tok.is(tok::semi))
429 ConsumeToken();
430 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000431 }
Mike Stump11289f42009-09-09 15:08:12 +0000432
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000433 // Check to see if we have a function *definition* which must have a body.
434 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
435 // Look at the next token to make sure that this isn't a function
436 // declaration. We have to check this because __attribute__ might be the
437 // start of a function definition in GCC-extended K&R C.
438 !isDeclarationAfterDeclarator()) {
439
Chris Lattner13901342010-07-11 22:42:07 +0000440 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000441 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
442 Diag(Tok, diag::err_function_declared_typedef);
443
444 // Recover by treating the 'typedef' as spurious.
445 DS.ClearStorageClassSpecs();
446 }
447
John McCall48871652010-08-21 09:40:31 +0000448 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000449 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000450 }
451
452 if (isDeclarationSpecifier()) {
453 // If there is an invalid declaration specifier right after the function
454 // prototype, then we must be in a missing semicolon case where this isn't
455 // actually a body. Just fall through into the code that handles it as a
456 // prototype, and let the top-level code handle the erroneous declspec
457 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000458 } else {
459 Diag(Tok, diag::err_expected_fn_body);
460 SkipUntil(tok::semi);
461 return DeclGroupPtrTy();
462 }
463 }
464
John McCall48871652010-08-21 09:40:31 +0000465 llvm::SmallVector<Decl *, 8> DeclsInGroup;
466 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000467 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000468 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000469 DeclsInGroup.push_back(FirstDecl);
470
471 // If we don't have a comma, it is either the end of the list (a ';') or an
472 // error, bail out.
473 while (Tok.is(tok::comma)) {
474 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000475 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000476
477 // Parse the next declarator.
478 D.clear();
479
480 // Accept attributes in an init-declarator. In the first declarator in a
481 // declaration, these would be part of the declspec. In subsequent
482 // declarators, they become part of the declarator itself, so that they
483 // don't apply to declarators after *this* one. Examples:
484 // short __attribute__((common)) var; -> declspec
485 // short var __attribute__((common)); -> declarator
486 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +0000487 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +0000488
489 ParseDeclarator(D);
490
John McCall48871652010-08-21 09:40:31 +0000491 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000492 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000493 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000494 DeclsInGroup.push_back(ThisDecl);
495 }
496
497 if (DeclEnd)
498 *DeclEnd = Tok.getLocation();
499
500 if (Context != Declarator::ForContext &&
501 ExpectAndConsume(tok::semi,
502 Context == Declarator::FileContext
503 ? diag::err_invalid_token_after_toplevel_declarator
504 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000505 // Okay, there was no semicolon and one was expected. If we see a
506 // declaration specifier, just assume it was missing and continue parsing.
507 // Otherwise things are very confused and we skip to recover.
508 if (!isDeclarationSpecifier()) {
509 SkipUntil(tok::r_brace, true, true);
510 if (Tok.is(tok::semi))
511 ConsumeToken();
512 }
John McCalld5a36322009-11-03 19:26:08 +0000513 }
514
Douglas Gregor0be31a22010-07-02 17:43:08 +0000515 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000516 DeclsInGroup.data(),
517 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000518}
519
Douglas Gregor23996282009-05-12 21:31:51 +0000520/// \brief Parse 'declaration' after parsing 'declaration-specifiers
521/// declarator'. This method parses the remainder of the declaration
522/// (including any attributes or initializer, among other things) and
523/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000524///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000525/// init-declarator: [C99 6.7]
526/// declarator
527/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000528/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
529/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000530/// [C++] declarator initializer[opt]
531///
532/// [C++] initializer:
533/// [C++] '=' initializer-clause
534/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000535/// [C++0x] '=' 'default' [TODO]
536/// [C++0x] '=' 'delete'
537///
538/// According to the standard grammar, =default and =delete are function
539/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000540///
John McCall48871652010-08-21 09:40:31 +0000541Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000542 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000543 // If a simple-asm-expr is present, parse it.
544 if (Tok.is(tok::kw_asm)) {
545 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +0000546 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor23996282009-05-12 21:31:51 +0000547 if (AsmLabel.isInvalid()) {
548 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000549 return 0;
Douglas Gregor23996282009-05-12 21:31:51 +0000550 }
Mike Stump11289f42009-09-09 15:08:12 +0000551
Douglas Gregor23996282009-05-12 21:31:51 +0000552 D.setAsmLabel(AsmLabel.release());
553 D.SetRangeEnd(Loc);
554 }
Mike Stump11289f42009-09-09 15:08:12 +0000555
John McCall53fa7142010-12-24 02:08:15 +0000556 MaybeParseGNUAttributes(D);
Mike Stump11289f42009-09-09 15:08:12 +0000557
Douglas Gregor23996282009-05-12 21:31:51 +0000558 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000559 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000560 switch (TemplateInfo.Kind) {
561 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000562 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000563 break;
564
565 case ParsedTemplateInfo::Template:
566 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000567 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000568 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000569 TemplateInfo.TemplateParams->data(),
570 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000571 D);
572 break;
573
574 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000575 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000576 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000577 TemplateInfo.ExternLoc,
578 TemplateInfo.TemplateLoc,
579 D);
580 if (ThisRes.isInvalid()) {
581 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000582 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000583 }
584
585 ThisDecl = ThisRes.get();
586 break;
587 }
588 }
Mike Stump11289f42009-09-09 15:08:12 +0000589
Richard Smith30482bc2011-02-20 03:19:35 +0000590 bool TypeContainsAuto =
591 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
592
Douglas Gregor23996282009-05-12 21:31:51 +0000593 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000594 if (isTokenEqualOrMistypedEqualEqual(
595 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000596 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000597 if (Tok.is(tok::kw_delete)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000598 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000599
600 if (!getLang().CPlusPlus0x)
601 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
602
Douglas Gregor23996282009-05-12 21:31:51 +0000603 Actions.SetDeclDeleted(ThisDecl, DelLoc);
604 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000605 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
606 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000607 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000608 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000609
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000610 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000611 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000612 ConsumeCodeCompletionToken();
613 SkipUntil(tok::comma, true, true);
614 return ThisDecl;
615 }
616
John McCalldadc5752010-08-24 06:29:42 +0000617 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000618
John McCall1f4ee7b2009-12-19 09:28:58 +0000619 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000620 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000621 ExitScope();
622 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000623
Douglas Gregor23996282009-05-12 21:31:51 +0000624 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000625 SkipUntil(tok::comma, true, true);
626 Actions.ActOnInitializerError(ThisDecl);
627 } else
Richard Smith30482bc2011-02-20 03:19:35 +0000628 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
629 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000630 }
631 } else if (Tok.is(tok::l_paren)) {
632 // Parse C++ direct initializer: '(' expression-list ')'
633 SourceLocation LParenLoc = ConsumeParen();
634 ExprVector Exprs(Actions);
635 CommaLocsTy CommaLocs;
636
Douglas Gregor613bf102009-12-22 17:47:17 +0000637 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
638 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000639 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000640 }
641
Douglas Gregor23996282009-05-12 21:31:51 +0000642 if (ParseExpressionList(Exprs, CommaLocs)) {
643 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000644
645 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000646 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000647 ExitScope();
648 }
Douglas Gregor23996282009-05-12 21:31:51 +0000649 } else {
650 // Match the ')'.
651 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
652
653 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
654 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000655
656 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000657 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000658 ExitScope();
659 }
660
Douglas Gregor23996282009-05-12 21:31:51 +0000661 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
662 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +0000663 RParenLoc,
664 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000665 }
666 } else {
Richard Smith30482bc2011-02-20 03:19:35 +0000667 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000668 }
669
670 return ThisDecl;
671}
672
Chris Lattner1890ac82006-08-13 01:16:23 +0000673/// ParseSpecifierQualifierList
674/// specifier-qualifier-list:
675/// type-specifier specifier-qualifier-list[opt]
676/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000677/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000678///
679void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
680 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
681 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000682 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000683
Chris Lattner1890ac82006-08-13 01:16:23 +0000684 // Validate declspec for type-name.
685 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000686 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +0000687 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000688 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000689
Chris Lattner1b22eed2006-11-28 05:12:07 +0000690 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000691 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000692 if (DS.getStorageClassSpecLoc().isValid())
693 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
694 else
695 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000696 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000697 }
Mike Stump11289f42009-09-09 15:08:12 +0000698
Chris Lattner1b22eed2006-11-28 05:12:07 +0000699 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000700 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000701 if (DS.isInlineSpecified())
702 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
703 if (DS.isVirtualSpecified())
704 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
705 if (DS.isExplicitSpecified())
706 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000707 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000708 }
709}
Chris Lattner53361ac2006-08-10 05:19:57 +0000710
Chris Lattner6cc055a2009-04-12 20:42:31 +0000711/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
712/// specified token is valid after the identifier in a declarator which
713/// immediately follows the declspec. For example, these things are valid:
714///
715/// int x [ 4]; // direct-declarator
716/// int x ( int y); // direct-declarator
717/// int(int x ) // direct-declarator
718/// int x ; // simple-declaration
719/// int x = 17; // init-declarator-list
720/// int x , y; // init-declarator-list
721/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000722/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000723/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000724///
725/// This is not, because 'x' does not immediately follow the declspec (though
726/// ')' happens to be valid anyway).
727/// int (x)
728///
729static bool isValidAfterIdentifierInDeclarator(const Token &T) {
730 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
731 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000732 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000733}
734
Chris Lattner20a0c612009-04-14 21:34:55 +0000735
736/// ParseImplicitInt - This method is called when we have an non-typename
737/// identifier in a declspec (which normally terminates the decl spec) when
738/// the declspec has no type specifier. In this case, the declspec is either
739/// malformed or is "implicit int" (in K&R and C89).
740///
741/// This method handles diagnosing this prettily and returns false if the
742/// declspec is done being processed. If it recovers and thinks there may be
743/// other pieces of declspec after it, it returns true.
744///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000745bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000746 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000747 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000748 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000749
Chris Lattner20a0c612009-04-14 21:34:55 +0000750 SourceLocation Loc = Tok.getLocation();
751 // If we see an identifier that is not a type name, we normally would
752 // parse it as the identifer being declared. However, when a typename
753 // is typo'd or the definition is not included, this will incorrectly
754 // parse the typename as the identifier name and fall over misparsing
755 // later parts of the diagnostic.
756 //
757 // As such, we try to do some look-ahead in cases where this would
758 // otherwise be an "implicit-int" case to see if this is invalid. For
759 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
760 // an identifier with implicit int, we'd get a parse error because the
761 // next token is obviously invalid for a type. Parse these as a case
762 // with an invalid type specifier.
763 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000764
Chris Lattner20a0c612009-04-14 21:34:55 +0000765 // Since we know that this either implicit int (which is rare) or an
766 // error, we'd do lookahead to try to do better recovery.
767 if (isValidAfterIdentifierInDeclarator(NextToken())) {
768 // If this token is valid for implicit int, e.g. "static x = 4", then
769 // we just avoid eating the identifier, so it will be parsed as the
770 // identifier in the declarator.
771 return false;
772 }
Mike Stump11289f42009-09-09 15:08:12 +0000773
Chris Lattner20a0c612009-04-14 21:34:55 +0000774 // Otherwise, if we don't consume this token, we are going to emit an
775 // error anyway. Try to recover from various common problems. Check
776 // to see if this was a reference to a tag name without a tag specified.
777 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000778 //
779 // C++ doesn't need this, and isTagName doesn't take SS.
780 if (SS == 0) {
781 const char *TagName = 0;
782 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000783
Douglas Gregor0be31a22010-07-02 17:43:08 +0000784 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +0000785 default: break;
786 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
787 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
788 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
789 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
790 }
Mike Stump11289f42009-09-09 15:08:12 +0000791
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000792 if (TagName) {
793 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +0000794 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +0000795 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000796
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000797 // Parse this as a tag as if the missing tag were present.
798 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +0000799 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000800 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000801 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000802 return true;
803 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000804 }
Mike Stump11289f42009-09-09 15:08:12 +0000805
Douglas Gregor15e56022009-10-13 23:27:22 +0000806 // This is almost certainly an invalid type name. Let the action emit a
807 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +0000808 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +0000809 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +0000810 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +0000811 // The action emitted a diagnostic, so we don't have to.
812 if (T) {
813 // The action has suggested that the type T could be used. Set that as
814 // the type in the declaration specifiers, consume the would-be type
815 // name token, and we're done.
816 const char *PrevSpec;
817 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +0000818 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +0000819 DS.SetRangeEnd(Tok.getLocation());
820 ConsumeToken();
821
822 // There may be other declaration specifiers after this.
823 return true;
824 }
825
826 // Fall through; the action had no suggestion for us.
827 } else {
828 // The action did not emit a diagnostic, so emit one now.
829 SourceRange R;
830 if (SS) R = SS->getRange();
831 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
832 }
Mike Stump11289f42009-09-09 15:08:12 +0000833
Douglas Gregor15e56022009-10-13 23:27:22 +0000834 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000835 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000836 unsigned DiagID;
837 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000838 DS.SetRangeEnd(Tok.getLocation());
839 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000840
Chris Lattner20a0c612009-04-14 21:34:55 +0000841 // TODO: Could inject an invalid typedef decl in an enclosing scope to
842 // avoid rippling error messages on subsequent uses of the same type,
843 // could be useful if #include was forgotten.
844 return false;
845}
846
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000847/// \brief Determine the declaration specifier context from the declarator
848/// context.
849///
850/// \param Context the declarator context, which is one of the
851/// Declarator::TheContext enumerator values.
852Parser::DeclSpecContext
853Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
854 if (Context == Declarator::MemberContext)
855 return DSC_class;
856 if (Context == Declarator::FileContext)
857 return DSC_top_level;
858 return DSC_normal;
859}
860
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000861/// ParseDeclarationSpecifiers
862/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000863/// storage-class-specifier declaration-specifiers[opt]
864/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000865/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000866/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000867///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000868/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000869/// 'typedef'
870/// 'extern'
871/// 'static'
872/// 'auto'
873/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000874/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000875/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000876/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000877/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000878/// [C++] 'virtual'
879/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000880/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000881/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000882/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000883
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000884///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000885void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000886 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000887 AccessSpecifier AS,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000888 DeclSpecContext DSContext) {
Chris Lattner2e232092008-03-13 06:29:04 +0000889 DS.SetRangeStart(Tok.getLocation());
Chris Lattner07865442010-11-09 20:14:26 +0000890 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000891 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000892 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000893 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000894 unsigned DiagID = 0;
895
Chris Lattner4d8f8732006-11-28 05:05:08 +0000896 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000897
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000898 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000899 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000900 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000901 // If this is not a declaration specifier token, we're done reading decl
902 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000903 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000904 return;
Mike Stump11289f42009-09-09 15:08:12 +0000905
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000906 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +0000907 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000908 if (DS.hasTypeSpecifier()) {
909 bool AllowNonIdentifiers
910 = (getCurScope()->getFlags() & (Scope::ControlScope |
911 Scope::BlockScope |
912 Scope::TemplateParamScope |
913 Scope::FunctionPrototypeScope |
914 Scope::AtCatchScope)) == 0;
915 bool AllowNestedNameSpecifiers
916 = DSContext == DSC_top_level ||
917 (DSContext == DSC_class && DS.isFriendSpecified());
918
Douglas Gregorbfcea8b2010-09-16 15:14:18 +0000919 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
920 AllowNonIdentifiers,
921 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000922 ConsumeCodeCompletionToken();
923 return;
924 }
925
Douglas Gregor80039242011-02-15 20:33:25 +0000926 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
927 CCC = Sema::PCC_LocalDeclarationSpecifiers;
928 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +0000929 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
930 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000931 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +0000932 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000933 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +0000934 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000935
936 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
937 ConsumeCodeCompletionToken();
938 return;
939 }
940
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000941 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000942 // C++ scope specifier. Annotate and loop, or bail out on error.
943 if (TryAnnotateCXXScopeToken(true)) {
944 if (!DS.hasTypeSpecifier())
945 DS.SetTypeSpecError();
946 goto DoneWithDeclSpec;
947 }
John McCall8bc2a702010-03-01 18:20:46 +0000948 if (Tok.is(tok::coloncolon)) // ::new or ::delete
949 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000950 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000951
952 case tok::annot_cxxscope: {
953 if (DS.hasTypeSpecifier())
954 goto DoneWithDeclSpec;
955
John McCall9dab4e62009-12-12 11:40:51 +0000956 CXXScopeSpec SS;
John McCall37ad5512010-08-23 06:44:23 +0000957 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCall9dab4e62009-12-12 11:40:51 +0000958 SS.setRange(Tok.getAnnotationRange());
959
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000960 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000961 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000962 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000963 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000964 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000965 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000966
967 // C++ [class.qual]p2:
968 // In a lookup in which the constructor is an acceptable lookup
969 // result and the nested-name-specifier nominates a class C:
970 //
971 // - if the name specified after the
972 // nested-name-specifier, when looked up in C, is the
973 // injected-class-name of C (Clause 9), or
974 //
975 // - if the name specified after the nested-name-specifier
976 // is the same as the identifier or the
977 // simple-template-id's template-name in the last
978 // component of the nested-name-specifier,
979 //
980 // the name is instead considered to name the constructor of
981 // class C.
982 //
983 // Thus, if the template-name is actually the constructor
984 // name, then the code is ill-formed; this interpretation is
985 // reinforced by the NAD status of core issue 635.
986 TemplateIdAnnotation *TemplateId
987 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000988 if ((DSContext == DSC_top_level ||
989 (DSContext == DSC_class && DS.isFriendSpecified())) &&
990 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000991 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000992 if (isConstructorDeclarator()) {
993 // The user meant this to be an out-of-line constructor
994 // definition, but template arguments are not allowed
995 // there. Just allow this as a constructor; we'll
996 // complain about it later.
997 goto DoneWithDeclSpec;
998 }
999
1000 // The user meant this to name a type, but it actually names
1001 // a constructor with some extraneous template
1002 // arguments. Complain, then parse it as a type as the user
1003 // intended.
1004 Diag(TemplateId->TemplateNameLoc,
1005 diag::err_out_of_line_template_id_names_constructor)
1006 << TemplateId->Name;
1007 }
1008
John McCall9dab4e62009-12-12 11:40:51 +00001009 DS.getTypeSpecScope() = SS;
1010 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001011 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001012 "ParseOptionalCXXScopeSpecifier not working");
1013 AnnotateTemplateIdTokenAsType(&SS);
1014 continue;
1015 }
1016
Douglas Gregorc5790df2009-09-28 07:26:33 +00001017 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001018 DS.getTypeSpecScope() = SS;
1019 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001020 if (Tok.getAnnotationValue()) {
1021 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001022 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1023 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001024 PrevSpec, DiagID, T);
1025 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001026 else
1027 DS.SetTypeSpecError();
1028 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1029 ConsumeToken(); // The typename
1030 }
1031
Douglas Gregor167fa622009-03-25 15:40:00 +00001032 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001033 goto DoneWithDeclSpec;
1034
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001035 // If we're in a context where the identifier could be a class name,
1036 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001037 if ((DSContext == DSC_top_level ||
1038 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001039 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001040 &SS)) {
1041 if (isConstructorDeclarator())
1042 goto DoneWithDeclSpec;
1043
1044 // As noted in C++ [class.qual]p2 (cited above), when the name
1045 // of the class is qualified in a context where it could name
1046 // a constructor, its a constructor name. However, we've
1047 // looked at the declarator, and the user probably meant this
1048 // to be a type. Complain that it isn't supposed to be treated
1049 // as a type, then proceed to parse it as a type.
1050 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1051 << Next.getIdentifierInfo();
1052 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001053
John McCallba7bf592010-08-24 05:47:05 +00001054 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1055 Next.getLocation(),
1056 getCurScope(), &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001057
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001058 // If the referenced identifier is not a type, then this declspec is
1059 // erroneous: We already checked about that it has no type specifier, and
1060 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001061 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001062 if (TypeRep == 0) {
1063 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001064 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001065 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001066 }
Mike Stump11289f42009-09-09 15:08:12 +00001067
John McCall9dab4e62009-12-12 11:40:51 +00001068 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001069 ConsumeToken(); // The C++ scope.
1070
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001071 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001072 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001073 if (isInvalid)
1074 break;
Mike Stump11289f42009-09-09 15:08:12 +00001075
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001076 DS.SetRangeEnd(Tok.getLocation());
1077 ConsumeToken(); // The typename.
1078
1079 continue;
1080 }
Mike Stump11289f42009-09-09 15:08:12 +00001081
Chris Lattnere387d9e2009-01-21 19:48:37 +00001082 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001083 if (Tok.getAnnotationValue()) {
1084 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001085 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001086 DiagID, T);
1087 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001088 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001089
1090 if (isInvalid)
1091 break;
1092
Chris Lattnere387d9e2009-01-21 19:48:37 +00001093 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1094 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001095
Chris Lattnere387d9e2009-01-21 19:48:37 +00001096 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1097 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001098 // Objective-C interface.
1099 if (Tok.is(tok::less) && getLang().ObjC1)
1100 ParseObjCProtocolQualifiers(DS);
1101
Chris Lattnere387d9e2009-01-21 19:48:37 +00001102 continue;
1103 }
Mike Stump11289f42009-09-09 15:08:12 +00001104
Chris Lattner16fac4f2008-07-26 01:18:38 +00001105 // typedef-name
1106 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001107 // In C++, check to see if this is a scope specifier like foo::bar::, if
1108 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001109 if (getLang().CPlusPlus) {
1110 if (TryAnnotateCXXScopeToken(true)) {
1111 if (!DS.hasTypeSpecifier())
1112 DS.SetTypeSpecError();
1113 goto DoneWithDeclSpec;
1114 }
1115 if (!Tok.is(tok::identifier))
1116 continue;
1117 }
Mike Stump11289f42009-09-09 15:08:12 +00001118
Chris Lattner16fac4f2008-07-26 01:18:38 +00001119 // This identifier can only be a typedef name if we haven't already seen
1120 // a type-specifier. Without this check we misparse:
1121 // typedef int X; struct Y { short X; }; as 'short int'.
1122 if (DS.hasTypeSpecifier())
1123 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001124
John Thompson22334602010-02-05 00:12:22 +00001125 // Check for need to substitute AltiVec keyword tokens.
1126 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1127 break;
1128
Chris Lattner16fac4f2008-07-26 01:18:38 +00001129 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001130 ParsedType TypeRep =
1131 Actions.getTypeName(*Tok.getIdentifierInfo(),
1132 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001133
Chris Lattner6cc055a2009-04-12 20:42:31 +00001134 // If this is not a typedef name, don't parse it as part of the declspec,
1135 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001136 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001137 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001138 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001139 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001140
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001141 // If we're in a context where the identifier could be a class name,
1142 // check whether this is a constructor declaration.
1143 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001144 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001145 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001146 goto DoneWithDeclSpec;
1147
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001148 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001149 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001150 if (isInvalid)
1151 break;
Mike Stump11289f42009-09-09 15:08:12 +00001152
Chris Lattner16fac4f2008-07-26 01:18:38 +00001153 DS.SetRangeEnd(Tok.getLocation());
1154 ConsumeToken(); // The identifier
1155
1156 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1157 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001158 // Objective-C interface.
1159 if (Tok.is(tok::less) && getLang().ObjC1)
1160 ParseObjCProtocolQualifiers(DS);
1161
Steve Naroffcd5e7822008-09-22 10:28:57 +00001162 // Need to support trailing type qualifiers (e.g. "id<p> const").
1163 // If a type specifier follows, it will be diagnosed elsewhere.
1164 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001165 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001166
1167 // type-name
1168 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001169 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001170 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001171 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001172 // This template-id does not refer to a type name, so we're
1173 // done with the type-specifiers.
1174 goto DoneWithDeclSpec;
1175 }
1176
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001177 // If we're in a context where the template-id could be a
1178 // constructor name or specialization, check whether this is a
1179 // constructor declaration.
1180 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001181 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001182 isConstructorDeclarator())
1183 goto DoneWithDeclSpec;
1184
Douglas Gregor7f741122009-02-25 19:37:18 +00001185 // Turn the template-id annotation token into a type annotation
1186 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001187 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001188 continue;
1189 }
1190
Chris Lattnere37e2332006-08-15 04:50:22 +00001191 // GNU attributes support.
1192 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001193 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001194 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001195
1196 // Microsoft declspec support.
1197 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001198 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001199 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001200
Steve Naroff44ac7772008-12-25 14:16:32 +00001201 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001202 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001203 // FIXME: Add handling here!
1204 break;
1205
1206 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001207 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001208 case tok::kw___cdecl:
1209 case tok::kw___stdcall:
1210 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001211 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001212 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001213 continue;
1214
Dawn Perchik335e16b2010-09-03 01:29:35 +00001215 // Borland single token adornments.
1216 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001217 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001218 continue;
1219
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001220 // OpenCL single token adornments.
1221 case tok::kw___kernel:
1222 ParseOpenCLAttributes(DS.getAttributes());
1223 continue;
1224
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001225 // storage-class-specifier
1226 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001227 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001228 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001229 break;
1230 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001231 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001232 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001233 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001234 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001235 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001236 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001237 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001238 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001239 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001240 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001241 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001242 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001243 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001244 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001245 break;
1246 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001247 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001248 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1249 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001250 else
John McCall49bfce42009-08-03 20:12:06 +00001251 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001252 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001253 break;
1254 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001255 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001256 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001257 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001258 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001259 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001260 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001261 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001262 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001263 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001264 break;
Mike Stump11289f42009-09-09 15:08:12 +00001265
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001266 // function-specifier
1267 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001268 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001269 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001270 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001271 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001272 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001273 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001274 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001275 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001276
Anders Carlssoncd8db412009-05-06 04:46:28 +00001277 // friend
1278 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001279 if (DSContext == DSC_class)
1280 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1281 else {
1282 PrevSpec = ""; // not actually used by the diagnostic
1283 DiagID = diag::err_friend_invalid_in_context;
1284 isInvalid = true;
1285 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001286 break;
Mike Stump11289f42009-09-09 15:08:12 +00001287
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001288 // constexpr
1289 case tok::kw_constexpr:
1290 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1291 break;
1292
Chris Lattnere387d9e2009-01-21 19:48:37 +00001293 // type-specifier
1294 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001295 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1296 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001297 break;
1298 case tok::kw_long:
1299 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001300 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1301 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001302 else
John McCall49bfce42009-08-03 20:12:06 +00001303 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1304 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001305 break;
1306 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001307 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1308 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001309 break;
1310 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001311 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1312 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001313 break;
1314 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001315 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1316 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001317 break;
1318 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001319 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1320 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001321 break;
1322 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001323 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1324 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001325 break;
1326 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001327 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1328 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001329 break;
1330 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001331 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1332 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001333 break;
1334 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001335 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1336 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001337 break;
1338 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001339 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1340 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001341 break;
1342 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001343 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1344 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001345 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001346 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001347 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1348 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001349 break;
1350 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001351 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1352 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001353 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001354 case tok::kw_bool:
1355 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001356 if (Tok.is(tok::kw_bool) &&
1357 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1358 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1359 PrevSpec = ""; // Not used by the diagnostic.
1360 DiagID = diag::err_bool_redeclaration;
1361 isInvalid = true;
1362 } else {
1363 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1364 DiagID);
1365 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001366 break;
1367 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001368 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1369 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001370 break;
1371 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001372 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1373 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001374 break;
1375 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001376 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1377 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001378 break;
John Thompson22334602010-02-05 00:12:22 +00001379 case tok::kw___vector:
1380 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1381 break;
1382 case tok::kw___pixel:
1383 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1384 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001385
1386 // class-specifier:
1387 case tok::kw_class:
1388 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001389 case tok::kw_union: {
1390 tok::TokenKind Kind = Tok.getKind();
1391 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001392 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001393 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001394 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001395
1396 // enum-specifier:
1397 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001398 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001399 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001400 continue;
1401
1402 // cv-qualifier:
1403 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001404 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1405 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001406 break;
1407 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001408 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1409 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001410 break;
1411 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001412 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1413 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001414 break;
1415
Douglas Gregor333489b2009-03-27 23:10:48 +00001416 // C++ typename-specifier:
1417 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001418 if (TryAnnotateTypeOrScopeToken()) {
1419 DS.SetTypeSpecError();
1420 goto DoneWithDeclSpec;
1421 }
1422 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001423 continue;
1424 break;
1425
Chris Lattnere387d9e2009-01-21 19:48:37 +00001426 // GNU typeof support.
1427 case tok::kw_typeof:
1428 ParseTypeofSpecifier(DS);
1429 continue;
1430
Anders Carlsson74948d02009-06-24 17:47:40 +00001431 case tok::kw_decltype:
1432 ParseDecltypeSpecifier(DS);
1433 continue;
1434
Steve Naroffcfdf6162008-06-05 00:02:44 +00001435 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001436 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001437 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1438 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001439 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001440 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001441
Douglas Gregor3a001f42010-11-19 17:10:50 +00001442 if (!ParseObjCProtocolQualifiers(DS))
1443 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1444 << FixItHint::CreateInsertion(Loc, "id")
1445 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001446
1447 // Need to support trailing type qualifiers (e.g. "id<p> const").
1448 // If a type specifier follows, it will be diagnosed elsewhere.
1449 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001450 }
John McCall49bfce42009-08-03 20:12:06 +00001451 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001452 if (isInvalid) {
1453 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001454 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001455
1456 if (DiagID == diag::ext_duplicate_declspec)
1457 Diag(Tok, DiagID)
1458 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1459 else
1460 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001461 }
Chris Lattner2e232092008-03-13 06:29:04 +00001462 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001463 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001464 }
1465}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001466
Chris Lattnera448d752009-01-06 06:59:53 +00001467/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001468/// primarily follow the C++ grammar with additions for C99 and GNU,
1469/// which together subsume the C grammar. Note that the C++
1470/// type-specifier also includes the C type-qualifier (for const,
1471/// volatile, and C99 restrict). Returns true if a type-specifier was
1472/// found (and parsed), false otherwise.
1473///
1474/// type-specifier: [C++ 7.1.5]
1475/// simple-type-specifier
1476/// class-specifier
1477/// enum-specifier
1478/// elaborated-type-specifier [TODO]
1479/// cv-qualifier
1480///
1481/// cv-qualifier: [C++ 7.1.5.1]
1482/// 'const'
1483/// 'volatile'
1484/// [C99] 'restrict'
1485///
1486/// simple-type-specifier: [ C++ 7.1.5.2]
1487/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1488/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1489/// 'char'
1490/// 'wchar_t'
1491/// 'bool'
1492/// 'short'
1493/// 'int'
1494/// 'long'
1495/// 'signed'
1496/// 'unsigned'
1497/// 'float'
1498/// 'double'
1499/// 'void'
1500/// [C99] '_Bool'
1501/// [C99] '_Complex'
1502/// [C99] '_Imaginary' // Removed in TC2?
1503/// [GNU] '_Decimal32'
1504/// [GNU] '_Decimal64'
1505/// [GNU] '_Decimal128'
1506/// [GNU] typeof-specifier
1507/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1508/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001509/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001510/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001511bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001512 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001513 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001514 const ParsedTemplateInfo &TemplateInfo,
1515 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001516 SourceLocation Loc = Tok.getLocation();
1517
1518 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001519 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001520 // If we already have a type specifier, this identifier is not a type.
1521 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1522 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1523 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1524 return false;
John Thompson22334602010-02-05 00:12:22 +00001525 // Check for need to substitute AltiVec keyword tokens.
1526 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1527 break;
1528 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001529 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001530 // Annotate typenames and C++ scope specifiers. If we get one, just
1531 // recurse to handle whatever we get.
1532 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001533 return true;
1534 if (Tok.is(tok::identifier))
1535 return false;
1536 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1537 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001538 case tok::coloncolon: // ::foo::bar
1539 if (NextToken().is(tok::kw_new) || // ::new
1540 NextToken().is(tok::kw_delete)) // ::delete
1541 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001542
Chris Lattner020bab92009-01-04 23:41:41 +00001543 // Annotate typenames and C++ scope specifiers. If we get one, just
1544 // recurse to handle whatever we get.
1545 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001546 return true;
1547 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1548 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001549
Douglas Gregor450c75a2008-11-07 15:42:26 +00001550 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001551 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001552 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00001553 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1554 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001555 DiagID, T);
1556 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001557 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001558 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1559 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001560
Douglas Gregor450c75a2008-11-07 15:42:26 +00001561 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1562 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1563 // Objective-C interface. If we don't have Objective-C or a '<', this is
1564 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001565 if (Tok.is(tok::less) && getLang().ObjC1)
1566 ParseObjCProtocolQualifiers(DS);
1567
Douglas Gregor450c75a2008-11-07 15:42:26 +00001568 return true;
1569 }
1570
1571 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001572 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001573 break;
1574 case tok::kw_long:
1575 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001576 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1577 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001578 else
John McCall49bfce42009-08-03 20:12:06 +00001579 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1580 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001581 break;
1582 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001583 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001584 break;
1585 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001586 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1587 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001588 break;
1589 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001590 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1591 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001592 break;
1593 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001594 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1595 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001596 break;
1597 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001598 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001599 break;
1600 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001601 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001602 break;
1603 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001604 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001605 break;
1606 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001607 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001608 break;
1609 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001610 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001611 break;
1612 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001613 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001614 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001615 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001616 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001617 break;
1618 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001619 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001620 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001621 case tok::kw_bool:
1622 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001623 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001624 break;
1625 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001626 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1627 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001628 break;
1629 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001630 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1631 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001632 break;
1633 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001634 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1635 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001636 break;
John Thompson22334602010-02-05 00:12:22 +00001637 case tok::kw___vector:
1638 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1639 break;
1640 case tok::kw___pixel:
1641 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1642 break;
1643
Douglas Gregor450c75a2008-11-07 15:42:26 +00001644 // class-specifier:
1645 case tok::kw_class:
1646 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001647 case tok::kw_union: {
1648 tok::TokenKind Kind = Tok.getKind();
1649 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001650 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1651 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001652 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001653 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001654
1655 // enum-specifier:
1656 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001657 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001658 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001659 return true;
1660
1661 // cv-qualifier:
1662 case tok::kw_const:
1663 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001664 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001665 break;
1666 case tok::kw_volatile:
1667 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001668 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001669 break;
1670 case tok::kw_restrict:
1671 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001672 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001673 break;
1674
1675 // GNU typeof support.
1676 case tok::kw_typeof:
1677 ParseTypeofSpecifier(DS);
1678 return true;
1679
Anders Carlsson74948d02009-06-24 17:47:40 +00001680 // C++0x decltype support.
1681 case tok::kw_decltype:
1682 ParseDecltypeSpecifier(DS);
1683 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001684
Anders Carlssonbae27372009-06-26 23:44:14 +00001685 // C++0x auto support.
1686 case tok::kw_auto:
1687 if (!getLang().CPlusPlus0x)
1688 return false;
1689
John McCall49bfce42009-08-03 20:12:06 +00001690 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001691 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001692
Eli Friedman53339e02009-06-08 23:27:34 +00001693 case tok::kw___ptr64:
1694 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001695 case tok::kw___cdecl:
1696 case tok::kw___stdcall:
1697 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001698 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001699 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001700 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001701
Dawn Perchik335e16b2010-09-03 01:29:35 +00001702 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001703 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001704 return true;
1705
Douglas Gregor450c75a2008-11-07 15:42:26 +00001706 default:
1707 // Not a type-specifier; do nothing.
1708 return false;
1709 }
1710
1711 // If the specifier combination wasn't legal, issue a diagnostic.
1712 if (isInvalid) {
1713 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001714 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001715 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001716 }
1717 DS.SetRangeEnd(Tok.getLocation());
1718 ConsumeToken(); // whatever we parsed above.
1719 return true;
1720}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001721
Chris Lattner70ae4912007-10-29 04:42:53 +00001722/// ParseStructDeclaration - Parse a struct declaration without the terminating
1723/// semicolon.
1724///
Chris Lattner90a26b02007-01-23 04:38:16 +00001725/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001726/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001727/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001728/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001729/// struct-declarator-list:
1730/// struct-declarator
1731/// struct-declarator-list ',' struct-declarator
1732/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1733/// struct-declarator:
1734/// declarator
1735/// [GNU] declarator attributes[opt]
1736/// declarator[opt] ':' constant-expression
1737/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1738///
Chris Lattnera12405b2008-04-10 06:46:29 +00001739void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001740ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001741 if (Tok.is(tok::kw___extension__)) {
1742 // __extension__ silences extension warnings in the subexpression.
1743 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001744 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001745 return ParseStructDeclaration(DS, Fields);
1746 }
Mike Stump11289f42009-09-09 15:08:12 +00001747
Steve Naroff97170802007-08-20 22:28:22 +00001748 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00001749 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001750
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001751 // If there are no declarators, this is a free-standing declaration
1752 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001753 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001754 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001755 return;
1756 }
1757
1758 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001759 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001760 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001761 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001762 FieldDeclarator DeclaratorInfo(DS);
1763
1764 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00001765 if (!FirstDeclarator)
1766 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00001767
Steve Naroff97170802007-08-20 22:28:22 +00001768 /// struct-declarator: declarator
1769 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001770 if (Tok.isNot(tok::colon)) {
1771 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1772 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001773 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001774 }
Mike Stump11289f42009-09-09 15:08:12 +00001775
Chris Lattner76c72282007-10-09 17:33:22 +00001776 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001777 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001778 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001779 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001780 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001781 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001782 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001783 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001784
Steve Naroff97170802007-08-20 22:28:22 +00001785 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001786 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001787
John McCallcfefb6d2009-11-03 02:38:08 +00001788 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00001789 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00001790 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001791
Steve Naroff97170802007-08-20 22:28:22 +00001792 // If we don't have a comma, it is either the end of the list (a ';')
1793 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001794 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001795 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001796
Steve Naroff97170802007-08-20 22:28:22 +00001797 // Consume the comma.
1798 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001799
John McCallcfefb6d2009-11-03 02:38:08 +00001800 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001801 }
Steve Naroff97170802007-08-20 22:28:22 +00001802}
1803
1804/// ParseStructUnionBody
1805/// struct-contents:
1806/// struct-declaration-list
1807/// [EXT] empty
1808/// [GNU] "struct-declaration-list" without terminatoring ';'
1809/// struct-declaration-list:
1810/// struct-declaration
1811/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001812/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001813///
Chris Lattner1300fb92007-01-23 23:42:53 +00001814void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001815 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00001816 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1817 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001818
Chris Lattner90a26b02007-01-23 04:38:16 +00001819 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001820
Douglas Gregor658b9552009-01-09 22:42:13 +00001821 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001822 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001823
Chris Lattner7b9ace62007-01-23 20:11:08 +00001824 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1825 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001826 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00001827 Diag(Tok, diag::ext_empty_struct_union)
1828 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001829
John McCall48871652010-08-21 09:40:31 +00001830 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001831
Chris Lattner7b9ace62007-01-23 20:11:08 +00001832 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001833 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001834 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001835
Chris Lattner736ed5d2007-06-09 05:59:07 +00001836 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001837 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001838 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00001839 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00001840 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001841 ConsumeToken();
1842 continue;
1843 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001844
1845 // Parse all the comma separated declarators.
1846 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001847
John McCallcfefb6d2009-11-03 02:38:08 +00001848 if (!Tok.is(tok::at)) {
1849 struct CFieldCallback : FieldCallback {
1850 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001851 Decl *TagDecl;
1852 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001853
John McCall48871652010-08-21 09:40:31 +00001854 CFieldCallback(Parser &P, Decl *TagDecl,
1855 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001856 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1857
John McCall48871652010-08-21 09:40:31 +00001858 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001859 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00001860 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00001861 FD.D.getDeclSpec().getSourceRange().getBegin(),
1862 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001863 FieldDecls.push_back(Field);
1864 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001865 }
John McCallcfefb6d2009-11-03 02:38:08 +00001866 } Callback(*this, TagDecl, FieldDecls);
1867
1868 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001869 } else { // Handle @defs
1870 ConsumeToken();
1871 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1872 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001873 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001874 continue;
1875 }
1876 ConsumeToken();
1877 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1878 if (!Tok.is(tok::identifier)) {
1879 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001880 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001881 continue;
1882 }
John McCall48871652010-08-21 09:40:31 +00001883 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001884 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001885 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001886 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1887 ConsumeToken();
1888 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001889 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001890
Chris Lattner76c72282007-10-09 17:33:22 +00001891 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001892 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001893 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001894 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001895 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001896 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001897 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1898 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001899 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001900 // If we stopped at a ';', eat it.
1901 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001902 }
1903 }
Mike Stump11289f42009-09-09 15:08:12 +00001904
Steve Naroff33a1e802007-10-29 21:38:07 +00001905 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001906
John McCall53fa7142010-12-24 02:08:15 +00001907 ParsedAttributes attrs;
Chris Lattner90a26b02007-01-23 04:38:16 +00001908 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001909 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00001910
Douglas Gregor0be31a22010-07-02 17:43:08 +00001911 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001912 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001913 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00001914 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001915 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001916 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001917}
1918
Chris Lattner3b561a32006-08-13 00:12:11 +00001919/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001920/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001921/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001922///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001923/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1924/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001925/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001926/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001927///
Douglas Gregor0bf31402010-10-08 23:50:27 +00001928/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1929/// [C++0x] enum-head '{' enumerator-list ',' '}'
1930///
1931/// enum-head: [C++0x]
1932/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1933/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1934///
1935/// enum-key: [C++0x]
1936/// 'enum'
1937/// 'enum' 'class'
1938/// 'enum' 'struct'
1939///
1940/// enum-base: [C++0x]
1941/// ':' type-specifier-seq
1942///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001943/// [C++] elaborated-type-specifier:
1944/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1945///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001946void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001947 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001948 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001949 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001950 if (Tok.is(tok::code_completion)) {
1951 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001952 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001953 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001954 }
1955
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001956 // If attributes exist after tag, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001957 ParsedAttributes attrs;
1958 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001959
Abramo Bagnarad7548482010-05-19 21:37:53 +00001960 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001961 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00001962 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00001963 return;
1964
1965 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001966 Diag(Tok, diag::err_expected_ident);
1967 if (Tok.isNot(tok::l_brace)) {
1968 // Has no name and is not a definition.
1969 // Skip the rest of this declarator, up until the comma or semicolon.
1970 SkipUntil(tok::comma, true);
1971 return;
1972 }
1973 }
1974 }
Mike Stump11289f42009-09-09 15:08:12 +00001975
Douglas Gregor0bf31402010-10-08 23:50:27 +00001976 bool IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001977 bool IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00001978
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001979 if (getLang().CPlusPlus0x &&
1980 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001981 IsScopedEnum = true;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001982 IsScopedUsingClassTag = Tok.is(tok::kw_class);
1983 ConsumeToken();
Douglas Gregor0bf31402010-10-08 23:50:27 +00001984 }
1985
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001986 // Must have either 'enum name' or 'enum {...}'.
1987 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1988 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001989
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001990 // Skip the rest of this declarator, up until the comma or semicolon.
1991 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001992 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001993 }
Mike Stump11289f42009-09-09 15:08:12 +00001994
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001995 // If an identifier is present, consume and remember it.
1996 IdentifierInfo *Name = 0;
1997 SourceLocation NameLoc;
1998 if (Tok.is(tok::identifier)) {
1999 Name = Tok.getIdentifierInfo();
2000 NameLoc = ConsumeToken();
2001 }
Mike Stump11289f42009-09-09 15:08:12 +00002002
Douglas Gregor0bf31402010-10-08 23:50:27 +00002003 if (!Name && IsScopedEnum) {
2004 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2005 // declaration of a scoped enumeration.
2006 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2007 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002008 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002009 }
2010
2011 TypeResult BaseType;
2012
Douglas Gregord1f69f62010-12-01 17:42:47 +00002013 // Parse the fixed underlying type.
Douglas Gregor0bf31402010-10-08 23:50:27 +00002014 if (getLang().CPlusPlus0x && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002015 bool PossibleBitfield = false;
2016 if (getCurScope()->getFlags() & Scope::ClassScope) {
2017 // If we're in class scope, this can either be an enum declaration with
2018 // an underlying type, or a declaration of a bitfield member. We try to
2019 // use a simple disambiguation scheme first to catch the common cases
2020 // (integer literal, sizeof); if it's still ambiguous, we then consider
2021 // anything that's a simple-type-specifier followed by '(' as an
2022 // expression. This suffices because function types are not valid
2023 // underlying types anyway.
2024 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2025 // If the next token starts an expression, we know we're parsing a
2026 // bit-field. This is the common case.
2027 if (TPR == TPResult::True())
2028 PossibleBitfield = true;
2029 // If the next token starts a type-specifier-seq, it may be either a
2030 // a fixed underlying type or the start of a function-style cast in C++;
2031 // lookahead one more token to see if it's obvious that we have a
2032 // fixed underlying type.
2033 else if (TPR == TPResult::False() &&
2034 GetLookAheadToken(2).getKind() == tok::semi) {
2035 // Consume the ':'.
2036 ConsumeToken();
2037 } else {
2038 // We have the start of a type-specifier-seq, so we have to perform
2039 // tentative parsing to determine whether we have an expression or a
2040 // type.
2041 TentativeParsingAction TPA(*this);
2042
2043 // Consume the ':'.
2044 ConsumeToken();
2045
2046 if (isCXXDeclarationSpecifier() != TPResult::True()) {
2047 // We'll parse this as a bitfield later.
2048 PossibleBitfield = true;
2049 TPA.Revert();
2050 } else {
2051 // We have a type-specifier-seq.
2052 TPA.Commit();
2053 }
2054 }
2055 } else {
2056 // Consume the ':'.
2057 ConsumeToken();
2058 }
2059
2060 if (!PossibleBitfield) {
2061 SourceRange Range;
2062 BaseType = ParseTypeName(&Range);
2063 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002064 }
2065
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002066 // There are three options here. If we have 'enum foo;', then this is a
2067 // forward declaration. If we have 'enum foo {...' then this is a
2068 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2069 //
2070 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2071 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2072 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2073 //
John McCallfaf5fb42010-08-26 23:41:50 +00002074 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002075 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002076 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002077 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002078 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002079 else
John McCallfaf5fb42010-08-26 23:41:50 +00002080 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002081
2082 // enums cannot be templates, although they can be referenced from a
2083 // template.
2084 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002085 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002086 Diag(Tok, diag::err_enum_template);
2087
2088 // Skip the rest of this declarator, up until the comma or semicolon.
2089 SkipUntil(tok::comma, true);
2090 return;
2091 }
2092
Douglas Gregord6ab8742009-05-28 23:31:59 +00002093 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002094 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002095 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2096 const char *PrevSpec = 0;
2097 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002098 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002099 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002100 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002101 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002102 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002103 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002104
Douglas Gregorba41d012010-04-24 16:38:41 +00002105 if (IsDependent) {
2106 // This enum has a dependent nested-name-specifier. Handle it as a
2107 // dependent tag.
2108 if (!Name) {
2109 DS.SetTypeSpecError();
2110 Diag(Tok, diag::err_expected_type_name_after_typename);
2111 return;
2112 }
2113
Douglas Gregor0be31a22010-07-02 17:43:08 +00002114 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002115 TUK, SS, Name, StartLoc,
2116 NameLoc);
2117 if (Type.isInvalid()) {
2118 DS.SetTypeSpecError();
2119 return;
2120 }
2121
2122 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallba7bf592010-08-24 05:47:05 +00002123 Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002124 Diag(StartLoc, DiagID) << PrevSpec;
2125
2126 return;
2127 }
Mike Stump11289f42009-09-09 15:08:12 +00002128
John McCall48871652010-08-21 09:40:31 +00002129 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002130 // The action failed to produce an enumeration tag. If this is a
2131 // definition, consume the entire definition.
2132 if (Tok.is(tok::l_brace)) {
2133 ConsumeBrace();
2134 SkipUntil(tok::r_brace);
2135 }
2136
2137 DS.SetTypeSpecError();
2138 return;
2139 }
2140
Chris Lattner76c72282007-10-09 17:33:22 +00002141 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002142 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002143
John McCallba7bf592010-08-24 05:47:05 +00002144 // FIXME: The DeclSpec should keep the locations of both the keyword
2145 // and the name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00002146 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCall48871652010-08-21 09:40:31 +00002147 TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002148 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002149}
2150
Chris Lattnerc1915e22007-01-25 07:29:02 +00002151/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2152/// enumerator-list:
2153/// enumerator
2154/// enumerator-list ',' enumerator
2155/// enumerator:
2156/// enumeration-constant
2157/// enumeration-constant '=' constant-expression
2158/// enumeration-constant:
2159/// identifier
2160///
John McCall48871652010-08-21 09:40:31 +00002161void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002162 // Enter the scope of the enum body and start the definition.
2163 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002164 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002165
Chris Lattnerc1915e22007-01-25 07:29:02 +00002166 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002167
Chris Lattner37256fb2007-08-27 17:24:30 +00002168 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002169 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002170 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002171
John McCall48871652010-08-21 09:40:31 +00002172 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002173
John McCall48871652010-08-21 09:40:31 +00002174 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002175
Chris Lattnerc1915e22007-01-25 07:29:02 +00002176 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002177 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002178 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2179 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002180
John McCall811a0f52010-10-22 23:36:17 +00002181 // If attributes exist after the enumerator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002182 ParsedAttributes attrs;
2183 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002184
Chris Lattnerc1915e22007-01-25 07:29:02 +00002185 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002186 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002187 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002188 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002189 AssignedVal = ParseConstantExpression();
2190 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002191 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002192 }
Mike Stump11289f42009-09-09 15:08:12 +00002193
Chris Lattnerc1915e22007-01-25 07:29:02 +00002194 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002195 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2196 LastEnumConstDecl,
2197 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002198 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002199 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002200 EnumConstantDecls.push_back(EnumConstDecl);
2201 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002202
Douglas Gregorce66d022010-09-07 14:51:08 +00002203 if (Tok.is(tok::identifier)) {
2204 // We're missing a comma between enumerators.
2205 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2206 Diag(Loc, diag::err_enumerator_list_missing_comma)
2207 << FixItHint::CreateInsertion(Loc, ", ");
2208 continue;
2209 }
2210
Chris Lattner76c72282007-10-09 17:33:22 +00002211 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002212 break;
2213 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002214
2215 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002216 !(getLang().C99 || getLang().CPlusPlus0x))
2217 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2218 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002219 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002220 }
Mike Stump11289f42009-09-09 15:08:12 +00002221
Chris Lattnerc1915e22007-01-25 07:29:02 +00002222 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002223 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002224
Chris Lattnerc1915e22007-01-25 07:29:02 +00002225 // If attributes exist after the identifier list, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002226 ParsedAttributes attrs;
2227 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002228
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002229 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2230 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002231 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002232
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002233 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002234 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002235}
Chris Lattner3b561a32006-08-13 00:12:11 +00002236
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002237/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002238/// start of a type-qualifier-list.
2239bool Parser::isTypeQualifier() const {
2240 switch (Tok.getKind()) {
2241 default: return false;
2242 // type-qualifier
2243 case tok::kw_const:
2244 case tok::kw_volatile:
2245 case tok::kw_restrict:
2246 return true;
2247 }
2248}
2249
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002250/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2251/// is definitely a type-specifier. Return false if it isn't part of a type
2252/// specifier or if we're not sure.
2253bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2254 switch (Tok.getKind()) {
2255 default: return false;
2256 // type-specifiers
2257 case tok::kw_short:
2258 case tok::kw_long:
2259 case tok::kw_signed:
2260 case tok::kw_unsigned:
2261 case tok::kw__Complex:
2262 case tok::kw__Imaginary:
2263 case tok::kw_void:
2264 case tok::kw_char:
2265 case tok::kw_wchar_t:
2266 case tok::kw_char16_t:
2267 case tok::kw_char32_t:
2268 case tok::kw_int:
2269 case tok::kw_float:
2270 case tok::kw_double:
2271 case tok::kw_bool:
2272 case tok::kw__Bool:
2273 case tok::kw__Decimal32:
2274 case tok::kw__Decimal64:
2275 case tok::kw__Decimal128:
2276 case tok::kw___vector:
2277
2278 // struct-or-union-specifier (C99) or class-specifier (C++)
2279 case tok::kw_class:
2280 case tok::kw_struct:
2281 case tok::kw_union:
2282 // enum-specifier
2283 case tok::kw_enum:
2284
2285 // typedef-name
2286 case tok::annot_typename:
2287 return true;
2288 }
2289}
2290
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002291/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002292/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002293bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002294 switch (Tok.getKind()) {
2295 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002296
Chris Lattner020bab92009-01-04 23:41:41 +00002297 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002298 if (TryAltiVecVectorToken())
2299 return true;
2300 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002301 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002302 // Annotate typenames and C++ scope specifiers. If we get one, just
2303 // recurse to handle whatever we get.
2304 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002305 return true;
2306 if (Tok.is(tok::identifier))
2307 return false;
2308 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002309
Chris Lattner020bab92009-01-04 23:41:41 +00002310 case tok::coloncolon: // ::foo::bar
2311 if (NextToken().is(tok::kw_new) || // ::new
2312 NextToken().is(tok::kw_delete)) // ::delete
2313 return false;
2314
Chris Lattner020bab92009-01-04 23:41:41 +00002315 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002316 return true;
2317 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002318
Chris Lattnere37e2332006-08-15 04:50:22 +00002319 // GNU attributes support.
2320 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002321 // GNU typeof support.
2322 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002323
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002324 // type-specifiers
2325 case tok::kw_short:
2326 case tok::kw_long:
2327 case tok::kw_signed:
2328 case tok::kw_unsigned:
2329 case tok::kw__Complex:
2330 case tok::kw__Imaginary:
2331 case tok::kw_void:
2332 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002333 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002334 case tok::kw_char16_t:
2335 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002336 case tok::kw_int:
2337 case tok::kw_float:
2338 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002339 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002340 case tok::kw__Bool:
2341 case tok::kw__Decimal32:
2342 case tok::kw__Decimal64:
2343 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002344 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002345
Chris Lattner861a2262008-04-13 18:59:07 +00002346 // struct-or-union-specifier (C99) or class-specifier (C++)
2347 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002348 case tok::kw_struct:
2349 case tok::kw_union:
2350 // enum-specifier
2351 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002352
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002353 // type-qualifier
2354 case tok::kw_const:
2355 case tok::kw_volatile:
2356 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002357
2358 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002359 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002360 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002361
Chris Lattner409bf7d2008-10-20 00:25:30 +00002362 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2363 case tok::less:
2364 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002365
Steve Naroff44ac7772008-12-25 14:16:32 +00002366 case tok::kw___cdecl:
2367 case tok::kw___stdcall:
2368 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002369 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002370 case tok::kw___w64:
2371 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002372 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002373 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002374 }
2375}
2376
Chris Lattneracd58a32006-08-06 17:24:14 +00002377/// isDeclarationSpecifier() - Return true if the current token is part of a
2378/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002379///
2380/// \param DisambiguatingWithExpression True to indicate that the purpose of
2381/// this check is to disambiguate between an expression and a declaration.
2382bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002383 switch (Tok.getKind()) {
2384 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002385
Chris Lattner020bab92009-01-04 23:41:41 +00002386 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002387 // Unfortunate hack to support "Class.factoryMethod" notation.
2388 if (getLang().ObjC1 && NextToken().is(tok::period))
2389 return false;
John Thompson22334602010-02-05 00:12:22 +00002390 if (TryAltiVecVectorToken())
2391 return true;
2392 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002393 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002394 // Annotate typenames and C++ scope specifiers. If we get one, just
2395 // recurse to handle whatever we get.
2396 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002397 return true;
2398 if (Tok.is(tok::identifier))
2399 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002400
2401 // If we're in Objective-C and we have an Objective-C class type followed
2402 // by an identifier and then either ':' or ']', in a place where an
2403 // expression is permitted, then this is probably a class message send
2404 // missing the initial '['. In this case, we won't consider this to be
2405 // the start of a declaration.
2406 if (DisambiguatingWithExpression &&
2407 isStartOfObjCClassMessageMissingOpenBracket())
2408 return false;
2409
John McCall1f476a12010-02-26 08:45:28 +00002410 return isDeclarationSpecifier();
2411
Chris Lattner020bab92009-01-04 23:41:41 +00002412 case tok::coloncolon: // ::foo::bar
2413 if (NextToken().is(tok::kw_new) || // ::new
2414 NextToken().is(tok::kw_delete)) // ::delete
2415 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002416
Chris Lattner020bab92009-01-04 23:41:41 +00002417 // Annotate typenames and C++ scope specifiers. If we get one, just
2418 // recurse to handle whatever we get.
2419 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002420 return true;
2421 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002422
Chris Lattneracd58a32006-08-06 17:24:14 +00002423 // storage-class-specifier
2424 case tok::kw_typedef:
2425 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002426 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002427 case tok::kw_static:
2428 case tok::kw_auto:
2429 case tok::kw_register:
2430 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002431
Chris Lattneracd58a32006-08-06 17:24:14 +00002432 // type-specifiers
2433 case tok::kw_short:
2434 case tok::kw_long:
2435 case tok::kw_signed:
2436 case tok::kw_unsigned:
2437 case tok::kw__Complex:
2438 case tok::kw__Imaginary:
2439 case tok::kw_void:
2440 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002441 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002442 case tok::kw_char16_t:
2443 case tok::kw_char32_t:
2444
Chris Lattneracd58a32006-08-06 17:24:14 +00002445 case tok::kw_int:
2446 case tok::kw_float:
2447 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002448 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002449 case tok::kw__Bool:
2450 case tok::kw__Decimal32:
2451 case tok::kw__Decimal64:
2452 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002453 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002454
Chris Lattner861a2262008-04-13 18:59:07 +00002455 // struct-or-union-specifier (C99) or class-specifier (C++)
2456 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002457 case tok::kw_struct:
2458 case tok::kw_union:
2459 // enum-specifier
2460 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002461
Chris Lattneracd58a32006-08-06 17:24:14 +00002462 // type-qualifier
2463 case tok::kw_const:
2464 case tok::kw_volatile:
2465 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002466
Chris Lattneracd58a32006-08-06 17:24:14 +00002467 // function-specifier
2468 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002469 case tok::kw_virtual:
2470 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002471
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002472 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002473 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002474
Chris Lattner599e47e2007-08-09 17:01:07 +00002475 // GNU typeof support.
2476 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002477
Chris Lattner599e47e2007-08-09 17:01:07 +00002478 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002479 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002480 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002481
Chris Lattner8b2ec162008-07-26 03:38:44 +00002482 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2483 case tok::less:
2484 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002485
Steve Narofff192fab2009-01-06 19:34:12 +00002486 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002487 case tok::kw___cdecl:
2488 case tok::kw___stdcall:
2489 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002490 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002491 case tok::kw___w64:
2492 case tok::kw___ptr64:
2493 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002494 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002495 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002496 }
2497}
2498
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002499bool Parser::isConstructorDeclarator() {
2500 TentativeParsingAction TPA(*this);
2501
2502 // Parse the C++ scope specifier.
2503 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002504 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002505 TPA.Revert();
2506 return false;
2507 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002508
2509 // Parse the constructor name.
2510 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2511 // We already know that we have a constructor name; just consume
2512 // the token.
2513 ConsumeToken();
2514 } else {
2515 TPA.Revert();
2516 return false;
2517 }
2518
2519 // Current class name must be followed by a left parentheses.
2520 if (Tok.isNot(tok::l_paren)) {
2521 TPA.Revert();
2522 return false;
2523 }
2524 ConsumeParen();
2525
2526 // A right parentheses or ellipsis signals that we have a constructor.
2527 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2528 TPA.Revert();
2529 return true;
2530 }
2531
2532 // If we need to, enter the specified scope.
2533 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002534 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002535 DeclScopeObj.EnterDeclaratorScope();
2536
Francois Pichet79f3a872011-01-31 04:54:32 +00002537 // Optionally skip Microsoft attributes.
2538 ParsedAttributes Attrs;
2539 MaybeParseMicrosoftAttributes(Attrs);
2540
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002541 // Check whether the next token(s) are part of a declaration
2542 // specifier, in which case we have the start of a parameter and,
2543 // therefore, we know that this is a constructor.
2544 bool IsConstructor = isDeclarationSpecifier();
2545 TPA.Revert();
2546 return IsConstructor;
2547}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002548
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002549/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00002550/// type-qualifier-list: [C99 6.7.5]
2551/// type-qualifier
2552/// [vendor] attributes
2553/// [ only if VendorAttributesAllowed=true ]
2554/// type-qualifier-list type-qualifier
2555/// [vendor] type-qualifier-list attributes
2556/// [ only if VendorAttributesAllowed=true ]
2557/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2558/// [ only if CXX0XAttributesAllowed=true ]
2559/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002560///
Dawn Perchik335e16b2010-09-03 01:29:35 +00002561void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2562 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00002563 bool CXX0XAttributesAllowed) {
2564 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2565 SourceLocation Loc = Tok.getLocation();
John McCall53fa7142010-12-24 02:08:15 +00002566 ParsedAttributesWithRange attrs;
2567 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002568 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00002569 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002570 else
2571 Diag(Loc, diag::err_attributes_not_allowed);
2572 }
2573
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002574 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002575 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002576 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002577 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002578 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002579
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002580 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00002581 case tok::code_completion:
2582 Actions.CodeCompleteTypeQualifiers(DS);
2583 ConsumeCodeCompletionToken();
2584 break;
2585
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002586 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002587 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2588 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002589 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002590 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002591 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2592 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002593 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002594 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002595 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2596 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002597 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002598 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002599 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002600 case tok::kw___cdecl:
2601 case tok::kw___stdcall:
2602 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002603 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002604 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002605 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002606 continue;
2607 }
2608 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002609 case tok::kw___pascal:
2610 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002611 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002612 continue;
2613 }
2614 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002615 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002616 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002617 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002618 continue; // do *not* consume the next token!
2619 }
2620 // otherwise, FALL THROUGH!
2621 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002622 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002623 // If this is not a type-qualifier token, we're done reading type
2624 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002625 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002626 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002627 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002628
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002629 // If the specifier combination wasn't legal, issue a diagnostic.
2630 if (isInvalid) {
2631 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002632 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002633 }
2634 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002635 }
2636}
2637
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002638
2639/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2640///
2641void Parser::ParseDeclarator(Declarator &D) {
2642 /// This implements the 'declarator' production in the C grammar, then checks
2643 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002644 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002645}
2646
Sebastian Redlbd150f42008-11-21 19:14:01 +00002647/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2648/// is parsed by the function passed to it. Pass null, and the direct-declarator
2649/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002650/// ptr-operator production.
2651///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002652/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2653/// [C] pointer[opt] direct-declarator
2654/// [C++] direct-declarator
2655/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002656///
2657/// pointer: [C99 6.7.5]
2658/// '*' type-qualifier-list[opt]
2659/// '*' type-qualifier-list[opt] pointer
2660///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002661/// ptr-operator:
2662/// '*' cv-qualifier-seq[opt]
2663/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002664/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002665/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002666/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002667/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002668void Parser::ParseDeclaratorInternal(Declarator &D,
2669 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002670 if (Diags.hasAllExtensionsSilenced())
2671 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002672
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002673 // C++ member pointers start with a '::' or a nested-name.
2674 // Member pointers get special handling, since there's no place for the
2675 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002676 if (getLang().CPlusPlus &&
2677 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2678 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002679 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002680 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00002681
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002682 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002683 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002684 // The scope spec really belongs to the direct-declarator.
2685 D.getCXXScopeSpec() = SS;
2686 if (DirectDeclParser)
2687 (this->*DirectDeclParser)(D);
2688 return;
2689 }
2690
2691 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002692 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002693 DeclSpec DS;
2694 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002695 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002696
2697 // Recurse to parse whatever is left.
2698 ParseDeclaratorInternal(D, DirectDeclParser);
2699
2700 // Sema will have to catch (syntactically invalid) pointers into global
2701 // scope. It has to catch pointers into namespace scope anyway.
2702 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall53fa7142010-12-24 02:08:15 +00002703 Loc, DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002704 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002705 return;
2706 }
2707 }
2708
2709 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002710 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002711 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002712 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002713 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002714 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002715 if (DirectDeclParser)
2716 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002717 return;
2718 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002719
Sebastian Redled0f3b02009-03-15 22:02:01 +00002720 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2721 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002722 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002723 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002724
Chris Lattner9eac9312009-03-27 04:18:06 +00002725 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002726 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002727 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002728
Bill Wendling3708c182007-05-27 10:15:43 +00002729 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002730 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002731
Bill Wendling3708c182007-05-27 10:15:43 +00002732 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002733 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002734 if (Kind == tok::star)
2735 // Remember that we parsed a pointer type, and remember the type-quals.
2736 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
John McCall53fa7142010-12-24 02:08:15 +00002737 DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002738 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002739 else
2740 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002741 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall53fa7142010-12-24 02:08:15 +00002742 Loc, DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002743 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002744 } else {
2745 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002746 DeclSpec DS;
2747
Sebastian Redl3b27be62009-03-23 00:00:23 +00002748 // Complain about rvalue references in C++03, but then go on and build
2749 // the declarator.
2750 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00002751 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00002752
Bill Wendling93efb222007-06-02 23:28:54 +00002753 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2754 // cv-qualifiers are introduced through the use of a typedef or of a
2755 // template type argument, in which case the cv-qualifiers are ignored.
2756 //
2757 // [GNU] Retricted references are allowed.
2758 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002759 // [C++0x] Attributes on references are not allowed.
2760 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002761 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002762
2763 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2764 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2765 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002766 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002767 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2768 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002769 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002770 }
Bill Wendling3708c182007-05-27 10:15:43 +00002771
2772 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002773 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002774
Douglas Gregor66583c52008-11-03 15:51:28 +00002775 if (D.getNumTypeObjects() > 0) {
2776 // C++ [dcl.ref]p4: There shall be no references to references.
2777 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2778 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002779 if (const IdentifierInfo *II = D.getIdentifier())
2780 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2781 << II;
2782 else
2783 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2784 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002785
Sebastian Redlbd150f42008-11-21 19:14:01 +00002786 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002787 // can go ahead and build the (technically ill-formed)
2788 // declarator: reference collapsing will take care of it.
2789 }
2790 }
2791
Bill Wendling3708c182007-05-27 10:15:43 +00002792 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002793 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
John McCall53fa7142010-12-24 02:08:15 +00002794 DS.takeAttributes(),
Sebastian Redled0f3b02009-03-15 22:02:01 +00002795 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002796 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002797 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002798}
2799
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002800/// ParseDirectDeclarator
2801/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002802/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002803/// '(' declarator ')'
2804/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002805/// [C90] direct-declarator '[' constant-expression[opt] ']'
2806/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2807/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2808/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2809/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002810/// direct-declarator '(' parameter-type-list ')'
2811/// direct-declarator '(' identifier-list[opt] ')'
2812/// [GNU] direct-declarator '(' parameter-forward-declarations
2813/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002814/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2815/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002816/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002817///
2818/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00002819/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00002820/// '::'[opt] nested-name-specifier[opt] type-name
2821///
2822/// id-expression: [C++ 5.1]
2823/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002824/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002825///
2826/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002827/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002828/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002829/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002830/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002831/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002832///
Chris Lattneracd58a32006-08-06 17:24:14 +00002833void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002834 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002835
Douglas Gregor7861a802009-11-03 01:35:08 +00002836 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2837 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002838 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00002839 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00002840 }
2841
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002842 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002843 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002844 // Change the declaration context for name lookup, until this function
2845 // is exited (and the declarator has been parsed).
2846 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002847 }
2848
Douglas Gregor27b4c162010-12-23 22:44:42 +00002849 // C++0x [dcl.fct]p14:
2850 // There is a syntactic ambiguity when an ellipsis occurs at the end
2851 // of a parameter-declaration-clause without a preceding comma. In
2852 // this case, the ellipsis is parsed as part of the
2853 // abstract-declarator if the type of the parameter names a template
2854 // parameter pack that has not been expanded; otherwise, it is parsed
2855 // as part of the parameter-declaration-clause.
2856 if (Tok.is(tok::ellipsis) &&
2857 !((D.getContext() == Declarator::PrototypeContext ||
2858 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00002859 NextToken().is(tok::r_paren) &&
2860 !Actions.containsUnexpandedParameterPacks(D)))
2861 D.setEllipsisLoc(ConsumeToken());
2862
Douglas Gregor7861a802009-11-03 01:35:08 +00002863 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2864 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2865 // We found something that indicates the start of an unqualified-id.
2866 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002867 bool AllowConstructorName;
2868 if (D.getDeclSpec().hasTypeSpecifier())
2869 AllowConstructorName = false;
2870 else if (D.getCXXScopeSpec().isSet())
2871 AllowConstructorName =
2872 (D.getContext() == Declarator::FileContext ||
2873 (D.getContext() == Declarator::MemberContext &&
2874 D.getDeclSpec().isFriendSpecified()));
2875 else
2876 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2877
Douglas Gregor7861a802009-11-03 01:35:08 +00002878 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2879 /*EnteringContext=*/true,
2880 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002881 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002882 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002883 D.getName()) ||
2884 // Once we're past the identifier, if the scope was bad, mark the
2885 // whole declarator bad.
2886 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002887 D.SetIdentifier(0, Tok.getLocation());
2888 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002889 } else {
2890 // Parsed the unqualified-id; update range information and move along.
2891 if (D.getSourceRange().getBegin().isInvalid())
2892 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2893 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002894 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002895 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002896 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002897 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002898 assert(!getLang().CPlusPlus &&
2899 "There's a C++-specific check for tok::identifier above");
2900 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2901 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2902 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002903 goto PastIdentifier;
2904 }
2905
2906 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002907 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002908 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002909 // Example: 'char (*X)' or 'int (*XX)(void)'
2910 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002911
2912 // If the declarator was parenthesized, we entered the declarator
2913 // scope when parsing the parenthesized declarator, then exited
2914 // the scope already. Re-enter the scope, if we need to.
2915 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002916 // If there was an error parsing parenthesized declarator, declarator
2917 // scope may have been enterred before. Don't do it again.
2918 if (!D.isInvalidType() &&
2919 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002920 // Change the declaration context for name lookup, until this function
2921 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002922 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002923 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002924 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002925 // This could be something simple like "int" (in which case the declarator
2926 // portion is empty), if an abstract-declarator is allowed.
2927 D.SetIdentifier(0, Tok.getLocation());
2928 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002929 if (D.getContext() == Declarator::MemberContext)
2930 Diag(Tok, diag::err_expected_member_name_or_semi)
2931 << D.getDeclSpec().getSourceRange();
2932 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002933 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002934 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002935 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002936 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002937 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002938 }
Mike Stump11289f42009-09-09 15:08:12 +00002939
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002940 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002941 assert(D.isPastIdentifier() &&
2942 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002943
Alexis Hunt96d5c762009-11-21 08:43:09 +00002944 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00002945 if (D.getIdentifier())
2946 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002947
Chris Lattneracd58a32006-08-06 17:24:14 +00002948 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002949 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002950 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2951 // In such a case, check if we actually have a function declarator; if it
2952 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002953 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2954 // When not in file scope, warn for ambiguous function declarators, just
2955 // in case the author intended it as a variable definition.
2956 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2957 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2958 break;
2959 }
John McCall53fa7142010-12-24 02:08:15 +00002960 ParsedAttributes attrs;
2961 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00002962 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002963 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002964 } else {
2965 break;
2966 }
2967 }
2968}
2969
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002970/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2971/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002972/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002973/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2974///
2975/// direct-declarator:
2976/// '(' declarator ')'
2977/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002978/// direct-declarator '(' parameter-type-list ')'
2979/// direct-declarator '(' identifier-list[opt] ')'
2980/// [GNU] direct-declarator '(' parameter-forward-declarations
2981/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002982///
2983void Parser::ParseParenDeclarator(Declarator &D) {
2984 SourceLocation StartLoc = ConsumeParen();
2985 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002986
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002987 // Eat any attributes before we look at whether this is a grouping or function
2988 // declarator paren. If this is a grouping paren, the attribute applies to
2989 // the type being built up, for example:
2990 // int (__attribute__(()) *x)(long y)
2991 // If this ends up not being a grouping paren, the attribute applies to the
2992 // first argument, for example:
2993 // int (__attribute__(()) int x)
2994 // In either case, we need to eat any attributes to be able to determine what
2995 // sort of paren this is.
2996 //
John McCall53fa7142010-12-24 02:08:15 +00002997 ParsedAttributes attrs;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002998 bool RequiresArg = false;
2999 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003000 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003001
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003002 // We require that the argument list (if this is a non-grouping paren) be
3003 // present even if the attribute list was empty.
3004 RequiresArg = true;
3005 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003006 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003007 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003008 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3009 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall53fa7142010-12-24 02:08:15 +00003010 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003011 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003012 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003013 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003014 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003015
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003016 // If we haven't past the identifier yet (or where the identifier would be
3017 // stored, if this is an abstract declarator), then this is probably just
3018 // grouping parens. However, if this could be an abstract-declarator, then
3019 // this could also be the start of function arguments (consider 'void()').
3020 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003021
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003022 if (!D.mayOmitIdentifier()) {
3023 // If this can't be an abstract-declarator, this *must* be a grouping
3024 // paren, because we haven't seen the identifier yet.
3025 isGrouping = true;
3026 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003027 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003028 isDeclarationSpecifier()) { // 'int(int)' is a function.
3029 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3030 // considered to be a type, not a K&R identifier-list.
3031 isGrouping = false;
3032 } else {
3033 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3034 isGrouping = true;
3035 }
Mike Stump11289f42009-09-09 15:08:12 +00003036
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003037 // If this is a grouping paren, handle:
3038 // direct-declarator: '(' declarator ')'
3039 // direct-declarator: '(' attributes declarator ')'
3040 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003041 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003042 D.setGroupingParens(true);
John McCall53fa7142010-12-24 02:08:15 +00003043 if (!attrs.empty())
3044 D.addAttributes(attrs.getList(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003045
Sebastian Redlbd150f42008-11-21 19:14:01 +00003046 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003047 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003048 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
3049 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc), EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003050
3051 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003052 return;
3053 }
Mike Stump11289f42009-09-09 15:08:12 +00003054
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003055 // Okay, if this wasn't a grouping paren, it must be the start of a function
3056 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003057 // identifier (and remember where it would have been), then call into
3058 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003059 D.SetIdentifier(0, Tok.getLocation());
3060
John McCall53fa7142010-12-24 02:08:15 +00003061 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003062}
3063
3064/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3065/// declarator D up to a paren, which indicates that we are parsing function
3066/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003067///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003068/// If AttrList is non-null, then the caller parsed those arguments immediately
3069/// after the open paren - they should be considered to be the first argument of
3070/// a parameter. If RequiresArg is true, then the first argument of the
3071/// function is required to be present and required to not be an identifier
3072/// list.
3073///
Chris Lattneracd58a32006-08-06 17:24:14 +00003074/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003075/// parameter-type-list: [C99 6.7.5]
3076/// parameter-list
3077/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003078/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003079///
3080/// parameter-list: [C99 6.7.5]
3081/// parameter-declaration
3082/// parameter-list ',' parameter-declaration
3083///
3084/// parameter-declaration: [C99 6.7.5]
3085/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003086/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003087/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003088/// declaration-specifiers abstract-declarator[opt]
3089/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003090/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003091/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003092///
Douglas Gregor54992352011-01-26 03:43:54 +00003093/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3094/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003095///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003096void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall53fa7142010-12-24 02:08:15 +00003097 ParsedAttributes &attrs,
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003098 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003099 // lparen is already consumed!
3100 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003101
Douglas Gregor7fb25412010-10-01 18:44:50 +00003102 ParsedType TrailingReturnType;
3103
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003104 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003105 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003106 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003107 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003108
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003109 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3110 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003111
3112 // cv-qualifier-seq[opt].
3113 DeclSpec DS;
Douglas Gregor54992352011-01-26 03:43:54 +00003114 SourceLocation RefQualifierLoc;
3115 bool RefQualifierIsLValueRef = true;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003116 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003117 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003118 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003119 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003120 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003121 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003122 MaybeParseCXX0XAttributes(attrs);
3123
Chris Lattnercf0bab22008-12-18 07:02:59 +00003124 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003125 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003126 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003127
Douglas Gregor54992352011-01-26 03:43:54 +00003128 // Parse ref-qualifier[opt]
3129 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3130 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003131 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003132
3133 RefQualifierIsLValueRef = Tok.is(tok::amp);
3134 RefQualifierLoc = ConsumeToken();
3135 EndLoc = RefQualifierLoc;
3136 }
3137
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003138 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003139 if (Tok.is(tok::kw_throw)) {
3140 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003141 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003142 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003143 hasAnyExceptionSpec);
3144 assert(Exceptions.size() == ExceptionRanges.size() &&
3145 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003146 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003147
3148 // Parse trailing-return-type.
3149 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3150 TrailingReturnType = ParseTrailingReturnType().get();
3151 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003152 }
3153
Chris Lattner371ed4e2008-04-06 06:57:35 +00003154 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003155 // int() -> no prototype, no '...'.
John McCall53fa7142010-12-24 02:08:15 +00003156 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3157 /*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003158 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003159 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003160 /*arglist*/ 0, 0,
3161 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003162 RefQualifierIsLValueRef,
3163 RefQualifierLoc,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003164 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003165 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003166 Exceptions.data(),
3167 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003168 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003169 LParenLoc, RParenLoc, D,
3170 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003171 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003172 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003173 }
3174
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003175 // Alternatively, this parameter list may be an identifier list form for a
3176 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003177 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3178 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003179 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003180 // K&R identifier lists can't have typedefs as identifiers, per
3181 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003182 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003183 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003184
Steve Naroffb0486722009-01-28 19:16:40 +00003185 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003186 // normal declarators, not for abstract-declarators. Get the first
3187 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003188 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003189 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003190
3191 // Identifier lists follow a really simple grammar: the identifiers can
3192 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3193 // identifier lists are really rare in the brave new modern world, and it
3194 // is very common for someone to typo a type in a non-k&r style list. If
3195 // we are presented with something like: "void foo(intptr x, float y)",
3196 // we don't want to start parsing the function declarator as though it is
3197 // a K&R style declarator just because intptr is an invalid type.
3198 //
3199 // To handle this, we check to see if the token after the first identifier
3200 // is a "," or ")". Only if so, do we parse it as an identifier list.
3201 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3202 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3203 FirstTok.getIdentifierInfo(),
3204 FirstTok.getLocation(), D);
3205
3206 // If we get here, the code is invalid. Push the first identifier back
3207 // into the token stream and parse the first argument as an (invalid)
3208 // normal argument declarator.
3209 PP.EnterToken(Tok);
3210 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003211 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003212 }
Mike Stump11289f42009-09-09 15:08:12 +00003213
Chris Lattner371ed4e2008-04-06 06:57:35 +00003214 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003215
Chris Lattner371ed4e2008-04-06 06:57:35 +00003216 // Build up an array of information about the parsed arguments.
3217 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003218
3219 // Enter function-declaration scope, limiting any declarators to the
3220 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003221 ParseScope PrototypeScope(this,
3222 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003223
Chris Lattner371ed4e2008-04-06 06:57:35 +00003224 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003225 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003226 while (1) {
3227 if (Tok.is(tok::ellipsis)) {
3228 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003229 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003230 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003231 }
Mike Stump11289f42009-09-09 15:08:12 +00003232
Chris Lattner371ed4e2008-04-06 06:57:35 +00003233 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003234 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003235 DeclSpec DS;
John McCall53fa7142010-12-24 02:08:15 +00003236
3237 // Skip any Microsoft attributes before a param.
3238 if (getLang().Microsoft && Tok.is(tok::l_square))
3239 ParseMicrosoftAttributes(DS.getAttributes());
3240
3241 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003242
3243 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00003244 // Take them so that we only apply the attributes to the first parameter.
3245 DS.takeAttributesFrom(attrs);
3246
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003247 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003248
Chris Lattner371ed4e2008-04-06 06:57:35 +00003249 // Parse the declarator. This is "PrototypeContext", because we must
3250 // accept either 'declarator' or 'abstract-declarator' here.
3251 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3252 ParseDeclarator(ParmDecl);
3253
3254 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00003255 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003256
Chris Lattner371ed4e2008-04-06 06:57:35 +00003257 // Remember this parsed parameter in ParamInfo.
3258 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003259
Douglas Gregor4d87df52008-12-16 21:30:33 +00003260 // DefArgToks is used when the parsing of default arguments needs
3261 // to be delayed.
3262 CachedTokens *DefArgToks = 0;
3263
Chris Lattner371ed4e2008-04-06 06:57:35 +00003264 // If no parameter was specified, verify that *something* was specified,
3265 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003266 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3267 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003268 // Completely missing, emit error.
3269 Diag(DSStart, diag::err_missing_param);
3270 } else {
3271 // Otherwise, we have something. Add it and let semantic analysis try
3272 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003273
Chris Lattner371ed4e2008-04-06 06:57:35 +00003274 // Inform the actions module about the parameter declarator, so it gets
3275 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003276 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003277
3278 // Parse the default argument, if any. We parse the default
3279 // arguments in all dialects; the semantic analysis in
3280 // ActOnParamDefaultArgument will reject the default argument in
3281 // C.
3282 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003283 SourceLocation EqualLoc = Tok.getLocation();
3284
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003285 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003286 if (D.getContext() == Declarator::MemberContext) {
3287 // If we're inside a class definition, cache the tokens
3288 // corresponding to the default argument. We'll actually parse
3289 // them when we see the end of the class definition.
3290 // FIXME: Templates will require something similar.
3291 // FIXME: Can we use a smart pointer for Toks?
3292 DefArgToks = new CachedTokens;
3293
Mike Stump11289f42009-09-09 15:08:12 +00003294 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003295 /*StopAtSemi=*/true,
3296 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003297 delete DefArgToks;
3298 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003299 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003300 } else {
3301 // Mark the end of the default argument so that we know when to
3302 // stop when we parse it later on.
3303 Token DefArgEnd;
3304 DefArgEnd.startToken();
3305 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3306 DefArgEnd.setLocation(Tok.getLocation());
3307 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003308 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003309 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003310 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003311 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003312 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003313 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003314
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003315 // The argument isn't actually potentially evaluated unless it is
3316 // used.
3317 EnterExpressionEvaluationContext Eval(Actions,
3318 Sema::PotentiallyEvaluatedIfUsed);
3319
John McCalldadc5752010-08-24 06:29:42 +00003320 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003321 if (DefArgResult.isInvalid()) {
3322 Actions.ActOnParamDefaultArgumentError(Param);
3323 SkipUntil(tok::comma, tok::r_paren, true, true);
3324 } else {
3325 // Inform the actions module about the default argument
3326 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003327 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003328 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003329 }
3330 }
Mike Stump11289f42009-09-09 15:08:12 +00003331
3332 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3333 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003334 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003335 }
3336
3337 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003338 if (Tok.isNot(tok::comma)) {
3339 if (Tok.is(tok::ellipsis)) {
3340 IsVariadic = true;
3341 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3342
3343 if (!getLang().CPlusPlus) {
3344 // We have ellipsis without a preceding ',', which is ill-formed
3345 // in C. Complain and provide the fix.
3346 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003347 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003348 }
3349 }
3350
3351 break;
3352 }
Mike Stump11289f42009-09-09 15:08:12 +00003353
Chris Lattner371ed4e2008-04-06 06:57:35 +00003354 // Consume the comma.
3355 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003356 }
Mike Stump11289f42009-09-09 15:08:12 +00003357
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003358 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003359 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3360 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003361
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003362 DeclSpec DS;
Douglas Gregor54992352011-01-26 03:43:54 +00003363 SourceLocation RefQualifierLoc;
3364 bool RefQualifierIsLValueRef = true;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003365 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003366 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003367 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003368 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003369 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003370
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003371 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003372 MaybeParseCXX0XAttributes(attrs);
3373
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003374 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003375 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003376 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003377 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003378
Douglas Gregor54992352011-01-26 03:43:54 +00003379 // Parse ref-qualifier[opt]
3380 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3381 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003382 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003383
3384 RefQualifierIsLValueRef = Tok.is(tok::amp);
3385 RefQualifierLoc = ConsumeToken();
3386 EndLoc = RefQualifierLoc;
3387 }
3388
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003389 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003390 if (Tok.is(tok::kw_throw)) {
3391 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003392 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003393 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003394 hasAnyExceptionSpec);
3395 assert(Exceptions.size() == ExceptionRanges.size() &&
3396 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003397 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003398
3399 // Parse trailing-return-type.
3400 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3401 TrailingReturnType = ParseTrailingReturnType().get();
3402 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003403 }
3404
Douglas Gregor7fb25412010-10-01 18:44:50 +00003405 // FIXME: We should leave the prototype scope before parsing the exception
3406 // specification, and then reenter it when parsing the trailing return type.
3407
3408 // Leave prototype scope.
3409 PrototypeScope.Exit();
3410
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003411 // Remember that we parsed a function type, and remember the attributes.
John McCall53fa7142010-12-24 02:08:15 +00003412 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3413 /*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003414 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003415 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003416 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003417 RefQualifierIsLValueRef,
3418 RefQualifierLoc,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003419 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003420 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003421 Exceptions.data(),
3422 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003423 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003424 LParenLoc, RParenLoc, D,
3425 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003426 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003427}
Chris Lattneracd58a32006-08-06 17:24:14 +00003428
Chris Lattner6c940e62008-04-06 06:34:08 +00003429/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3430/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003431/// first identifier has already been consumed, and the current token is the
3432/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003433///
3434/// identifier-list: [C99 6.7.5]
3435/// identifier
3436/// identifier-list ',' identifier
3437///
3438void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003439 IdentifierInfo *FirstIdent,
3440 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003441 Declarator &D) {
3442 // Build up an array of information about the parsed arguments.
3443 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3444 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003445
Chris Lattner6c940e62008-04-06 06:34:08 +00003446 // If there was no identifier specified for the declarator, either we are in
3447 // an abstract-declarator, or we are in a parameter declarator which was found
3448 // to be abstract. In abstract-declarators, identifier lists are not valid:
3449 // diagnose this.
3450 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003451 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003452
Chris Lattner9453ab82010-05-14 17:23:36 +00003453 // The first identifier was already read, and is known to be the first
3454 // identifier in the list. Remember this identifier in ParamInfo.
3455 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003456 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003457
Chris Lattner6c940e62008-04-06 06:34:08 +00003458 while (Tok.is(tok::comma)) {
3459 // Eat the comma.
3460 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003461
Chris Lattner9186f552008-04-06 06:39:19 +00003462 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003463 if (Tok.isNot(tok::identifier)) {
3464 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003465 SkipUntil(tok::r_paren);
3466 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003467 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003468
Chris Lattner6c940e62008-04-06 06:34:08 +00003469 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003470
3471 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003472 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003473 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003474
Chris Lattner6c940e62008-04-06 06:34:08 +00003475 // Verify that the argument identifier has not already been mentioned.
3476 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003477 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003478 } else {
3479 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003480 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003481 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003482 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003483 }
Mike Stump11289f42009-09-09 15:08:12 +00003484
Chris Lattner6c940e62008-04-06 06:34:08 +00003485 // Eat the identifier.
3486 ConsumeToken();
3487 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003488
3489 // If we have the closing ')', eat it and we're done.
3490 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3491
Chris Lattner9186f552008-04-06 06:39:19 +00003492 // Remember that we parsed a function type, and remember the attributes. This
3493 // function type is always a K&R style function type, which is not varargs and
3494 // has no prototype.
John McCall53fa7142010-12-24 02:08:15 +00003495 D.AddTypeInfo(DeclaratorChunk::getFunction(ParsedAttributes(),
3496 /*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003497 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003498 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003499 /*TypeQuals*/0,
Douglas Gregor54992352011-01-26 03:43:54 +00003500 true, SourceLocation(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003501 /*exception*/false,
3502 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003503 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003504 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003505}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003506
Chris Lattnere8074e62006-08-06 18:30:15 +00003507/// [C90] direct-declarator '[' constant-expression[opt] ']'
3508/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3509/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3510/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3511/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3512void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003513 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003514
Chris Lattner84a11622008-12-18 07:27:21 +00003515 // C array syntax has many features, but by-far the most common is [] and [4].
3516 // This code does a fast path to handle some of the most obvious cases.
3517 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003518 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall53fa7142010-12-24 02:08:15 +00003519 ParsedAttributes attrs;
3520 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003521
Chris Lattner84a11622008-12-18 07:27:21 +00003522 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00003523 ExprResult NumElements;
John McCall53fa7142010-12-24 02:08:15 +00003524 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00003525 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003526 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003527 return;
3528 } else if (Tok.getKind() == tok::numeric_constant &&
3529 GetLookAheadToken(1).is(tok::r_square)) {
3530 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00003531 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003532 ConsumeToken();
3533
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003534 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall53fa7142010-12-24 02:08:15 +00003535 ParsedAttributes attrs;
3536 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003537
Chris Lattner84a11622008-12-18 07:27:21 +00003538 // Remember that we parsed a array type, and remember its features.
John McCall53fa7142010-12-24 02:08:15 +00003539 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, 0,
3540 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00003541 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003542 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003543 return;
3544 }
Mike Stump11289f42009-09-09 15:08:12 +00003545
Chris Lattnere8074e62006-08-06 18:30:15 +00003546 // If valid, this location is the position where we read the 'static' keyword.
3547 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003548 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003549 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003550
Chris Lattnere8074e62006-08-06 18:30:15 +00003551 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003552 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003553 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003554 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003555
Chris Lattnere8074e62006-08-06 18:30:15 +00003556 // If we haven't already read 'static', check to see if there is one after the
3557 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003558 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003559 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003560
Chris Lattnere8074e62006-08-06 18:30:15 +00003561 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003562 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00003563 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003564
Chris Lattner521ff2b2008-04-06 05:26:30 +00003565 // Handle the case where we have '[*]' as the array size. However, a leading
3566 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3567 // the the token after the star is a ']'. Since stars in arrays are
3568 // infrequent, use of lookahead is not costly here.
3569 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003570 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003571
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003572 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003573 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003574 StaticLoc = SourceLocation(); // Drop the static.
3575 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003576 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003577 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003578 // Note, in C89, this production uses the constant-expr production instead
3579 // of assignment-expr. The only difference is that assignment-expr allows
3580 // things like '=' and '*='. Sema rejects these in C89 mode because they
3581 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003582
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003583 // Parse the constant-expression or assignment-expression now (depending
3584 // on dialect).
3585 if (getLang().CPlusPlus)
3586 NumElements = ParseConstantExpression();
3587 else
3588 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003589 }
Mike Stump11289f42009-09-09 15:08:12 +00003590
Chris Lattner62591722006-08-12 18:40:58 +00003591 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003592 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003593 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003594 // If the expression was invalid, skip it.
3595 SkipUntil(tok::r_square);
3596 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003597 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003598
3599 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3600
John McCall53fa7142010-12-24 02:08:15 +00003601 ParsedAttributes attrs;
3602 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003603
Chris Lattner84a11622008-12-18 07:27:21 +00003604 // Remember that we parsed a array type, and remember its features.
John McCall53fa7142010-12-24 02:08:15 +00003605 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), attrs,
Chris Lattnercbc426d2006-12-02 06:43:02 +00003606 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003607 NumElements.release(),
3608 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003609 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003610}
3611
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003612/// [GNU] typeof-specifier:
3613/// typeof ( expressions )
3614/// typeof ( type-name )
3615/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003616///
3617void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003618 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003619 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003620 SourceLocation StartLoc = ConsumeToken();
3621
John McCalle8595032010-01-13 20:03:27 +00003622 const bool hasParens = Tok.is(tok::l_paren);
3623
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003624 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00003625 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003626 SourceRange CastRange;
John McCalldadc5752010-08-24 06:29:42 +00003627 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall6caebb12010-08-25 02:45:51 +00003628 isCastExpr,
3629 CastTy,
3630 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003631 if (hasParens)
3632 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003633
3634 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003635 // FIXME: Not accurate, the range gets one token more than it should.
3636 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003637 else
3638 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003639
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003640 if (isCastExpr) {
3641 if (!CastTy) {
3642 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003643 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003644 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003645
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003646 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003647 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003648 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3649 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003650 DiagID, CastTy))
3651 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003652 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003653 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003654
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003655 // If we get here, the operand to the typeof was an expresion.
3656 if (Operand.isInvalid()) {
3657 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003658 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003659 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003660
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003661 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003662 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003663 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3664 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00003665 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00003666 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003667}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003668
3669
3670/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3671/// from TryAltiVecVectorToken.
3672bool Parser::TryAltiVecVectorTokenOutOfLine() {
3673 Token Next = NextToken();
3674 switch (Next.getKind()) {
3675 default: return false;
3676 case tok::kw_short:
3677 case tok::kw_long:
3678 case tok::kw_signed:
3679 case tok::kw_unsigned:
3680 case tok::kw_void:
3681 case tok::kw_char:
3682 case tok::kw_int:
3683 case tok::kw_float:
3684 case tok::kw_double:
3685 case tok::kw_bool:
3686 case tok::kw___pixel:
3687 Tok.setKind(tok::kw___vector);
3688 return true;
3689 case tok::identifier:
3690 if (Next.getIdentifierInfo() == Ident_pixel) {
3691 Tok.setKind(tok::kw___vector);
3692 return true;
3693 }
3694 return false;
3695 }
3696}
3697
3698bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3699 const char *&PrevSpec, unsigned &DiagID,
3700 bool &isInvalid) {
3701 if (Tok.getIdentifierInfo() == Ident_vector) {
3702 Token Next = NextToken();
3703 switch (Next.getKind()) {
3704 case tok::kw_short:
3705 case tok::kw_long:
3706 case tok::kw_signed:
3707 case tok::kw_unsigned:
3708 case tok::kw_void:
3709 case tok::kw_char:
3710 case tok::kw_int:
3711 case tok::kw_float:
3712 case tok::kw_double:
3713 case tok::kw_bool:
3714 case tok::kw___pixel:
3715 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3716 return true;
3717 case tok::identifier:
3718 if (Next.getIdentifierInfo() == Ident_pixel) {
3719 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3720 return true;
3721 }
3722 break;
3723 default:
3724 break;
3725 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003726 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003727 DS.isTypeAltiVecVector()) {
3728 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3729 return true;
3730 }
3731 return false;
3732}
3733