blob: 2999fdf5d4b3f212e982c6d6ba423b20e6ec6b35 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000016#include "clang/Sema/Scope.h"
17#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000019#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000020#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// C99 6.7: Declarations.
25//===----------------------------------------------------------------------===//
26
Chris Lattnerf5fbd792006-08-10 23:56:11 +000027/// ParseTypeName
28/// type-name: [C99 6.7.6]
29/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000030///
31/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000032TypeResult Parser::ParseTypeName(SourceRange *Range,
33 Declarator::TheContext Context) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000034 // Parse the common declaration-specifiers piece.
35 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000036 ParseSpecifierQualifierList(DS);
Sebastian Redld6434562009-05-29 18:02:33 +000037
Chris Lattnerf5fbd792006-08-10 23:56:11 +000038 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000039 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000040 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000041 if (Range)
42 *Range = DeclaratorInfo.getSourceRange();
43
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000044 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000045 return true;
46
Douglas Gregor0be31a22010-07-02 17:43:08 +000047 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000048}
49
Alexis Hunt96d5c762009-11-21 08:43:09 +000050/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000051///
52/// [GNU] attributes:
53/// attribute
54/// attributes attribute
55///
56/// [GNU] attribute:
57/// '__attribute__' '(' '(' attribute-list ')' ')'
58///
59/// [GNU] attribute-list:
60/// attrib
61/// attribute_list ',' attrib
62///
63/// [GNU] attrib:
64/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000065/// attrib-name
66/// attrib-name '(' identifier ')'
67/// attrib-name '(' identifier ',' nonempty-expr-list ')'
68/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000069///
Steve Naroff0f2fe172007-06-01 17:11:19 +000070/// [GNU] attrib-name:
71/// identifier
72/// typespec
73/// typequal
74/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000075///
Steve Naroff0f2fe172007-06-01 17:11:19 +000076/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000077/// token lookahead. Comment from gcc: "If they start with an identifier
78/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +000079/// start with that identifier; otherwise they are an expression list."
80///
81/// At the moment, I am not doing 2 token lookahead. I am also unaware of
82/// any attributes that don't work (based on my limited testing). Most
83/// attributes are very simple in practice. Until we find a bug, I don't see
84/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000085
John McCall53fa7142010-12-24 02:08:15 +000086void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
87 SourceLocation *endLoc) {
Alexis Hunt96d5c762009-11-21 08:43:09 +000088 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattner76c72282007-10-09 17:33:22 +000090 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000091 ConsumeToken();
92 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
93 "attribute")) {
94 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +000095 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +000096 }
97 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
98 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +000099 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000100 }
101 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000102 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
103 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000104
105 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000106 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
107 ConsumeToken();
108 continue;
109 }
110 // we have an identifier or declaration specifier (const, int, etc.)
111 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
112 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000113
Douglas Gregora2f49452010-03-16 19:09:18 +0000114 // check if we have a "parameterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000115 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000116 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000117
Chris Lattner76c72282007-10-09 17:33:22 +0000118 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000119 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
120 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000121
122 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000123 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000124 ConsumeParen(); // ignore the right paren loc for now
John McCall53fa7142010-12-24 02:08:15 +0000125 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
126 ParmName, ParmLoc, 0, 0));
Chris Lattner76c72282007-10-09 17:33:22 +0000127 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000128 ConsumeToken();
129 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000130 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000131 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000132
Steve Naroff0f2fe172007-06-01 17:11:19 +0000133 // now parse the non-empty comma separated list of expressions
134 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000135 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000136 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000137 ArgExprsOk = false;
138 SkipUntil(tok::r_paren);
139 break;
140 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000141 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000142 }
Chris Lattner76c72282007-10-09 17:33:22 +0000143 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000144 break;
145 ConsumeToken(); // Eat the comma, move to the next argument
146 }
Chris Lattner76c72282007-10-09 17:33:22 +0000147 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000148 ConsumeParen(); // ignore the right paren loc for now
John McCall53fa7142010-12-24 02:08:15 +0000149 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0,
150 AttrNameLoc, ParmName, ParmLoc,
151 ArgExprs.take(), ArgExprs.size()));
Steve Naroff0f2fe172007-06-01 17:11:19 +0000152 }
153 }
154 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000155 switch (Tok.getKind()) {
156 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000157 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000158 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000159 ConsumeParen(); // ignore the right paren loc for now
John McCall53fa7142010-12-24 02:08:15 +0000160 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
161 0, SourceLocation(), 0, 0));
Nate Begemanf2758702009-06-26 06:32:41 +0000162 break;
163 case tok::kw_char:
164 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000165 case tok::kw_char16_t:
166 case tok::kw_char32_t:
Nate Begemanf2758702009-06-26 06:32:41 +0000167 case tok::kw_bool:
168 case tok::kw_short:
169 case tok::kw_int:
170 case tok::kw_long:
171 case tok::kw_signed:
172 case tok::kw_unsigned:
173 case tok::kw_float:
174 case tok::kw_double:
175 case tok::kw_void:
John McCall53fa7142010-12-24 02:08:15 +0000176 case tok::kw_typeof: {
177 AttributeList *attr
178 = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
179 0, SourceLocation(), 0, 0);
180 attrs.add(attr);
181 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian9d7d3d82010-08-17 23:19:16 +0000182 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begemanf2758702009-06-26 06:32:41 +0000183 // If it's a builtin type name, eat it and expect a rparen
184 // __attribute__(( vec_type_hint(char) ))
185 ConsumeToken();
Nate Begemanf2758702009-06-26 06:32:41 +0000186 if (Tok.is(tok::r_paren))
187 ConsumeParen();
188 break;
John McCall53fa7142010-12-24 02:08:15 +0000189 }
Nate Begemanf2758702009-06-26 06:32:41 +0000190 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000191 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000192 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000193 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000194
Steve Naroff0f2fe172007-06-01 17:11:19 +0000195 // now parse the list of expressions
196 while (1) {
John McCalldadc5752010-08-24 06:29:42 +0000197 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000198 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000199 ArgExprsOk = false;
200 SkipUntil(tok::r_paren);
201 break;
202 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000203 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000204 }
Chris Lattner76c72282007-10-09 17:33:22 +0000205 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000206 break;
207 ConsumeToken(); // Eat the comma, move to the next argument
208 }
209 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000210 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000211 ConsumeParen(); // ignore the right paren loc for now
John McCall53fa7142010-12-24 02:08:15 +0000212 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0,
213 AttrNameLoc, 0, SourceLocation(),
214 ArgExprs.take(), ArgExprs.size()));
Steve Naroff0f2fe172007-06-01 17:11:19 +0000215 }
Nate Begemanf2758702009-06-26 06:32:41 +0000216 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000217 }
218 }
219 } else {
John McCall53fa7142010-12-24 02:08:15 +0000220 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
221 0, SourceLocation(), 0, 0));
Steve Naroff0f2fe172007-06-01 17:11:19 +0000222 }
223 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000224 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000225 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000226 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000227 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
228 SkipUntil(tok::r_paren, false);
229 }
John McCall53fa7142010-12-24 02:08:15 +0000230 if (endLoc)
231 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000232 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000233}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000234
Eli Friedman06de2b52009-06-08 07:21:15 +0000235/// ParseMicrosoftDeclSpec - Parse an __declspec construct
236///
237/// [MS] decl-specifier:
238/// __declspec ( extended-decl-modifier-seq )
239///
240/// [MS] extended-decl-modifier-seq:
241/// extended-decl-modifier[opt]
242/// extended-decl-modifier extended-decl-modifier-seq
243
John McCall53fa7142010-12-24 02:08:15 +0000244void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000245 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000246
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000247 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000248 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
249 "declspec")) {
250 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000251 return;
Eli Friedman06de2b52009-06-08 07:21:15 +0000252 }
Eli Friedman53339e02009-06-08 23:27:34 +0000253 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000254 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
255 SourceLocation AttrNameLoc = ConsumeToken();
256 if (Tok.is(tok::l_paren)) {
257 ConsumeParen();
258 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
259 // correctly.
John McCalldadc5752010-08-24 06:29:42 +0000260 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedman06de2b52009-06-08 07:21:15 +0000261 if (!ArgExpr.isInvalid()) {
John McCall37ad5512010-08-23 06:44:23 +0000262 Expr *ExprList = ArgExpr.take();
John McCall53fa7142010-12-24 02:08:15 +0000263 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
264 SourceLocation(), &ExprList, 1, true));
Eli Friedman06de2b52009-06-08 07:21:15 +0000265 }
266 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
267 SkipUntil(tok::r_paren, false);
268 } else {
John McCall53fa7142010-12-24 02:08:15 +0000269 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
270 0, SourceLocation(), 0, 0, true));
Eli Friedman06de2b52009-06-08 07:21:15 +0000271 }
272 }
273 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
274 SkipUntil(tok::r_paren, false);
John McCall53fa7142010-12-24 02:08:15 +0000275 return;
Eli Friedman53339e02009-06-08 23:27:34 +0000276}
277
John McCall53fa7142010-12-24 02:08:15 +0000278void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000279 // Treat these like attributes
280 // FIXME: Allow Sema to distinguish between these and real attributes!
281 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000282 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
283 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000284 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
285 SourceLocation AttrNameLoc = ConsumeToken();
286 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
287 // FIXME: Support these properly!
288 continue;
John McCall53fa7142010-12-24 02:08:15 +0000289 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
290 SourceLocation(), 0, 0, true));
Eli Friedman53339e02009-06-08 23:27:34 +0000291 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000292}
293
John McCall53fa7142010-12-24 02:08:15 +0000294void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000295 // Treat these like attributes
296 while (Tok.is(tok::kw___pascal)) {
297 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
298 SourceLocation AttrNameLoc = ConsumeToken();
John McCall53fa7142010-12-24 02:08:15 +0000299 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
300 SourceLocation(), 0, 0, true));
Dawn Perchik335e16b2010-09-03 01:29:35 +0000301 }
John McCall53fa7142010-12-24 02:08:15 +0000302}
303
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000304void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
305 // Treat these like attributes
306 while (Tok.is(tok::kw___kernel)) {
307 SourceLocation AttrNameLoc = ConsumeToken();
308 attrs.add(AttrFactory.Create(PP.getIdentifierInfo("opencl_kernel_function"),
309 AttrNameLoc, 0, AttrNameLoc, 0,
310 SourceLocation(), 0, 0, false));
311 }
312}
313
John McCall53fa7142010-12-24 02:08:15 +0000314void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
315 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
316 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +0000317}
318
Chris Lattner53361ac2006-08-10 05:19:57 +0000319/// ParseDeclaration - Parse a full 'declaration', which consists of
320/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000321/// 'Context' should be a Declarator::TheContext value. This returns the
322/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000323///
324/// declaration: [C99 6.7]
325/// block-declaration ->
326/// simple-declaration
327/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000328/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000329/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000330/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000331/// [C++] using-declaration
Sebastian Redlf769df52009-03-24 22:27:57 +0000332/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000333/// others... [FIXME]
334///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000335Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
336 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000337 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000338 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +0000339 ParenBraceBracketBalancer BalancerRAIIObj(*this);
340
John McCall48871652010-08-21 09:40:31 +0000341 Decl *SingleDecl = 0;
Chris Lattnera5235172007-08-25 06:57:03 +0000342 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000343 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000344 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +0000345 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000346 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000347 break;
Sebastian Redl67667942010-08-27 23:12:46 +0000348 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +0000349 // Could be the start of an inline namespace. Allowed as an ext in C++03.
350 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +0000351 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +0000352 SourceLocation InlineLoc = ConsumeToken();
353 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
354 break;
355 }
John McCall53fa7142010-12-24 02:08:15 +0000356 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000357 true);
Chris Lattnera5235172007-08-25 06:57:03 +0000358 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +0000359 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000360 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000361 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000362 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +0000363 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall53fa7142010-12-24 02:08:15 +0000364 DeclEnd, attrs);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000365 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000366 case tok::kw_static_assert:
John McCall53fa7142010-12-24 02:08:15 +0000367 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +0000368 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000369 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000370 default:
John McCall53fa7142010-12-24 02:08:15 +0000371 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +0000372 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000373
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000374 // This routine returns a DeclGroup, if the thing we parsed only contains a
375 // single decl, convert it now.
376 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000377}
378
379/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
380/// declaration-specifiers init-declarator-list[opt] ';'
381///[C90/C++]init-declarator-list ';' [TODO]
382/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000383///
384/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +0000385/// declaration. If it is true, it checks for and eats it.
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000386Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
387 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000388 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +0000389 ParsedAttributes &attrs,
Chris Lattner005fc1b2010-04-05 18:18:31 +0000390 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000391 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000392 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +0000393 DS.takeAttributesFrom(attrs);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000394 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +0000395 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanian1db5c942010-09-28 20:42:35 +0000396 StmtResult R = Actions.ActOnVlaStmt(DS);
397 if (R.isUsable())
398 Stmts.push_back(R.release());
Mike Stump11289f42009-09-09 15:08:12 +0000399
Chris Lattner0e894622006-08-13 19:58:17 +0000400 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
401 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000402 if (Tok.is(tok::semi)) {
Chris Lattner005fc1b2010-04-05 18:18:31 +0000403 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000404 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallb54367d2010-05-21 20:45:30 +0000405 DS);
John McCall28a6aea2009-11-04 02:18:39 +0000406 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000407 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000408 }
Mike Stump11289f42009-09-09 15:08:12 +0000409
Chris Lattner005fc1b2010-04-05 18:18:31 +0000410 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld5a36322009-11-03 19:26:08 +0000411}
Mike Stump11289f42009-09-09 15:08:12 +0000412
John McCalld5a36322009-11-03 19:26:08 +0000413/// ParseDeclGroup - Having concluded that this is either a function
414/// definition or a group of object declarations, actually parse the
415/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000416Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
417 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000418 bool AllowFunctionDefinitions,
419 SourceLocation *DeclEnd) {
420 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000421 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000422 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000423
John McCalld5a36322009-11-03 19:26:08 +0000424 // Bail out if the first declarator didn't seem well-formed.
425 if (!D.hasName() && !D.mayOmitIdentifier()) {
426 // Skip until ; or }.
427 SkipUntil(tok::r_brace, true, true);
428 if (Tok.is(tok::semi))
429 ConsumeToken();
430 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000431 }
Mike Stump11289f42009-09-09 15:08:12 +0000432
Chris Lattnerdbb1e932010-07-11 22:24:20 +0000433 // Check to see if we have a function *definition* which must have a body.
434 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
435 // Look at the next token to make sure that this isn't a function
436 // declaration. We have to check this because __attribute__ might be the
437 // start of a function definition in GCC-extended K&R C.
438 !isDeclarationAfterDeclarator()) {
439
Chris Lattner13901342010-07-11 22:42:07 +0000440 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +0000441 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
442 Diag(Tok, diag::err_function_declared_typedef);
443
444 // Recover by treating the 'typedef' as spurious.
445 DS.ClearStorageClassSpecs();
446 }
447
John McCall48871652010-08-21 09:40:31 +0000448 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld5a36322009-11-03 19:26:08 +0000449 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +0000450 }
451
452 if (isDeclarationSpecifier()) {
453 // If there is an invalid declaration specifier right after the function
454 // prototype, then we must be in a missing semicolon case where this isn't
455 // actually a body. Just fall through into the code that handles it as a
456 // prototype, and let the top-level code handle the erroneous declspec
457 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +0000458 } else {
459 Diag(Tok, diag::err_expected_fn_body);
460 SkipUntil(tok::semi);
461 return DeclGroupPtrTy();
462 }
463 }
464
John McCall48871652010-08-21 09:40:31 +0000465 llvm::SmallVector<Decl *, 8> DeclsInGroup;
466 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000467 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +0000468 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +0000469 DeclsInGroup.push_back(FirstDecl);
470
471 // If we don't have a comma, it is either the end of the list (a ';') or an
472 // error, bail out.
473 while (Tok.is(tok::comma)) {
474 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000475 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000476
477 // Parse the next declarator.
478 D.clear();
479
480 // Accept attributes in an init-declarator. In the first declarator in a
481 // declaration, these would be part of the declspec. In subsequent
482 // declarators, they become part of the declarator itself, so that they
483 // don't apply to declarators after *this* one. Examples:
484 // short __attribute__((common)) var; -> declspec
485 // short var __attribute__((common)); -> declarator
486 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +0000487 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +0000488
489 ParseDeclarator(D);
490
John McCall48871652010-08-21 09:40:31 +0000491 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000492 D.complete(ThisDecl);
John McCall48871652010-08-21 09:40:31 +0000493 if (ThisDecl)
John McCalld5a36322009-11-03 19:26:08 +0000494 DeclsInGroup.push_back(ThisDecl);
495 }
496
497 if (DeclEnd)
498 *DeclEnd = Tok.getLocation();
499
500 if (Context != Declarator::ForContext &&
501 ExpectAndConsume(tok::semi,
502 Context == Declarator::FileContext
503 ? diag::err_invalid_token_after_toplevel_declarator
504 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +0000505 // Okay, there was no semicolon and one was expected. If we see a
506 // declaration specifier, just assume it was missing and continue parsing.
507 // Otherwise things are very confused and we skip to recover.
508 if (!isDeclarationSpecifier()) {
509 SkipUntil(tok::r_brace, true, true);
510 if (Tok.is(tok::semi))
511 ConsumeToken();
512 }
John McCalld5a36322009-11-03 19:26:08 +0000513 }
514
Douglas Gregor0be31a22010-07-02 17:43:08 +0000515 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +0000516 DeclsInGroup.data(),
517 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000518}
519
Douglas Gregor23996282009-05-12 21:31:51 +0000520/// \brief Parse 'declaration' after parsing 'declaration-specifiers
521/// declarator'. This method parses the remainder of the declaration
522/// (including any attributes or initializer, among other things) and
523/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000524///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000525/// init-declarator: [C99 6.7]
526/// declarator
527/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000528/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
529/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000530/// [C++] declarator initializer[opt]
531///
532/// [C++] initializer:
533/// [C++] '=' initializer-clause
534/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000535/// [C++0x] '=' 'default' [TODO]
536/// [C++0x] '=' 'delete'
537///
538/// According to the standard grammar, =default and =delete are function
539/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000540///
John McCall48871652010-08-21 09:40:31 +0000541Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000542 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000543 // If a simple-asm-expr is present, parse it.
544 if (Tok.is(tok::kw_asm)) {
545 SourceLocation Loc;
John McCalldadc5752010-08-24 06:29:42 +0000546 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor23996282009-05-12 21:31:51 +0000547 if (AsmLabel.isInvalid()) {
548 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000549 return 0;
Douglas Gregor23996282009-05-12 21:31:51 +0000550 }
Mike Stump11289f42009-09-09 15:08:12 +0000551
Douglas Gregor23996282009-05-12 21:31:51 +0000552 D.setAsmLabel(AsmLabel.release());
553 D.SetRangeEnd(Loc);
554 }
Mike Stump11289f42009-09-09 15:08:12 +0000555
John McCall53fa7142010-12-24 02:08:15 +0000556 MaybeParseGNUAttributes(D);
Mike Stump11289f42009-09-09 15:08:12 +0000557
Douglas Gregor23996282009-05-12 21:31:51 +0000558 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +0000559 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000560 switch (TemplateInfo.Kind) {
561 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000562 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +0000563 break;
564
565 case ParsedTemplateInfo::Template:
566 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +0000567 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000568 MultiTemplateParamsArg(Actions,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000569 TemplateInfo.TemplateParams->data(),
570 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000571 D);
572 break;
573
574 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCall48871652010-08-21 09:40:31 +0000575 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +0000576 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +0000577 TemplateInfo.ExternLoc,
578 TemplateInfo.TemplateLoc,
579 D);
580 if (ThisRes.isInvalid()) {
581 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +0000582 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +0000583 }
584
585 ThisDecl = ThisRes.get();
586 break;
587 }
588 }
Mike Stump11289f42009-09-09 15:08:12 +0000589
Richard Smith30482bc2011-02-20 03:19:35 +0000590 bool TypeContainsAuto =
591 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
592
Douglas Gregor23996282009-05-12 21:31:51 +0000593 // Parse declarator '=' initializer.
Argyrios Kyrtzidisb5c7c512010-10-08 02:39:23 +0000594 if (isTokenEqualOrMistypedEqualEqual(
595 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000596 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000597 if (Tok.is(tok::kw_delete)) {
Douglas Gregor23996282009-05-12 21:31:51 +0000598 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +0000599
600 if (!getLang().CPlusPlus0x)
601 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
602
Douglas Gregor23996282009-05-12 21:31:51 +0000603 Actions.SetDeclDeleted(ThisDecl, DelLoc);
604 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000605 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
606 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000607 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000608 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000609
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000610 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000611 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor7aa6b222010-05-30 01:49:25 +0000612 ConsumeCodeCompletionToken();
613 SkipUntil(tok::comma, true, true);
614 return ThisDecl;
615 }
616
John McCalldadc5752010-08-24 06:29:42 +0000617 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000618
John McCall1f4ee7b2009-12-19 09:28:58 +0000619 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000620 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000621 ExitScope();
622 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000623
Douglas Gregor23996282009-05-12 21:31:51 +0000624 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +0000625 SkipUntil(tok::comma, true, true);
626 Actions.ActOnInitializerError(ThisDecl);
627 } else
Richard Smith30482bc2011-02-20 03:19:35 +0000628 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
629 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000630 }
631 } else if (Tok.is(tok::l_paren)) {
632 // Parse C++ direct initializer: '(' expression-list ')'
633 SourceLocation LParenLoc = ConsumeParen();
634 ExprVector Exprs(Actions);
635 CommaLocsTy CommaLocs;
636
Douglas Gregor613bf102009-12-22 17:47:17 +0000637 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
638 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +0000639 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000640 }
641
Douglas Gregor23996282009-05-12 21:31:51 +0000642 if (ParseExpressionList(Exprs, CommaLocs)) {
643 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000644
645 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000646 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000647 ExitScope();
648 }
Douglas Gregor23996282009-05-12 21:31:51 +0000649 } else {
650 // Match the ')'.
651 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
652
653 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
654 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000655
656 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000657 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +0000658 ExitScope();
659 }
660
Douglas Gregor23996282009-05-12 21:31:51 +0000661 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
662 move_arg(Exprs),
Richard Smith30482bc2011-02-20 03:19:35 +0000663 RParenLoc,
664 TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000665 }
666 } else {
Richard Smith30482bc2011-02-20 03:19:35 +0000667 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000668 }
669
Richard Smithb2bc2e62011-02-21 20:05:19 +0000670 Actions.FinalizeDeclaration(ThisDecl);
671
Douglas Gregor23996282009-05-12 21:31:51 +0000672 return ThisDecl;
673}
674
Chris Lattner1890ac82006-08-13 01:16:23 +0000675/// ParseSpecifierQualifierList
676/// specifier-qualifier-list:
677/// type-specifier specifier-qualifier-list[opt]
678/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000679/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000680///
681void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
682 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
683 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000684 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000685
Chris Lattner1890ac82006-08-13 01:16:23 +0000686 // Validate declspec for type-name.
687 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000688 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall53fa7142010-12-24 02:08:15 +0000689 !DS.hasAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000690 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000691
Chris Lattner1b22eed2006-11-28 05:12:07 +0000692 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000693 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000694 if (DS.getStorageClassSpecLoc().isValid())
695 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
696 else
697 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000698 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000699 }
Mike Stump11289f42009-09-09 15:08:12 +0000700
Chris Lattner1b22eed2006-11-28 05:12:07 +0000701 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000702 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000703 if (DS.isInlineSpecified())
704 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
705 if (DS.isVirtualSpecified())
706 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
707 if (DS.isExplicitSpecified())
708 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000709 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000710 }
711}
Chris Lattner53361ac2006-08-10 05:19:57 +0000712
Chris Lattner6cc055a2009-04-12 20:42:31 +0000713/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
714/// specified token is valid after the identifier in a declarator which
715/// immediately follows the declspec. For example, these things are valid:
716///
717/// int x [ 4]; // direct-declarator
718/// int x ( int y); // direct-declarator
719/// int(int x ) // direct-declarator
720/// int x ; // simple-declaration
721/// int x = 17; // init-declarator-list
722/// int x , y; // init-declarator-list
723/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000724/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000725/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000726///
727/// This is not, because 'x' does not immediately follow the declspec (though
728/// ')' happens to be valid anyway).
729/// int (x)
730///
731static bool isValidAfterIdentifierInDeclarator(const Token &T) {
732 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
733 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000734 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000735}
736
Chris Lattner20a0c612009-04-14 21:34:55 +0000737
738/// ParseImplicitInt - This method is called when we have an non-typename
739/// identifier in a declspec (which normally terminates the decl spec) when
740/// the declspec has no type specifier. In this case, the declspec is either
741/// malformed or is "implicit int" (in K&R and C89).
742///
743/// This method handles diagnosing this prettily and returns false if the
744/// declspec is done being processed. If it recovers and thinks there may be
745/// other pieces of declspec after it, it returns true.
746///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000747bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000748 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000749 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000750 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000751
Chris Lattner20a0c612009-04-14 21:34:55 +0000752 SourceLocation Loc = Tok.getLocation();
753 // If we see an identifier that is not a type name, we normally would
754 // parse it as the identifer being declared. However, when a typename
755 // is typo'd or the definition is not included, this will incorrectly
756 // parse the typename as the identifier name and fall over misparsing
757 // later parts of the diagnostic.
758 //
759 // As such, we try to do some look-ahead in cases where this would
760 // otherwise be an "implicit-int" case to see if this is invalid. For
761 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
762 // an identifier with implicit int, we'd get a parse error because the
763 // next token is obviously invalid for a type. Parse these as a case
764 // with an invalid type specifier.
765 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000766
Chris Lattner20a0c612009-04-14 21:34:55 +0000767 // Since we know that this either implicit int (which is rare) or an
768 // error, we'd do lookahead to try to do better recovery.
769 if (isValidAfterIdentifierInDeclarator(NextToken())) {
770 // If this token is valid for implicit int, e.g. "static x = 4", then
771 // we just avoid eating the identifier, so it will be parsed as the
772 // identifier in the declarator.
773 return false;
774 }
Mike Stump11289f42009-09-09 15:08:12 +0000775
Chris Lattner20a0c612009-04-14 21:34:55 +0000776 // Otherwise, if we don't consume this token, we are going to emit an
777 // error anyway. Try to recover from various common problems. Check
778 // to see if this was a reference to a tag name without a tag specified.
779 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000780 //
781 // C++ doesn't need this, and isTagName doesn't take SS.
782 if (SS == 0) {
783 const char *TagName = 0;
784 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000785
Douglas Gregor0be31a22010-07-02 17:43:08 +0000786 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +0000787 default: break;
788 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
789 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
790 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
791 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
792 }
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000794 if (TagName) {
795 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall38200b02010-02-14 01:03:10 +0000796 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +0000797 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000798
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000799 // Parse this as a tag as if the missing tag were present.
800 if (TagKind == tok::kw_enum)
Douglas Gregordc70c3a2010-03-02 17:53:14 +0000801 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000802 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000803 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000804 return true;
805 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000806 }
Mike Stump11289f42009-09-09 15:08:12 +0000807
Douglas Gregor15e56022009-10-13 23:27:22 +0000808 // This is almost certainly an invalid type name. Let the action emit a
809 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +0000810 ParsedType T;
Douglas Gregor15e56022009-10-13 23:27:22 +0000811 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor0be31a22010-07-02 17:43:08 +0000812 getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +0000813 // The action emitted a diagnostic, so we don't have to.
814 if (T) {
815 // The action has suggested that the type T could be used. Set that as
816 // the type in the declaration specifiers, consume the would-be type
817 // name token, and we're done.
818 const char *PrevSpec;
819 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +0000820 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +0000821 DS.SetRangeEnd(Tok.getLocation());
822 ConsumeToken();
823
824 // There may be other declaration specifiers after this.
825 return true;
826 }
827
828 // Fall through; the action had no suggestion for us.
829 } else {
830 // The action did not emit a diagnostic, so emit one now.
831 SourceRange R;
832 if (SS) R = SS->getRange();
833 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
834 }
Mike Stump11289f42009-09-09 15:08:12 +0000835
Douglas Gregor15e56022009-10-13 23:27:22 +0000836 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000837 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000838 unsigned DiagID;
839 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000840 DS.SetRangeEnd(Tok.getLocation());
841 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000842
Chris Lattner20a0c612009-04-14 21:34:55 +0000843 // TODO: Could inject an invalid typedef decl in an enclosing scope to
844 // avoid rippling error messages on subsequent uses of the same type,
845 // could be useful if #include was forgotten.
846 return false;
847}
848
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000849/// \brief Determine the declaration specifier context from the declarator
850/// context.
851///
852/// \param Context the declarator context, which is one of the
853/// Declarator::TheContext enumerator values.
854Parser::DeclSpecContext
855Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
856 if (Context == Declarator::MemberContext)
857 return DSC_class;
858 if (Context == Declarator::FileContext)
859 return DSC_top_level;
860 return DSC_normal;
861}
862
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000863/// ParseDeclarationSpecifiers
864/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000865/// storage-class-specifier declaration-specifiers[opt]
866/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000867/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000868/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000869///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000870/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000871/// 'typedef'
872/// 'extern'
873/// 'static'
874/// 'auto'
875/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000876/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000877/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000878/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000879/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000880/// [C++] 'virtual'
881/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000882/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000883/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000884/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000885
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000886///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000887void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000888 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000889 AccessSpecifier AS,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000890 DeclSpecContext DSContext) {
Chris Lattner2e232092008-03-13 06:29:04 +0000891 DS.SetRangeStart(Tok.getLocation());
Chris Lattner07865442010-11-09 20:14:26 +0000892 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000893 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000894 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000895 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000896 unsigned DiagID = 0;
897
Chris Lattner4d8f8732006-11-28 05:05:08 +0000898 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000899
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000900 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000901 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000902 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000903 // If this is not a declaration specifier token, we're done reading decl
904 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000905 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000906 return;
Mike Stump11289f42009-09-09 15:08:12 +0000907
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000908 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +0000909 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000910 if (DS.hasTypeSpecifier()) {
911 bool AllowNonIdentifiers
912 = (getCurScope()->getFlags() & (Scope::ControlScope |
913 Scope::BlockScope |
914 Scope::TemplateParamScope |
915 Scope::FunctionPrototypeScope |
916 Scope::AtCatchScope)) == 0;
917 bool AllowNestedNameSpecifiers
918 = DSContext == DSC_top_level ||
919 (DSContext == DSC_class && DS.isFriendSpecified());
920
Douglas Gregorbfcea8b2010-09-16 15:14:18 +0000921 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
922 AllowNonIdentifiers,
923 AllowNestedNameSpecifiers);
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000924 ConsumeCodeCompletionToken();
925 return;
926 }
927
Douglas Gregor80039242011-02-15 20:33:25 +0000928 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
929 CCC = Sema::PCC_LocalDeclarationSpecifiers;
930 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallfaf5fb42010-08-26 23:41:50 +0000931 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
932 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000933 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +0000934 CCC = Sema::PCC_Class;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000935 else if (ObjCImpDecl)
John McCallfaf5fb42010-08-26 23:41:50 +0000936 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000937
938 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
939 ConsumeCodeCompletionToken();
940 return;
941 }
942
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000943 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +0000944 // C++ scope specifier. Annotate and loop, or bail out on error.
945 if (TryAnnotateCXXScopeToken(true)) {
946 if (!DS.hasTypeSpecifier())
947 DS.SetTypeSpecError();
948 goto DoneWithDeclSpec;
949 }
John McCall8bc2a702010-03-01 18:20:46 +0000950 if (Tok.is(tok::coloncolon)) // ::new or ::delete
951 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +0000952 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000953
954 case tok::annot_cxxscope: {
955 if (DS.hasTypeSpecifier())
956 goto DoneWithDeclSpec;
957
John McCall9dab4e62009-12-12 11:40:51 +0000958 CXXScopeSpec SS;
John McCall37ad5512010-08-23 06:44:23 +0000959 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCall9dab4e62009-12-12 11:40:51 +0000960 SS.setRange(Tok.getAnnotationRange());
961
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000962 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000963 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000964 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000965 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000966 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000967 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000968
969 // C++ [class.qual]p2:
970 // In a lookup in which the constructor is an acceptable lookup
971 // result and the nested-name-specifier nominates a class C:
972 //
973 // - if the name specified after the
974 // nested-name-specifier, when looked up in C, is the
975 // injected-class-name of C (Clause 9), or
976 //
977 // - if the name specified after the nested-name-specifier
978 // is the same as the identifier or the
979 // simple-template-id's template-name in the last
980 // component of the nested-name-specifier,
981 //
982 // the name is instead considered to name the constructor of
983 // class C.
984 //
985 // Thus, if the template-name is actually the constructor
986 // name, then the code is ill-formed; this interpretation is
987 // reinforced by the NAD status of core issue 635.
988 TemplateIdAnnotation *TemplateId
989 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCall84821e72010-04-13 06:39:49 +0000990 if ((DSContext == DSC_top_level ||
991 (DSContext == DSC_class && DS.isFriendSpecified())) &&
992 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +0000993 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000994 if (isConstructorDeclarator()) {
995 // The user meant this to be an out-of-line constructor
996 // definition, but template arguments are not allowed
997 // there. Just allow this as a constructor; we'll
998 // complain about it later.
999 goto DoneWithDeclSpec;
1000 }
1001
1002 // The user meant this to name a type, but it actually names
1003 // a constructor with some extraneous template
1004 // arguments. Complain, then parse it as a type as the user
1005 // intended.
1006 Diag(TemplateId->TemplateNameLoc,
1007 diag::err_out_of_line_template_id_names_constructor)
1008 << TemplateId->Name;
1009 }
1010
John McCall9dab4e62009-12-12 11:40:51 +00001011 DS.getTypeSpecScope() = SS;
1012 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00001013 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00001014 "ParseOptionalCXXScopeSpecifier not working");
1015 AnnotateTemplateIdTokenAsType(&SS);
1016 continue;
1017 }
1018
Douglas Gregorc5790df2009-09-28 07:26:33 +00001019 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00001020 DS.getTypeSpecScope() = SS;
1021 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00001022 if (Tok.getAnnotationValue()) {
1023 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00001024 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1025 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00001026 PrevSpec, DiagID, T);
1027 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00001028 else
1029 DS.SetTypeSpecError();
1030 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1031 ConsumeToken(); // The typename
1032 }
1033
Douglas Gregor167fa622009-03-25 15:40:00 +00001034 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001035 goto DoneWithDeclSpec;
1036
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001037 // If we're in a context where the identifier could be a class name,
1038 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00001039 if ((DSContext == DSC_top_level ||
1040 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001041 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001042 &SS)) {
1043 if (isConstructorDeclarator())
1044 goto DoneWithDeclSpec;
1045
1046 // As noted in C++ [class.qual]p2 (cited above), when the name
1047 // of the class is qualified in a context where it could name
1048 // a constructor, its a constructor name. However, we've
1049 // looked at the declarator, and the user probably meant this
1050 // to be a type. Complain that it isn't supposed to be treated
1051 // as a type, then proceed to parse it as a type.
1052 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1053 << Next.getIdentifierInfo();
1054 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001055
John McCallba7bf592010-08-24 05:47:05 +00001056 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1057 Next.getLocation(),
1058 getCurScope(), &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001059
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001060 // If the referenced identifier is not a type, then this declspec is
1061 // erroneous: We already checked about that it has no type specifier, and
1062 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00001063 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001064 if (TypeRep == 0) {
1065 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001066 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001067 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001068 }
Mike Stump11289f42009-09-09 15:08:12 +00001069
John McCall9dab4e62009-12-12 11:40:51 +00001070 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001071 ConsumeToken(); // The C++ scope.
1072
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001073 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001074 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001075 if (isInvalid)
1076 break;
Mike Stump11289f42009-09-09 15:08:12 +00001077
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001078 DS.SetRangeEnd(Tok.getLocation());
1079 ConsumeToken(); // The typename.
1080
1081 continue;
1082 }
Mike Stump11289f42009-09-09 15:08:12 +00001083
Chris Lattnere387d9e2009-01-21 19:48:37 +00001084 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001085 if (Tok.getAnnotationValue()) {
1086 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00001087 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001088 DiagID, T);
1089 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001090 DS.SetTypeSpecError();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001091
1092 if (isInvalid)
1093 break;
1094
Chris Lattnere387d9e2009-01-21 19:48:37 +00001095 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1096 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001097
Chris Lattnere387d9e2009-01-21 19:48:37 +00001098 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1099 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001100 // Objective-C interface.
1101 if (Tok.is(tok::less) && getLang().ObjC1)
1102 ParseObjCProtocolQualifiers(DS);
1103
Chris Lattnere387d9e2009-01-21 19:48:37 +00001104 continue;
1105 }
Mike Stump11289f42009-09-09 15:08:12 +00001106
Chris Lattner16fac4f2008-07-26 01:18:38 +00001107 // typedef-name
1108 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001109 // In C++, check to see if this is a scope specifier like foo::bar::, if
1110 // so handle it as such. This is important for ctor parsing.
John McCall1f476a12010-02-26 08:45:28 +00001111 if (getLang().CPlusPlus) {
1112 if (TryAnnotateCXXScopeToken(true)) {
1113 if (!DS.hasTypeSpecifier())
1114 DS.SetTypeSpecError();
1115 goto DoneWithDeclSpec;
1116 }
1117 if (!Tok.is(tok::identifier))
1118 continue;
1119 }
Mike Stump11289f42009-09-09 15:08:12 +00001120
Chris Lattner16fac4f2008-07-26 01:18:38 +00001121 // This identifier can only be a typedef name if we haven't already seen
1122 // a type-specifier. Without this check we misparse:
1123 // typedef int X; struct Y { short X; }; as 'short int'.
1124 if (DS.hasTypeSpecifier())
1125 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001126
John Thompson22334602010-02-05 00:12:22 +00001127 // Check for need to substitute AltiVec keyword tokens.
1128 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1129 break;
1130
Chris Lattner16fac4f2008-07-26 01:18:38 +00001131 // It has to be available as a typedef too!
John McCallba7bf592010-08-24 05:47:05 +00001132 ParsedType TypeRep =
1133 Actions.getTypeName(*Tok.getIdentifierInfo(),
1134 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00001135
Chris Lattner6cc055a2009-04-12 20:42:31 +00001136 // If this is not a typedef name, don't parse it as part of the declspec,
1137 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00001138 if (!TypeRep) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001139 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001140 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001141 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001142
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001143 // If we're in a context where the identifier could be a class name,
1144 // check whether this is a constructor declaration.
1145 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001146 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001147 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001148 goto DoneWithDeclSpec;
1149
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001150 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001151 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001152 if (isInvalid)
1153 break;
Mike Stump11289f42009-09-09 15:08:12 +00001154
Chris Lattner16fac4f2008-07-26 01:18:38 +00001155 DS.SetRangeEnd(Tok.getLocation());
1156 ConsumeToken(); // The identifier
1157
1158 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1159 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001160 // Objective-C interface.
1161 if (Tok.is(tok::less) && getLang().ObjC1)
1162 ParseObjCProtocolQualifiers(DS);
1163
Steve Naroffcd5e7822008-09-22 10:28:57 +00001164 // Need to support trailing type qualifiers (e.g. "id<p> const").
1165 // If a type specifier follows, it will be diagnosed elsewhere.
1166 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001167 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001168
1169 // type-name
1170 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001171 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001172 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001173 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001174 // This template-id does not refer to a type name, so we're
1175 // done with the type-specifiers.
1176 goto DoneWithDeclSpec;
1177 }
1178
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001179 // If we're in a context where the template-id could be a
1180 // constructor name or specialization, check whether this is a
1181 // constructor declaration.
1182 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001183 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001184 isConstructorDeclarator())
1185 goto DoneWithDeclSpec;
1186
Douglas Gregor7f741122009-02-25 19:37:18 +00001187 // Turn the template-id annotation token into a type annotation
1188 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001189 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001190 continue;
1191 }
1192
Chris Lattnere37e2332006-08-15 04:50:22 +00001193 // GNU attributes support.
1194 case tok::kw___attribute:
John McCall53fa7142010-12-24 02:08:15 +00001195 ParseGNUAttributes(DS.getAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001196 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001197
1198 // Microsoft declspec support.
1199 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00001200 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001201 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001202
Steve Naroff44ac7772008-12-25 14:16:32 +00001203 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001204 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001205 // FIXME: Add handling here!
1206 break;
1207
1208 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001209 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001210 case tok::kw___cdecl:
1211 case tok::kw___stdcall:
1212 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001213 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001214 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00001215 continue;
1216
Dawn Perchik335e16b2010-09-03 01:29:35 +00001217 // Borland single token adornments.
1218 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001219 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001220 continue;
1221
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00001222 // OpenCL single token adornments.
1223 case tok::kw___kernel:
1224 ParseOpenCLAttributes(DS.getAttributes());
1225 continue;
1226
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001227 // storage-class-specifier
1228 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001229 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001230 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001231 break;
1232 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001233 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001234 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001235 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001236 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001237 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001238 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001239 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournede32b202011-02-11 19:59:54 +00001240 PrevSpec, DiagID, getLang());
Steve Naroff2050b0d2007-12-18 00:16:02 +00001241 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001242 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001243 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001244 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001245 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001246 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001247 break;
1248 case tok::kw_auto:
Fariborz Jahanian786e04c2011-02-21 18:37:13 +00001249 if (getLang().CPlusPlus0x || getLang().ObjC1)
John McCall49bfce42009-08-03 20:12:06 +00001250 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1251 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001252 else
John McCall49bfce42009-08-03 20:12:06 +00001253 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001254 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001255 break;
1256 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001257 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001258 DiagID, getLang());
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001259 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001260 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001261 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournede32b202011-02-11 19:59:54 +00001262 DiagID, getLang());
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001263 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001264 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001265 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001266 break;
Mike Stump11289f42009-09-09 15:08:12 +00001267
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001268 // function-specifier
1269 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001270 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001271 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001272 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001273 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001274 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001275 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001276 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001277 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001278
Anders Carlssoncd8db412009-05-06 04:46:28 +00001279 // friend
1280 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001281 if (DSContext == DSC_class)
1282 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1283 else {
1284 PrevSpec = ""; // not actually used by the diagnostic
1285 DiagID = diag::err_friend_invalid_in_context;
1286 isInvalid = true;
1287 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001288 break;
Mike Stump11289f42009-09-09 15:08:12 +00001289
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001290 // constexpr
1291 case tok::kw_constexpr:
1292 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1293 break;
1294
Chris Lattnere387d9e2009-01-21 19:48:37 +00001295 // type-specifier
1296 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001297 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1298 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001299 break;
1300 case tok::kw_long:
1301 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001302 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1303 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001304 else
John McCall49bfce42009-08-03 20:12:06 +00001305 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1306 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001307 break;
1308 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001309 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1310 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001311 break;
1312 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001313 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1314 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001315 break;
1316 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001317 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1318 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001319 break;
1320 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001321 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1322 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001323 break;
1324 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001325 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1326 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001327 break;
1328 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1330 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001331 break;
1332 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001333 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1334 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001335 break;
1336 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001337 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1338 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001339 break;
1340 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1342 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001343 break;
1344 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001345 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1346 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001347 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001348 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001349 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1350 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001351 break;
1352 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1354 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001355 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001356 case tok::kw_bool:
1357 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00001358 if (Tok.is(tok::kw_bool) &&
1359 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1360 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1361 PrevSpec = ""; // Not used by the diagnostic.
1362 DiagID = diag::err_bool_redeclaration;
1363 isInvalid = true;
1364 } else {
1365 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1366 DiagID);
1367 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001368 break;
1369 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001370 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1371 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001372 break;
1373 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001374 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1375 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001376 break;
1377 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001378 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1379 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001380 break;
John Thompson22334602010-02-05 00:12:22 +00001381 case tok::kw___vector:
1382 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1383 break;
1384 case tok::kw___pixel:
1385 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1386 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001387
1388 // class-specifier:
1389 case tok::kw_class:
1390 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001391 case tok::kw_union: {
1392 tok::TokenKind Kind = Tok.getKind();
1393 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001394 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001395 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001396 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001397
1398 // enum-specifier:
1399 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001400 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001401 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001402 continue;
1403
1404 // cv-qualifier:
1405 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001406 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1407 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001408 break;
1409 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001410 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1411 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001412 break;
1413 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001414 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1415 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001416 break;
1417
Douglas Gregor333489b2009-03-27 23:10:48 +00001418 // C++ typename-specifier:
1419 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00001420 if (TryAnnotateTypeOrScopeToken()) {
1421 DS.SetTypeSpecError();
1422 goto DoneWithDeclSpec;
1423 }
1424 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00001425 continue;
1426 break;
1427
Chris Lattnere387d9e2009-01-21 19:48:37 +00001428 // GNU typeof support.
1429 case tok::kw_typeof:
1430 ParseTypeofSpecifier(DS);
1431 continue;
1432
Anders Carlsson74948d02009-06-24 17:47:40 +00001433 case tok::kw_decltype:
1434 ParseDecltypeSpecifier(DS);
1435 continue;
1436
Steve Naroffcfdf6162008-06-05 00:02:44 +00001437 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001438 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001439 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1440 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001441 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001442 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001443
Douglas Gregor3a001f42010-11-19 17:10:50 +00001444 if (!ParseObjCProtocolQualifiers(DS))
1445 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1446 << FixItHint::CreateInsertion(Loc, "id")
1447 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001448
1449 // Need to support trailing type qualifiers (e.g. "id<p> const").
1450 // If a type specifier follows, it will be diagnosed elsewhere.
1451 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001452 }
John McCall49bfce42009-08-03 20:12:06 +00001453 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001454 if (isInvalid) {
1455 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001456 assert(DiagID);
Douglas Gregora05f5ab2010-08-23 14:34:43 +00001457
1458 if (DiagID == diag::ext_duplicate_declspec)
1459 Diag(Tok, DiagID)
1460 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1461 else
1462 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001463 }
Chris Lattner2e232092008-03-13 06:29:04 +00001464 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001465 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001466 }
1467}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001468
Chris Lattnera448d752009-01-06 06:59:53 +00001469/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001470/// primarily follow the C++ grammar with additions for C99 and GNU,
1471/// which together subsume the C grammar. Note that the C++
1472/// type-specifier also includes the C type-qualifier (for const,
1473/// volatile, and C99 restrict). Returns true if a type-specifier was
1474/// found (and parsed), false otherwise.
1475///
1476/// type-specifier: [C++ 7.1.5]
1477/// simple-type-specifier
1478/// class-specifier
1479/// enum-specifier
1480/// elaborated-type-specifier [TODO]
1481/// cv-qualifier
1482///
1483/// cv-qualifier: [C++ 7.1.5.1]
1484/// 'const'
1485/// 'volatile'
1486/// [C99] 'restrict'
1487///
1488/// simple-type-specifier: [ C++ 7.1.5.2]
1489/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1490/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1491/// 'char'
1492/// 'wchar_t'
1493/// 'bool'
1494/// 'short'
1495/// 'int'
1496/// 'long'
1497/// 'signed'
1498/// 'unsigned'
1499/// 'float'
1500/// 'double'
1501/// 'void'
1502/// [C99] '_Bool'
1503/// [C99] '_Complex'
1504/// [C99] '_Imaginary' // Removed in TC2?
1505/// [GNU] '_Decimal32'
1506/// [GNU] '_Decimal64'
1507/// [GNU] '_Decimal128'
1508/// [GNU] typeof-specifier
1509/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1510/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001511/// [C++0x] 'decltype' ( expression )
John Thompson22334602010-02-05 00:12:22 +00001512/// [AltiVec] '__vector'
John McCall49bfce42009-08-03 20:12:06 +00001513bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001514 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001515 unsigned &DiagID,
Sebastian Redl2b372722010-02-03 21:21:43 +00001516 const ParsedTemplateInfo &TemplateInfo,
1517 bool SuppressDeclarations) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001518 SourceLocation Loc = Tok.getLocation();
1519
1520 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001521 case tok::identifier: // foo::bar
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001522 // If we already have a type specifier, this identifier is not a type.
1523 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1524 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1525 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1526 return false;
John Thompson22334602010-02-05 00:12:22 +00001527 // Check for need to substitute AltiVec keyword tokens.
1528 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1529 break;
1530 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00001531 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001532 // Annotate typenames and C++ scope specifiers. If we get one, just
1533 // recurse to handle whatever we get.
1534 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001535 return true;
1536 if (Tok.is(tok::identifier))
1537 return false;
1538 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1539 TemplateInfo, SuppressDeclarations);
Chris Lattner020bab92009-01-04 23:41:41 +00001540 case tok::coloncolon: // ::foo::bar
1541 if (NextToken().is(tok::kw_new) || // ::new
1542 NextToken().is(tok::kw_delete)) // ::delete
1543 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001544
Chris Lattner020bab92009-01-04 23:41:41 +00001545 // Annotate typenames and C++ scope specifiers. If we get one, just
1546 // recurse to handle whatever we get.
1547 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00001548 return true;
1549 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1550 TemplateInfo, SuppressDeclarations);
Mike Stump11289f42009-09-09 15:08:12 +00001551
Douglas Gregor450c75a2008-11-07 15:42:26 +00001552 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001553 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00001554 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber77430342010-11-22 10:30:56 +00001555 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1556 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00001557 DiagID, T);
1558 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001559 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001560 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1561 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001562
Douglas Gregor450c75a2008-11-07 15:42:26 +00001563 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1564 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1565 // Objective-C interface. If we don't have Objective-C or a '<', this is
1566 // just a normal reference to a typedef name.
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001567 if (Tok.is(tok::less) && getLang().ObjC1)
1568 ParseObjCProtocolQualifiers(DS);
1569
Douglas Gregor450c75a2008-11-07 15:42:26 +00001570 return true;
1571 }
1572
1573 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001574 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001575 break;
1576 case tok::kw_long:
1577 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001578 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1579 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001580 else
John McCall49bfce42009-08-03 20:12:06 +00001581 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1582 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001583 break;
1584 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001585 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001586 break;
1587 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001588 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1589 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001590 break;
1591 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001592 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1593 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001594 break;
1595 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001596 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1597 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001598 break;
1599 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001600 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001601 break;
1602 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001603 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001604 break;
1605 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001606 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001607 break;
1608 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001609 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001610 break;
1611 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001612 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001613 break;
1614 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001615 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001616 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001617 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001618 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001619 break;
1620 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001621 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001622 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001623 case tok::kw_bool:
1624 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001625 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001626 break;
1627 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001628 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1629 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001630 break;
1631 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001632 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1633 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001634 break;
1635 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001636 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1637 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001638 break;
John Thompson22334602010-02-05 00:12:22 +00001639 case tok::kw___vector:
1640 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1641 break;
1642 case tok::kw___pixel:
1643 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1644 break;
1645
Douglas Gregor450c75a2008-11-07 15:42:26 +00001646 // class-specifier:
1647 case tok::kw_class:
1648 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001649 case tok::kw_union: {
1650 tok::TokenKind Kind = Tok.getKind();
1651 ConsumeToken();
Sebastian Redl2b372722010-02-03 21:21:43 +00001652 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1653 SuppressDeclarations);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001654 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001655 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001656
1657 // enum-specifier:
1658 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001659 ConsumeToken();
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001660 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001661 return true;
1662
1663 // cv-qualifier:
1664 case tok::kw_const:
1665 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001666 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001667 break;
1668 case tok::kw_volatile:
1669 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001670 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001671 break;
1672 case tok::kw_restrict:
1673 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001674 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001675 break;
1676
1677 // GNU typeof support.
1678 case tok::kw_typeof:
1679 ParseTypeofSpecifier(DS);
1680 return true;
1681
Anders Carlsson74948d02009-06-24 17:47:40 +00001682 // C++0x decltype support.
1683 case tok::kw_decltype:
1684 ParseDecltypeSpecifier(DS);
1685 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001686
Anders Carlssonbae27372009-06-26 23:44:14 +00001687 // C++0x auto support.
1688 case tok::kw_auto:
1689 if (!getLang().CPlusPlus0x)
1690 return false;
1691
John McCall49bfce42009-08-03 20:12:06 +00001692 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001693 break;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001694
Eli Friedman53339e02009-06-08 23:27:34 +00001695 case tok::kw___ptr64:
1696 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001697 case tok::kw___cdecl:
1698 case tok::kw___stdcall:
1699 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00001700 case tok::kw___thiscall:
John McCall53fa7142010-12-24 02:08:15 +00001701 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001702 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001703
Dawn Perchik335e16b2010-09-03 01:29:35 +00001704 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00001705 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00001706 return true;
1707
Douglas Gregor450c75a2008-11-07 15:42:26 +00001708 default:
1709 // Not a type-specifier; do nothing.
1710 return false;
1711 }
1712
1713 // If the specifier combination wasn't legal, issue a diagnostic.
1714 if (isInvalid) {
1715 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001716 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001717 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001718 }
1719 DS.SetRangeEnd(Tok.getLocation());
1720 ConsumeToken(); // whatever we parsed above.
1721 return true;
1722}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001723
Chris Lattner70ae4912007-10-29 04:42:53 +00001724/// ParseStructDeclaration - Parse a struct declaration without the terminating
1725/// semicolon.
1726///
Chris Lattner90a26b02007-01-23 04:38:16 +00001727/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001728/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001729/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001730/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001731/// struct-declarator-list:
1732/// struct-declarator
1733/// struct-declarator-list ',' struct-declarator
1734/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1735/// struct-declarator:
1736/// declarator
1737/// [GNU] declarator attributes[opt]
1738/// declarator[opt] ':' constant-expression
1739/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1740///
Chris Lattnera12405b2008-04-10 06:46:29 +00001741void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001742ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001743 if (Tok.is(tok::kw___extension__)) {
1744 // __extension__ silences extension warnings in the subexpression.
1745 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001746 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001747 return ParseStructDeclaration(DS, Fields);
1748 }
Mike Stump11289f42009-09-09 15:08:12 +00001749
Steve Naroff97170802007-08-20 22:28:22 +00001750 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00001751 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001752
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001753 // If there are no declarators, this is a free-standing declaration
1754 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001755 if (Tok.is(tok::semi)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001756 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001757 return;
1758 }
1759
1760 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001761 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001762 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001763 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001764 FieldDeclarator DeclaratorInfo(DS);
1765
1766 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00001767 if (!FirstDeclarator)
1768 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00001769
Steve Naroff97170802007-08-20 22:28:22 +00001770 /// struct-declarator: declarator
1771 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001772 if (Tok.isNot(tok::colon)) {
1773 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1774 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001775 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001776 }
Mike Stump11289f42009-09-09 15:08:12 +00001777
Chris Lattner76c72282007-10-09 17:33:22 +00001778 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001779 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00001780 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001781 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001782 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001783 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001784 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001785 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001786
Steve Naroff97170802007-08-20 22:28:22 +00001787 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001788 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001789
John McCallcfefb6d2009-11-03 02:38:08 +00001790 // We're done with this declarator; invoke the callback.
John McCall48871652010-08-21 09:40:31 +00001791 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall28a6aea2009-11-04 02:18:39 +00001792 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001793
Steve Naroff97170802007-08-20 22:28:22 +00001794 // If we don't have a comma, it is either the end of the list (a ';')
1795 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001796 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001797 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001798
Steve Naroff97170802007-08-20 22:28:22 +00001799 // Consume the comma.
1800 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001801
John McCallcfefb6d2009-11-03 02:38:08 +00001802 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001803 }
Steve Naroff97170802007-08-20 22:28:22 +00001804}
1805
1806/// ParseStructUnionBody
1807/// struct-contents:
1808/// struct-declaration-list
1809/// [EXT] empty
1810/// [GNU] "struct-declaration-list" without terminatoring ';'
1811/// struct-declaration-list:
1812/// struct-declaration
1813/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001814/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001815///
Chris Lattner1300fb92007-01-23 23:42:53 +00001816void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00001817 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00001818 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1819 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001820
Chris Lattner90a26b02007-01-23 04:38:16 +00001821 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001822
Douglas Gregor658b9552009-01-09 22:42:13 +00001823 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001824 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001825
Chris Lattner7b9ace62007-01-23 20:11:08 +00001826 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1827 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001828 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregorda2955e2010-07-29 14:29:34 +00001829 Diag(Tok, diag::ext_empty_struct_union)
1830 << (TagType == TST_union);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001831
John McCall48871652010-08-21 09:40:31 +00001832 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001833
Chris Lattner7b9ace62007-01-23 20:11:08 +00001834 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001835 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001836 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001837
Chris Lattner736ed5d2007-06-09 05:59:07 +00001838 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001839 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001840 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor13d05682010-06-16 23:08:59 +00001841 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregora771f462010-03-31 17:46:05 +00001842 << FixItHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001843 ConsumeToken();
1844 continue;
1845 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001846
1847 // Parse all the comma separated declarators.
1848 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001849
John McCallcfefb6d2009-11-03 02:38:08 +00001850 if (!Tok.is(tok::at)) {
1851 struct CFieldCallback : FieldCallback {
1852 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001853 Decl *TagDecl;
1854 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001855
John McCall48871652010-08-21 09:40:31 +00001856 CFieldCallback(Parser &P, Decl *TagDecl,
1857 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001858 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1859
John McCall48871652010-08-21 09:40:31 +00001860 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001861 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00001862 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00001863 FD.D.getDeclSpec().getSourceRange().getBegin(),
1864 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001865 FieldDecls.push_back(Field);
1866 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001867 }
John McCallcfefb6d2009-11-03 02:38:08 +00001868 } Callback(*this, TagDecl, FieldDecls);
1869
1870 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001871 } else { // Handle @defs
1872 ConsumeToken();
1873 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1874 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00001875 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001876 continue;
1877 }
1878 ConsumeToken();
1879 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1880 if (!Tok.is(tok::identifier)) {
1881 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00001882 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00001883 continue;
1884 }
John McCall48871652010-08-21 09:40:31 +00001885 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001886 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001887 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001888 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1889 ConsumeToken();
1890 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001891 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001892
Chris Lattner76c72282007-10-09 17:33:22 +00001893 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001894 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001895 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00001896 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001897 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001898 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00001899 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1900 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00001901 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00001902 // If we stopped at a ';', eat it.
1903 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00001904 }
1905 }
Mike Stump11289f42009-09-09 15:08:12 +00001906
Steve Naroff33a1e802007-10-29 21:38:07 +00001907 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001908
John McCall53fa7142010-12-24 02:08:15 +00001909 ParsedAttributes attrs;
Chris Lattner90a26b02007-01-23 04:38:16 +00001910 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001911 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00001912
Douglas Gregor0be31a22010-07-02 17:43:08 +00001913 Actions.ActOnFields(getCurScope(),
Jay Foad7d0479f2009-05-21 09:52:38 +00001914 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001915 LBraceLoc, RBraceLoc,
John McCall53fa7142010-12-24 02:08:15 +00001916 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001917 StructScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001918 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001919}
1920
Chris Lattner3b561a32006-08-13 00:12:11 +00001921/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001922/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001923/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001924///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001925/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1926/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001927/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001928/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001929///
Douglas Gregor0bf31402010-10-08 23:50:27 +00001930/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1931/// [C++0x] enum-head '{' enumerator-list ',' '}'
1932///
1933/// enum-head: [C++0x]
1934/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1935/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1936///
1937/// enum-key: [C++0x]
1938/// 'enum'
1939/// 'enum' 'class'
1940/// 'enum' 'struct'
1941///
1942/// enum-base: [C++0x]
1943/// ':' type-specifier-seq
1944///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001945/// [C++] elaborated-type-specifier:
1946/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1947///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001948void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00001949 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001950 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001951 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001952 if (Tok.is(tok::code_completion)) {
1953 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001954 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001955 ConsumeCodeCompletionToken();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001956 }
1957
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001958 // If attributes exist after tag, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001959 ParsedAttributes attrs;
1960 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001961
Abramo Bagnarad7548482010-05-19 21:37:53 +00001962 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall1f476a12010-02-26 08:45:28 +00001963 if (getLang().CPlusPlus) {
John McCallba7bf592010-08-24 05:47:05 +00001964 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall1f476a12010-02-26 08:45:28 +00001965 return;
1966
1967 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001968 Diag(Tok, diag::err_expected_ident);
1969 if (Tok.isNot(tok::l_brace)) {
1970 // Has no name and is not a definition.
1971 // Skip the rest of this declarator, up until the comma or semicolon.
1972 SkipUntil(tok::comma, true);
1973 return;
1974 }
1975 }
1976 }
Mike Stump11289f42009-09-09 15:08:12 +00001977
Douglas Gregora1aec292011-02-22 20:32:04 +00001978 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor0bf31402010-10-08 23:50:27 +00001979 bool IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001980 bool IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00001981
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001982 if (getLang().CPlusPlus0x &&
1983 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001984 IsScopedEnum = true;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001985 IsScopedUsingClassTag = Tok.is(tok::kw_class);
1986 ConsumeToken();
Douglas Gregor0bf31402010-10-08 23:50:27 +00001987 }
1988
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001989 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00001990 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
1991 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001992 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001993
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001994 // Skip the rest of this declarator, up until the comma or semicolon.
1995 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001996 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001997 }
Mike Stump11289f42009-09-09 15:08:12 +00001998
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001999 // If an identifier is present, consume and remember it.
2000 IdentifierInfo *Name = 0;
2001 SourceLocation NameLoc;
2002 if (Tok.is(tok::identifier)) {
2003 Name = Tok.getIdentifierInfo();
2004 NameLoc = ConsumeToken();
2005 }
Mike Stump11289f42009-09-09 15:08:12 +00002006
Douglas Gregor0bf31402010-10-08 23:50:27 +00002007 if (!Name && IsScopedEnum) {
2008 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2009 // declaration of a scoped enumeration.
2010 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2011 IsScopedEnum = false;
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002012 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00002013 }
2014
2015 TypeResult BaseType;
2016
Douglas Gregord1f69f62010-12-01 17:42:47 +00002017 // Parse the fixed underlying type.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002018 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002019 bool PossibleBitfield = false;
2020 if (getCurScope()->getFlags() & Scope::ClassScope) {
2021 // If we're in class scope, this can either be an enum declaration with
2022 // an underlying type, or a declaration of a bitfield member. We try to
2023 // use a simple disambiguation scheme first to catch the common cases
2024 // (integer literal, sizeof); if it's still ambiguous, we then consider
2025 // anything that's a simple-type-specifier followed by '(' as an
2026 // expression. This suffices because function types are not valid
2027 // underlying types anyway.
2028 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2029 // If the next token starts an expression, we know we're parsing a
2030 // bit-field. This is the common case.
2031 if (TPR == TPResult::True())
2032 PossibleBitfield = true;
2033 // If the next token starts a type-specifier-seq, it may be either a
2034 // a fixed underlying type or the start of a function-style cast in C++;
2035 // lookahead one more token to see if it's obvious that we have a
2036 // fixed underlying type.
2037 else if (TPR == TPResult::False() &&
2038 GetLookAheadToken(2).getKind() == tok::semi) {
2039 // Consume the ':'.
2040 ConsumeToken();
2041 } else {
2042 // We have the start of a type-specifier-seq, so we have to perform
2043 // tentative parsing to determine whether we have an expression or a
2044 // type.
2045 TentativeParsingAction TPA(*this);
2046
2047 // Consume the ':'.
2048 ConsumeToken();
2049
Douglas Gregora1aec292011-02-22 20:32:04 +00002050 if ((getLang().CPlusPlus &&
2051 isCXXDeclarationSpecifier() != TPResult::True()) ||
2052 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00002053 // We'll parse this as a bitfield later.
2054 PossibleBitfield = true;
2055 TPA.Revert();
2056 } else {
2057 // We have a type-specifier-seq.
2058 TPA.Commit();
2059 }
2060 }
2061 } else {
2062 // Consume the ':'.
2063 ConsumeToken();
2064 }
2065
2066 if (!PossibleBitfield) {
2067 SourceRange Range;
2068 BaseType = ParseTypeName(&Range);
Douglas Gregora1aec292011-02-22 20:32:04 +00002069
2070 if (!getLang().CPlusPlus0x)
2071 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2072 << Range;
Douglas Gregord1f69f62010-12-01 17:42:47 +00002073 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00002074 }
2075
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002076 // There are three options here. If we have 'enum foo;', then this is a
2077 // forward declaration. If we have 'enum foo {...' then this is a
2078 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2079 //
2080 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2081 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2082 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2083 //
John McCallfaf5fb42010-08-26 23:41:50 +00002084 Sema::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002085 if (Tok.is(tok::l_brace))
John McCallfaf5fb42010-08-26 23:41:50 +00002086 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002087 else if (Tok.is(tok::semi))
John McCallfaf5fb42010-08-26 23:41:50 +00002088 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00002089 else
John McCallfaf5fb42010-08-26 23:41:50 +00002090 TUK = Sema::TUK_Reference;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002091
2092 // enums cannot be templates, although they can be referenced from a
2093 // template.
2094 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00002095 TUK != Sema::TUK_Reference) {
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00002096 Diag(Tok, diag::err_enum_template);
2097
2098 // Skip the rest of this declarator, up until the comma or semicolon.
2099 SkipUntil(tok::comma, true);
2100 return;
2101 }
2102
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00002103 if (!Name && TUK != Sema::TUK_Definition) {
2104 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2105
2106 // Skip the rest of this declarator, up until the comma or semicolon.
2107 SkipUntil(tok::comma, true);
2108 return;
2109 }
2110
Douglas Gregord6ab8742009-05-28 23:31:59 +00002111 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00002112 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00002113 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2114 const char *PrevSpec = 0;
2115 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00002116 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00002117 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCall48871652010-08-21 09:40:31 +00002118 AS,
John McCallfaf5fb42010-08-26 23:41:50 +00002119 MultiTemplateParamsArg(Actions),
Douglas Gregor0bf31402010-10-08 23:50:27 +00002120 Owned, IsDependent, IsScopedEnum,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002121 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00002122
Douglas Gregorba41d012010-04-24 16:38:41 +00002123 if (IsDependent) {
2124 // This enum has a dependent nested-name-specifier. Handle it as a
2125 // dependent tag.
2126 if (!Name) {
2127 DS.SetTypeSpecError();
2128 Diag(Tok, diag::err_expected_type_name_after_typename);
2129 return;
2130 }
2131
Douglas Gregor0be31a22010-07-02 17:43:08 +00002132 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregorba41d012010-04-24 16:38:41 +00002133 TUK, SS, Name, StartLoc,
2134 NameLoc);
2135 if (Type.isInvalid()) {
2136 DS.SetTypeSpecError();
2137 return;
2138 }
2139
2140 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallba7bf592010-08-24 05:47:05 +00002141 Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00002142 Diag(StartLoc, DiagID) << PrevSpec;
2143
2144 return;
2145 }
Mike Stump11289f42009-09-09 15:08:12 +00002146
John McCall48871652010-08-21 09:40:31 +00002147 if (!TagDecl) {
Douglas Gregorba41d012010-04-24 16:38:41 +00002148 // The action failed to produce an enumeration tag. If this is a
2149 // definition, consume the entire definition.
2150 if (Tok.is(tok::l_brace)) {
2151 ConsumeBrace();
2152 SkipUntil(tok::r_brace);
2153 }
2154
2155 DS.SetTypeSpecError();
2156 return;
2157 }
2158
Chris Lattner76c72282007-10-09 17:33:22 +00002159 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002160 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002161
John McCallba7bf592010-08-24 05:47:05 +00002162 // FIXME: The DeclSpec should keep the locations of both the keyword
2163 // and the name (if there is one).
Douglas Gregor72100632010-01-25 16:33:23 +00002164 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCall48871652010-08-21 09:40:31 +00002165 TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00002166 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00002167}
2168
Chris Lattnerc1915e22007-01-25 07:29:02 +00002169/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2170/// enumerator-list:
2171/// enumerator
2172/// enumerator-list ',' enumerator
2173/// enumerator:
2174/// enumeration-constant
2175/// enumeration-constant '=' constant-expression
2176/// enumeration-constant:
2177/// identifier
2178///
John McCall48871652010-08-21 09:40:31 +00002179void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00002180 // Enter the scope of the enum body and start the definition.
2181 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002182 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00002183
Chris Lattnerc1915e22007-01-25 07:29:02 +00002184 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00002185
Chris Lattner37256fb2007-08-27 17:24:30 +00002186 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00002187 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00002188 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00002189
John McCall48871652010-08-21 09:40:31 +00002190 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00002191
John McCall48871652010-08-21 09:40:31 +00002192 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002193
Chris Lattnerc1915e22007-01-25 07:29:02 +00002194 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002195 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002196 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2197 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002198
John McCall811a0f52010-10-22 23:36:17 +00002199 // If attributes exist after the enumerator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002200 ParsedAttributes attrs;
2201 MaybeParseGNUAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00002202
Chris Lattnerc1915e22007-01-25 07:29:02 +00002203 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00002204 ExprResult AssignedVal;
Chris Lattner76c72282007-10-09 17:33:22 +00002205 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00002206 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002207 AssignedVal = ParseConstantExpression();
2208 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00002209 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002210 }
Mike Stump11289f42009-09-09 15:08:12 +00002211
Chris Lattnerc1915e22007-01-25 07:29:02 +00002212 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00002213 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2214 LastEnumConstDecl,
2215 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00002216 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00002217 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00002218 EnumConstantDecls.push_back(EnumConstDecl);
2219 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00002220
Douglas Gregorce66d022010-09-07 14:51:08 +00002221 if (Tok.is(tok::identifier)) {
2222 // We're missing a comma between enumerators.
2223 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2224 Diag(Loc, diag::err_enumerator_list_missing_comma)
2225 << FixItHint::CreateInsertion(Loc, ", ");
2226 continue;
2227 }
2228
Chris Lattner76c72282007-10-09 17:33:22 +00002229 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00002230 break;
2231 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002232
2233 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00002234 !(getLang().C99 || getLang().CPlusPlus0x))
2235 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2236 << getLang().CPlusPlus
Douglas Gregora771f462010-03-31 17:46:05 +00002237 << FixItHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002238 }
Mike Stump11289f42009-09-09 15:08:12 +00002239
Chris Lattnerc1915e22007-01-25 07:29:02 +00002240 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00002241 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002242
Chris Lattnerc1915e22007-01-25 07:29:02 +00002243 // If attributes exist after the identifier list, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002244 ParsedAttributes attrs;
2245 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002246
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00002247 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2248 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall53fa7142010-12-24 02:08:15 +00002249 getCurScope(), attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00002250
Douglas Gregor82ac25e2009-01-08 20:45:30 +00002251 EnumScope.Exit();
Douglas Gregor0be31a22010-07-02 17:43:08 +00002252 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00002253}
Chris Lattner3b561a32006-08-13 00:12:11 +00002254
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002255/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002256/// start of a type-qualifier-list.
2257bool Parser::isTypeQualifier() const {
2258 switch (Tok.getKind()) {
2259 default: return false;
2260 // type-qualifier
2261 case tok::kw_const:
2262 case tok::kw_volatile:
2263 case tok::kw_restrict:
2264 return true;
2265 }
2266}
2267
Chris Lattnerfd48afe2010-02-28 18:18:36 +00002268/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2269/// is definitely a type-specifier. Return false if it isn't part of a type
2270/// specifier or if we're not sure.
2271bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2272 switch (Tok.getKind()) {
2273 default: return false;
2274 // type-specifiers
2275 case tok::kw_short:
2276 case tok::kw_long:
2277 case tok::kw_signed:
2278 case tok::kw_unsigned:
2279 case tok::kw__Complex:
2280 case tok::kw__Imaginary:
2281 case tok::kw_void:
2282 case tok::kw_char:
2283 case tok::kw_wchar_t:
2284 case tok::kw_char16_t:
2285 case tok::kw_char32_t:
2286 case tok::kw_int:
2287 case tok::kw_float:
2288 case tok::kw_double:
2289 case tok::kw_bool:
2290 case tok::kw__Bool:
2291 case tok::kw__Decimal32:
2292 case tok::kw__Decimal64:
2293 case tok::kw__Decimal128:
2294 case tok::kw___vector:
2295
2296 // struct-or-union-specifier (C99) or class-specifier (C++)
2297 case tok::kw_class:
2298 case tok::kw_struct:
2299 case tok::kw_union:
2300 // enum-specifier
2301 case tok::kw_enum:
2302
2303 // typedef-name
2304 case tok::annot_typename:
2305 return true;
2306 }
2307}
2308
Steve Naroff69e8f9e2008-02-11 23:15:56 +00002309/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002310/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002311bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002312 switch (Tok.getKind()) {
2313 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002314
Chris Lattner020bab92009-01-04 23:41:41 +00002315 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00002316 if (TryAltiVecVectorToken())
2317 return true;
2318 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002319 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002320 // Annotate typenames and C++ scope specifiers. If we get one, just
2321 // recurse to handle whatever we get.
2322 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002323 return true;
2324 if (Tok.is(tok::identifier))
2325 return false;
2326 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00002327
Chris Lattner020bab92009-01-04 23:41:41 +00002328 case tok::coloncolon: // ::foo::bar
2329 if (NextToken().is(tok::kw_new) || // ::new
2330 NextToken().is(tok::kw_delete)) // ::delete
2331 return false;
2332
Chris Lattner020bab92009-01-04 23:41:41 +00002333 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002334 return true;
2335 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00002336
Chris Lattnere37e2332006-08-15 04:50:22 +00002337 // GNU attributes support.
2338 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002339 // GNU typeof support.
2340 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002341
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002342 // type-specifiers
2343 case tok::kw_short:
2344 case tok::kw_long:
2345 case tok::kw_signed:
2346 case tok::kw_unsigned:
2347 case tok::kw__Complex:
2348 case tok::kw__Imaginary:
2349 case tok::kw_void:
2350 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002351 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002352 case tok::kw_char16_t:
2353 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002354 case tok::kw_int:
2355 case tok::kw_float:
2356 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002357 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002358 case tok::kw__Bool:
2359 case tok::kw__Decimal32:
2360 case tok::kw__Decimal64:
2361 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002362 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002363
Chris Lattner861a2262008-04-13 18:59:07 +00002364 // struct-or-union-specifier (C99) or class-specifier (C++)
2365 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002366 case tok::kw_struct:
2367 case tok::kw_union:
2368 // enum-specifier
2369 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002370
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002371 // type-qualifier
2372 case tok::kw_const:
2373 case tok::kw_volatile:
2374 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002375
2376 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002377 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002378 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002379
Chris Lattner409bf7d2008-10-20 00:25:30 +00002380 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2381 case tok::less:
2382 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002383
Steve Naroff44ac7772008-12-25 14:16:32 +00002384 case tok::kw___cdecl:
2385 case tok::kw___stdcall:
2386 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002387 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002388 case tok::kw___w64:
2389 case tok::kw___ptr64:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002390 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002391 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002392 }
2393}
2394
Chris Lattneracd58a32006-08-06 17:24:14 +00002395/// isDeclarationSpecifier() - Return true if the current token is part of a
2396/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002397///
2398/// \param DisambiguatingWithExpression True to indicate that the purpose of
2399/// this check is to disambiguate between an expression and a declaration.
2400bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002401 switch (Tok.getKind()) {
2402 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002403
Chris Lattner020bab92009-01-04 23:41:41 +00002404 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002405 // Unfortunate hack to support "Class.factoryMethod" notation.
2406 if (getLang().ObjC1 && NextToken().is(tok::period))
2407 return false;
John Thompson22334602010-02-05 00:12:22 +00002408 if (TryAltiVecVectorToken())
2409 return true;
2410 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00002411 case tok::kw_typename: // typename T::type
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 if (Tok.is(tok::identifier))
2417 return false;
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00002418
2419 // If we're in Objective-C and we have an Objective-C class type followed
2420 // by an identifier and then either ':' or ']', in a place where an
2421 // expression is permitted, then this is probably a class message send
2422 // missing the initial '['. In this case, we won't consider this to be
2423 // the start of a declaration.
2424 if (DisambiguatingWithExpression &&
2425 isStartOfObjCClassMessageMissingOpenBracket())
2426 return false;
2427
John McCall1f476a12010-02-26 08:45:28 +00002428 return isDeclarationSpecifier();
2429
Chris Lattner020bab92009-01-04 23:41:41 +00002430 case tok::coloncolon: // ::foo::bar
2431 if (NextToken().is(tok::kw_new) || // ::new
2432 NextToken().is(tok::kw_delete)) // ::delete
2433 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002434
Chris Lattner020bab92009-01-04 23:41:41 +00002435 // Annotate typenames and C++ scope specifiers. If we get one, just
2436 // recurse to handle whatever we get.
2437 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00002438 return true;
2439 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00002440
Chris Lattneracd58a32006-08-06 17:24:14 +00002441 // storage-class-specifier
2442 case tok::kw_typedef:
2443 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002444 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002445 case tok::kw_static:
2446 case tok::kw_auto:
2447 case tok::kw_register:
2448 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002449
Chris Lattneracd58a32006-08-06 17:24:14 +00002450 // type-specifiers
2451 case tok::kw_short:
2452 case tok::kw_long:
2453 case tok::kw_signed:
2454 case tok::kw_unsigned:
2455 case tok::kw__Complex:
2456 case tok::kw__Imaginary:
2457 case tok::kw_void:
2458 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002459 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002460 case tok::kw_char16_t:
2461 case tok::kw_char32_t:
2462
Chris Lattneracd58a32006-08-06 17:24:14 +00002463 case tok::kw_int:
2464 case tok::kw_float:
2465 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002466 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002467 case tok::kw__Bool:
2468 case tok::kw__Decimal32:
2469 case tok::kw__Decimal64:
2470 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00002471 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00002472
Chris Lattner861a2262008-04-13 18:59:07 +00002473 // struct-or-union-specifier (C99) or class-specifier (C++)
2474 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002475 case tok::kw_struct:
2476 case tok::kw_union:
2477 // enum-specifier
2478 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002479
Chris Lattneracd58a32006-08-06 17:24:14 +00002480 // type-qualifier
2481 case tok::kw_const:
2482 case tok::kw_volatile:
2483 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002484
Chris Lattneracd58a32006-08-06 17:24:14 +00002485 // function-specifier
2486 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002487 case tok::kw_virtual:
2488 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002489
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002490 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002491 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002492
Chris Lattner599e47e2007-08-09 17:01:07 +00002493 // GNU typeof support.
2494 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002495
Chris Lattner599e47e2007-08-09 17:01:07 +00002496 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002497 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002498 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002499
Chris Lattner8b2ec162008-07-26 03:38:44 +00002500 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2501 case tok::less:
2502 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002503
Steve Narofff192fab2009-01-06 19:34:12 +00002504 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002505 case tok::kw___cdecl:
2506 case tok::kw___stdcall:
2507 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002508 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00002509 case tok::kw___w64:
2510 case tok::kw___ptr64:
2511 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002512 case tok::kw___pascal:
Eli Friedman53339e02009-06-08 23:27:34 +00002513 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002514 }
2515}
2516
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002517bool Parser::isConstructorDeclarator() {
2518 TentativeParsingAction TPA(*this);
2519
2520 // Parse the C++ scope specifier.
2521 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002522 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall1f476a12010-02-26 08:45:28 +00002523 TPA.Revert();
2524 return false;
2525 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002526
2527 // Parse the constructor name.
2528 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2529 // We already know that we have a constructor name; just consume
2530 // the token.
2531 ConsumeToken();
2532 } else {
2533 TPA.Revert();
2534 return false;
2535 }
2536
2537 // Current class name must be followed by a left parentheses.
2538 if (Tok.isNot(tok::l_paren)) {
2539 TPA.Revert();
2540 return false;
2541 }
2542 ConsumeParen();
2543
2544 // A right parentheses or ellipsis signals that we have a constructor.
2545 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2546 TPA.Revert();
2547 return true;
2548 }
2549
2550 // If we need to, enter the specified scope.
2551 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00002552 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002553 DeclScopeObj.EnterDeclaratorScope();
2554
Francois Pichet79f3a872011-01-31 04:54:32 +00002555 // Optionally skip Microsoft attributes.
2556 ParsedAttributes Attrs;
2557 MaybeParseMicrosoftAttributes(Attrs);
2558
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002559 // Check whether the next token(s) are part of a declaration
2560 // specifier, in which case we have the start of a parameter and,
2561 // therefore, we know that this is a constructor.
2562 bool IsConstructor = isDeclarationSpecifier();
2563 TPA.Revert();
2564 return IsConstructor;
2565}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002566
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002567/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00002568/// type-qualifier-list: [C99 6.7.5]
2569/// type-qualifier
2570/// [vendor] attributes
2571/// [ only if VendorAttributesAllowed=true ]
2572/// type-qualifier-list type-qualifier
2573/// [vendor] type-qualifier-list attributes
2574/// [ only if VendorAttributesAllowed=true ]
2575/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2576/// [ only if CXX0XAttributesAllowed=true ]
2577/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002578///
Dawn Perchik335e16b2010-09-03 01:29:35 +00002579void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2580 bool VendorAttributesAllowed,
Alexis Hunt96d5c762009-11-21 08:43:09 +00002581 bool CXX0XAttributesAllowed) {
2582 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2583 SourceLocation Loc = Tok.getLocation();
John McCall53fa7142010-12-24 02:08:15 +00002584 ParsedAttributesWithRange attrs;
2585 ParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002586 if (CXX0XAttributesAllowed)
John McCall53fa7142010-12-24 02:08:15 +00002587 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002588 else
2589 Diag(Loc, diag::err_attributes_not_allowed);
2590 }
2591
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002592 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002593 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002594 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002595 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002596 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002597
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002598 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00002599 case tok::code_completion:
2600 Actions.CodeCompleteTypeQualifiers(DS);
2601 ConsumeCodeCompletionToken();
2602 break;
2603
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002604 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002605 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2606 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002607 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002608 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002609 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2610 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002611 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002612 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002613 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2614 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002615 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002616 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002617 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002618 case tok::kw___cdecl:
2619 case tok::kw___stdcall:
2620 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002621 case tok::kw___thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002622 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002623 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002624 continue;
2625 }
2626 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002627 case tok::kw___pascal:
2628 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002629 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002630 continue;
2631 }
2632 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002633 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002634 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00002635 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002636 continue; // do *not* consume the next token!
2637 }
2638 // otherwise, FALL THROUGH!
2639 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002640 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002641 // If this is not a type-qualifier token, we're done reading type
2642 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002643 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002644 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002645 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002646
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002647 // If the specifier combination wasn't legal, issue a diagnostic.
2648 if (isInvalid) {
2649 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002650 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002651 }
2652 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002653 }
2654}
2655
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002656
2657/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2658///
2659void Parser::ParseDeclarator(Declarator &D) {
2660 /// This implements the 'declarator' production in the C grammar, then checks
2661 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002662 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002663}
2664
Sebastian Redlbd150f42008-11-21 19:14:01 +00002665/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2666/// is parsed by the function passed to it. Pass null, and the direct-declarator
2667/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002668/// ptr-operator production.
2669///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002670/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2671/// [C] pointer[opt] direct-declarator
2672/// [C++] direct-declarator
2673/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002674///
2675/// pointer: [C99 6.7.5]
2676/// '*' type-qualifier-list[opt]
2677/// '*' type-qualifier-list[opt] pointer
2678///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002679/// ptr-operator:
2680/// '*' cv-qualifier-seq[opt]
2681/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002682/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002683/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002684/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002685/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002686void Parser::ParseDeclaratorInternal(Declarator &D,
2687 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002688 if (Diags.hasAllExtensionsSilenced())
2689 D.setExtension();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002690
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002691 // C++ member pointers start with a '::' or a nested-name.
2692 // Member pointers get special handling, since there's no place for the
2693 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002694 if (getLang().CPlusPlus &&
2695 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2696 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002697 CXXScopeSpec SS;
John McCallba7bf592010-08-24 05:47:05 +00002698 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall1f476a12010-02-26 08:45:28 +00002699
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00002700 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00002701 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002702 // The scope spec really belongs to the direct-declarator.
2703 D.getCXXScopeSpec() = SS;
2704 if (DirectDeclParser)
2705 (this->*DirectDeclParser)(D);
2706 return;
2707 }
2708
2709 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002710 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002711 DeclSpec DS;
2712 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002713 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002714
2715 // Recurse to parse whatever is left.
2716 ParseDeclaratorInternal(D, DirectDeclParser);
2717
2718 // Sema will have to catch (syntactically invalid) pointers into global
2719 // scope. It has to catch pointers into namespace scope anyway.
2720 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall53fa7142010-12-24 02:08:15 +00002721 Loc, DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002722 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002723 return;
2724 }
2725 }
2726
2727 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002728 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002729 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002730 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002731 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002732 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002733 if (DirectDeclParser)
2734 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002735 return;
2736 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002737
Sebastian Redled0f3b02009-03-15 22:02:01 +00002738 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2739 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002740 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002741 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002742
Chris Lattner9eac9312009-03-27 04:18:06 +00002743 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002744 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002745 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002746
Bill Wendling3708c182007-05-27 10:15:43 +00002747 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002748 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002749
Bill Wendling3708c182007-05-27 10:15:43 +00002750 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002751 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002752 if (Kind == tok::star)
2753 // Remember that we parsed a pointer type, and remember the type-quals.
2754 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
John McCall53fa7142010-12-24 02:08:15 +00002755 DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002756 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002757 else
2758 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002759 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall53fa7142010-12-24 02:08:15 +00002760 Loc, DS.takeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002761 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002762 } else {
2763 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002764 DeclSpec DS;
2765
Sebastian Redl3b27be62009-03-23 00:00:23 +00002766 // Complain about rvalue references in C++03, but then go on and build
2767 // the declarator.
2768 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor00984992011-01-25 02:17:32 +00002769 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00002770
Bill Wendling93efb222007-06-02 23:28:54 +00002771 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2772 // cv-qualifiers are introduced through the use of a typedef or of a
2773 // template type argument, in which case the cv-qualifiers are ignored.
2774 //
2775 // [GNU] Retricted references are allowed.
2776 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002777 // [C++0x] Attributes on references are not allowed.
2778 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002779 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002780
2781 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2782 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2783 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002784 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002785 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2786 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002787 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002788 }
Bill Wendling3708c182007-05-27 10:15:43 +00002789
2790 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002791 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002792
Douglas Gregor66583c52008-11-03 15:51:28 +00002793 if (D.getNumTypeObjects() > 0) {
2794 // C++ [dcl.ref]p4: There shall be no references to references.
2795 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2796 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002797 if (const IdentifierInfo *II = D.getIdentifier())
2798 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2799 << II;
2800 else
2801 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2802 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002803
Sebastian Redlbd150f42008-11-21 19:14:01 +00002804 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002805 // can go ahead and build the (technically ill-formed)
2806 // declarator: reference collapsing will take care of it.
2807 }
2808 }
2809
Bill Wendling3708c182007-05-27 10:15:43 +00002810 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002811 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
John McCall53fa7142010-12-24 02:08:15 +00002812 DS.takeAttributes(),
Sebastian Redled0f3b02009-03-15 22:02:01 +00002813 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002814 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002815 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002816}
2817
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002818/// ParseDirectDeclarator
2819/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002820/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002821/// '(' declarator ')'
2822/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002823/// [C90] direct-declarator '[' constant-expression[opt] ']'
2824/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2825/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2826/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2827/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002828/// direct-declarator '(' parameter-type-list ')'
2829/// direct-declarator '(' identifier-list[opt] ')'
2830/// [GNU] direct-declarator '(' parameter-forward-declarations
2831/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002832/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2833/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002834/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002835///
2836/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00002837/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00002838/// '::'[opt] nested-name-specifier[opt] type-name
2839///
2840/// id-expression: [C++ 5.1]
2841/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002842/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002843///
2844/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002845/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002846/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002847/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002848/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002849/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002850///
Chris Lattneracd58a32006-08-06 17:24:14 +00002851void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002852 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002853
Douglas Gregor7861a802009-11-03 01:35:08 +00002854 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2855 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002856 if (D.getCXXScopeSpec().isEmpty()) {
John McCallba7bf592010-08-24 05:47:05 +00002857 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall1f476a12010-02-26 08:45:28 +00002858 }
2859
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002860 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00002861 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00002862 // Change the declaration context for name lookup, until this function
2863 // is exited (and the declarator has been parsed).
2864 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002865 }
2866
Douglas Gregor27b4c162010-12-23 22:44:42 +00002867 // C++0x [dcl.fct]p14:
2868 // There is a syntactic ambiguity when an ellipsis occurs at the end
2869 // of a parameter-declaration-clause without a preceding comma. In
2870 // this case, the ellipsis is parsed as part of the
2871 // abstract-declarator if the type of the parameter names a template
2872 // parameter pack that has not been expanded; otherwise, it is parsed
2873 // as part of the parameter-declaration-clause.
2874 if (Tok.is(tok::ellipsis) &&
2875 !((D.getContext() == Declarator::PrototypeContext ||
2876 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00002877 NextToken().is(tok::r_paren) &&
2878 !Actions.containsUnexpandedParameterPacks(D)))
2879 D.setEllipsisLoc(ConsumeToken());
2880
Douglas Gregor7861a802009-11-03 01:35:08 +00002881 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2882 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2883 // We found something that indicates the start of an unqualified-id.
2884 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00002885 bool AllowConstructorName;
2886 if (D.getDeclSpec().hasTypeSpecifier())
2887 AllowConstructorName = false;
2888 else if (D.getCXXScopeSpec().isSet())
2889 AllowConstructorName =
2890 (D.getContext() == Declarator::FileContext ||
2891 (D.getContext() == Declarator::MemberContext &&
2892 D.getDeclSpec().isFriendSpecified()));
2893 else
2894 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2895
Douglas Gregor7861a802009-11-03 01:35:08 +00002896 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2897 /*EnteringContext=*/true,
2898 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002899 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00002900 ParsedType(),
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00002901 D.getName()) ||
2902 // Once we're past the identifier, if the scope was bad, mark the
2903 // whole declarator bad.
2904 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002905 D.SetIdentifier(0, Tok.getLocation());
2906 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002907 } else {
2908 // Parsed the unqualified-id; update range information and move along.
2909 if (D.getSourceRange().getBegin().isInvalid())
2910 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2911 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002912 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002913 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002914 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002915 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002916 assert(!getLang().CPlusPlus &&
2917 "There's a C++-specific check for tok::identifier above");
2918 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2919 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2920 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002921 goto PastIdentifier;
2922 }
2923
2924 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002925 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002926 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002927 // Example: 'char (*X)' or 'int (*XX)(void)'
2928 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002929
2930 // If the declarator was parenthesized, we entered the declarator
2931 // scope when parsing the parenthesized declarator, then exited
2932 // the scope already. Re-enter the scope, if we need to.
2933 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002934 // If there was an error parsing parenthesized declarator, declarator
2935 // scope may have been enterred before. Don't do it again.
2936 if (!D.isInvalidType() &&
2937 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002938 // Change the declaration context for name lookup, until this function
2939 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00002940 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002941 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002942 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002943 // This could be something simple like "int" (in which case the declarator
2944 // portion is empty), if an abstract-declarator is allowed.
2945 D.SetIdentifier(0, Tok.getLocation());
2946 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002947 if (D.getContext() == Declarator::MemberContext)
2948 Diag(Tok, diag::err_expected_member_name_or_semi)
2949 << D.getDeclSpec().getSourceRange();
2950 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002951 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002952 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002953 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002954 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002955 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002956 }
Mike Stump11289f42009-09-09 15:08:12 +00002957
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002958 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002959 assert(D.isPastIdentifier() &&
2960 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002961
Alexis Hunt96d5c762009-11-21 08:43:09 +00002962 // Don't parse attributes unless we have an identifier.
John McCall53fa7142010-12-24 02:08:15 +00002963 if (D.getIdentifier())
2964 MaybeParseCXX0XAttributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002965
Chris Lattneracd58a32006-08-06 17:24:14 +00002966 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002967 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002968 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2969 // In such a case, check if we actually have a function declarator; if it
2970 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002971 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2972 // When not in file scope, warn for ambiguous function declarators, just
2973 // in case the author intended it as a variable definition.
2974 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2975 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2976 break;
2977 }
John McCall53fa7142010-12-24 02:08:15 +00002978 ParsedAttributes attrs;
2979 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner76c72282007-10-09 17:33:22 +00002980 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002981 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002982 } else {
2983 break;
2984 }
2985 }
2986}
2987
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002988/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2989/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002990/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002991/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2992///
2993/// direct-declarator:
2994/// '(' declarator ')'
2995/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002996/// direct-declarator '(' parameter-type-list ')'
2997/// direct-declarator '(' identifier-list[opt] ')'
2998/// [GNU] direct-declarator '(' parameter-forward-declarations
2999/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003000///
3001void Parser::ParseParenDeclarator(Declarator &D) {
3002 SourceLocation StartLoc = ConsumeParen();
3003 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00003004
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003005 // Eat any attributes before we look at whether this is a grouping or function
3006 // declarator paren. If this is a grouping paren, the attribute applies to
3007 // the type being built up, for example:
3008 // int (__attribute__(()) *x)(long y)
3009 // If this ends up not being a grouping paren, the attribute applies to the
3010 // first argument, for example:
3011 // int (__attribute__(()) int x)
3012 // In either case, we need to eat any attributes to be able to determine what
3013 // sort of paren this is.
3014 //
John McCall53fa7142010-12-24 02:08:15 +00003015 ParsedAttributes attrs;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003016 bool RequiresArg = false;
3017 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00003018 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003019
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003020 // We require that the argument list (if this is a non-grouping paren) be
3021 // present even if the attribute list was empty.
3022 RequiresArg = true;
3023 }
Steve Naroff44ac7772008-12-25 14:16:32 +00003024 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00003025 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +00003026 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3027 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall53fa7142010-12-24 02:08:15 +00003028 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman53339e02009-06-08 23:27:34 +00003029 }
Dawn Perchik335e16b2010-09-03 01:29:35 +00003030 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003031 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00003032 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003033
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003034 // If we haven't past the identifier yet (or where the identifier would be
3035 // stored, if this is an abstract declarator), then this is probably just
3036 // grouping parens. However, if this could be an abstract-declarator, then
3037 // this could also be the start of function arguments (consider 'void()').
3038 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00003039
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003040 if (!D.mayOmitIdentifier()) {
3041 // If this can't be an abstract-declarator, this *must* be a grouping
3042 // paren, because we haven't seen the identifier yet.
3043 isGrouping = true;
3044 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00003045 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003046 isDeclarationSpecifier()) { // 'int(int)' is a function.
3047 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3048 // considered to be a type, not a K&R identifier-list.
3049 isGrouping = false;
3050 } else {
3051 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3052 isGrouping = true;
3053 }
Mike Stump11289f42009-09-09 15:08:12 +00003054
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003055 // If this is a grouping paren, handle:
3056 // direct-declarator: '(' declarator ')'
3057 // direct-declarator: '(' attributes declarator ')'
3058 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003059 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003060 D.setGroupingParens(true);
John McCall53fa7142010-12-24 02:08:15 +00003061 if (!attrs.empty())
3062 D.addAttributes(attrs.getList(), SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00003063
Sebastian Redlbd150f42008-11-21 19:14:01 +00003064 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003065 // Match the ')'.
Abramo Bagnara924a8f32010-12-10 16:29:40 +00003066 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
3067 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc), EndLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00003068
3069 D.setGroupingParens(hadGroupingParens);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003070 return;
3071 }
Mike Stump11289f42009-09-09 15:08:12 +00003072
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003073 // Okay, if this wasn't a grouping paren, it must be the start of a function
3074 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003075 // identifier (and remember where it would have been), then call into
3076 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003077 D.SetIdentifier(0, Tok.getLocation());
3078
John McCall53fa7142010-12-24 02:08:15 +00003079 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003080}
3081
3082/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3083/// declarator D up to a paren, which indicates that we are parsing function
3084/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00003085///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003086/// If AttrList is non-null, then the caller parsed those arguments immediately
3087/// after the open paren - they should be considered to be the first argument of
3088/// a parameter. If RequiresArg is true, then the first argument of the
3089/// function is required to be present and required to not be an identifier
3090/// list.
3091///
Chris Lattneracd58a32006-08-06 17:24:14 +00003092/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003093/// parameter-type-list: [C99 6.7.5]
3094/// parameter-list
3095/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003096/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003097///
3098/// parameter-list: [C99 6.7.5]
3099/// parameter-declaration
3100/// parameter-list ',' parameter-declaration
3101///
3102/// parameter-declaration: [C99 6.7.5]
3103/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003104/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003105/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00003106/// declaration-specifiers abstract-declarator[opt]
3107/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00003108/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00003109/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003110///
Douglas Gregor54992352011-01-26 03:43:54 +00003111/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3112/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003113///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003114void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall53fa7142010-12-24 02:08:15 +00003115 ParsedAttributes &attrs,
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003116 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003117 // lparen is already consumed!
3118 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00003119
Douglas Gregor7fb25412010-10-01 18:44:50 +00003120 ParsedType TrailingReturnType;
3121
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003122 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00003123 if (Tok.is(tok::r_paren)) {
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003124 if (RequiresArg)
Chris Lattner6d29c102008-11-18 07:48:38 +00003125 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003126
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003127 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3128 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003129
3130 // cv-qualifier-seq[opt].
3131 DeclSpec DS;
Douglas Gregor54992352011-01-26 03:43:54 +00003132 SourceLocation RefQualifierLoc;
3133 bool RefQualifierIsLValueRef = true;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003134 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003135 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003136 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003137 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003138 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003139 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003140 MaybeParseCXX0XAttributes(attrs);
3141
Chris Lattnercf0bab22008-12-18 07:02:59 +00003142 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003143 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003144 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003145
Douglas Gregor54992352011-01-26 03:43:54 +00003146 // Parse ref-qualifier[opt]
3147 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3148 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003149 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003150
3151 RefQualifierIsLValueRef = Tok.is(tok::amp);
3152 RefQualifierLoc = ConsumeToken();
3153 EndLoc = RefQualifierLoc;
3154 }
3155
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003156 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003157 if (Tok.is(tok::kw_throw)) {
3158 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003159 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003160 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003161 hasAnyExceptionSpec);
3162 assert(Exceptions.size() == ExceptionRanges.size() &&
3163 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003164 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003165
3166 // Parse trailing-return-type.
3167 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3168 TrailingReturnType = ParseTrailingReturnType().get();
3169 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003170 }
3171
Chris Lattner371ed4e2008-04-06 06:57:35 +00003172 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00003173 // int() -> no prototype, no '...'.
John McCall53fa7142010-12-24 02:08:15 +00003174 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3175 /*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00003176 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003177 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003178 /*arglist*/ 0, 0,
3179 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003180 RefQualifierIsLValueRef,
3181 RefQualifierLoc,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003182 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003183 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003184 Exceptions.data(),
3185 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003186 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003187 LParenLoc, RParenLoc, D,
3188 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003189 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00003190 return;
Sebastian Redld6434562009-05-29 18:02:33 +00003191 }
3192
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003193 // Alternatively, this parameter list may be an identifier list form for a
3194 // K&R-style function: void foo(a,b,c)
John Thompson22334602010-02-05 00:12:22 +00003195 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3196 && !TryAltiVecVectorToken()) {
John McCall1f476a12010-02-26 08:45:28 +00003197 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003198 // K&R identifier lists can't have typedefs as identifiers, per
3199 // C99 6.7.5.3p11.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00003200 if (RequiresArg)
Steve Naroffb0486722009-01-28 19:16:40 +00003201 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner9453ab82010-05-14 17:23:36 +00003202
Steve Naroffb0486722009-01-28 19:16:40 +00003203 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner9453ab82010-05-14 17:23:36 +00003204 // normal declarators, not for abstract-declarators. Get the first
3205 // identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003206 Token FirstTok = Tok;
Chris Lattner9453ab82010-05-14 17:23:36 +00003207 ConsumeToken(); // eat the first identifier.
Chris Lattnerff895c12010-05-14 17:44:56 +00003208
3209 // Identifier lists follow a really simple grammar: the identifiers can
3210 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3211 // identifier lists are really rare in the brave new modern world, and it
3212 // is very common for someone to typo a type in a non-k&r style list. If
3213 // we are presented with something like: "void foo(intptr x, float y)",
3214 // we don't want to start parsing the function declarator as though it is
3215 // a K&R style declarator just because intptr is an invalid type.
3216 //
3217 // To handle this, we check to see if the token after the first identifier
3218 // is a "," or ")". Only if so, do we parse it as an identifier list.
3219 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3220 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3221 FirstTok.getIdentifierInfo(),
3222 FirstTok.getLocation(), D);
3223
3224 // If we get here, the code is invalid. Push the first identifier back
3225 // into the token stream and parse the first argument as an (invalid)
3226 // normal argument declarator.
3227 PP.EnterToken(Tok);
3228 Tok = FirstTok;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003229 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00003230 }
Mike Stump11289f42009-09-09 15:08:12 +00003231
Chris Lattner371ed4e2008-04-06 06:57:35 +00003232 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00003233
Chris Lattner371ed4e2008-04-06 06:57:35 +00003234 // Build up an array of information about the parsed arguments.
3235 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003236
3237 // Enter function-declaration scope, limiting any declarators to the
3238 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00003239 ParseScope PrototypeScope(this,
3240 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00003241
Chris Lattner371ed4e2008-04-06 06:57:35 +00003242 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003243 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00003244 while (1) {
3245 if (Tok.is(tok::ellipsis)) {
3246 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00003247 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003248 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00003249 }
Mike Stump11289f42009-09-09 15:08:12 +00003250
Chris Lattner371ed4e2008-04-06 06:57:35 +00003251 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00003252 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00003253 DeclSpec DS;
John McCall53fa7142010-12-24 02:08:15 +00003254
3255 // Skip any Microsoft attributes before a param.
3256 if (getLang().Microsoft && Tok.is(tok::l_square))
3257 ParseMicrosoftAttributes(DS.getAttributes());
3258
3259 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00003260
3261 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00003262 // Take them so that we only apply the attributes to the first parameter.
3263 DS.takeAttributesFrom(attrs);
3264
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003265 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003266
Chris Lattner371ed4e2008-04-06 06:57:35 +00003267 // Parse the declarator. This is "PrototypeContext", because we must
3268 // accept either 'declarator' or 'abstract-declarator' here.
3269 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3270 ParseDeclarator(ParmDecl);
3271
3272 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00003273 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003274
Chris Lattner371ed4e2008-04-06 06:57:35 +00003275 // Remember this parsed parameter in ParamInfo.
3276 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00003277
Douglas Gregor4d87df52008-12-16 21:30:33 +00003278 // DefArgToks is used when the parsing of default arguments needs
3279 // to be delayed.
3280 CachedTokens *DefArgToks = 0;
3281
Chris Lattner371ed4e2008-04-06 06:57:35 +00003282 // If no parameter was specified, verify that *something* was specified,
3283 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00003284 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3285 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00003286 // Completely missing, emit error.
3287 Diag(DSStart, diag::err_missing_param);
3288 } else {
3289 // Otherwise, we have something. Add it and let semantic analysis try
3290 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00003291
Chris Lattner371ed4e2008-04-06 06:57:35 +00003292 // Inform the actions module about the parameter declarator, so it gets
3293 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00003294 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003295
3296 // Parse the default argument, if any. We parse the default
3297 // arguments in all dialects; the semantic analysis in
3298 // ActOnParamDefaultArgument will reject the default argument in
3299 // C.
3300 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00003301 SourceLocation EqualLoc = Tok.getLocation();
3302
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003303 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00003304 if (D.getContext() == Declarator::MemberContext) {
3305 // If we're inside a class definition, cache the tokens
3306 // corresponding to the default argument. We'll actually parse
3307 // them when we see the end of the class definition.
3308 // FIXME: Templates will require something similar.
3309 // FIXME: Can we use a smart pointer for Toks?
3310 DefArgToks = new CachedTokens;
3311
Mike Stump11289f42009-09-09 15:08:12 +00003312 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00003313 /*StopAtSemi=*/true,
3314 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003315 delete DefArgToks;
3316 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00003317 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003318 } else {
3319 // Mark the end of the default argument so that we know when to
3320 // stop when we parse it later on.
3321 Token DefArgEnd;
3322 DefArgEnd.startToken();
3323 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3324 DefArgEnd.setLocation(Tok.getLocation());
3325 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00003326 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00003327 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00003328 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003329 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00003330 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00003331 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003332
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00003333 // The argument isn't actually potentially evaluated unless it is
3334 // used.
3335 EnterExpressionEvaluationContext Eval(Actions,
3336 Sema::PotentiallyEvaluatedIfUsed);
3337
John McCalldadc5752010-08-24 06:29:42 +00003338 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003339 if (DefArgResult.isInvalid()) {
3340 Actions.ActOnParamDefaultArgumentError(Param);
3341 SkipUntil(tok::comma, tok::r_paren, true, true);
3342 } else {
3343 // Inform the actions module about the default argument
3344 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00003345 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00003346 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00003347 }
3348 }
Mike Stump11289f42009-09-09 15:08:12 +00003349
3350 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3351 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00003352 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00003353 }
3354
3355 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003356 if (Tok.isNot(tok::comma)) {
3357 if (Tok.is(tok::ellipsis)) {
3358 IsVariadic = true;
3359 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3360
3361 if (!getLang().CPlusPlus) {
3362 // We have ellipsis without a preceding ',', which is ill-formed
3363 // in C. Complain and provide the fix.
3364 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00003365 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00003366 }
3367 }
3368
3369 break;
3370 }
Mike Stump11289f42009-09-09 15:08:12 +00003371
Chris Lattner371ed4e2008-04-06 06:57:35 +00003372 // Consume the comma.
3373 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00003374 }
Mike Stump11289f42009-09-09 15:08:12 +00003375
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003376 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003377 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3378 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003379
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003380 DeclSpec DS;
Douglas Gregor54992352011-01-26 03:43:54 +00003381 SourceLocation RefQualifierLoc;
3382 bool RefQualifierIsLValueRef = true;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003383 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003384 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003385 bool hasAnyExceptionSpec = false;
John McCallba7bf592010-08-24 05:47:05 +00003386 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redld6434562009-05-29 18:02:33 +00003387 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00003388
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003389 if (getLang().CPlusPlus) {
John McCall53fa7142010-12-24 02:08:15 +00003390 MaybeParseCXX0XAttributes(attrs);
3391
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003392 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00003393 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003394 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003395 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003396
Douglas Gregor54992352011-01-26 03:43:54 +00003397 // Parse ref-qualifier[opt]
3398 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3399 if (!getLang().CPlusPlus0x)
Douglas Gregora5271302011-01-26 20:35:32 +00003400 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor54992352011-01-26 03:43:54 +00003401
3402 RefQualifierIsLValueRef = Tok.is(tok::amp);
3403 RefQualifierLoc = ConsumeToken();
3404 EndLoc = RefQualifierLoc;
3405 }
3406
Douglas Gregor2afd0be2008-11-25 03:22:00 +00003407 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003408 if (Tok.is(tok::kw_throw)) {
3409 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003410 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003411 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00003412 hasAnyExceptionSpec);
3413 assert(Exceptions.size() == ExceptionRanges.size() &&
3414 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003415 }
Douglas Gregor7fb25412010-10-01 18:44:50 +00003416
3417 // Parse trailing-return-type.
3418 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3419 TrailingReturnType = ParseTrailingReturnType().get();
3420 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003421 }
3422
Douglas Gregor7fb25412010-10-01 18:44:50 +00003423 // FIXME: We should leave the prototype scope before parsing the exception
3424 // specification, and then reenter it when parsing the trailing return type.
3425
3426 // Leave prototype scope.
3427 PrototypeScope.Exit();
3428
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00003429 // Remember that we parsed a function type, and remember the attributes.
John McCall53fa7142010-12-24 02:08:15 +00003430 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3431 /*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003432 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00003433 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00003434 DS.getTypeQualifiers(),
Douglas Gregor54992352011-01-26 03:43:54 +00003435 RefQualifierIsLValueRef,
3436 RefQualifierLoc,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003437 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003438 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00003439 Exceptions.data(),
3440 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003441 Exceptions.size(),
Douglas Gregor7fb25412010-10-01 18:44:50 +00003442 LParenLoc, RParenLoc, D,
3443 TrailingReturnType),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003444 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003445}
Chris Lattneracd58a32006-08-06 17:24:14 +00003446
Chris Lattner6c940e62008-04-06 06:34:08 +00003447/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3448/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner9453ab82010-05-14 17:23:36 +00003449/// first identifier has already been consumed, and the current token is the
3450/// token right after it.
Chris Lattner6c940e62008-04-06 06:34:08 +00003451///
3452/// identifier-list: [C99 6.7.5]
3453/// identifier
3454/// identifier-list ',' identifier
3455///
3456void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner9453ab82010-05-14 17:23:36 +00003457 IdentifierInfo *FirstIdent,
3458 SourceLocation FirstIdentLoc,
Chris Lattner6c940e62008-04-06 06:34:08 +00003459 Declarator &D) {
3460 // Build up an array of information about the parsed arguments.
3461 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3462 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00003463
Chris Lattner6c940e62008-04-06 06:34:08 +00003464 // If there was no identifier specified for the declarator, either we are in
3465 // an abstract-declarator, or we are in a parameter declarator which was found
3466 // to be abstract. In abstract-declarators, identifier lists are not valid:
3467 // diagnose this.
3468 if (!D.getIdentifier())
Chris Lattner9453ab82010-05-14 17:23:36 +00003469 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner6c940e62008-04-06 06:34:08 +00003470
Chris Lattner9453ab82010-05-14 17:23:36 +00003471 // The first identifier was already read, and is known to be the first
3472 // identifier in the list. Remember this identifier in ParamInfo.
3473 ParamsSoFar.insert(FirstIdent);
John McCall48871652010-08-21 09:40:31 +00003474 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump11289f42009-09-09 15:08:12 +00003475
Chris Lattner6c940e62008-04-06 06:34:08 +00003476 while (Tok.is(tok::comma)) {
3477 // Eat the comma.
3478 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003479
Chris Lattner9186f552008-04-06 06:39:19 +00003480 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00003481 if (Tok.isNot(tok::identifier)) {
3482 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00003483 SkipUntil(tok::r_paren);
3484 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00003485 }
Chris Lattner67b450c2008-04-06 06:47:48 +00003486
Chris Lattner6c940e62008-04-06 06:34:08 +00003487 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00003488
3489 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003490 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerebad6a22008-11-19 07:37:42 +00003491 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00003492
Chris Lattner6c940e62008-04-06 06:34:08 +00003493 // Verify that the argument identifier has not already been mentioned.
3494 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003495 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003496 } else {
3497 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003498 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003499 Tok.getLocation(),
John McCall48871652010-08-21 09:40:31 +00003500 0));
Chris Lattner9186f552008-04-06 06:39:19 +00003501 }
Mike Stump11289f42009-09-09 15:08:12 +00003502
Chris Lattner6c940e62008-04-06 06:34:08 +00003503 // Eat the identifier.
3504 ConsumeToken();
3505 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003506
3507 // If we have the closing ')', eat it and we're done.
3508 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3509
Chris Lattner9186f552008-04-06 06:39:19 +00003510 // Remember that we parsed a function type, and remember the attributes. This
3511 // function type is always a K&R style function type, which is not varargs and
3512 // has no prototype.
John McCall53fa7142010-12-24 02:08:15 +00003513 D.AddTypeInfo(DeclaratorChunk::getFunction(ParsedAttributes(),
3514 /*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003515 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003516 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003517 /*TypeQuals*/0,
Douglas Gregor54992352011-01-26 03:43:54 +00003518 true, SourceLocation(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003519 /*exception*/false,
3520 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003521 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003522 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003523}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003524
Chris Lattnere8074e62006-08-06 18:30:15 +00003525/// [C90] direct-declarator '[' constant-expression[opt] ']'
3526/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3527/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3528/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3529/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3530void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003531 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003532
Chris Lattner84a11622008-12-18 07:27:21 +00003533 // C array syntax has many features, but by-far the most common is [] and [4].
3534 // This code does a fast path to handle some of the most obvious cases.
3535 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003536 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall53fa7142010-12-24 02:08:15 +00003537 ParsedAttributes attrs;
3538 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003539
Chris Lattner84a11622008-12-18 07:27:21 +00003540 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00003541 ExprResult NumElements;
John McCall53fa7142010-12-24 02:08:15 +00003542 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, false, 0,
Douglas Gregor04318252009-07-06 15:59:29 +00003543 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003544 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003545 return;
3546 } else if (Tok.getKind() == tok::numeric_constant &&
3547 GetLookAheadToken(1).is(tok::r_square)) {
3548 // [4] is very common. Parse the numeric constant expression.
John McCalldadc5752010-08-24 06:29:42 +00003549 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003550 ConsumeToken();
3551
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003552 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall53fa7142010-12-24 02:08:15 +00003553 ParsedAttributes attrs;
3554 MaybeParseCXX0XAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00003555
Chris Lattner84a11622008-12-18 07:27:21 +00003556 // Remember that we parsed a array type, and remember its features.
John McCall53fa7142010-12-24 02:08:15 +00003557 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, 0,
3558 ExprRes.release(),
Douglas Gregor04318252009-07-06 15:59:29 +00003559 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003560 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003561 return;
3562 }
Mike Stump11289f42009-09-09 15:08:12 +00003563
Chris Lattnere8074e62006-08-06 18:30:15 +00003564 // If valid, this location is the position where we read the 'static' keyword.
3565 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003566 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003567 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003568
Chris Lattnere8074e62006-08-06 18:30:15 +00003569 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003570 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003571 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003572 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003573
Chris Lattnere8074e62006-08-06 18:30:15 +00003574 // If we haven't already read 'static', check to see if there is one after the
3575 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003576 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003577 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003578
Chris Lattnere8074e62006-08-06 18:30:15 +00003579 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003580 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00003581 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00003582
Chris Lattner521ff2b2008-04-06 05:26:30 +00003583 // Handle the case where we have '[*]' as the array size. However, a leading
3584 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3585 // the the token after the star is a ']'. Since stars in arrays are
3586 // infrequent, use of lookahead is not costly here.
3587 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003588 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003589
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003590 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003591 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003592 StaticLoc = SourceLocation(); // Drop the static.
3593 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003594 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003595 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003596 // Note, in C89, this production uses the constant-expr production instead
3597 // of assignment-expr. The only difference is that assignment-expr allows
3598 // things like '=' and '*='. Sema rejects these in C89 mode because they
3599 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003600
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003601 // Parse the constant-expression or assignment-expression now (depending
3602 // on dialect).
3603 if (getLang().CPlusPlus)
3604 NumElements = ParseConstantExpression();
3605 else
3606 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003607 }
Mike Stump11289f42009-09-09 15:08:12 +00003608
Chris Lattner62591722006-08-12 18:40:58 +00003609 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003610 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003611 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003612 // If the expression was invalid, skip it.
3613 SkipUntil(tok::r_square);
3614 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003615 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003616
3617 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3618
John McCall53fa7142010-12-24 02:08:15 +00003619 ParsedAttributes attrs;
3620 MaybeParseCXX0XAttributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003621
Chris Lattner84a11622008-12-18 07:27:21 +00003622 // Remember that we parsed a array type, and remember its features.
John McCall53fa7142010-12-24 02:08:15 +00003623 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), attrs,
Chris Lattnercbc426d2006-12-02 06:43:02 +00003624 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003625 NumElements.release(),
3626 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003627 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003628}
3629
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003630/// [GNU] typeof-specifier:
3631/// typeof ( expressions )
3632/// typeof ( type-name )
3633/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003634///
3635void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003636 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003637 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003638 SourceLocation StartLoc = ConsumeToken();
3639
John McCalle8595032010-01-13 20:03:27 +00003640 const bool hasParens = Tok.is(tok::l_paren);
3641
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003642 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00003643 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003644 SourceRange CastRange;
John McCalldadc5752010-08-24 06:29:42 +00003645 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall6caebb12010-08-25 02:45:51 +00003646 isCastExpr,
3647 CastTy,
3648 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003649 if (hasParens)
3650 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003651
3652 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003653 // FIXME: Not accurate, the range gets one token more than it should.
3654 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003655 else
3656 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003657
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003658 if (isCastExpr) {
3659 if (!CastTy) {
3660 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003661 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003662 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003663
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003664 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003665 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003666 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3667 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003668 DiagID, CastTy))
3669 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003670 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003671 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003672
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003673 // If we get here, the operand to the typeof was an expresion.
3674 if (Operand.isInvalid()) {
3675 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003676 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003677 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003678
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003679 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003680 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003681 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3682 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00003683 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00003684 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003685}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003686
3687
3688/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3689/// from TryAltiVecVectorToken.
3690bool Parser::TryAltiVecVectorTokenOutOfLine() {
3691 Token Next = NextToken();
3692 switch (Next.getKind()) {
3693 default: return false;
3694 case tok::kw_short:
3695 case tok::kw_long:
3696 case tok::kw_signed:
3697 case tok::kw_unsigned:
3698 case tok::kw_void:
3699 case tok::kw_char:
3700 case tok::kw_int:
3701 case tok::kw_float:
3702 case tok::kw_double:
3703 case tok::kw_bool:
3704 case tok::kw___pixel:
3705 Tok.setKind(tok::kw___vector);
3706 return true;
3707 case tok::identifier:
3708 if (Next.getIdentifierInfo() == Ident_pixel) {
3709 Tok.setKind(tok::kw___vector);
3710 return true;
3711 }
3712 return false;
3713 }
3714}
3715
3716bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3717 const char *&PrevSpec, unsigned &DiagID,
3718 bool &isInvalid) {
3719 if (Tok.getIdentifierInfo() == Ident_vector) {
3720 Token Next = NextToken();
3721 switch (Next.getKind()) {
3722 case tok::kw_short:
3723 case tok::kw_long:
3724 case tok::kw_signed:
3725 case tok::kw_unsigned:
3726 case tok::kw_void:
3727 case tok::kw_char:
3728 case tok::kw_int:
3729 case tok::kw_float:
3730 case tok::kw_double:
3731 case tok::kw_bool:
3732 case tok::kw___pixel:
3733 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3734 return true;
3735 case tok::identifier:
3736 if (Next.getIdentifierInfo() == Ident_pixel) {
3737 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3738 return true;
3739 }
3740 break;
3741 default:
3742 break;
3743 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00003744 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00003745 DS.isTypeAltiVecVector()) {
3746 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3747 return true;
3748 }
3749 return false;
3750}
3751