blob: b815031939833e082e196b6dd38c7c281593e651 [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,
395 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
Douglas Gregor23996282009-05-12 21:31:51 +0000590 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000591 if (isTokenEqualOrMistypedEqualEqual(
592 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000593 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000594 if (Tok.is(tok::kw_delete)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000595 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000596
597 if (!getLang().CPlusPlus0x)
598 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
599
Douglas Gregor23996282009-05-12 21:31:51 +0000600 Actions.SetDeclDeleted(ThisDecl, DelLoc);
601 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000602 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
603 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000604 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000605 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000606
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000607 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000608 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000609 ConsumeCodeCompletionToken();
610 SkipUntil(tok::comma, true, true);
611 return ThisDecl;
612 }
613
John McCalldadc5752010-08-24 06:29:42 +0000614 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000615
John McCall1f4ee7b2009-12-19 09:28:58 +0000616 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000617 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000618 ExitScope();
619 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000620
Douglas Gregor23996282009-05-12 21:31:51 +0000621 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000622 SkipUntil(tok::comma, true, true);
623 Actions.ActOnInitializerError(ThisDecl);
624 } else
John McCallb268a282010-08-23 23:25:46 +0000625 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor23996282009-05-12 21:31:51 +0000626 }
627 } else if (Tok.is(tok::l_paren)) {
628 // Parse C++ direct initializer: '(' expression-list ')'
629 SourceLocation LParenLoc = ConsumeParen();
630 ExprVector Exprs(Actions);
631 CommaLocsTy CommaLocs;
632
Douglas Gregor613bf102009-12-22 17:47:17 +0000633 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
634 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000635 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000636 }
637
Douglas Gregor23996282009-05-12 21:31:51 +0000638 if (ParseExpressionList(Exprs, CommaLocs)) {
639 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000640
641 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000642 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000643 ExitScope();
644 }
Douglas Gregor23996282009-05-12 21:31:51 +0000645 } else {
646 // Match the ')'.
647 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
648
649 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
650 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000651
652 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000653 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000654 ExitScope();
655 }
656
Douglas Gregor23996282009-05-12 21:31:51 +0000657 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
658 move_arg(Exprs),
Douglas Gregorce5aa332010-09-09 16:33:13 +0000659 RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000660 }
661 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000662 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000663 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
664 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000665 }
666
667 return ThisDecl;
668}
669
Chris Lattner1890ac82006-08-13 01:16:23 +0000670/// ParseSpecifierQualifierList
671/// specifier-qualifier-list:
672/// type-specifier specifier-qualifier-list[opt]
673/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000674/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000675///
676void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
677 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
678 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000679 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000680
Chris Lattner1890ac82006-08-13 01:16:23 +0000681 // Validate declspec for type-name.
682 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000683 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +0000684 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000685 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000686
Chris Lattner1b22eed2006-11-28 05:12:07 +0000687 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000688 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000689 if (DS.getStorageClassSpecLoc().isValid())
690 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
691 else
692 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000693 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000694 }
Mike Stump11289f42009-09-09 15:08:12 +0000695
Chris Lattner1b22eed2006-11-28 05:12:07 +0000696 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000697 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000698 if (DS.isInlineSpecified())
699 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
700 if (DS.isVirtualSpecified())
701 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
702 if (DS.isExplicitSpecified())
703 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000704 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000705 }
706}
Chris Lattner53361ac2006-08-10 05:19:57 +0000707
Chris Lattner6cc055a2009-04-12 20:42:31 +0000708/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
709/// specified token is valid after the identifier in a declarator which
710/// immediately follows the declspec. For example, these things are valid:
711///
712/// int x [ 4]; // direct-declarator
713/// int x ( int y); // direct-declarator
714/// int(int x ) // direct-declarator
715/// int x ; // simple-declaration
716/// int x = 17; // init-declarator-list
717/// int x , y; // init-declarator-list
718/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000719/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000720/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000721///
722/// This is not, because 'x' does not immediately follow the declspec (though
723/// ')' happens to be valid anyway).
724/// int (x)
725///
726static bool isValidAfterIdentifierInDeclarator(const Token &T) {
727 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
728 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000729 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000730}
731
Chris Lattner20a0c612009-04-14 21:34:55 +0000732
733/// ParseImplicitInt - This method is called when we have an non-typename
734/// identifier in a declspec (which normally terminates the decl spec) when
735/// the declspec has no type specifier. In this case, the declspec is either
736/// malformed or is "implicit int" (in K&R and C89).
737///
738/// This method handles diagnosing this prettily and returns false if the
739/// declspec is done being processed. If it recovers and thinks there may be
740/// other pieces of declspec after it, it returns true.
741///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000742bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000743 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000744 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000745 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000746
Chris Lattner20a0c612009-04-14 21:34:55 +0000747 SourceLocation Loc = Tok.getLocation();
748 // If we see an identifier that is not a type name, we normally would
749 // parse it as the identifer being declared. However, when a typename
750 // is typo'd or the definition is not included, this will incorrectly
751 // parse the typename as the identifier name and fall over misparsing
752 // later parts of the diagnostic.
753 //
754 // As such, we try to do some look-ahead in cases where this would
755 // otherwise be an "implicit-int" case to see if this is invalid. For
756 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
757 // an identifier with implicit int, we'd get a parse error because the
758 // next token is obviously invalid for a type. Parse these as a case
759 // with an invalid type specifier.
760 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000761
Chris Lattner20a0c612009-04-14 21:34:55 +0000762 // Since we know that this either implicit int (which is rare) or an
763 // error, we'd do lookahead to try to do better recovery.
764 if (isValidAfterIdentifierInDeclarator(NextToken())) {
765 // If this token is valid for implicit int, e.g. "static x = 4", then
766 // we just avoid eating the identifier, so it will be parsed as the
767 // identifier in the declarator.
768 return false;
769 }
Mike Stump11289f42009-09-09 15:08:12 +0000770
Chris Lattner20a0c612009-04-14 21:34:55 +0000771 // Otherwise, if we don't consume this token, we are going to emit an
772 // error anyway. Try to recover from various common problems. Check
773 // to see if this was a reference to a tag name without a tag specified.
774 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000775 //
776 // C++ doesn't need this, and isTagName doesn't take SS.
777 if (SS == 0) {
778 const char *TagName = 0;
779 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000780
Douglas Gregor0be31a22010-07-02 17:43:08 +0000781 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +0000782 default: break;
783 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
784 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
785 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
786 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
787 }
Mike Stump11289f42009-09-09 15:08:12 +0000788
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000789 if (TagName) {
790 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +0000791 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +0000792 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000794 // Parse this as a tag as if the missing tag were present.
795 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +0000796 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000797 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000798 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000799 return true;
800 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000801 }
Mike Stump11289f42009-09-09 15:08:12 +0000802
Douglas Gregor15e56022009-10-13 23:27:22 +0000803 // This is almost certainly an invalid type name. Let the action emit a
804 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +0000805 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +0000806 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +0000807 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +0000808 // The action emitted a diagnostic, so we don't have to.
809 if (T) {
810 // The action has suggested that the type T could be used. Set that as
811 // the type in the declaration specifiers, consume the would-be type
812 // name token, and we're done.
813 const char *PrevSpec;
814 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +0000815 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +0000816 DS.SetRangeEnd(Tok.getLocation());
817 ConsumeToken();
818
819 // There may be other declaration specifiers after this.
820 return true;
821 }
822
823 // Fall through; the action had no suggestion for us.
824 } else {
825 // The action did not emit a diagnostic, so emit one now.
826 SourceRange R;
827 if (SS) R = SS->getRange();
828 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
829 }
Mike Stump11289f42009-09-09 15:08:12 +0000830
Douglas Gregor15e56022009-10-13 23:27:22 +0000831 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000832 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000833 unsigned DiagID;
834 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000835 DS.SetRangeEnd(Tok.getLocation());
836 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000837
Chris Lattner20a0c612009-04-14 21:34:55 +0000838 // TODO: Could inject an invalid typedef decl in an enclosing scope to
839 // avoid rippling error messages on subsequent uses of the same type,
840 // could be useful if #include was forgotten.
841 return false;
842}
843
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000844/// \brief Determine the declaration specifier context from the declarator
845/// context.
846///
847/// \param Context the declarator context, which is one of the
848/// Declarator::TheContext enumerator values.
849Parser::DeclSpecContext
850Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
851 if (Context == Declarator::MemberContext)
852 return DSC_class;
853 if (Context == Declarator::FileContext)
854 return DSC_top_level;
855 return DSC_normal;
856}
857
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000858/// ParseDeclarationSpecifiers
859/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000860/// storage-class-specifier declaration-specifiers[opt]
861/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000862/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000863/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000864///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000865/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000866/// 'typedef'
867/// 'extern'
868/// 'static'
869/// 'auto'
870/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000871/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000872/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000873/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000874/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000875/// [C++] 'virtual'
876/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000877/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000878/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000879/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000880
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000881///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000882void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000883 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000884 AccessSpecifier AS,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000885 DeclSpecContext DSContext) {
Chris Lattner2e232092008-03-13 06:29:04 +0000886 DS.SetRangeStart(Tok.getLocation());
Chris Lattner07865442010-11-09 20:14:26 +0000887 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000888 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000889 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000890 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000891 unsigned DiagID = 0;
892
Chris Lattner4d8f8732006-11-28 05:05:08 +0000893 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000894
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000895 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000896 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000897 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000898 // If this is not a declaration specifier token, we're done reading decl
899 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000900 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000901 return;
Mike Stump11289f42009-09-09 15:08:12 +0000902
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000903 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +0000904 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000905 if (DS.hasTypeSpecifier()) {
906 bool AllowNonIdentifiers
907 = (getCurScope()->getFlags() & (Scope::ControlScope |
908 Scope::BlockScope |
909 Scope::TemplateParamScope |
910 Scope::FunctionPrototypeScope |
911 Scope::AtCatchScope)) == 0;
912 bool AllowNestedNameSpecifiers
913 = DSContext == DSC_top_level ||
914 (DSContext == DSC_class && DS.isFriendSpecified());
915
Douglas Gregorbfcea8b2010-09-16 15:14:18 +0000916 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
917 AllowNonIdentifiers,
918 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000919 ConsumeCodeCompletionToken();
920 return;
921 }
922
923 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +0000924 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
925 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000926 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +0000927 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000928 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +0000929 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000930
931 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
932 ConsumeCodeCompletionToken();
933 return;
934 }
935
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000936 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000937 // C++ scope specifier. Annotate and loop, or bail out on error.
938 if (TryAnnotateCXXScopeToken(true)) {
939 if (!DS.hasTypeSpecifier())
940 DS.SetTypeSpecError();
941 goto DoneWithDeclSpec;
942 }
John McCall8bc2a702010-03-01 18:20:46 +0000943 if (Tok.is(tok::coloncolon)) // ::new or ::delete
944 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000945 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000946
947 case tok::annot_cxxscope: {
948 if (DS.hasTypeSpecifier())
949 goto DoneWithDeclSpec;
950
John McCall9dab4e62009-12-12 11:40:51 +0000951 CXXScopeSpec SS;
John McCall37ad5512010-08-23 06:44:23 +0000952 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCall9dab4e62009-12-12 11:40:51 +0000953 SS.setRange(Tok.getAnnotationRange());
954
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000955 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000956 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000957 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000958 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000959 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000960 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000961
962 // C++ [class.qual]p2:
963 // In a lookup in which the constructor is an acceptable lookup
964 // result and the nested-name-specifier nominates a class C:
965 //
966 // - if the name specified after the
967 // nested-name-specifier, when looked up in C, is the
968 // injected-class-name of C (Clause 9), or
969 //
970 // - if the name specified after the nested-name-specifier
971 // is the same as the identifier or the
972 // simple-template-id's template-name in the last
973 // component of the nested-name-specifier,
974 //
975 // the name is instead considered to name the constructor of
976 // class C.
977 //
978 // Thus, if the template-name is actually the constructor
979 // name, then the code is ill-formed; this interpretation is
980 // reinforced by the NAD status of core issue 635.
981 TemplateIdAnnotation *TemplateId
982 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000983 if ((DSContext == DSC_top_level ||
984 (DSContext == DSC_class && DS.isFriendSpecified())) &&
985 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000986 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000987 if (isConstructorDeclarator()) {
988 // The user meant this to be an out-of-line constructor
989 // definition, but template arguments are not allowed
990 // there. Just allow this as a constructor; we'll
991 // complain about it later.
992 goto DoneWithDeclSpec;
993 }
994
995 // The user meant this to name a type, but it actually names
996 // a constructor with some extraneous template
997 // arguments. Complain, then parse it as a type as the user
998 // intended.
999 Diag(TemplateId->TemplateNameLoc,
1000 diag::err_out_of_line_template_id_names_constructor)
1001 << TemplateId->Name;
1002 }
1003
John McCall9dab4e62009-12-12 11:40:51 +00001004 DS.getTypeSpecScope() = SS;
1005 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001006 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001007 "ParseOptionalCXXScopeSpecifier not working");
1008 AnnotateTemplateIdTokenAsType(&SS);
1009 continue;
1010 }
1011
Douglas Gregorc5790df2009-09-28 07:26:33 +00001012 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001013 DS.getTypeSpecScope() = SS;
1014 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001015 if (Tok.getAnnotationValue()) {
1016 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001017 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1018 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001019 PrevSpec, DiagID, T);
1020 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001021 else
1022 DS.SetTypeSpecError();
1023 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1024 ConsumeToken(); // The typename
1025 }
1026
Douglas Gregor167fa622009-03-25 15:40:00 +00001027 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001028 goto DoneWithDeclSpec;
1029
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001030 // If we're in a context where the identifier could be a class name,
1031 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001032 if ((DSContext == DSC_top_level ||
1033 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001034 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001035 &SS)) {
1036 if (isConstructorDeclarator())
1037 goto DoneWithDeclSpec;
1038
1039 // As noted in C++ [class.qual]p2 (cited above), when the name
1040 // of the class is qualified in a context where it could name
1041 // a constructor, its a constructor name. However, we've
1042 // looked at the declarator, and the user probably meant this
1043 // to be a type. Complain that it isn't supposed to be treated
1044 // as a type, then proceed to parse it as a type.
1045 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1046 << Next.getIdentifierInfo();
1047 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001048
John McCallba7bf592010-08-24 05:47:05 +00001049 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1050 Next.getLocation(),
1051 getCurScope(), &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001052
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001053 // If the referenced identifier is not a type, then this declspec is
1054 // erroneous: We already checked about that it has no type specifier, and
1055 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001056 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001057 if (TypeRep == 0) {
1058 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001059 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001060 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001061 }
Mike Stump11289f42009-09-09 15:08:12 +00001062
John McCall9dab4e62009-12-12 11:40:51 +00001063 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001064 ConsumeToken(); // The C++ scope.
1065
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001066 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001067 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001068 if (isInvalid)
1069 break;
Mike Stump11289f42009-09-09 15:08:12 +00001070
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001071 DS.SetRangeEnd(Tok.getLocation());
1072 ConsumeToken(); // The typename.
1073
1074 continue;
1075 }
Mike Stump11289f42009-09-09 15:08:12 +00001076
Chris Lattnere387d9e2009-01-21 19:48:37 +00001077 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001078 if (Tok.getAnnotationValue()) {
1079 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001080 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001081 DiagID, T);
1082 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001083 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001084
1085 if (isInvalid)
1086 break;
1087
Chris Lattnere387d9e2009-01-21 19:48:37 +00001088 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1089 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001090
Chris Lattnere387d9e2009-01-21 19:48:37 +00001091 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1092 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001093 // Objective-C interface.
1094 if (Tok.is(tok::less) && getLang().ObjC1)
1095 ParseObjCProtocolQualifiers(DS);
1096
Chris Lattnere387d9e2009-01-21 19:48:37 +00001097 continue;
1098 }
Mike Stump11289f42009-09-09 15:08:12 +00001099
Chris Lattner16fac4f2008-07-26 01:18:38 +00001100 // typedef-name
1101 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001102 // In C++, check to see if this is a scope specifier like foo::bar::, if
1103 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001104 if (getLang().CPlusPlus) {
1105 if (TryAnnotateCXXScopeToken(true)) {
1106 if (!DS.hasTypeSpecifier())
1107 DS.SetTypeSpecError();
1108 goto DoneWithDeclSpec;
1109 }
1110 if (!Tok.is(tok::identifier))
1111 continue;
1112 }
Mike Stump11289f42009-09-09 15:08:12 +00001113
Chris Lattner16fac4f2008-07-26 01:18:38 +00001114 // This identifier can only be a typedef name if we haven't already seen
1115 // a type-specifier. Without this check we misparse:
1116 // typedef int X; struct Y { short X; }; as 'short int'.
1117 if (DS.hasTypeSpecifier())
1118 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001119
John Thompson22334602010-02-05 00:12:22 +00001120 // Check for need to substitute AltiVec keyword tokens.
1121 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1122 break;
1123
Chris Lattner16fac4f2008-07-26 01:18:38 +00001124 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001125 ParsedType TypeRep =
1126 Actions.getTypeName(*Tok.getIdentifierInfo(),
1127 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001128
Chris Lattner6cc055a2009-04-12 20:42:31 +00001129 // If this is not a typedef name, don't parse it as part of the declspec,
1130 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001131 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001132 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001133 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001134 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001135
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001136 // If we're in a context where the identifier could be a class name,
1137 // check whether this is a constructor declaration.
1138 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001139 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001140 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001141 goto DoneWithDeclSpec;
1142
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001143 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001144 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001145 if (isInvalid)
1146 break;
Mike Stump11289f42009-09-09 15:08:12 +00001147
Chris Lattner16fac4f2008-07-26 01:18:38 +00001148 DS.SetRangeEnd(Tok.getLocation());
1149 ConsumeToken(); // The identifier
1150
1151 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1152 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001153 // Objective-C interface.
1154 if (Tok.is(tok::less) && getLang().ObjC1)
1155 ParseObjCProtocolQualifiers(DS);
1156
Steve Naroffcd5e7822008-09-22 10:28:57 +00001157 // Need to support trailing type qualifiers (e.g. "id<p> const").
1158 // If a type specifier follows, it will be diagnosed elsewhere.
1159 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001160 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001161
1162 // type-name
1163 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001164 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001165 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001166 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001167 // This template-id does not refer to a type name, so we're
1168 // done with the type-specifiers.
1169 goto DoneWithDeclSpec;
1170 }
1171
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001172 // If we're in a context where the template-id could be a
1173 // constructor name or specialization, check whether this is a
1174 // constructor declaration.
1175 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001176 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001177 isConstructorDeclarator())
1178 goto DoneWithDeclSpec;
1179
Douglas Gregor7f741122009-02-25 19:37:18 +00001180 // Turn the template-id annotation token into a type annotation
1181 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001182 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001183 continue;
1184 }
1185
Chris Lattnere37e2332006-08-15 04:50:22 +00001186 // GNU attributes support.
1187 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001188 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001189 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001190
1191 // Microsoft declspec support.
1192 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001193 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001194 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001195
Steve Naroff44ac7772008-12-25 14:16:32 +00001196 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001197 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001198 // FIXME: Add handling here!
1199 break;
1200
1201 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001202 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001203 case tok::kw___cdecl:
1204 case tok::kw___stdcall:
1205 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001206 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001207 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001208 continue;
1209
Dawn Perchik335e16b2010-09-03 01:29:35 +00001210 // Borland single token adornments.
1211 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001212 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001213 continue;
1214
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001215 // OpenCL single token adornments.
1216 case tok::kw___kernel:
1217 ParseOpenCLAttributes(DS.getAttributes());
1218 continue;
1219
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001220 // storage-class-specifier
1221 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001222 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001223 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001224 break;
1225 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001226 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001227 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001228 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001229 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001230 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001231 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001232 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001233 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001234 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001235 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001236 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001237 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001238 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001239 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001240 break;
1241 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001242 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001243 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1244 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001245 else
John McCall49bfce42009-08-03 20:12:06 +00001246 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001247 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001248 break;
1249 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001250 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001251 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001252 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001253 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001254 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001255 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001256 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001257 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001258 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001259 break;
Mike Stump11289f42009-09-09 15:08:12 +00001260
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001261 // function-specifier
1262 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001263 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001264 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001265 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001266 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001267 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001268 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001269 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001270 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001271
Anders Carlssoncd8db412009-05-06 04:46:28 +00001272 // friend
1273 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001274 if (DSContext == DSC_class)
1275 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1276 else {
1277 PrevSpec = ""; // not actually used by the diagnostic
1278 DiagID = diag::err_friend_invalid_in_context;
1279 isInvalid = true;
1280 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001281 break;
Mike Stump11289f42009-09-09 15:08:12 +00001282
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001283 // constexpr
1284 case tok::kw_constexpr:
1285 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1286 break;
1287
Chris Lattnere387d9e2009-01-21 19:48:37 +00001288 // type-specifier
1289 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001290 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1291 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001292 break;
1293 case tok::kw_long:
1294 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001295 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1296 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001297 else
John McCall49bfce42009-08-03 20:12:06 +00001298 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1299 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001300 break;
1301 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001302 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1303 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001304 break;
1305 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001306 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1307 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001308 break;
1309 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001310 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1311 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001312 break;
1313 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001314 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1315 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001316 break;
1317 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001318 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1319 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001320 break;
1321 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001322 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1323 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001324 break;
1325 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1327 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001328 break;
1329 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001330 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1331 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001332 break;
1333 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001334 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1335 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001336 break;
1337 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001338 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1339 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001340 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001341 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001342 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1343 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001344 break;
1345 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001346 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1347 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001348 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001349 case tok::kw_bool:
1350 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001351 if (Tok.is(tok::kw_bool) &&
1352 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1353 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1354 PrevSpec = ""; // Not used by the diagnostic.
1355 DiagID = diag::err_bool_redeclaration;
1356 isInvalid = true;
1357 } else {
1358 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1359 DiagID);
1360 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001361 break;
1362 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001363 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1364 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001365 break;
1366 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001367 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1368 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001369 break;
1370 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001371 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1372 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001373 break;
John Thompson22334602010-02-05 00:12:22 +00001374 case tok::kw___vector:
1375 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1376 break;
1377 case tok::kw___pixel:
1378 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1379 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001380
1381 // class-specifier:
1382 case tok::kw_class:
1383 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001384 case tok::kw_union: {
1385 tok::TokenKind Kind = Tok.getKind();
1386 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001387 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001388 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001389 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001390
1391 // enum-specifier:
1392 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001393 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001394 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001395 continue;
1396
1397 // cv-qualifier:
1398 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001399 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1400 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001401 break;
1402 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001403 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1404 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001405 break;
1406 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001407 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1408 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001409 break;
1410
Douglas Gregor333489b2009-03-27 23:10:48 +00001411 // C++ typename-specifier:
1412 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001413 if (TryAnnotateTypeOrScopeToken()) {
1414 DS.SetTypeSpecError();
1415 goto DoneWithDeclSpec;
1416 }
1417 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001418 continue;
1419 break;
1420
Chris Lattnere387d9e2009-01-21 19:48:37 +00001421 // GNU typeof support.
1422 case tok::kw_typeof:
1423 ParseTypeofSpecifier(DS);
1424 continue;
1425
Anders Carlsson74948d02009-06-24 17:47:40 +00001426 case tok::kw_decltype:
1427 ParseDecltypeSpecifier(DS);
1428 continue;
1429
Steve Naroffcfdf6162008-06-05 00:02:44 +00001430 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001431 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001432 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1433 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001434 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001435 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001436
Douglas Gregor3a001f42010-11-19 17:10:50 +00001437 if (!ParseObjCProtocolQualifiers(DS))
1438 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1439 << FixItHint::CreateInsertion(Loc, "id")
1440 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001441
1442 // Need to support trailing type qualifiers (e.g. "id<p> const").
1443 // If a type specifier follows, it will be diagnosed elsewhere.
1444 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001445 }
John McCall49bfce42009-08-03 20:12:06 +00001446 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001447 if (isInvalid) {
1448 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001449 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001450
1451 if (DiagID == diag::ext_duplicate_declspec)
1452 Diag(Tok, DiagID)
1453 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1454 else
1455 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001456 }
Chris Lattner2e232092008-03-13 06:29:04 +00001457 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001458 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001459 }
1460}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001461
Chris Lattnera448d752009-01-06 06:59:53 +00001462/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001463/// primarily follow the C++ grammar with additions for C99 and GNU,
1464/// which together subsume the C grammar. Note that the C++
1465/// type-specifier also includes the C type-qualifier (for const,
1466/// volatile, and C99 restrict). Returns true if a type-specifier was
1467/// found (and parsed), false otherwise.
1468///
1469/// type-specifier: [C++ 7.1.5]
1470/// simple-type-specifier
1471/// class-specifier
1472/// enum-specifier
1473/// elaborated-type-specifier [TODO]
1474/// cv-qualifier
1475///
1476/// cv-qualifier: [C++ 7.1.5.1]
1477/// 'const'
1478/// 'volatile'
1479/// [C99] 'restrict'
1480///
1481/// simple-type-specifier: [ C++ 7.1.5.2]
1482/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1483/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1484/// 'char'
1485/// 'wchar_t'
1486/// 'bool'
1487/// 'short'
1488/// 'int'
1489/// 'long'
1490/// 'signed'
1491/// 'unsigned'
1492/// 'float'
1493/// 'double'
1494/// 'void'
1495/// [C99] '_Bool'
1496/// [C99] '_Complex'
1497/// [C99] '_Imaginary' // Removed in TC2?
1498/// [GNU] '_Decimal32'
1499/// [GNU] '_Decimal64'
1500/// [GNU] '_Decimal128'
1501/// [GNU] typeof-specifier
1502/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1503/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001504/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001505/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001506bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001507 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001508 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001509 const ParsedTemplateInfo &TemplateInfo,
1510 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001511 SourceLocation Loc = Tok.getLocation();
1512
1513 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001514 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001515 // If we already have a type specifier, this identifier is not a type.
1516 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1517 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1518 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1519 return false;
John Thompson22334602010-02-05 00:12:22 +00001520 // Check for need to substitute AltiVec keyword tokens.
1521 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1522 break;
1523 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001524 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001525 // Annotate typenames and C++ scope specifiers. If we get one, just
1526 // recurse to handle whatever we get.
1527 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001528 return true;
1529 if (Tok.is(tok::identifier))
1530 return false;
1531 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1532 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001533 case tok::coloncolon: // ::foo::bar
1534 if (NextToken().is(tok::kw_new) || // ::new
1535 NextToken().is(tok::kw_delete)) // ::delete
1536 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001537
Chris Lattner020bab92009-01-04 23:41:41 +00001538 // Annotate typenames and C++ scope specifiers. If we get one, just
1539 // recurse to handle whatever we get.
1540 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001541 return true;
1542 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1543 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001544
Douglas Gregor450c75a2008-11-07 15:42:26 +00001545 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001546 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001547 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00001548 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1549 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001550 DiagID, T);
1551 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001552 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001553 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1554 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001555
Douglas Gregor450c75a2008-11-07 15:42:26 +00001556 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1557 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1558 // Objective-C interface. If we don't have Objective-C or a '<', this is
1559 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001560 if (Tok.is(tok::less) && getLang().ObjC1)
1561 ParseObjCProtocolQualifiers(DS);
1562
Douglas Gregor450c75a2008-11-07 15:42:26 +00001563 return true;
1564 }
1565
1566 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001567 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001568 break;
1569 case tok::kw_long:
1570 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001571 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1572 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001573 else
John McCall49bfce42009-08-03 20:12:06 +00001574 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1575 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001576 break;
1577 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001578 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001579 break;
1580 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001581 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1582 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001583 break;
1584 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001585 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1586 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001587 break;
1588 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001589 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1590 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001591 break;
1592 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001593 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001594 break;
1595 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001596 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001597 break;
1598 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001599 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001600 break;
1601 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001602 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001603 break;
1604 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001605 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001606 break;
1607 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001608 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001609 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001610 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001611 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001612 break;
1613 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001614 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001615 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001616 case tok::kw_bool:
1617 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001618 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001619 break;
1620 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001621 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1622 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001623 break;
1624 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001625 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1626 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001627 break;
1628 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001629 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1630 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001631 break;
John Thompson22334602010-02-05 00:12:22 +00001632 case tok::kw___vector:
1633 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1634 break;
1635 case tok::kw___pixel:
1636 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1637 break;
1638
Douglas Gregor450c75a2008-11-07 15:42:26 +00001639 // class-specifier:
1640 case tok::kw_class:
1641 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001642 case tok::kw_union: {
1643 tok::TokenKind Kind = Tok.getKind();
1644 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001645 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1646 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001647 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001648 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001649
1650 // enum-specifier:
1651 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001652 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001653 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001654 return true;
1655
1656 // cv-qualifier:
1657 case tok::kw_const:
1658 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001659 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001660 break;
1661 case tok::kw_volatile:
1662 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001663 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001664 break;
1665 case tok::kw_restrict:
1666 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001667 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001668 break;
1669
1670 // GNU typeof support.
1671 case tok::kw_typeof:
1672 ParseTypeofSpecifier(DS);
1673 return true;
1674
Anders Carlsson74948d02009-06-24 17:47:40 +00001675 // C++0x decltype support.
1676 case tok::kw_decltype:
1677 ParseDecltypeSpecifier(DS);
1678 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001679
Anders Carlssonbae27372009-06-26 23:44:14 +00001680 // C++0x auto support.
1681 case tok::kw_auto:
1682 if (!getLang().CPlusPlus0x)
1683 return false;
1684
John McCall49bfce42009-08-03 20:12:06 +00001685 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001686 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001687
Eli Friedman53339e02009-06-08 23:27:34 +00001688 case tok::kw___ptr64:
1689 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001690 case tok::kw___cdecl:
1691 case tok::kw___stdcall:
1692 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001693 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001694 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001695 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001696
Dawn Perchik335e16b2010-09-03 01:29:35 +00001697 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001698 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001699 return true;
1700
Douglas Gregor450c75a2008-11-07 15:42:26 +00001701 default:
1702 // Not a type-specifier; do nothing.
1703 return false;
1704 }
1705
1706 // If the specifier combination wasn't legal, issue a diagnostic.
1707 if (isInvalid) {
1708 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001709 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001710 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001711 }
1712 DS.SetRangeEnd(Tok.getLocation());
1713 ConsumeToken(); // whatever we parsed above.
1714 return true;
1715}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001716
Chris Lattner70ae4912007-10-29 04:42:53 +00001717/// ParseStructDeclaration - Parse a struct declaration without the terminating
1718/// semicolon.
1719///
Chris Lattner90a26b02007-01-23 04:38:16 +00001720/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001721/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001722/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001723/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001724/// struct-declarator-list:
1725/// struct-declarator
1726/// struct-declarator-list ',' struct-declarator
1727/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1728/// struct-declarator:
1729/// declarator
1730/// [GNU] declarator attributes[opt]
1731/// declarator[opt] ':' constant-expression
1732/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1733///
Chris Lattnera12405b2008-04-10 06:46:29 +00001734void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001735ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001736 if (Tok.is(tok::kw___extension__)) {
1737 // __extension__ silences extension warnings in the subexpression.
1738 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001739 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001740 return ParseStructDeclaration(DS, Fields);
1741 }
Mike Stump11289f42009-09-09 15:08:12 +00001742
Steve Naroff97170802007-08-20 22:28:22 +00001743 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00001744 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001745
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001746 // If there are no declarators, this is a free-standing declaration
1747 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001748 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001749 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001750 return;
1751 }
1752
1753 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001754 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001755 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001756 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001757 FieldDeclarator DeclaratorInfo(DS);
1758
1759 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00001760 if (!FirstDeclarator)
1761 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00001762
Steve Naroff97170802007-08-20 22:28:22 +00001763 /// struct-declarator: declarator
1764 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001765 if (Tok.isNot(tok::colon)) {
1766 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1767 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001768 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001769 }
Mike Stump11289f42009-09-09 15:08:12 +00001770
Chris Lattner76c72282007-10-09 17:33:22 +00001771 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001772 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001773 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001774 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001775 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001776 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001777 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001778 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001779
Steve Naroff97170802007-08-20 22:28:22 +00001780 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001781 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001782
John McCallcfefb6d2009-11-03 02:38:08 +00001783 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00001784 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00001785 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001786
Steve Naroff97170802007-08-20 22:28:22 +00001787 // If we don't have a comma, it is either the end of the list (a ';')
1788 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001789 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001790 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001791
Steve Naroff97170802007-08-20 22:28:22 +00001792 // Consume the comma.
1793 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001794
John McCallcfefb6d2009-11-03 02:38:08 +00001795 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001796 }
Steve Naroff97170802007-08-20 22:28:22 +00001797}
1798
1799/// ParseStructUnionBody
1800/// struct-contents:
1801/// struct-declaration-list
1802/// [EXT] empty
1803/// [GNU] "struct-declaration-list" without terminatoring ';'
1804/// struct-declaration-list:
1805/// struct-declaration
1806/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001807/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001808///
Chris Lattner1300fb92007-01-23 23:42:53 +00001809void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001810 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00001811 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1812 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001813
Chris Lattner90a26b02007-01-23 04:38:16 +00001814 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001815
Douglas Gregor658b9552009-01-09 22:42:13 +00001816 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001817 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001818
Chris Lattner7b9ace62007-01-23 20:11:08 +00001819 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1820 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001821 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00001822 Diag(Tok, diag::ext_empty_struct_union)
1823 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001824
John McCall48871652010-08-21 09:40:31 +00001825 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001826
Chris Lattner7b9ace62007-01-23 20:11:08 +00001827 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001828 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001829 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001830
Chris Lattner736ed5d2007-06-09 05:59:07 +00001831 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001832 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001833 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00001834 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00001835 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001836 ConsumeToken();
1837 continue;
1838 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001839
1840 // Parse all the comma separated declarators.
1841 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001842
John McCallcfefb6d2009-11-03 02:38:08 +00001843 if (!Tok.is(tok::at)) {
1844 struct CFieldCallback : FieldCallback {
1845 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001846 Decl *TagDecl;
1847 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001848
John McCall48871652010-08-21 09:40:31 +00001849 CFieldCallback(Parser &P, Decl *TagDecl,
1850 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001851 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1852
John McCall48871652010-08-21 09:40:31 +00001853 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001854 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00001855 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00001856 FD.D.getDeclSpec().getSourceRange().getBegin(),
1857 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001858 FieldDecls.push_back(Field);
1859 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001860 }
John McCallcfefb6d2009-11-03 02:38:08 +00001861 } Callback(*this, TagDecl, FieldDecls);
1862
1863 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001864 } else { // Handle @defs
1865 ConsumeToken();
1866 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1867 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001868 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001869 continue;
1870 }
1871 ConsumeToken();
1872 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1873 if (!Tok.is(tok::identifier)) {
1874 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001875 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001876 continue;
1877 }
John McCall48871652010-08-21 09:40:31 +00001878 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001879 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001880 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001881 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1882 ConsumeToken();
1883 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001884 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001885
Chris Lattner76c72282007-10-09 17:33:22 +00001886 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001887 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001888 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001889 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001890 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001891 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001892 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1893 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001894 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001895 // If we stopped at a ';', eat it.
1896 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001897 }
1898 }
Mike Stump11289f42009-09-09 15:08:12 +00001899
Steve Naroff33a1e802007-10-29 21:38:07 +00001900 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001901
John McCall53fa7142010-12-24 02:08:15 +00001902 ParsedAttributes attrs;
Chris Lattner90a26b02007-01-23 04:38:16 +00001903 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001904 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00001905
Douglas Gregor0be31a22010-07-02 17:43:08 +00001906 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001907 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001908 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00001909 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001910 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001911 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001912}
1913
Chris Lattner3b561a32006-08-13 00:12:11 +00001914/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001915/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001916/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001917///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001918/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1919/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001920/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001921/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001922///
Douglas Gregor0bf31402010-10-08 23:50:27 +00001923/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1924/// [C++0x] enum-head '{' enumerator-list ',' '}'
1925///
1926/// enum-head: [C++0x]
1927/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1928/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1929///
1930/// enum-key: [C++0x]
1931/// 'enum'
1932/// 'enum' 'class'
1933/// 'enum' 'struct'
1934///
1935/// enum-base: [C++0x]
1936/// ':' type-specifier-seq
1937///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001938/// [C++] elaborated-type-specifier:
1939/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1940///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001941void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001942 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001943 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001944 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001945 if (Tok.is(tok::code_completion)) {
1946 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001947 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001948 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001949 }
1950
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001951 // If attributes exist after tag, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001952 ParsedAttributes attrs;
1953 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001954
Abramo Bagnarad7548482010-05-19 21:37:53 +00001955 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001956 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00001957 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00001958 return;
1959
1960 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001961 Diag(Tok, diag::err_expected_ident);
1962 if (Tok.isNot(tok::l_brace)) {
1963 // Has no name and is not a definition.
1964 // Skip the rest of this declarator, up until the comma or semicolon.
1965 SkipUntil(tok::comma, true);
1966 return;
1967 }
1968 }
1969 }
Mike Stump11289f42009-09-09 15:08:12 +00001970
Douglas Gregor0bf31402010-10-08 23:50:27 +00001971 bool IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001972 bool IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00001973
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001974 if (getLang().CPlusPlus0x &&
1975 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001976 IsScopedEnum = true;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001977 IsScopedUsingClassTag = Tok.is(tok::kw_class);
1978 ConsumeToken();
Douglas Gregor0bf31402010-10-08 23:50:27 +00001979 }
1980
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001981 // Must have either 'enum name' or 'enum {...}'.
1982 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1983 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001984
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001985 // Skip the rest of this declarator, up until the comma or semicolon.
1986 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001987 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001988 }
Mike Stump11289f42009-09-09 15:08:12 +00001989
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001990 // If an identifier is present, consume and remember it.
1991 IdentifierInfo *Name = 0;
1992 SourceLocation NameLoc;
1993 if (Tok.is(tok::identifier)) {
1994 Name = Tok.getIdentifierInfo();
1995 NameLoc = ConsumeToken();
1996 }
Mike Stump11289f42009-09-09 15:08:12 +00001997
Douglas Gregor0bf31402010-10-08 23:50:27 +00001998 if (!Name && IsScopedEnum) {
1999 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2000 // declaration of a scoped enumeration.
2001 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2002 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002003 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002004 }
2005
2006 TypeResult BaseType;
2007
Douglas Gregord1f69f62010-12-01 17:42:47 +00002008 // Parse the fixed underlying type.
Douglas Gregor0bf31402010-10-08 23:50:27 +00002009 if (getLang().CPlusPlus0x && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002010 bool PossibleBitfield = false;
2011 if (getCurScope()->getFlags() & Scope::ClassScope) {
2012 // If we're in class scope, this can either be an enum declaration with
2013 // an underlying type, or a declaration of a bitfield member. We try to
2014 // use a simple disambiguation scheme first to catch the common cases
2015 // (integer literal, sizeof); if it's still ambiguous, we then consider
2016 // anything that's a simple-type-specifier followed by '(' as an
2017 // expression. This suffices because function types are not valid
2018 // underlying types anyway.
2019 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2020 // If the next token starts an expression, we know we're parsing a
2021 // bit-field. This is the common case.
2022 if (TPR == TPResult::True())
2023 PossibleBitfield = true;
2024 // If the next token starts a type-specifier-seq, it may be either a
2025 // a fixed underlying type or the start of a function-style cast in C++;
2026 // lookahead one more token to see if it's obvious that we have a
2027 // fixed underlying type.
2028 else if (TPR == TPResult::False() &&
2029 GetLookAheadToken(2).getKind() == tok::semi) {
2030 // Consume the ':'.
2031 ConsumeToken();
2032 } else {
2033 // We have the start of a type-specifier-seq, so we have to perform
2034 // tentative parsing to determine whether we have an expression or a
2035 // type.
2036 TentativeParsingAction TPA(*this);
2037
2038 // Consume the ':'.
2039 ConsumeToken();
2040
2041 if (isCXXDeclarationSpecifier() != TPResult::True()) {
2042 // We'll parse this as a bitfield later.
2043 PossibleBitfield = true;
2044 TPA.Revert();
2045 } else {
2046 // We have a type-specifier-seq.
2047 TPA.Commit();
2048 }
2049 }
2050 } else {
2051 // Consume the ':'.
2052 ConsumeToken();
2053 }
2054
2055 if (!PossibleBitfield) {
2056 SourceRange Range;
2057 BaseType = ParseTypeName(&Range);
2058 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002059 }
2060
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002061 // There are three options here. If we have 'enum foo;', then this is a
2062 // forward declaration. If we have 'enum foo {...' then this is a
2063 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2064 //
2065 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2066 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2067 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2068 //
John McCallfaf5fb42010-08-26 23:41:50 +00002069 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002070 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002071 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002072 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002073 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002074 else
John McCallfaf5fb42010-08-26 23:41:50 +00002075 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002076
2077 // enums cannot be templates, although they can be referenced from a
2078 // template.
2079 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002080 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002081 Diag(Tok, diag::err_enum_template);
2082
2083 // Skip the rest of this declarator, up until the comma or semicolon.
2084 SkipUntil(tok::comma, true);
2085 return;
2086 }
2087
Douglas Gregord6ab8742009-05-28 23:31:59 +00002088 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002089 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002090 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2091 const char *PrevSpec = 0;
2092 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002093 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002094 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002095 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002096 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002097 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002098 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002099
Douglas Gregorba41d012010-04-24 16:38:41 +00002100 if (IsDependent) {
2101 // This enum has a dependent nested-name-specifier. Handle it as a
2102 // dependent tag.
2103 if (!Name) {
2104 DS.SetTypeSpecError();
2105 Diag(Tok, diag::err_expected_type_name_after_typename);
2106 return;
2107 }
2108
Douglas Gregor0be31a22010-07-02 17:43:08 +00002109 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002110 TUK, SS, Name, StartLoc,
2111 NameLoc);
2112 if (Type.isInvalid()) {
2113 DS.SetTypeSpecError();
2114 return;
2115 }
2116
2117 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallba7bf592010-08-24 05:47:05 +00002118 Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002119 Diag(StartLoc, DiagID) << PrevSpec;
2120
2121 return;
2122 }
Mike Stump11289f42009-09-09 15:08:12 +00002123
John McCall48871652010-08-21 09:40:31 +00002124 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002125 // The action failed to produce an enumeration tag. If this is a
2126 // definition, consume the entire definition.
2127 if (Tok.is(tok::l_brace)) {
2128 ConsumeBrace();
2129 SkipUntil(tok::r_brace);
2130 }
2131
2132 DS.SetTypeSpecError();
2133 return;
2134 }
2135
Chris Lattner76c72282007-10-09 17:33:22 +00002136 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002137 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002138
John McCallba7bf592010-08-24 05:47:05 +00002139 // FIXME: The DeclSpec should keep the locations of both the keyword
2140 // and the name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00002141 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCall48871652010-08-21 09:40:31 +00002142 TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002143 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002144}
2145
Chris Lattnerc1915e22007-01-25 07:29:02 +00002146/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2147/// enumerator-list:
2148/// enumerator
2149/// enumerator-list ',' enumerator
2150/// enumerator:
2151/// enumeration-constant
2152/// enumeration-constant '=' constant-expression
2153/// enumeration-constant:
2154/// identifier
2155///
John McCall48871652010-08-21 09:40:31 +00002156void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002157 // Enter the scope of the enum body and start the definition.
2158 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002159 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002160
Chris Lattnerc1915e22007-01-25 07:29:02 +00002161 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002162
Chris Lattner37256fb2007-08-27 17:24:30 +00002163 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002164 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002165 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002166
John McCall48871652010-08-21 09:40:31 +00002167 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002168
John McCall48871652010-08-21 09:40:31 +00002169 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002170
Chris Lattnerc1915e22007-01-25 07:29:02 +00002171 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002172 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002173 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2174 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002175
John McCall811a0f52010-10-22 23:36:17 +00002176 // If attributes exist after the enumerator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002177 ParsedAttributes attrs;
2178 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002179
Chris Lattnerc1915e22007-01-25 07:29:02 +00002180 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002181 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002182 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002183 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002184 AssignedVal = ParseConstantExpression();
2185 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002186 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002187 }
Mike Stump11289f42009-09-09 15:08:12 +00002188
Chris Lattnerc1915e22007-01-25 07:29:02 +00002189 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002190 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2191 LastEnumConstDecl,
2192 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002193 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002194 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002195 EnumConstantDecls.push_back(EnumConstDecl);
2196 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002197
Douglas Gregorce66d022010-09-07 14:51:08 +00002198 if (Tok.is(tok::identifier)) {
2199 // We're missing a comma between enumerators.
2200 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2201 Diag(Loc, diag::err_enumerator_list_missing_comma)
2202 << FixItHint::CreateInsertion(Loc, ", ");
2203 continue;
2204 }
2205
Chris Lattner76c72282007-10-09 17:33:22 +00002206 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002207 break;
2208 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002209
2210 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002211 !(getLang().C99 || getLang().CPlusPlus0x))
2212 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2213 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002214 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002215 }
Mike Stump11289f42009-09-09 15:08:12 +00002216
Chris Lattnerc1915e22007-01-25 07:29:02 +00002217 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002218 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002219
Chris Lattnerc1915e22007-01-25 07:29:02 +00002220 // If attributes exist after the identifier list, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002221 ParsedAttributes attrs;
2222 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002223
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002224 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2225 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002226 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002227
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002228 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002229 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002230}
Chris Lattner3b561a32006-08-13 00:12:11 +00002231
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002232/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002233/// start of a type-qualifier-list.
2234bool Parser::isTypeQualifier() const {
2235 switch (Tok.getKind()) {
2236 default: return false;
2237 // type-qualifier
2238 case tok::kw_const:
2239 case tok::kw_volatile:
2240 case tok::kw_restrict:
2241 return true;
2242 }
2243}
2244
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002245/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2246/// is definitely a type-specifier. Return false if it isn't part of a type
2247/// specifier or if we're not sure.
2248bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2249 switch (Tok.getKind()) {
2250 default: return false;
2251 // type-specifiers
2252 case tok::kw_short:
2253 case tok::kw_long:
2254 case tok::kw_signed:
2255 case tok::kw_unsigned:
2256 case tok::kw__Complex:
2257 case tok::kw__Imaginary:
2258 case tok::kw_void:
2259 case tok::kw_char:
2260 case tok::kw_wchar_t:
2261 case tok::kw_char16_t:
2262 case tok::kw_char32_t:
2263 case tok::kw_int:
2264 case tok::kw_float:
2265 case tok::kw_double:
2266 case tok::kw_bool:
2267 case tok::kw__Bool:
2268 case tok::kw__Decimal32:
2269 case tok::kw__Decimal64:
2270 case tok::kw__Decimal128:
2271 case tok::kw___vector:
2272
2273 // struct-or-union-specifier (C99) or class-specifier (C++)
2274 case tok::kw_class:
2275 case tok::kw_struct:
2276 case tok::kw_union:
2277 // enum-specifier
2278 case tok::kw_enum:
2279
2280 // typedef-name
2281 case tok::annot_typename:
2282 return true;
2283 }
2284}
2285
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002286/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002287/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002288bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002289 switch (Tok.getKind()) {
2290 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002291
Chris Lattner020bab92009-01-04 23:41:41 +00002292 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002293 if (TryAltiVecVectorToken())
2294 return true;
2295 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002296 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002297 // Annotate typenames and C++ scope specifiers. If we get one, just
2298 // recurse to handle whatever we get.
2299 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002300 return true;
2301 if (Tok.is(tok::identifier))
2302 return false;
2303 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002304
Chris Lattner020bab92009-01-04 23:41:41 +00002305 case tok::coloncolon: // ::foo::bar
2306 if (NextToken().is(tok::kw_new) || // ::new
2307 NextToken().is(tok::kw_delete)) // ::delete
2308 return false;
2309
Chris Lattner020bab92009-01-04 23:41:41 +00002310 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002311 return true;
2312 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002313
Chris Lattnere37e2332006-08-15 04:50:22 +00002314 // GNU attributes support.
2315 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002316 // GNU typeof support.
2317 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002318
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002319 // type-specifiers
2320 case tok::kw_short:
2321 case tok::kw_long:
2322 case tok::kw_signed:
2323 case tok::kw_unsigned:
2324 case tok::kw__Complex:
2325 case tok::kw__Imaginary:
2326 case tok::kw_void:
2327 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002328 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002329 case tok::kw_char16_t:
2330 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002331 case tok::kw_int:
2332 case tok::kw_float:
2333 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002334 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002335 case tok::kw__Bool:
2336 case tok::kw__Decimal32:
2337 case tok::kw__Decimal64:
2338 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002339 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002340
Chris Lattner861a2262008-04-13 18:59:07 +00002341 // struct-or-union-specifier (C99) or class-specifier (C++)
2342 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002343 case tok::kw_struct:
2344 case tok::kw_union:
2345 // enum-specifier
2346 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002347
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002348 // type-qualifier
2349 case tok::kw_const:
2350 case tok::kw_volatile:
2351 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002352
2353 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002354 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002355 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002356
Chris Lattner409bf7d2008-10-20 00:25:30 +00002357 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2358 case tok::less:
2359 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002360
Steve Naroff44ac7772008-12-25 14:16:32 +00002361 case tok::kw___cdecl:
2362 case tok::kw___stdcall:
2363 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002364 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002365 case tok::kw___w64:
2366 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002367 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002368 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002369 }
2370}
2371
Chris Lattneracd58a32006-08-06 17:24:14 +00002372/// isDeclarationSpecifier() - Return true if the current token is part of a
2373/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002374///
2375/// \param DisambiguatingWithExpression True to indicate that the purpose of
2376/// this check is to disambiguate between an expression and a declaration.
2377bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002378 switch (Tok.getKind()) {
2379 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002380
Chris Lattner020bab92009-01-04 23:41:41 +00002381 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002382 // Unfortunate hack to support "Class.factoryMethod" notation.
2383 if (getLang().ObjC1 && NextToken().is(tok::period))
2384 return false;
John Thompson22334602010-02-05 00:12:22 +00002385 if (TryAltiVecVectorToken())
2386 return true;
2387 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002388 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002389 // Annotate typenames and C++ scope specifiers. If we get one, just
2390 // recurse to handle whatever we get.
2391 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002392 return true;
2393 if (Tok.is(tok::identifier))
2394 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002395
2396 // If we're in Objective-C and we have an Objective-C class type followed
2397 // by an identifier and then either ':' or ']', in a place where an
2398 // expression is permitted, then this is probably a class message send
2399 // missing the initial '['. In this case, we won't consider this to be
2400 // the start of a declaration.
2401 if (DisambiguatingWithExpression &&
2402 isStartOfObjCClassMessageMissingOpenBracket())
2403 return false;
2404
John McCall1f476a12010-02-26 08:45:28 +00002405 return isDeclarationSpecifier();
2406
Chris Lattner020bab92009-01-04 23:41:41 +00002407 case tok::coloncolon: // ::foo::bar
2408 if (NextToken().is(tok::kw_new) || // ::new
2409 NextToken().is(tok::kw_delete)) // ::delete
2410 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002411
Chris Lattner020bab92009-01-04 23:41:41 +00002412 // Annotate typenames and C++ scope specifiers. If we get one, just
2413 // recurse to handle whatever we get.
2414 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002415 return true;
2416 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002417
Chris Lattneracd58a32006-08-06 17:24:14 +00002418 // storage-class-specifier
2419 case tok::kw_typedef:
2420 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002421 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002422 case tok::kw_static:
2423 case tok::kw_auto:
2424 case tok::kw_register:
2425 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002426
Chris Lattneracd58a32006-08-06 17:24:14 +00002427 // type-specifiers
2428 case tok::kw_short:
2429 case tok::kw_long:
2430 case tok::kw_signed:
2431 case tok::kw_unsigned:
2432 case tok::kw__Complex:
2433 case tok::kw__Imaginary:
2434 case tok::kw_void:
2435 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002436 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002437 case tok::kw_char16_t:
2438 case tok::kw_char32_t:
2439
Chris Lattneracd58a32006-08-06 17:24:14 +00002440 case tok::kw_int:
2441 case tok::kw_float:
2442 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002443 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002444 case tok::kw__Bool:
2445 case tok::kw__Decimal32:
2446 case tok::kw__Decimal64:
2447 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002448 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002449
Chris Lattner861a2262008-04-13 18:59:07 +00002450 // struct-or-union-specifier (C99) or class-specifier (C++)
2451 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002452 case tok::kw_struct:
2453 case tok::kw_union:
2454 // enum-specifier
2455 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002456
Chris Lattneracd58a32006-08-06 17:24:14 +00002457 // type-qualifier
2458 case tok::kw_const:
2459 case tok::kw_volatile:
2460 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002461
Chris Lattneracd58a32006-08-06 17:24:14 +00002462 // function-specifier
2463 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002464 case tok::kw_virtual:
2465 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002466
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002467 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002468 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002469
Chris Lattner599e47e2007-08-09 17:01:07 +00002470 // GNU typeof support.
2471 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002472
Chris Lattner599e47e2007-08-09 17:01:07 +00002473 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002474 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002475 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002476
Chris Lattner8b2ec162008-07-26 03:38:44 +00002477 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2478 case tok::less:
2479 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002480
Steve Narofff192fab2009-01-06 19:34:12 +00002481 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002482 case tok::kw___cdecl:
2483 case tok::kw___stdcall:
2484 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002485 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002486 case tok::kw___w64:
2487 case tok::kw___ptr64:
2488 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002489 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002490 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002491 }
2492}
2493
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002494bool Parser::isConstructorDeclarator() {
2495 TentativeParsingAction TPA(*this);
2496
2497 // Parse the C++ scope specifier.
2498 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002499 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002500 TPA.Revert();
2501 return false;
2502 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002503
2504 // Parse the constructor name.
2505 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2506 // We already know that we have a constructor name; just consume
2507 // the token.
2508 ConsumeToken();
2509 } else {
2510 TPA.Revert();
2511 return false;
2512 }
2513
2514 // Current class name must be followed by a left parentheses.
2515 if (Tok.isNot(tok::l_paren)) {
2516 TPA.Revert();
2517 return false;
2518 }
2519 ConsumeParen();
2520
2521 // A right parentheses or ellipsis signals that we have a constructor.
2522 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2523 TPA.Revert();
2524 return true;
2525 }
2526
2527 // If we need to, enter the specified scope.
2528 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002529 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002530 DeclScopeObj.EnterDeclaratorScope();
2531
Francois Pichet79f3a872011-01-31 04:54:32 +00002532 // Optionally skip Microsoft attributes.
2533 ParsedAttributes Attrs;
2534 MaybeParseMicrosoftAttributes(Attrs);
2535
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002536 // Check whether the next token(s) are part of a declaration
2537 // specifier, in which case we have the start of a parameter and,
2538 // therefore, we know that this is a constructor.
2539 bool IsConstructor = isDeclarationSpecifier();
2540 TPA.Revert();
2541 return IsConstructor;
2542}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002543
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002544/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00002545/// type-qualifier-list: [C99 6.7.5]
2546/// type-qualifier
2547/// [vendor] attributes
2548/// [ only if VendorAttributesAllowed=true ]
2549/// type-qualifier-list type-qualifier
2550/// [vendor] type-qualifier-list attributes
2551/// [ only if VendorAttributesAllowed=true ]
2552/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2553/// [ only if CXX0XAttributesAllowed=true ]
2554/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002555///
Dawn Perchik335e16b2010-09-03 01:29:35 +00002556void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2557 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00002558 bool CXX0XAttributesAllowed) {
2559 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2560 SourceLocation Loc = Tok.getLocation();
John McCall53fa7142010-12-24 02:08:15 +00002561 ParsedAttributesWithRange attrs;
2562 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002563 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00002564 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002565 else
2566 Diag(Loc, diag::err_attributes_not_allowed);
2567 }
2568
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002569 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002570 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002571 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002572 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002573 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002574
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002575 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00002576 case tok::code_completion:
2577 Actions.CodeCompleteTypeQualifiers(DS);
2578 ConsumeCodeCompletionToken();
2579 break;
2580
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002581 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002582 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2583 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002584 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002585 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002586 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2587 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002588 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002589 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002590 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2591 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002592 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002593 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002594 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002595 case tok::kw___cdecl:
2596 case tok::kw___stdcall:
2597 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002598 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002599 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002600 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002601 continue;
2602 }
2603 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002604 case tok::kw___pascal:
2605 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002606 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002607 continue;
2608 }
2609 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002610 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002611 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002612 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002613 continue; // do *not* consume the next token!
2614 }
2615 // otherwise, FALL THROUGH!
2616 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002617 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002618 // If this is not a type-qualifier token, we're done reading type
2619 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002620 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002621 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002622 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002623
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002624 // If the specifier combination wasn't legal, issue a diagnostic.
2625 if (isInvalid) {
2626 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002627 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002628 }
2629 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002630 }
2631}
2632
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002633
2634/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2635///
2636void Parser::ParseDeclarator(Declarator &D) {
2637 /// This implements the 'declarator' production in the C grammar, then checks
2638 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002639 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002640}
2641
Sebastian Redlbd150f42008-11-21 19:14:01 +00002642/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2643/// is parsed by the function passed to it. Pass null, and the direct-declarator
2644/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002645/// ptr-operator production.
2646///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002647/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2648/// [C] pointer[opt] direct-declarator
2649/// [C++] direct-declarator
2650/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002651///
2652/// pointer: [C99 6.7.5]
2653/// '*' type-qualifier-list[opt]
2654/// '*' type-qualifier-list[opt] pointer
2655///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002656/// ptr-operator:
2657/// '*' cv-qualifier-seq[opt]
2658/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002659/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002660/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002661/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002662/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002663void Parser::ParseDeclaratorInternal(Declarator &D,
2664 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002665 if (Diags.hasAllExtensionsSilenced())
2666 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002667
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002668 // C++ member pointers start with a '::' or a nested-name.
2669 // Member pointers get special handling, since there's no place for the
2670 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002671 if (getLang().CPlusPlus &&
2672 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2673 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002674 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002675 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00002676
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002677 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002678 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002679 // The scope spec really belongs to the direct-declarator.
2680 D.getCXXScopeSpec() = SS;
2681 if (DirectDeclParser)
2682 (this->*DirectDeclParser)(D);
2683 return;
2684 }
2685
2686 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002687 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002688 DeclSpec DS;
2689 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002690 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002691
2692 // Recurse to parse whatever is left.
2693 ParseDeclaratorInternal(D, DirectDeclParser);
2694
2695 // Sema will have to catch (syntactically invalid) pointers into global
2696 // scope. It has to catch pointers into namespace scope anyway.
2697 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall53fa7142010-12-24 02:08:15 +00002698 Loc, DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002699 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002700 return;
2701 }
2702 }
2703
2704 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002705 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002706 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002707 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002708 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002709 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002710 if (DirectDeclParser)
2711 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002712 return;
2713 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002714
Sebastian Redled0f3b02009-03-15 22:02:01 +00002715 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2716 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002717 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002718 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002719
Chris Lattner9eac9312009-03-27 04:18:06 +00002720 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002721 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002722 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002723
Bill Wendling3708c182007-05-27 10:15:43 +00002724 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002725 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002726
Bill Wendling3708c182007-05-27 10:15:43 +00002727 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002728 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002729 if (Kind == tok::star)
2730 // Remember that we parsed a pointer type, and remember the type-quals.
2731 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
John McCall53fa7142010-12-24 02:08:15 +00002732 DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002733 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002734 else
2735 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002736 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall53fa7142010-12-24 02:08:15 +00002737 Loc, DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002738 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002739 } else {
2740 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002741 DeclSpec DS;
2742
Sebastian Redl3b27be62009-03-23 00:00:23 +00002743 // Complain about rvalue references in C++03, but then go on and build
2744 // the declarator.
2745 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00002746 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00002747
Bill Wendling93efb222007-06-02 23:28:54 +00002748 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2749 // cv-qualifiers are introduced through the use of a typedef or of a
2750 // template type argument, in which case the cv-qualifiers are ignored.
2751 //
2752 // [GNU] Retricted references are allowed.
2753 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002754 // [C++0x] Attributes on references are not allowed.
2755 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002756 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002757
2758 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2759 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2760 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002761 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002762 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2763 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002764 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002765 }
Bill Wendling3708c182007-05-27 10:15:43 +00002766
2767 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002768 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002769
Douglas Gregor66583c52008-11-03 15:51:28 +00002770 if (D.getNumTypeObjects() > 0) {
2771 // C++ [dcl.ref]p4: There shall be no references to references.
2772 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2773 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002774 if (const IdentifierInfo *II = D.getIdentifier())
2775 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2776 << II;
2777 else
2778 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2779 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002780
Sebastian Redlbd150f42008-11-21 19:14:01 +00002781 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002782 // can go ahead and build the (technically ill-formed)
2783 // declarator: reference collapsing will take care of it.
2784 }
2785 }
2786
Bill Wendling3708c182007-05-27 10:15:43 +00002787 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002788 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
John McCall53fa7142010-12-24 02:08:15 +00002789 DS.takeAttributes(),
Sebastian Redled0f3b02009-03-15 22:02:01 +00002790 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002791 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002792 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002793}
2794
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002795/// ParseDirectDeclarator
2796/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002797/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002798/// '(' declarator ')'
2799/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002800/// [C90] direct-declarator '[' constant-expression[opt] ']'
2801/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2802/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2803/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2804/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002805/// direct-declarator '(' parameter-type-list ')'
2806/// direct-declarator '(' identifier-list[opt] ')'
2807/// [GNU] direct-declarator '(' parameter-forward-declarations
2808/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002809/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2810/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002811/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002812///
2813/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00002814/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00002815/// '::'[opt] nested-name-specifier[opt] type-name
2816///
2817/// id-expression: [C++ 5.1]
2818/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002819/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002820///
2821/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002822/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002823/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002824/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002825/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002826/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002827///
Chris Lattneracd58a32006-08-06 17:24:14 +00002828void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002829 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002830
Douglas Gregor7861a802009-11-03 01:35:08 +00002831 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2832 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002833 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00002834 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00002835 }
2836
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002837 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002838 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002839 // Change the declaration context for name lookup, until this function
2840 // is exited (and the declarator has been parsed).
2841 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002842 }
2843
Douglas Gregor27b4c162010-12-23 22:44:42 +00002844 // C++0x [dcl.fct]p14:
2845 // There is a syntactic ambiguity when an ellipsis occurs at the end
2846 // of a parameter-declaration-clause without a preceding comma. In
2847 // this case, the ellipsis is parsed as part of the
2848 // abstract-declarator if the type of the parameter names a template
2849 // parameter pack that has not been expanded; otherwise, it is parsed
2850 // as part of the parameter-declaration-clause.
2851 if (Tok.is(tok::ellipsis) &&
2852 !((D.getContext() == Declarator::PrototypeContext ||
2853 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00002854 NextToken().is(tok::r_paren) &&
2855 !Actions.containsUnexpandedParameterPacks(D)))
2856 D.setEllipsisLoc(ConsumeToken());
2857
Douglas Gregor7861a802009-11-03 01:35:08 +00002858 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2859 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2860 // We found something that indicates the start of an unqualified-id.
2861 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002862 bool AllowConstructorName;
2863 if (D.getDeclSpec().hasTypeSpecifier())
2864 AllowConstructorName = false;
2865 else if (D.getCXXScopeSpec().isSet())
2866 AllowConstructorName =
2867 (D.getContext() == Declarator::FileContext ||
2868 (D.getContext() == Declarator::MemberContext &&
2869 D.getDeclSpec().isFriendSpecified()));
2870 else
2871 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2872
Douglas Gregor7861a802009-11-03 01:35:08 +00002873 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2874 /*EnteringContext=*/true,
2875 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002876 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002877 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002878 D.getName()) ||
2879 // Once we're past the identifier, if the scope was bad, mark the
2880 // whole declarator bad.
2881 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002882 D.SetIdentifier(0, Tok.getLocation());
2883 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002884 } else {
2885 // Parsed the unqualified-id; update range information and move along.
2886 if (D.getSourceRange().getBegin().isInvalid())
2887 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2888 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002889 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002890 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002891 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002892 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002893 assert(!getLang().CPlusPlus &&
2894 "There's a C++-specific check for tok::identifier above");
2895 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2896 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2897 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002898 goto PastIdentifier;
2899 }
2900
2901 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002902 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002903 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002904 // Example: 'char (*X)' or 'int (*XX)(void)'
2905 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002906
2907 // If the declarator was parenthesized, we entered the declarator
2908 // scope when parsing the parenthesized declarator, then exited
2909 // the scope already. Re-enter the scope, if we need to.
2910 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002911 // If there was an error parsing parenthesized declarator, declarator
2912 // scope may have been enterred before. Don't do it again.
2913 if (!D.isInvalidType() &&
2914 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002915 // Change the declaration context for name lookup, until this function
2916 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002917 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002918 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002919 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002920 // This could be something simple like "int" (in which case the declarator
2921 // portion is empty), if an abstract-declarator is allowed.
2922 D.SetIdentifier(0, Tok.getLocation());
2923 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002924 if (D.getContext() == Declarator::MemberContext)
2925 Diag(Tok, diag::err_expected_member_name_or_semi)
2926 << D.getDeclSpec().getSourceRange();
2927 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002928 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002929 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002930 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002931 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002932 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002933 }
Mike Stump11289f42009-09-09 15:08:12 +00002934
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002935 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002936 assert(D.isPastIdentifier() &&
2937 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002938
Alexis Hunt96d5c762009-11-21 08:43:09 +00002939 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00002940 if (D.getIdentifier())
2941 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002942
Chris Lattneracd58a32006-08-06 17:24:14 +00002943 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002944 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002945 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2946 // In such a case, check if we actually have a function declarator; if it
2947 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002948 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2949 // When not in file scope, warn for ambiguous function declarators, just
2950 // in case the author intended it as a variable definition.
2951 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2952 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2953 break;
2954 }
John McCall53fa7142010-12-24 02:08:15 +00002955 ParsedAttributes attrs;
2956 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00002957 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002958 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002959 } else {
2960 break;
2961 }
2962 }
2963}
2964
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002965/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2966/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002967/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002968/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2969///
2970/// direct-declarator:
2971/// '(' declarator ')'
2972/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002973/// direct-declarator '(' parameter-type-list ')'
2974/// direct-declarator '(' identifier-list[opt] ')'
2975/// [GNU] direct-declarator '(' parameter-forward-declarations
2976/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002977///
2978void Parser::ParseParenDeclarator(Declarator &D) {
2979 SourceLocation StartLoc = ConsumeParen();
2980 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002981
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002982 // Eat any attributes before we look at whether this is a grouping or function
2983 // declarator paren. If this is a grouping paren, the attribute applies to
2984 // the type being built up, for example:
2985 // int (__attribute__(()) *x)(long y)
2986 // If this ends up not being a grouping paren, the attribute applies to the
2987 // first argument, for example:
2988 // int (__attribute__(()) int x)
2989 // In either case, we need to eat any attributes to be able to determine what
2990 // sort of paren this is.
2991 //
John McCall53fa7142010-12-24 02:08:15 +00002992 ParsedAttributes attrs;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002993 bool RequiresArg = false;
2994 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00002995 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00002996
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002997 // We require that the argument list (if this is a non-grouping paren) be
2998 // present even if the attribute list was empty.
2999 RequiresArg = true;
3000 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003001 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003002 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003003 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3004 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall53fa7142010-12-24 02:08:15 +00003005 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003006 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003007 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003008 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003009 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003010
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003011 // If we haven't past the identifier yet (or where the identifier would be
3012 // stored, if this is an abstract declarator), then this is probably just
3013 // grouping parens. However, if this could be an abstract-declarator, then
3014 // this could also be the start of function arguments (consider 'void()').
3015 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003016
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003017 if (!D.mayOmitIdentifier()) {
3018 // If this can't be an abstract-declarator, this *must* be a grouping
3019 // paren, because we haven't seen the identifier yet.
3020 isGrouping = true;
3021 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003022 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003023 isDeclarationSpecifier()) { // 'int(int)' is a function.
3024 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3025 // considered to be a type, not a K&R identifier-list.
3026 isGrouping = false;
3027 } else {
3028 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3029 isGrouping = true;
3030 }
Mike Stump11289f42009-09-09 15:08:12 +00003031
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003032 // If this is a grouping paren, handle:
3033 // direct-declarator: '(' declarator ')'
3034 // direct-declarator: '(' attributes declarator ')'
3035 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003036 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003037 D.setGroupingParens(true);
John McCall53fa7142010-12-24 02:08:15 +00003038 if (!attrs.empty())
3039 D.addAttributes(attrs.getList(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003040
Sebastian Redlbd150f42008-11-21 19:14:01 +00003041 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003042 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003043 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
3044 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc), EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003045
3046 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003047 return;
3048 }
Mike Stump11289f42009-09-09 15:08:12 +00003049
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003050 // Okay, if this wasn't a grouping paren, it must be the start of a function
3051 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003052 // identifier (and remember where it would have been), then call into
3053 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003054 D.SetIdentifier(0, Tok.getLocation());
3055
John McCall53fa7142010-12-24 02:08:15 +00003056 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003057}
3058
3059/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3060/// declarator D up to a paren, which indicates that we are parsing function
3061/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003062///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003063/// If AttrList is non-null, then the caller parsed those arguments immediately
3064/// after the open paren - they should be considered to be the first argument of
3065/// a parameter. If RequiresArg is true, then the first argument of the
3066/// function is required to be present and required to not be an identifier
3067/// list.
3068///
Chris Lattneracd58a32006-08-06 17:24:14 +00003069/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003070/// parameter-type-list: [C99 6.7.5]
3071/// parameter-list
3072/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003073/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003074///
3075/// parameter-list: [C99 6.7.5]
3076/// parameter-declaration
3077/// parameter-list ',' parameter-declaration
3078///
3079/// parameter-declaration: [C99 6.7.5]
3080/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003081/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003082/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003083/// declaration-specifiers abstract-declarator[opt]
3084/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003085/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003086/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003087///
Douglas Gregor54992352011-01-26 03:43:54 +00003088/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3089/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003090///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003091void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall53fa7142010-12-24 02:08:15 +00003092 ParsedAttributes &attrs,
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003093 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003094 // lparen is already consumed!
3095 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003096
Douglas Gregor7fb25412010-10-01 18:44:50 +00003097 ParsedType TrailingReturnType;
3098
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003099 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003100 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003101 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003102 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003103
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003104 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3105 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003106
3107 // cv-qualifier-seq[opt].
3108 DeclSpec DS;
Douglas Gregor54992352011-01-26 03:43:54 +00003109 SourceLocation RefQualifierLoc;
3110 bool RefQualifierIsLValueRef = true;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003111 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003112 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003113 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003114 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003115 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003116 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003117 MaybeParseCXX0XAttributes(attrs);
3118
Chris Lattnercf0bab22008-12-18 07:02:59 +00003119 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003120 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003121 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003122
Douglas Gregor54992352011-01-26 03:43:54 +00003123 // Parse ref-qualifier[opt]
3124 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3125 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003126 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003127
3128 RefQualifierIsLValueRef = Tok.is(tok::amp);
3129 RefQualifierLoc = ConsumeToken();
3130 EndLoc = RefQualifierLoc;
3131 }
3132
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003133 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003134 if (Tok.is(tok::kw_throw)) {
3135 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003136 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003137 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003138 hasAnyExceptionSpec);
3139 assert(Exceptions.size() == ExceptionRanges.size() &&
3140 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003141 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003142
3143 // Parse trailing-return-type.
3144 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3145 TrailingReturnType = ParseTrailingReturnType().get();
3146 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003147 }
3148
Chris Lattner371ed4e2008-04-06 06:57:35 +00003149 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003150 // int() -> no prototype, no '...'.
John McCall53fa7142010-12-24 02:08:15 +00003151 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3152 /*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003153 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003154 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003155 /*arglist*/ 0, 0,
3156 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003157 RefQualifierIsLValueRef,
3158 RefQualifierLoc,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003159 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003160 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003161 Exceptions.data(),
3162 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003163 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003164 LParenLoc, RParenLoc, D,
3165 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003166 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003167 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003168 }
3169
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003170 // Alternatively, this parameter list may be an identifier list form for a
3171 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003172 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3173 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003174 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003175 // K&R identifier lists can't have typedefs as identifiers, per
3176 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003177 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003178 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003179
Steve Naroffb0486722009-01-28 19:16:40 +00003180 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003181 // normal declarators, not for abstract-declarators. Get the first
3182 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003183 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003184 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003185
3186 // Identifier lists follow a really simple grammar: the identifiers can
3187 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3188 // identifier lists are really rare in the brave new modern world, and it
3189 // is very common for someone to typo a type in a non-k&r style list. If
3190 // we are presented with something like: "void foo(intptr x, float y)",
3191 // we don't want to start parsing the function declarator as though it is
3192 // a K&R style declarator just because intptr is an invalid type.
3193 //
3194 // To handle this, we check to see if the token after the first identifier
3195 // is a "," or ")". Only if so, do we parse it as an identifier list.
3196 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3197 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3198 FirstTok.getIdentifierInfo(),
3199 FirstTok.getLocation(), D);
3200
3201 // If we get here, the code is invalid. Push the first identifier back
3202 // into the token stream and parse the first argument as an (invalid)
3203 // normal argument declarator.
3204 PP.EnterToken(Tok);
3205 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003206 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003207 }
Mike Stump11289f42009-09-09 15:08:12 +00003208
Chris Lattner371ed4e2008-04-06 06:57:35 +00003209 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003210
Chris Lattner371ed4e2008-04-06 06:57:35 +00003211 // Build up an array of information about the parsed arguments.
3212 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003213
3214 // Enter function-declaration scope, limiting any declarators to the
3215 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003216 ParseScope PrototypeScope(this,
3217 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003218
Chris Lattner371ed4e2008-04-06 06:57:35 +00003219 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003220 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003221 while (1) {
3222 if (Tok.is(tok::ellipsis)) {
3223 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003224 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003225 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003226 }
Mike Stump11289f42009-09-09 15:08:12 +00003227
Chris Lattner371ed4e2008-04-06 06:57:35 +00003228 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003229 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003230 DeclSpec DS;
John McCall53fa7142010-12-24 02:08:15 +00003231
3232 // Skip any Microsoft attributes before a param.
3233 if (getLang().Microsoft && Tok.is(tok::l_square))
3234 ParseMicrosoftAttributes(DS.getAttributes());
3235
3236 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003237
3238 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00003239 // Take them so that we only apply the attributes to the first parameter.
3240 DS.takeAttributesFrom(attrs);
3241
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003242 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003243
Chris Lattner371ed4e2008-04-06 06:57:35 +00003244 // Parse the declarator. This is "PrototypeContext", because we must
3245 // accept either 'declarator' or 'abstract-declarator' here.
3246 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3247 ParseDeclarator(ParmDecl);
3248
3249 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00003250 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003251
Chris Lattner371ed4e2008-04-06 06:57:35 +00003252 // Remember this parsed parameter in ParamInfo.
3253 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003254
Douglas Gregor4d87df52008-12-16 21:30:33 +00003255 // DefArgToks is used when the parsing of default arguments needs
3256 // to be delayed.
3257 CachedTokens *DefArgToks = 0;
3258
Chris Lattner371ed4e2008-04-06 06:57:35 +00003259 // If no parameter was specified, verify that *something* was specified,
3260 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003261 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3262 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003263 // Completely missing, emit error.
3264 Diag(DSStart, diag::err_missing_param);
3265 } else {
3266 // Otherwise, we have something. Add it and let semantic analysis try
3267 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003268
Chris Lattner371ed4e2008-04-06 06:57:35 +00003269 // Inform the actions module about the parameter declarator, so it gets
3270 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003271 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003272
3273 // Parse the default argument, if any. We parse the default
3274 // arguments in all dialects; the semantic analysis in
3275 // ActOnParamDefaultArgument will reject the default argument in
3276 // C.
3277 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003278 SourceLocation EqualLoc = Tok.getLocation();
3279
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003280 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003281 if (D.getContext() == Declarator::MemberContext) {
3282 // If we're inside a class definition, cache the tokens
3283 // corresponding to the default argument. We'll actually parse
3284 // them when we see the end of the class definition.
3285 // FIXME: Templates will require something similar.
3286 // FIXME: Can we use a smart pointer for Toks?
3287 DefArgToks = new CachedTokens;
3288
Mike Stump11289f42009-09-09 15:08:12 +00003289 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003290 /*StopAtSemi=*/true,
3291 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003292 delete DefArgToks;
3293 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003294 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003295 } else {
3296 // Mark the end of the default argument so that we know when to
3297 // stop when we parse it later on.
3298 Token DefArgEnd;
3299 DefArgEnd.startToken();
3300 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3301 DefArgEnd.setLocation(Tok.getLocation());
3302 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003303 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003304 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003305 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003306 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003307 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003308 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003309
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003310 // The argument isn't actually potentially evaluated unless it is
3311 // used.
3312 EnterExpressionEvaluationContext Eval(Actions,
3313 Sema::PotentiallyEvaluatedIfUsed);
3314
John McCalldadc5752010-08-24 06:29:42 +00003315 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003316 if (DefArgResult.isInvalid()) {
3317 Actions.ActOnParamDefaultArgumentError(Param);
3318 SkipUntil(tok::comma, tok::r_paren, true, true);
3319 } else {
3320 // Inform the actions module about the default argument
3321 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003322 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003323 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003324 }
3325 }
Mike Stump11289f42009-09-09 15:08:12 +00003326
3327 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3328 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003329 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003330 }
3331
3332 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003333 if (Tok.isNot(tok::comma)) {
3334 if (Tok.is(tok::ellipsis)) {
3335 IsVariadic = true;
3336 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3337
3338 if (!getLang().CPlusPlus) {
3339 // We have ellipsis without a preceding ',', which is ill-formed
3340 // in C. Complain and provide the fix.
3341 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003342 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003343 }
3344 }
3345
3346 break;
3347 }
Mike Stump11289f42009-09-09 15:08:12 +00003348
Chris Lattner371ed4e2008-04-06 06:57:35 +00003349 // Consume the comma.
3350 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003351 }
Mike Stump11289f42009-09-09 15:08:12 +00003352
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003353 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003354 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3355 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003356
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003357 DeclSpec DS;
Douglas Gregor54992352011-01-26 03:43:54 +00003358 SourceLocation RefQualifierLoc;
3359 bool RefQualifierIsLValueRef = true;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003360 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003361 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003362 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003363 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003364 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003365
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003366 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003367 MaybeParseCXX0XAttributes(attrs);
3368
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003369 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003370 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003371 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003372 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003373
Douglas Gregor54992352011-01-26 03:43:54 +00003374 // Parse ref-qualifier[opt]
3375 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3376 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003377 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003378
3379 RefQualifierIsLValueRef = Tok.is(tok::amp);
3380 RefQualifierLoc = ConsumeToken();
3381 EndLoc = RefQualifierLoc;
3382 }
3383
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003384 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003385 if (Tok.is(tok::kw_throw)) {
3386 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003387 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003388 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003389 hasAnyExceptionSpec);
3390 assert(Exceptions.size() == ExceptionRanges.size() &&
3391 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003392 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003393
3394 // Parse trailing-return-type.
3395 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3396 TrailingReturnType = ParseTrailingReturnType().get();
3397 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003398 }
3399
Douglas Gregor7fb25412010-10-01 18:44:50 +00003400 // FIXME: We should leave the prototype scope before parsing the exception
3401 // specification, and then reenter it when parsing the trailing return type.
3402
3403 // Leave prototype scope.
3404 PrototypeScope.Exit();
3405
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003406 // Remember that we parsed a function type, and remember the attributes.
John McCall53fa7142010-12-24 02:08:15 +00003407 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3408 /*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003409 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003410 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003411 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003412 RefQualifierIsLValueRef,
3413 RefQualifierLoc,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003414 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003415 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003416 Exceptions.data(),
3417 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003418 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003419 LParenLoc, RParenLoc, D,
3420 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003421 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003422}
Chris Lattneracd58a32006-08-06 17:24:14 +00003423
Chris Lattner6c940e62008-04-06 06:34:08 +00003424/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3425/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003426/// first identifier has already been consumed, and the current token is the
3427/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003428///
3429/// identifier-list: [C99 6.7.5]
3430/// identifier
3431/// identifier-list ',' identifier
3432///
3433void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003434 IdentifierInfo *FirstIdent,
3435 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003436 Declarator &D) {
3437 // Build up an array of information about the parsed arguments.
3438 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3439 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003440
Chris Lattner6c940e62008-04-06 06:34:08 +00003441 // If there was no identifier specified for the declarator, either we are in
3442 // an abstract-declarator, or we are in a parameter declarator which was found
3443 // to be abstract. In abstract-declarators, identifier lists are not valid:
3444 // diagnose this.
3445 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003446 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003447
Chris Lattner9453ab82010-05-14 17:23:36 +00003448 // The first identifier was already read, and is known to be the first
3449 // identifier in the list. Remember this identifier in ParamInfo.
3450 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003451 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003452
Chris Lattner6c940e62008-04-06 06:34:08 +00003453 while (Tok.is(tok::comma)) {
3454 // Eat the comma.
3455 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003456
Chris Lattner9186f552008-04-06 06:39:19 +00003457 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003458 if (Tok.isNot(tok::identifier)) {
3459 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003460 SkipUntil(tok::r_paren);
3461 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003462 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003463
Chris Lattner6c940e62008-04-06 06:34:08 +00003464 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003465
3466 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003467 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003468 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003469
Chris Lattner6c940e62008-04-06 06:34:08 +00003470 // Verify that the argument identifier has not already been mentioned.
3471 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003472 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003473 } else {
3474 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003475 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003476 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003477 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003478 }
Mike Stump11289f42009-09-09 15:08:12 +00003479
Chris Lattner6c940e62008-04-06 06:34:08 +00003480 // Eat the identifier.
3481 ConsumeToken();
3482 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003483
3484 // If we have the closing ')', eat it and we're done.
3485 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3486
Chris Lattner9186f552008-04-06 06:39:19 +00003487 // Remember that we parsed a function type, and remember the attributes. This
3488 // function type is always a K&R style function type, which is not varargs and
3489 // has no prototype.
John McCall53fa7142010-12-24 02:08:15 +00003490 D.AddTypeInfo(DeclaratorChunk::getFunction(ParsedAttributes(),
3491 /*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003492 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003493 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003494 /*TypeQuals*/0,
Douglas Gregor54992352011-01-26 03:43:54 +00003495 true, SourceLocation(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003496 /*exception*/false,
3497 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003498 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003499 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003500}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003501
Chris Lattnere8074e62006-08-06 18:30:15 +00003502/// [C90] direct-declarator '[' constant-expression[opt] ']'
3503/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3504/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3505/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3506/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3507void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003508 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003509
Chris Lattner84a11622008-12-18 07:27:21 +00003510 // C array syntax has many features, but by-far the most common is [] and [4].
3511 // This code does a fast path to handle some of the most obvious cases.
3512 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003513 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall53fa7142010-12-24 02:08:15 +00003514 ParsedAttributes attrs;
3515 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003516
Chris Lattner84a11622008-12-18 07:27:21 +00003517 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00003518 ExprResult NumElements;
John McCall53fa7142010-12-24 02:08:15 +00003519 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00003520 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003521 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003522 return;
3523 } else if (Tok.getKind() == tok::numeric_constant &&
3524 GetLookAheadToken(1).is(tok::r_square)) {
3525 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00003526 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003527 ConsumeToken();
3528
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003529 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall53fa7142010-12-24 02:08:15 +00003530 ParsedAttributes attrs;
3531 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003532
Chris Lattner84a11622008-12-18 07:27:21 +00003533 // Remember that we parsed a array type, and remember its features.
John McCall53fa7142010-12-24 02:08:15 +00003534 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, 0,
3535 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00003536 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003537 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003538 return;
3539 }
Mike Stump11289f42009-09-09 15:08:12 +00003540
Chris Lattnere8074e62006-08-06 18:30:15 +00003541 // If valid, this location is the position where we read the 'static' keyword.
3542 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003543 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003544 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003545
Chris Lattnere8074e62006-08-06 18:30:15 +00003546 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003547 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003548 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003549 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003550
Chris Lattnere8074e62006-08-06 18:30:15 +00003551 // If we haven't already read 'static', check to see if there is one after the
3552 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003553 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003554 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003555
Chris Lattnere8074e62006-08-06 18:30:15 +00003556 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003557 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00003558 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003559
Chris Lattner521ff2b2008-04-06 05:26:30 +00003560 // Handle the case where we have '[*]' as the array size. However, a leading
3561 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3562 // the the token after the star is a ']'. Since stars in arrays are
3563 // infrequent, use of lookahead is not costly here.
3564 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003565 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003566
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003567 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003568 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003569 StaticLoc = SourceLocation(); // Drop the static.
3570 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003571 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003572 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003573 // Note, in C89, this production uses the constant-expr production instead
3574 // of assignment-expr. The only difference is that assignment-expr allows
3575 // things like '=' and '*='. Sema rejects these in C89 mode because they
3576 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003577
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003578 // Parse the constant-expression or assignment-expression now (depending
3579 // on dialect).
3580 if (getLang().CPlusPlus)
3581 NumElements = ParseConstantExpression();
3582 else
3583 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003584 }
Mike Stump11289f42009-09-09 15:08:12 +00003585
Chris Lattner62591722006-08-12 18:40:58 +00003586 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003587 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003588 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003589 // If the expression was invalid, skip it.
3590 SkipUntil(tok::r_square);
3591 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003592 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003593
3594 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3595
John McCall53fa7142010-12-24 02:08:15 +00003596 ParsedAttributes attrs;
3597 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003598
Chris Lattner84a11622008-12-18 07:27:21 +00003599 // Remember that we parsed a array type, and remember its features.
John McCall53fa7142010-12-24 02:08:15 +00003600 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), attrs,
Chris Lattnercbc426d2006-12-02 06:43:02 +00003601 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003602 NumElements.release(),
3603 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003604 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003605}
3606
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003607/// [GNU] typeof-specifier:
3608/// typeof ( expressions )
3609/// typeof ( type-name )
3610/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003611///
3612void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003613 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003614 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003615 SourceLocation StartLoc = ConsumeToken();
3616
John McCalle8595032010-01-13 20:03:27 +00003617 const bool hasParens = Tok.is(tok::l_paren);
3618
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003619 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00003620 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003621 SourceRange CastRange;
John McCalldadc5752010-08-24 06:29:42 +00003622 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall6caebb12010-08-25 02:45:51 +00003623 isCastExpr,
3624 CastTy,
3625 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003626 if (hasParens)
3627 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003628
3629 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003630 // FIXME: Not accurate, the range gets one token more than it should.
3631 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003632 else
3633 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003634
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003635 if (isCastExpr) {
3636 if (!CastTy) {
3637 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003638 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003639 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003640
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003641 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003642 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003643 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3644 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003645 DiagID, CastTy))
3646 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003647 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003648 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003649
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003650 // If we get here, the operand to the typeof was an expresion.
3651 if (Operand.isInvalid()) {
3652 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003653 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003654 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003655
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003656 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003657 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003658 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3659 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00003660 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00003661 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003662}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003663
3664
3665/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3666/// from TryAltiVecVectorToken.
3667bool Parser::TryAltiVecVectorTokenOutOfLine() {
3668 Token Next = NextToken();
3669 switch (Next.getKind()) {
3670 default: return false;
3671 case tok::kw_short:
3672 case tok::kw_long:
3673 case tok::kw_signed:
3674 case tok::kw_unsigned:
3675 case tok::kw_void:
3676 case tok::kw_char:
3677 case tok::kw_int:
3678 case tok::kw_float:
3679 case tok::kw_double:
3680 case tok::kw_bool:
3681 case tok::kw___pixel:
3682 Tok.setKind(tok::kw___vector);
3683 return true;
3684 case tok::identifier:
3685 if (Next.getIdentifierInfo() == Ident_pixel) {
3686 Tok.setKind(tok::kw___vector);
3687 return true;
3688 }
3689 return false;
3690 }
3691}
3692
3693bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3694 const char *&PrevSpec, unsigned &DiagID,
3695 bool &isInvalid) {
3696 if (Tok.getIdentifierInfo() == Ident_vector) {
3697 Token Next = NextToken();
3698 switch (Next.getKind()) {
3699 case tok::kw_short:
3700 case tok::kw_long:
3701 case tok::kw_signed:
3702 case tok::kw_unsigned:
3703 case tok::kw_void:
3704 case tok::kw_char:
3705 case tok::kw_int:
3706 case tok::kw_float:
3707 case tok::kw_double:
3708 case tok::kw_bool:
3709 case tok::kw___pixel:
3710 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3711 return true;
3712 case tok::identifier:
3713 if (Next.getIdentifierInfo() == Ident_pixel) {
3714 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3715 return true;
3716 }
3717 break;
3718 default:
3719 break;
3720 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003721 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003722 DS.isTypeAltiVecVector()) {
3723 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3724 return true;
3725 }
3726 return false;
3727}
3728