blob: 42083803b6912fbda3816c2280f59fd9d9fdb40c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
John McCall19510852010-08-20 18:27:03 +000016#include "clang/Sema/Scope.h"
17#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000019#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallSet.h"
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// C99 6.7: Declarations.
25//===----------------------------------------------------------------------===//
26
27/// ParseTypeName
28/// type-name: [C99 6.7.6]
29/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000030///
31/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000032TypeResult Parser::ParseTypeName(SourceRange *Range,
33 Declarator::TheContext Context) {
Reid Spencer5f016e22007-07-11 17:01:13 +000034 // Parse the common declaration-specifiers piece.
35 DeclSpec DS;
36 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000037
Reid Spencer5f016e22007-07-11 17:01:13 +000038 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000039 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000040 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000041 if (Range)
42 *Range = DeclaratorInfo.getSourceRange();
43
Chris Lattnereaaebc72009-04-25 08:06:05 +000044 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000045 return true;
46
Douglas Gregor23c94db2010-07-02 17:43:08 +000047 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000048}
49
Sean Huntbbd37c62009-11-21 08:43:09 +000050/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +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
65/// attrib-name
66/// attrib-name '(' identifier ')'
67/// attrib-name '(' identifier ',' nonempty-expr-list ')'
68/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
69///
70/// [GNU] attrib-name:
71/// identifier
72/// typespec
73/// typequal
74/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000075///
Reid Spencer5f016e22007-07-11 17:01:13 +000076/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-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
Reid Spencer5f016e22007-07-11 17:01:13 +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.
85
John McCall7f040a92010-12-24 02:08:15 +000086void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
87 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +000088 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner04d66662007-10-09 17:33:22 +000090 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 McCall7f040a92010-12-24 02:08:15 +000095 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000096 }
97 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
98 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +000099 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 }
101 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000102 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
103 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000104
105 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +0000113
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000114 // check if we have a "parameterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000115 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattner04d66662007-10-09 17:33:22 +0000118 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
120 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000121
122 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 // __attribute__(( mode(byte) ))
124 ConsumeParen(); // ignore the right paren loc for now
John McCall7f040a92010-12-24 02:08:15 +0000125 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
126 ParmName, ParmLoc, 0, 0));
Chris Lattner04d66662007-10-09 17:33:22 +0000127 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 ConsumeToken();
129 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000130 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 // now parse the non-empty comma separated list of expressions
134 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000135 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000136 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 ArgExprsOk = false;
138 SkipUntil(tok::r_paren);
139 break;
140 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000141 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 }
Chris Lattner04d66662007-10-09 17:33:22 +0000143 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 break;
145 ConsumeToken(); // Eat the comma, move to the next argument
146 }
Chris Lattner04d66662007-10-09 17:33:22 +0000147 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 ConsumeParen(); // ignore the right paren loc for now
John McCall7f040a92010-12-24 02:08:15 +0000149 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0,
150 AttrNameLoc, ParmName, ParmLoc,
151 ArgExprs.take(), ArgExprs.size()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
153 }
154 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000155 switch (Tok.getKind()) {
156 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // __attribute__(( nonnull() ))
159 ConsumeParen(); // ignore the right paren loc for now
John McCall7f040a92010-12-24 02:08:15 +0000160 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
161 0, SourceLocation(), 0, 0));
Nate Begeman6f3d8382009-06-26 06:32:41 +0000162 break;
163 case tok::kw_char:
164 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000165 case tok::kw_char16_t:
166 case tok::kw_char32_t:
Nate Begeman6f3d8382009-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 McCall7f040a92010-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 Jahanian1b72fa72010-08-17 23:19:16 +0000182 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-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 Begeman6f3d8382009-06-26 06:32:41 +0000186 if (Tok.is(tok::r_paren))
187 ConsumeParen();
188 break;
John McCall7f040a92010-12-24 02:08:15 +0000189 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000190 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000192 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 // now parse the list of expressions
196 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000197 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000198 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 ArgExprsOk = false;
200 SkipUntil(tok::r_paren);
201 break;
202 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000203 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 }
Chris Lattner04d66662007-10-09 17:33:22 +0000205 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 break;
207 ConsumeToken(); // Eat the comma, move to the next argument
208 }
209 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000210 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 ConsumeParen(); // ignore the right paren loc for now
John McCall7f040a92010-12-24 02:08:15 +0000212 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0,
213 AttrNameLoc, 0, SourceLocation(),
214 ArgExprs.take(), ArgExprs.size()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000216 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 }
218 }
219 } else {
John McCall7f040a92010-12-24 02:08:15 +0000220 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
221 0, SourceLocation(), 0, 0));
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 }
223 }
224 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000226 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000227 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
228 SkipUntil(tok::r_paren, false);
229 }
John McCall7f040a92010-12-24 02:08:15 +0000230 if (endLoc)
231 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000233}
234
Eli Friedmana23b4852009-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 McCall7f040a92010-12-24 02:08:15 +0000244void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000245 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000246
Steve Narofff59e17e2008-12-24 20:59:21 +0000247 ConsumeToken();
Eli Friedmana23b4852009-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 McCall7f040a92010-12-24 02:08:15 +0000251 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000252 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000253 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-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 McCall60d7b3a2010-08-24 06:29:42 +0000260 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000261 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000262 Expr *ExprList = ArgExpr.take();
John McCall7f040a92010-12-24 02:08:15 +0000263 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
264 SourceLocation(), &ExprList, 1, true));
Eli Friedmana23b4852009-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 McCall7f040a92010-12-24 02:08:15 +0000269 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
270 0, SourceLocation(), 0, 0, true));
Eli Friedmana23b4852009-06-08 07:21:15 +0000271 }
272 }
273 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
274 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000275 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000276}
277
John McCall7f040a92010-12-24 02:08:15 +0000278void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-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 Gregorf813a2c2010-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 Friedman290eeb02009-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 McCall7f040a92010-12-24 02:08:15 +0000289 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
290 SourceLocation(), 0, 0, true));
Eli Friedman290eeb02009-06-08 23:27:34 +0000291 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000292}
293
John McCall7f040a92010-12-24 02:08:15 +0000294void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-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 McCall7f040a92010-12-24 02:08:15 +0000299 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
300 SourceLocation(), 0, 0, true));
Dawn Perchik52fc3142010-09-03 01:29:35 +0000301 }
John McCall7f040a92010-12-24 02:08:15 +0000302}
303
Peter Collingbournef315fa82011-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 McCall7f040a92010-12-24 02:08:15 +0000314void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
315 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
316 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000317}
318
Reid Spencer5f016e22007-07-11 17:01:13 +0000319/// ParseDeclaration - Parse a full 'declaration', which consists of
320/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000321/// 'Context' should be a Declarator::TheContext value. This returns the
322/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000323///
324/// declaration: [C99 6.7]
325/// block-declaration ->
326/// simple-declaration
327/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000328/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000329/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000330/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000331/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000332/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000333/// others... [FIXME]
334///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000335Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
336 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000337 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000338 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000339 ParenBraceBracketBalancer BalancerRAIIObj(*this);
340
John McCalld226f652010-08-21 09:40:31 +0000341 Decl *SingleDecl = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000342 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000343 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000344 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000345 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000346 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000347 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000348 case tok::kw_inline:
Sebastian Redl88e64ca2010-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 McCall7f040a92010-12-24 02:08:15 +0000351 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000352 SourceLocation InlineLoc = ConsumeToken();
353 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
354 break;
355 }
John McCall7f040a92010-12-24 02:08:15 +0000356 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000357 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000358 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000359 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000360 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000361 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000362 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000363 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall7f040a92010-12-24 02:08:15 +0000364 DeclEnd, attrs);
Chris Lattner682bf922009-03-29 16:50:03 +0000365 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000366 case tok::kw_static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000367 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000368 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000369 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000370 default:
John McCall7f040a92010-12-24 02:08:15 +0000371 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000372 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000373
Chris Lattner682bf922009-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 Lattner8f08cb72007-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 Lattnercd147752009-03-29 17:27:48 +0000383///
384/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000385/// declaration. If it is true, it checks for and eats it.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000386Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
387 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000388 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000389 ParsedAttributes &attrs,
Chris Lattner5c5db552010-04-05 18:18:31 +0000390 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000392 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000393 DS.takeAttributesFrom(attrs);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000394 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +0000395 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000396 StmtResult R = Actions.ActOnVlaStmt(DS);
397 if (R.isUsable())
398 Stmts.push_back(R.release());
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
401 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000402 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000403 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000404 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallaec03712010-05-21 20:45:30 +0000405 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000406 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000407 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattner5c5db552010-04-05 18:18:31 +0000410 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000411}
Mike Stump1eb44332009-09-09 15:08:12 +0000412
John McCalld8ac0572009-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 McCall54abf7d2009-11-04 02:18:39 +0000416Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
417 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000418 bool AllowFunctionDefinitions,
419 SourceLocation *DeclEnd) {
420 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000421 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000422 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000423
John McCalld8ac0572009-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 Lattner23c4b182009-03-29 17:18:04 +0000431 }
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Chris Lattnerc82daef2010-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 Lattner004659a2010-07-11 22:42:07 +0000440 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-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 McCalld226f652010-08-21 09:40:31 +0000448 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000449 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-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 McCalld8ac0572009-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 McCalld226f652010-08-21 09:40:31 +0000465 llvm::SmallVector<Decl *, 8> DeclsInGroup;
466 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000467 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000468 if (FirstDecl)
John McCalld8ac0572009-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 Lattner23c4b182009-03-29 17:18:04 +0000475 ConsumeToken();
John McCalld8ac0572009-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 McCall7f040a92010-12-24 02:08:15 +0000487 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +0000488
489 ParseDeclarator(D);
490
John McCalld226f652010-08-21 09:40:31 +0000491 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000492 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000493 if (ThisDecl)
John McCalld8ac0572009-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 Lattner004659a2010-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 McCalld8ac0572009-11-03 19:26:08 +0000513 }
514
Douglas Gregor23c94db2010-07-02 17:43:08 +0000515 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000516 DeclsInGroup.data(),
517 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000518}
519
Douglas Gregor1426e532009-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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000524///
Reid Spencer5f016e22007-07-11 17:01:13 +0000525/// init-declarator: [C99 6.7]
526/// declarator
527/// declarator '=' initializer
528/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
529/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000530/// [C++] declarator initializer[opt]
531///
532/// [C++] initializer:
533/// [C++] '=' initializer-clause
534/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000540///
John McCalld226f652010-08-21 09:40:31 +0000541Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000542 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-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 McCall60d7b3a2010-08-24 06:29:42 +0000546 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor1426e532009-05-12 21:31:51 +0000547 if (AsmLabel.isInvalid()) {
548 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000549 return 0;
Douglas Gregor1426e532009-05-12 21:31:51 +0000550 }
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Douglas Gregor1426e532009-05-12 21:31:51 +0000552 D.setAsmLabel(AsmLabel.release());
553 D.SetRangeEnd(Loc);
554 }
Mike Stump1eb44332009-09-09 15:08:12 +0000555
John McCall7f040a92010-12-24 02:08:15 +0000556 MaybeParseGNUAttributes(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Douglas Gregor1426e532009-05-12 21:31:51 +0000558 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000559 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000560 switch (TemplateInfo.Kind) {
561 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000562 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000563 break;
564
565 case ParsedTemplateInfo::Template:
566 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000567 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000568 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000569 TemplateInfo.TemplateParams->data(),
570 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000571 D);
572 break;
573
574 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000575 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000576 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000577 TemplateInfo.ExternLoc,
578 TemplateInfo.TemplateLoc,
579 D);
580 if (ThisRes.isInvalid()) {
581 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000582 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000583 }
584
585 ThisDecl = ThisRes.get();
586 break;
587 }
588 }
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Richard Smith34b41d92011-02-20 03:19:35 +0000590 bool TypeContainsAuto =
591 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
592
Douglas Gregor1426e532009-05-12 21:31:51 +0000593 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000594 if (isTokenEqualOrMistypedEqualEqual(
595 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000596 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000597 if (Tok.is(tok::kw_delete)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000598 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000599
600 if (!getLang().CPlusPlus0x)
601 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
602
Douglas Gregor1426e532009-05-12 21:31:51 +0000603 Actions.SetDeclDeleted(ThisDecl, DelLoc);
604 } else {
John McCall731ad842009-12-19 09:28:58 +0000605 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
606 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000607 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000608 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000609
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000610 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000611 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000612 ConsumeCodeCompletionToken();
613 SkipUntil(tok::comma, true, true);
614 return ThisDecl;
615 }
616
John McCall60d7b3a2010-08-24 06:29:42 +0000617 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000618
John McCall731ad842009-12-19 09:28:58 +0000619 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000620 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000621 ExitScope();
622 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000623
Douglas Gregor1426e532009-05-12 21:31:51 +0000624 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000625 SkipUntil(tok::comma, true, true);
626 Actions.ActOnInitializerError(ThisDecl);
627 } else
Richard Smith34b41d92011-02-20 03:19:35 +0000628 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
629 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-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 Gregorb4debae2009-12-22 17:47:17 +0000637 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
638 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000639 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000640 }
641
Douglas Gregor1426e532009-05-12 21:31:51 +0000642 if (ParseExpressionList(Exprs, CommaLocs)) {
643 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000644
645 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000646 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000647 ExitScope();
648 }
Douglas Gregor1426e532009-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 Gregorb4debae2009-12-22 17:47:17 +0000655
656 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000657 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000658 ExitScope();
659 }
660
Douglas Gregor1426e532009-05-12 21:31:51 +0000661 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
662 move_arg(Exprs),
Richard Smith34b41d92011-02-20 03:19:35 +0000663 RParenLoc,
664 TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000665 }
666 } else {
Richard Smith34b41d92011-02-20 03:19:35 +0000667 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000668 }
669
Richard Smith483b9f32011-02-21 20:05:19 +0000670 Actions.FinalizeDeclaration(ThisDecl);
671
Douglas Gregor1426e532009-05-12 21:31:51 +0000672 return ThisDecl;
673}
674
Reid Spencer5f016e22007-07-11 17:01:13 +0000675/// ParseSpecifierQualifierList
676/// specifier-qualifier-list:
677/// type-specifier specifier-qualifier-list[opt]
678/// type-qualifier specifier-qualifier-list[opt]
679/// [GNU] attributes specifier-qualifier-list[opt]
680///
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.
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 // Validate declspec for type-name.
687 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000688 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +0000689 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 // Issue diagnostic and remove storage class if present.
693 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
694 if (DS.getStorageClassSpecLoc().isValid())
695 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
696 else
697 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
698 DS.ClearStorageClassSpecs();
699 }
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 // Issue diagnostic and remove function specfier if present.
702 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-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);
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 DS.ClearFunctionSpecs();
710 }
711}
712
Chris Lattnerc199ab32009-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 Lattnerb6645dd2009-04-14 21:16:09 +0000724/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000725/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-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 Lattnerb6645dd2009-04-14 21:16:09 +0000734 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000735}
736
Chris Lattnere40c2952009-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 Lattnerf4382f52009-04-14 22:17:06 +0000747bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000748 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000749 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000750 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Chris Lattnere40c2952009-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 Stump1eb44332009-09-09 15:08:12 +0000766
Chris Lattnere40c2952009-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 Stump1eb44332009-09-09 15:08:12 +0000775
Chris Lattnere40c2952009-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 Lattnerf4382f52009-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 Stump1eb44332009-09-09 15:08:12 +0000785
Douglas Gregor23c94db2010-07-02 17:43:08 +0000786 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-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 Stump1eb44332009-09-09 15:08:12 +0000793
Chris Lattnerf4382f52009-04-14 22:17:06 +0000794 if (TagName) {
795 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000796 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000797 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Chris Lattnerf4382f52009-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 Gregor9b9edd62010-03-02 17:53:14 +0000801 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000802 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000803 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000804 return true;
805 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000806 }
Mike Stump1eb44332009-09-09 15:08:12 +0000807
Douglas Gregora786fdb2009-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 McCallb3d87482010-08-24 05:47:05 +0000810 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +0000811 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +0000812 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-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 McCallb3d87482010-08-24 05:47:05 +0000820 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-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 Stump1eb44332009-09-09 15:08:12 +0000835
Douglas Gregora786fdb2009-10-13 23:27:22 +0000836 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000837 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000838 unsigned DiagID;
839 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000840 DS.SetRangeEnd(Tok.getLocation());
841 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Chris Lattnere40c2952009-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 Gregor0efc2c12010-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
Reid Spencer5f016e22007-07-11 17:01:13 +0000863/// ParseDeclarationSpecifiers
864/// declaration-specifiers: [C99 6.7]
865/// storage-class-specifier declaration-specifiers[opt]
866/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000867/// [C99] function-specifier declaration-specifiers[opt]
868/// [GNU] attributes declaration-specifiers[opt]
869///
870/// storage-class-specifier: [C99 6.7.1]
871/// 'typedef'
872/// 'extern'
873/// 'static'
874/// 'auto'
875/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000876/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000877/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000878/// function-specifier: [C99 6.7.4]
879/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000880/// [C++] 'virtual'
881/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +0000882/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000883/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000884/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000885
Reid Spencer5f016e22007-07-11 17:01:13 +0000886///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000887void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000888 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000889 AccessSpecifier AS,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000890 DeclSpecContext DSContext) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000891 DS.SetRangeStart(Tok.getLocation());
Chris Lattner729ad832010-11-09 20:14:26 +0000892 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000894 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000896 unsigned DiagID = 0;
897
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000899
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000901 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000902 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 // If this is not a declaration specifier token, we're done reading decl
904 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000905 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000908 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +0000909 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-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 Gregorc7b6d882010-09-16 15:14:18 +0000921 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
922 AllowNonIdentifiers,
923 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000924 ConsumeCodeCompletionToken();
925 return;
926 }
927
Douglas Gregor68e3c2e2011-02-15 20:33:25 +0000928 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
929 CCC = Sema::PCC_LocalDeclarationSpecifiers;
930 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +0000931 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
932 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000933 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +0000934 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000935 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +0000936 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000937
938 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
939 ConsumeCodeCompletionToken();
940 return;
941 }
942
Chris Lattner5e02c472009-01-05 00:07:25 +0000943 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-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 McCall2e0a7152010-03-01 18:20:46 +0000950 if (Tok.is(tok::coloncolon)) // ::new or ::delete
951 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000952 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000953
954 case tok::annot_cxxscope: {
955 if (DS.hasTypeSpecifier())
956 goto DoneWithDeclSpec;
957
John McCallaa87d332009-12-12 11:40:51 +0000958 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +0000959 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
960 Tok.getAnnotationRange(),
961 SS);
John McCallaa87d332009-12-12 11:40:51 +0000962
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000963 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000964 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000965 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000966 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000967 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000968 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000969
970 // C++ [class.qual]p2:
971 // In a lookup in which the constructor is an acceptable lookup
972 // result and the nested-name-specifier nominates a class C:
973 //
974 // - if the name specified after the
975 // nested-name-specifier, when looked up in C, is the
976 // injected-class-name of C (Clause 9), or
977 //
978 // - if the name specified after the nested-name-specifier
979 // is the same as the identifier or the
980 // simple-template-id's template-name in the last
981 // component of the nested-name-specifier,
982 //
983 // the name is instead considered to name the constructor of
984 // class C.
985 //
986 // Thus, if the template-name is actually the constructor
987 // name, then the code is ill-formed; this interpretation is
988 // reinforced by the NAD status of core issue 635.
989 TemplateIdAnnotation *TemplateId
990 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000991 if ((DSContext == DSC_top_level ||
992 (DSContext == DSC_class && DS.isFriendSpecified())) &&
993 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000994 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000995 if (isConstructorDeclarator()) {
996 // The user meant this to be an out-of-line constructor
997 // definition, but template arguments are not allowed
998 // there. Just allow this as a constructor; we'll
999 // complain about it later.
1000 goto DoneWithDeclSpec;
1001 }
1002
1003 // The user meant this to name a type, but it actually names
1004 // a constructor with some extraneous template
1005 // arguments. Complain, then parse it as a type as the user
1006 // intended.
1007 Diag(TemplateId->TemplateNameLoc,
1008 diag::err_out_of_line_template_id_names_constructor)
1009 << TemplateId->Name;
1010 }
1011
John McCallaa87d332009-12-12 11:40:51 +00001012 DS.getTypeSpecScope() = SS;
1013 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001014 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001015 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00001016 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00001017 continue;
1018 }
1019
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001020 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001021 DS.getTypeSpecScope() = SS;
1022 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001023 if (Tok.getAnnotationValue()) {
1024 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001025 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1026 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001027 PrevSpec, DiagID, T);
1028 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001029 else
1030 DS.SetTypeSpecError();
1031 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1032 ConsumeToken(); // The typename
1033 }
1034
Douglas Gregor9135c722009-03-25 15:40:00 +00001035 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001036 goto DoneWithDeclSpec;
1037
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001038 // If we're in a context where the identifier could be a class name,
1039 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001040 if ((DSContext == DSC_top_level ||
1041 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001042 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001043 &SS)) {
1044 if (isConstructorDeclarator())
1045 goto DoneWithDeclSpec;
1046
1047 // As noted in C++ [class.qual]p2 (cited above), when the name
1048 // of the class is qualified in a context where it could name
1049 // a constructor, its a constructor name. However, we've
1050 // looked at the declarator, and the user probably meant this
1051 // to be a type. Complain that it isn't supposed to be treated
1052 // as a type, then proceed to parse it as a type.
1053 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1054 << Next.getIdentifierInfo();
1055 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001056
John McCallb3d87482010-08-24 05:47:05 +00001057 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1058 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00001059 getCurScope(), &SS,
1060 false, false, ParsedType(),
1061 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001062
Chris Lattnerf4382f52009-04-14 22:17:06 +00001063 // If the referenced identifier is not a type, then this declspec is
1064 // erroneous: We already checked about that it has no type specifier, and
1065 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001066 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001067 if (TypeRep == 0) {
1068 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001069 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001070 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001071 }
Mike Stump1eb44332009-09-09 15:08:12 +00001072
John McCallaa87d332009-12-12 11:40:51 +00001073 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001074 ConsumeToken(); // The C++ scope.
1075
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001076 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001077 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001078 if (isInvalid)
1079 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001081 DS.SetRangeEnd(Tok.getLocation());
1082 ConsumeToken(); // The typename.
1083
1084 continue;
1085 }
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Chris Lattner80d0c892009-01-21 19:48:37 +00001087 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001088 if (Tok.getAnnotationValue()) {
1089 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001090 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001091 DiagID, T);
1092 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001093 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001094
1095 if (isInvalid)
1096 break;
1097
Chris Lattner80d0c892009-01-21 19:48:37 +00001098 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1099 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001100
Chris Lattner80d0c892009-01-21 19:48:37 +00001101 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1102 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001103 // Objective-C interface.
1104 if (Tok.is(tok::less) && getLang().ObjC1)
1105 ParseObjCProtocolQualifiers(DS);
1106
Chris Lattner80d0c892009-01-21 19:48:37 +00001107 continue;
1108 }
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Chris Lattner3bd934a2008-07-26 01:18:38 +00001110 // typedef-name
1111 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001112 // In C++, check to see if this is a scope specifier like foo::bar::, if
1113 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001114 if (getLang().CPlusPlus) {
1115 if (TryAnnotateCXXScopeToken(true)) {
1116 if (!DS.hasTypeSpecifier())
1117 DS.SetTypeSpecError();
1118 goto DoneWithDeclSpec;
1119 }
1120 if (!Tok.is(tok::identifier))
1121 continue;
1122 }
Mike Stump1eb44332009-09-09 15:08:12 +00001123
Chris Lattner3bd934a2008-07-26 01:18:38 +00001124 // This identifier can only be a typedef name if we haven't already seen
1125 // a type-specifier. Without this check we misparse:
1126 // typedef int X; struct Y { short X; }; as 'short int'.
1127 if (DS.hasTypeSpecifier())
1128 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001129
John Thompson82287d12010-02-05 00:12:22 +00001130 // Check for need to substitute AltiVec keyword tokens.
1131 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1132 break;
1133
Chris Lattner3bd934a2008-07-26 01:18:38 +00001134 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001135 ParsedType TypeRep =
1136 Actions.getTypeName(*Tok.getIdentifierInfo(),
1137 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001138
Chris Lattnerc199ab32009-04-12 20:42:31 +00001139 // If this is not a typedef name, don't parse it as part of the declspec,
1140 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001141 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001142 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001143 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001144 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001145
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001146 // If we're in a context where the identifier could be a class name,
1147 // check whether this is a constructor declaration.
1148 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001149 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001150 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001151 goto DoneWithDeclSpec;
1152
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001153 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001154 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001155 if (isInvalid)
1156 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Chris Lattner3bd934a2008-07-26 01:18:38 +00001158 DS.SetRangeEnd(Tok.getLocation());
1159 ConsumeToken(); // The identifier
1160
1161 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1162 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001163 // Objective-C interface.
1164 if (Tok.is(tok::less) && getLang().ObjC1)
1165 ParseObjCProtocolQualifiers(DS);
1166
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001167 // Need to support trailing type qualifiers (e.g. "id<p> const").
1168 // If a type specifier follows, it will be diagnosed elsewhere.
1169 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001170 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001171
1172 // type-name
1173 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001174 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001175 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001176 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001177 // This template-id does not refer to a type name, so we're
1178 // done with the type-specifiers.
1179 goto DoneWithDeclSpec;
1180 }
1181
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001182 // If we're in a context where the template-id could be a
1183 // constructor name or specialization, check whether this is a
1184 // constructor declaration.
1185 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001186 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001187 isConstructorDeclarator())
1188 goto DoneWithDeclSpec;
1189
Douglas Gregor39a8de12009-02-25 19:37:18 +00001190 // Turn the template-id annotation token into a type annotation
1191 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001192 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001193 continue;
1194 }
1195
Reid Spencer5f016e22007-07-11 17:01:13 +00001196 // GNU attributes support.
1197 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001198 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001200
1201 // Microsoft declspec support.
1202 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001203 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001204 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Steve Naroff239f0732008-12-25 14:16:32 +00001206 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001207 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001208 // FIXME: Add handling here!
1209 break;
1210
1211 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001212 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001213 case tok::kw___cdecl:
1214 case tok::kw___stdcall:
1215 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001216 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001217 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001218 continue;
1219
Dawn Perchik52fc3142010-09-03 01:29:35 +00001220 // Borland single token adornments.
1221 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001222 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001223 continue;
1224
Peter Collingbournef315fa82011-02-14 01:42:53 +00001225 // OpenCL single token adornments.
1226 case tok::kw___kernel:
1227 ParseOpenCLAttributes(DS.getAttributes());
1228 continue;
1229
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 // storage-class-specifier
1231 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001232 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001233 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 break;
1235 case tok::kw_extern:
1236 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001237 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001238 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001239 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001241 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001242 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001243 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001244 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 case tok::kw_static:
1246 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001247 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001248 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001249 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 break;
1251 case tok::kw_auto:
Douglas Gregor18d8b792011-03-14 21:43:30 +00001252 if (getLang().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001253 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
1254 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1255 DiagID, getLang());
1256 if (!isInvalid)
1257 Diag(Tok, diag::auto_storage_class)
1258 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
1259 }
1260 else
1261 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1262 DiagID);
1263 }
Anders Carlssone89d1592009-06-26 18:41:36 +00001264 else
John McCallfec54012009-08-03 20:12:06 +00001265 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001266 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 break;
1268 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001269 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001270 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001272 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001273 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001274 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001275 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001277 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 // function-specifier
1281 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001282 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001284 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001285 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001286 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001287 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001288 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001289 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001290
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001291 // friend
1292 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001293 if (DSContext == DSC_class)
1294 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1295 else {
1296 PrevSpec = ""; // not actually used by the diagnostic
1297 DiagID = diag::err_friend_invalid_in_context;
1298 isInvalid = true;
1299 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001300 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Sebastian Redl2ac67232009-11-05 15:47:02 +00001302 // constexpr
1303 case tok::kw_constexpr:
1304 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1305 break;
1306
Chris Lattner80d0c892009-01-21 19:48:37 +00001307 // type-specifier
1308 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001309 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1310 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001311 break;
1312 case tok::kw_long:
1313 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001314 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1315 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001316 else
John McCallfec54012009-08-03 20:12:06 +00001317 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1318 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001319 break;
1320 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001321 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1322 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001323 break;
1324 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001325 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1326 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001327 break;
1328 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001329 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1330 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001331 break;
1332 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001333 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1334 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001335 break;
1336 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001337 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1338 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001339 break;
1340 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1342 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001343 break;
1344 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001345 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1346 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001347 break;
1348 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001349 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1350 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001351 break;
1352 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001353 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1354 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001355 break;
1356 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001357 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1358 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001359 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001360 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001361 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1362 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001363 break;
1364 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001365 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1366 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001367 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001368 case tok::kw_bool:
1369 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001370 if (Tok.is(tok::kw_bool) &&
1371 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1372 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1373 PrevSpec = ""; // Not used by the diagnostic.
1374 DiagID = diag::err_bool_redeclaration;
1375 isInvalid = true;
1376 } else {
1377 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1378 DiagID);
1379 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001380 break;
1381 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001382 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1383 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001384 break;
1385 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001386 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1387 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001388 break;
1389 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001390 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1391 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001392 break;
John Thompson82287d12010-02-05 00:12:22 +00001393 case tok::kw___vector:
1394 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1395 break;
1396 case tok::kw___pixel:
1397 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1398 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001399
1400 // class-specifier:
1401 case tok::kw_class:
1402 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001403 case tok::kw_union: {
1404 tok::TokenKind Kind = Tok.getKind();
1405 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001406 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001407 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001408 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001409
1410 // enum-specifier:
1411 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001412 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001413 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001414 continue;
1415
1416 // cv-qualifier:
1417 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001418 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1419 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001420 break;
1421 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001422 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1423 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001424 break;
1425 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001426 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1427 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001428 break;
1429
Douglas Gregord57959a2009-03-27 23:10:48 +00001430 // C++ typename-specifier:
1431 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001432 if (TryAnnotateTypeOrScopeToken()) {
1433 DS.SetTypeSpecError();
1434 goto DoneWithDeclSpec;
1435 }
1436 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001437 continue;
1438 break;
1439
Chris Lattner80d0c892009-01-21 19:48:37 +00001440 // GNU typeof support.
1441 case tok::kw_typeof:
1442 ParseTypeofSpecifier(DS);
1443 continue;
1444
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001445 case tok::kw_decltype:
1446 ParseDecltypeSpecifier(DS);
1447 continue;
1448
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001449 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001450 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001451 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1452 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001453 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001454 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Douglas Gregor46f936e2010-11-19 17:10:50 +00001456 if (!ParseObjCProtocolQualifiers(DS))
1457 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1458 << FixItHint::CreateInsertion(Loc, "id")
1459 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001460
1461 // Need to support trailing type qualifiers (e.g. "id<p> const").
1462 // If a type specifier follows, it will be diagnosed elsewhere.
1463 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001464 }
John McCallfec54012009-08-03 20:12:06 +00001465 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001466 if (isInvalid) {
1467 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001468 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001469
1470 if (DiagID == diag::ext_duplicate_declspec)
1471 Diag(Tok, DiagID)
1472 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1473 else
1474 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001475 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00001476
Chris Lattner81c018d2008-03-13 06:29:04 +00001477 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001478 ConsumeToken();
1479 }
1480}
Douglas Gregoradcac882008-12-01 23:54:00 +00001481
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001482/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001483/// primarily follow the C++ grammar with additions for C99 and GNU,
1484/// which together subsume the C grammar. Note that the C++
1485/// type-specifier also includes the C type-qualifier (for const,
1486/// volatile, and C99 restrict). Returns true if a type-specifier was
1487/// found (and parsed), false otherwise.
1488///
1489/// type-specifier: [C++ 7.1.5]
1490/// simple-type-specifier
1491/// class-specifier
1492/// enum-specifier
1493/// elaborated-type-specifier [TODO]
1494/// cv-qualifier
1495///
1496/// cv-qualifier: [C++ 7.1.5.1]
1497/// 'const'
1498/// 'volatile'
1499/// [C99] 'restrict'
1500///
1501/// simple-type-specifier: [ C++ 7.1.5.2]
1502/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1503/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1504/// 'char'
1505/// 'wchar_t'
1506/// 'bool'
1507/// 'short'
1508/// 'int'
1509/// 'long'
1510/// 'signed'
1511/// 'unsigned'
1512/// 'float'
1513/// 'double'
1514/// 'void'
1515/// [C99] '_Bool'
1516/// [C99] '_Complex'
1517/// [C99] '_Imaginary' // Removed in TC2?
1518/// [GNU] '_Decimal32'
1519/// [GNU] '_Decimal64'
1520/// [GNU] '_Decimal128'
1521/// [GNU] typeof-specifier
1522/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1523/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001524/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001525/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001526bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001527 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001528 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001529 const ParsedTemplateInfo &TemplateInfo,
1530 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001531 SourceLocation Loc = Tok.getLocation();
1532
1533 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001534 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001535 // If we already have a type specifier, this identifier is not a type.
1536 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1537 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1538 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1539 return false;
John Thompson82287d12010-02-05 00:12:22 +00001540 // Check for need to substitute AltiVec keyword tokens.
1541 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1542 break;
1543 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001544 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-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 McCall9ba61662010-02-26 08:45:28 +00001548 return true;
1549 if (Tok.is(tok::identifier))
1550 return false;
1551 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1552 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001553 case tok::coloncolon: // ::foo::bar
1554 if (NextToken().is(tok::kw_new) || // ::new
1555 NextToken().is(tok::kw_delete)) // ::delete
1556 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Chris Lattner166a8fc2009-01-04 23:41:41 +00001558 // Annotate typenames and C++ scope specifiers. If we get one, just
1559 // recurse to handle whatever we get.
1560 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001561 return true;
1562 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1563 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001564
Douglas Gregor12e083c2008-11-07 15:42:26 +00001565 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001566 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001567 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00001568 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1569 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001570 DiagID, T);
1571 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001572 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001573 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1574 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Douglas Gregor12e083c2008-11-07 15:42:26 +00001576 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1577 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1578 // Objective-C interface. If we don't have Objective-C or a '<', this is
1579 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001580 if (Tok.is(tok::less) && getLang().ObjC1)
1581 ParseObjCProtocolQualifiers(DS);
1582
Douglas Gregor12e083c2008-11-07 15:42:26 +00001583 return true;
1584 }
1585
1586 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001587 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001588 break;
1589 case tok::kw_long:
1590 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001591 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1592 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001593 else
John McCallfec54012009-08-03 20:12:06 +00001594 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1595 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001596 break;
1597 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001598 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001599 break;
1600 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001601 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1602 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001603 break;
1604 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001605 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1606 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001607 break;
1608 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001609 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1610 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001611 break;
1612 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001613 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001614 break;
1615 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001616 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001617 break;
1618 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001619 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001620 break;
1621 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001622 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001623 break;
1624 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001625 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001626 break;
1627 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001628 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001629 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001630 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001631 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001632 break;
1633 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001634 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001635 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001636 case tok::kw_bool:
1637 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001638 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001639 break;
1640 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001641 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1642 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001643 break;
1644 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001645 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1646 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001647 break;
1648 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001649 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1650 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001651 break;
John Thompson82287d12010-02-05 00:12:22 +00001652 case tok::kw___vector:
1653 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1654 break;
1655 case tok::kw___pixel:
1656 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1657 break;
1658
Douglas Gregor12e083c2008-11-07 15:42:26 +00001659 // class-specifier:
1660 case tok::kw_class:
1661 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001662 case tok::kw_union: {
1663 tok::TokenKind Kind = Tok.getKind();
1664 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001665 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1666 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001667 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001668 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001669
1670 // enum-specifier:
1671 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001672 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001673 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001674 return true;
1675
1676 // cv-qualifier:
1677 case tok::kw_const:
1678 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001679 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001680 break;
1681 case tok::kw_volatile:
1682 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001683 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001684 break;
1685 case tok::kw_restrict:
1686 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001687 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001688 break;
1689
1690 // GNU typeof support.
1691 case tok::kw_typeof:
1692 ParseTypeofSpecifier(DS);
1693 return true;
1694
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001695 // C++0x decltype support.
1696 case tok::kw_decltype:
1697 ParseDecltypeSpecifier(DS);
1698 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001700 // C++0x auto support.
1701 case tok::kw_auto:
1702 if (!getLang().CPlusPlus0x)
1703 return false;
1704
John McCallfec54012009-08-03 20:12:06 +00001705 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001706 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001707
Eli Friedman290eeb02009-06-08 23:27:34 +00001708 case tok::kw___ptr64:
1709 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001710 case tok::kw___cdecl:
1711 case tok::kw___stdcall:
1712 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001713 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001714 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001715 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001716
Dawn Perchik52fc3142010-09-03 01:29:35 +00001717 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001718 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001719 return true;
1720
Douglas Gregor12e083c2008-11-07 15:42:26 +00001721 default:
1722 // Not a type-specifier; do nothing.
1723 return false;
1724 }
1725
1726 // If the specifier combination wasn't legal, issue a diagnostic.
1727 if (isInvalid) {
1728 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001729 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001730 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001731 }
1732 DS.SetRangeEnd(Tok.getLocation());
1733 ConsumeToken(); // whatever we parsed above.
1734 return true;
1735}
Reid Spencer5f016e22007-07-11 17:01:13 +00001736
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001737/// ParseStructDeclaration - Parse a struct declaration without the terminating
1738/// semicolon.
1739///
Reid Spencer5f016e22007-07-11 17:01:13 +00001740/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001741/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001742/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001743/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001744/// struct-declarator-list:
1745/// struct-declarator
1746/// struct-declarator-list ',' struct-declarator
1747/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1748/// struct-declarator:
1749/// declarator
1750/// [GNU] declarator attributes[opt]
1751/// declarator[opt] ':' constant-expression
1752/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1753///
Chris Lattnere1359422008-04-10 06:46:29 +00001754void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001755ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001756 if (Tok.is(tok::kw___extension__)) {
1757 // __extension__ silences extension warnings in the subexpression.
1758 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001759 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001760 return ParseStructDeclaration(DS, Fields);
1761 }
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Steve Naroff28a7ca82007-08-20 22:28:22 +00001763 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001764 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001765
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001766 // If there are no declarators, this is a free-standing declaration
1767 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001768 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001769 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001770 return;
1771 }
1772
1773 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001774 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001775 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001776 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001777 FieldDeclarator DeclaratorInfo(DS);
1778
1779 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00001780 if (!FirstDeclarator)
1781 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00001782
Steve Naroff28a7ca82007-08-20 22:28:22 +00001783 /// struct-declarator: declarator
1784 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001785 if (Tok.isNot(tok::colon)) {
1786 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1787 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001788 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001789 }
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Chris Lattner04d66662007-10-09 17:33:22 +00001791 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001792 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001793 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001794 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001795 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001796 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001797 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001798 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001799
Steve Naroff28a7ca82007-08-20 22:28:22 +00001800 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001801 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001802
John McCallbdd563e2009-11-03 02:38:08 +00001803 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00001804 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00001805 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001806
Steve Naroff28a7ca82007-08-20 22:28:22 +00001807 // If we don't have a comma, it is either the end of the list (a ';')
1808 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001809 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001810 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001811
Steve Naroff28a7ca82007-08-20 22:28:22 +00001812 // Consume the comma.
1813 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001814
John McCallbdd563e2009-11-03 02:38:08 +00001815 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001816 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001817}
1818
1819/// ParseStructUnionBody
1820/// struct-contents:
1821/// struct-declaration-list
1822/// [EXT] empty
1823/// [GNU] "struct-declaration-list" without terminatoring ';'
1824/// struct-declaration-list:
1825/// struct-declaration
1826/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001827/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001828///
Reid Spencer5f016e22007-07-11 17:01:13 +00001829void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001830 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00001831 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1832 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001833
Reid Spencer5f016e22007-07-11 17:01:13 +00001834 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001836 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001837 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00001838
Reid Spencer5f016e22007-07-11 17:01:13 +00001839 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1840 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001841 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00001842 Diag(Tok, diag::ext_empty_struct_union)
1843 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00001844
John McCalld226f652010-08-21 09:40:31 +00001845 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001846
Reid Spencer5f016e22007-07-11 17:01:13 +00001847 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001848 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001849 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Reid Spencer5f016e22007-07-11 17:01:13 +00001851 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001852 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001853 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00001854 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00001855 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001856 ConsumeToken();
1857 continue;
1858 }
Chris Lattnere1359422008-04-10 06:46:29 +00001859
1860 // Parse all the comma separated declarators.
1861 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001862
John McCallbdd563e2009-11-03 02:38:08 +00001863 if (!Tok.is(tok::at)) {
1864 struct CFieldCallback : FieldCallback {
1865 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001866 Decl *TagDecl;
1867 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001868
John McCalld226f652010-08-21 09:40:31 +00001869 CFieldCallback(Parser &P, Decl *TagDecl,
1870 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001871 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1872
John McCalld226f652010-08-21 09:40:31 +00001873 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001874 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00001875 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00001876 FD.D.getDeclSpec().getSourceRange().getBegin(),
1877 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001878 FieldDecls.push_back(Field);
1879 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001880 }
John McCallbdd563e2009-11-03 02:38:08 +00001881 } Callback(*this, TagDecl, FieldDecls);
1882
1883 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001884 } else { // Handle @defs
1885 ConsumeToken();
1886 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1887 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001888 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001889 continue;
1890 }
1891 ConsumeToken();
1892 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1893 if (!Tok.is(tok::identifier)) {
1894 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001895 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001896 continue;
1897 }
John McCalld226f652010-08-21 09:40:31 +00001898 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001899 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001900 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001901 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1902 ConsumeToken();
1903 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001904 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001905
Chris Lattner04d66662007-10-09 17:33:22 +00001906 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001907 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001908 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001909 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001910 break;
1911 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001912 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1913 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001914 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001915 // If we stopped at a ';', eat it.
1916 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001917 }
1918 }
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Steve Naroff60fccee2007-10-29 21:38:07 +00001920 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001921
John McCall7f040a92010-12-24 02:08:15 +00001922 ParsedAttributes attrs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001923 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001924 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001925
Douglas Gregor23c94db2010-07-02 17:43:08 +00001926 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001927 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001928 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00001929 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00001930 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001931 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001932}
1933
Reid Spencer5f016e22007-07-11 17:01:13 +00001934/// ParseEnumSpecifier
1935/// enum-specifier: [C99 6.7.2.2]
1936/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001937///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001938/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1939/// '}' attributes[opt]
1940/// 'enum' identifier
1941/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001942///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001943/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1944/// [C++0x] enum-head '{' enumerator-list ',' '}'
1945///
1946/// enum-head: [C++0x]
1947/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1948/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1949///
1950/// enum-key: [C++0x]
1951/// 'enum'
1952/// 'enum' 'class'
1953/// 'enum' 'struct'
1954///
1955/// enum-base: [C++0x]
1956/// ':' type-specifier-seq
1957///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001958/// [C++] elaborated-type-specifier:
1959/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1960///
Chris Lattner4c97d762009-04-12 21:49:30 +00001961void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001962 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001963 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001964 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001965 if (Tok.is(tok::code_completion)) {
1966 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001967 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00001968 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00001969 }
1970
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001971 // If attributes exist after tag, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001972 ParsedAttributes attrs;
1973 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001974
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001975 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00001976 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00001977 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00001978 return;
1979
1980 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001981 Diag(Tok, diag::err_expected_ident);
1982 if (Tok.isNot(tok::l_brace)) {
1983 // Has no name and is not a definition.
1984 // Skip the rest of this declarator, up until the comma or semicolon.
1985 SkipUntil(tok::comma, true);
1986 return;
1987 }
1988 }
1989 }
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Douglas Gregor86f208c2011-02-22 20:32:04 +00001991 bool AllowFixedUnderlyingType = getLang().CPlusPlus0x || getLang().Microsoft;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001992 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001993 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001994
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001995 if (getLang().CPlusPlus0x &&
1996 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001997 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001998 IsScopedUsingClassTag = Tok.is(tok::kw_class);
1999 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002000 }
2001
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002002 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002003 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
2004 (AllowFixedUnderlyingType && Tok.isNot(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002005 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00002006
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002007 // Skip the rest of this declarator, up until the comma or semicolon.
2008 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002009 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002010 }
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002012 // If an identifier is present, consume and remember it.
2013 IdentifierInfo *Name = 0;
2014 SourceLocation NameLoc;
2015 if (Tok.is(tok::identifier)) {
2016 Name = Tok.getIdentifierInfo();
2017 NameLoc = ConsumeToken();
2018 }
Mike Stump1eb44332009-09-09 15:08:12 +00002019
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002020 if (!Name && IsScopedEnum) {
2021 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2022 // declaration of a scoped enumeration.
2023 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2024 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002025 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002026 }
2027
2028 TypeResult BaseType;
2029
Douglas Gregora61b3e72010-12-01 17:42:47 +00002030 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00002031 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002032 bool PossibleBitfield = false;
2033 if (getCurScope()->getFlags() & Scope::ClassScope) {
2034 // If we're in class scope, this can either be an enum declaration with
2035 // an underlying type, or a declaration of a bitfield member. We try to
2036 // use a simple disambiguation scheme first to catch the common cases
2037 // (integer literal, sizeof); if it's still ambiguous, we then consider
2038 // anything that's a simple-type-specifier followed by '(' as an
2039 // expression. This suffices because function types are not valid
2040 // underlying types anyway.
2041 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2042 // If the next token starts an expression, we know we're parsing a
2043 // bit-field. This is the common case.
2044 if (TPR == TPResult::True())
2045 PossibleBitfield = true;
2046 // If the next token starts a type-specifier-seq, it may be either a
2047 // a fixed underlying type or the start of a function-style cast in C++;
2048 // lookahead one more token to see if it's obvious that we have a
2049 // fixed underlying type.
2050 else if (TPR == TPResult::False() &&
2051 GetLookAheadToken(2).getKind() == tok::semi) {
2052 // Consume the ':'.
2053 ConsumeToken();
2054 } else {
2055 // We have the start of a type-specifier-seq, so we have to perform
2056 // tentative parsing to determine whether we have an expression or a
2057 // type.
2058 TentativeParsingAction TPA(*this);
2059
2060 // Consume the ':'.
2061 ConsumeToken();
2062
Douglas Gregor86f208c2011-02-22 20:32:04 +00002063 if ((getLang().CPlusPlus &&
2064 isCXXDeclarationSpecifier() != TPResult::True()) ||
2065 (!getLang().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002066 // We'll parse this as a bitfield later.
2067 PossibleBitfield = true;
2068 TPA.Revert();
2069 } else {
2070 // We have a type-specifier-seq.
2071 TPA.Commit();
2072 }
2073 }
2074 } else {
2075 // Consume the ':'.
2076 ConsumeToken();
2077 }
2078
2079 if (!PossibleBitfield) {
2080 SourceRange Range;
2081 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00002082
2083 if (!getLang().CPlusPlus0x)
2084 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
2085 << Range;
Douglas Gregora61b3e72010-12-01 17:42:47 +00002086 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002087 }
2088
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002089 // There are three options here. If we have 'enum foo;', then this is a
2090 // forward declaration. If we have 'enum foo {...' then this is a
2091 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2092 //
2093 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2094 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2095 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2096 //
John McCallf312b1e2010-08-26 23:41:50 +00002097 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002098 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002099 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002100 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002101 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002102 else
John McCallf312b1e2010-08-26 23:41:50 +00002103 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002104
2105 // enums cannot be templates, although they can be referenced from a
2106 // template.
2107 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002108 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002109 Diag(Tok, diag::err_enum_template);
2110
2111 // Skip the rest of this declarator, up until the comma or semicolon.
2112 SkipUntil(tok::comma, true);
2113 return;
2114 }
2115
Douglas Gregorb9075602011-02-22 02:55:24 +00002116 if (!Name && TUK != Sema::TUK_Definition) {
2117 Diag(Tok, diag::err_enumerator_unnamed_no_def);
2118
2119 // Skip the rest of this declarator, up until the comma or semicolon.
2120 SkipUntil(tok::comma, true);
2121 return;
2122 }
2123
Douglas Gregor402abb52009-05-28 23:31:59 +00002124 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002125 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002126 const char *PrevSpec = 0;
2127 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002128 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002129 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002130 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002131 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002132 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002133 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002134
Douglas Gregor48c89f42010-04-24 16:38:41 +00002135 if (IsDependent) {
2136 // This enum has a dependent nested-name-specifier. Handle it as a
2137 // dependent tag.
2138 if (!Name) {
2139 DS.SetTypeSpecError();
2140 Diag(Tok, diag::err_expected_type_name_after_typename);
2141 return;
2142 }
2143
Douglas Gregor23c94db2010-07-02 17:43:08 +00002144 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002145 TUK, SS, Name, StartLoc,
2146 NameLoc);
2147 if (Type.isInvalid()) {
2148 DS.SetTypeSpecError();
2149 return;
2150 }
2151
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002152 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
2153 NameLoc.isValid() ? NameLoc : StartLoc,
2154 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002155 Diag(StartLoc, DiagID) << PrevSpec;
2156
2157 return;
2158 }
Mike Stump1eb44332009-09-09 15:08:12 +00002159
John McCalld226f652010-08-21 09:40:31 +00002160 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002161 // The action failed to produce an enumeration tag. If this is a
2162 // definition, consume the entire definition.
2163 if (Tok.is(tok::l_brace)) {
2164 ConsumeBrace();
2165 SkipUntil(tok::r_brace);
2166 }
2167
2168 DS.SetTypeSpecError();
2169 return;
2170 }
2171
Chris Lattner04d66662007-10-09 17:33:22 +00002172 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002173 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002174
Abramo Bagnara0daaf322011-03-16 20:16:18 +00002175 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
2176 NameLoc.isValid() ? NameLoc : StartLoc,
2177 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002178 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002179}
2180
2181/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2182/// enumerator-list:
2183/// enumerator
2184/// enumerator-list ',' enumerator
2185/// enumerator:
2186/// enumeration-constant
2187/// enumeration-constant '=' constant-expression
2188/// enumeration-constant:
2189/// identifier
2190///
John McCalld226f652010-08-21 09:40:31 +00002191void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002192 // Enter the scope of the enum body and start the definition.
2193 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002194 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002195
Reid Spencer5f016e22007-07-11 17:01:13 +00002196 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002197
Chris Lattner7946dd32007-08-27 17:24:30 +00002198 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002199 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002200 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002201
John McCalld226f652010-08-21 09:40:31 +00002202 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002203
John McCalld226f652010-08-21 09:40:31 +00002204 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002205
Reid Spencer5f016e22007-07-11 17:01:13 +00002206 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002207 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002208 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2209 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002210
John McCall5b629aa2010-10-22 23:36:17 +00002211 // If attributes exist after the enumerator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002212 ParsedAttributes attrs;
2213 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002214
Reid Spencer5f016e22007-07-11 17:01:13 +00002215 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002216 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002217 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002218 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002219 AssignedVal = ParseConstantExpression();
2220 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002221 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002222 }
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Reid Spencer5f016e22007-07-11 17:01:13 +00002224 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002225 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2226 LastEnumConstDecl,
2227 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002228 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002229 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002230 EnumConstantDecls.push_back(EnumConstDecl);
2231 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002232
Douglas Gregor751f6922010-09-07 14:51:08 +00002233 if (Tok.is(tok::identifier)) {
2234 // We're missing a comma between enumerators.
2235 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2236 Diag(Loc, diag::err_enumerator_list_missing_comma)
2237 << FixItHint::CreateInsertion(Loc, ", ");
2238 continue;
2239 }
2240
Chris Lattner04d66662007-10-09 17:33:22 +00002241 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002242 break;
2243 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002244
2245 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002246 !(getLang().C99 || getLang().CPlusPlus0x))
2247 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2248 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002249 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002250 }
Mike Stump1eb44332009-09-09 15:08:12 +00002251
Reid Spencer5f016e22007-07-11 17:01:13 +00002252 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002253 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002254
Reid Spencer5f016e22007-07-11 17:01:13 +00002255 // If attributes exist after the identifier list, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002256 ParsedAttributes attrs;
2257 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002258
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002259 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2260 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002261 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002262
Douglas Gregor72de6672009-01-08 20:45:30 +00002263 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002264 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002265}
2266
2267/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002268/// start of a type-qualifier-list.
2269bool Parser::isTypeQualifier() const {
2270 switch (Tok.getKind()) {
2271 default: return false;
2272 // type-qualifier
2273 case tok::kw_const:
2274 case tok::kw_volatile:
2275 case tok::kw_restrict:
2276 return true;
2277 }
2278}
2279
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002280/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2281/// is definitely a type-specifier. Return false if it isn't part of a type
2282/// specifier or if we're not sure.
2283bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2284 switch (Tok.getKind()) {
2285 default: return false;
2286 // type-specifiers
2287 case tok::kw_short:
2288 case tok::kw_long:
2289 case tok::kw_signed:
2290 case tok::kw_unsigned:
2291 case tok::kw__Complex:
2292 case tok::kw__Imaginary:
2293 case tok::kw_void:
2294 case tok::kw_char:
2295 case tok::kw_wchar_t:
2296 case tok::kw_char16_t:
2297 case tok::kw_char32_t:
2298 case tok::kw_int:
2299 case tok::kw_float:
2300 case tok::kw_double:
2301 case tok::kw_bool:
2302 case tok::kw__Bool:
2303 case tok::kw__Decimal32:
2304 case tok::kw__Decimal64:
2305 case tok::kw__Decimal128:
2306 case tok::kw___vector:
2307
2308 // struct-or-union-specifier (C99) or class-specifier (C++)
2309 case tok::kw_class:
2310 case tok::kw_struct:
2311 case tok::kw_union:
2312 // enum-specifier
2313 case tok::kw_enum:
2314
2315 // typedef-name
2316 case tok::annot_typename:
2317 return true;
2318 }
2319}
2320
Steve Naroff5f8aa692008-02-11 23:15:56 +00002321/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002322/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002323bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002324 switch (Tok.getKind()) {
2325 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002326
Chris Lattner166a8fc2009-01-04 23:41:41 +00002327 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002328 if (TryAltiVecVectorToken())
2329 return true;
2330 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002331 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002332 // Annotate typenames and C++ scope specifiers. If we get one, just
2333 // recurse to handle whatever we get.
2334 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002335 return true;
2336 if (Tok.is(tok::identifier))
2337 return false;
2338 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002339
Chris Lattner166a8fc2009-01-04 23:41:41 +00002340 case tok::coloncolon: // ::foo::bar
2341 if (NextToken().is(tok::kw_new) || // ::new
2342 NextToken().is(tok::kw_delete)) // ::delete
2343 return false;
2344
Chris Lattner166a8fc2009-01-04 23:41:41 +00002345 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002346 return true;
2347 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002348
Reid Spencer5f016e22007-07-11 17:01:13 +00002349 // GNU attributes support.
2350 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002351 // GNU typeof support.
2352 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Reid Spencer5f016e22007-07-11 17:01:13 +00002354 // type-specifiers
2355 case tok::kw_short:
2356 case tok::kw_long:
2357 case tok::kw_signed:
2358 case tok::kw_unsigned:
2359 case tok::kw__Complex:
2360 case tok::kw__Imaginary:
2361 case tok::kw_void:
2362 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002363 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002364 case tok::kw_char16_t:
2365 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002366 case tok::kw_int:
2367 case tok::kw_float:
2368 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002369 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002370 case tok::kw__Bool:
2371 case tok::kw__Decimal32:
2372 case tok::kw__Decimal64:
2373 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002374 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002375
Chris Lattner99dc9142008-04-13 18:59:07 +00002376 // struct-or-union-specifier (C99) or class-specifier (C++)
2377 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002378 case tok::kw_struct:
2379 case tok::kw_union:
2380 // enum-specifier
2381 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002382
Reid Spencer5f016e22007-07-11 17:01:13 +00002383 // type-qualifier
2384 case tok::kw_const:
2385 case tok::kw_volatile:
2386 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002387
2388 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002389 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002390 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002391
Chris Lattner7c186be2008-10-20 00:25:30 +00002392 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2393 case tok::less:
2394 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002395
Steve Naroff239f0732008-12-25 14:16:32 +00002396 case tok::kw___cdecl:
2397 case tok::kw___stdcall:
2398 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002399 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002400 case tok::kw___w64:
2401 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002402 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002403 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002404 }
2405}
2406
2407/// isDeclarationSpecifier() - Return true if the current token is part of a
2408/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002409///
2410/// \param DisambiguatingWithExpression True to indicate that the purpose of
2411/// this check is to disambiguate between an expression and a declaration.
2412bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002413 switch (Tok.getKind()) {
2414 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002415
Chris Lattner166a8fc2009-01-04 23:41:41 +00002416 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002417 // Unfortunate hack to support "Class.factoryMethod" notation.
2418 if (getLang().ObjC1 && NextToken().is(tok::period))
2419 return false;
John Thompson82287d12010-02-05 00:12:22 +00002420 if (TryAltiVecVectorToken())
2421 return true;
2422 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002423 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002424 // Annotate typenames and C++ scope specifiers. If we get one, just
2425 // recurse to handle whatever we get.
2426 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002427 return true;
2428 if (Tok.is(tok::identifier))
2429 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002430
2431 // If we're in Objective-C and we have an Objective-C class type followed
2432 // by an identifier and then either ':' or ']', in a place where an
2433 // expression is permitted, then this is probably a class message send
2434 // missing the initial '['. In this case, we won't consider this to be
2435 // the start of a declaration.
2436 if (DisambiguatingWithExpression &&
2437 isStartOfObjCClassMessageMissingOpenBracket())
2438 return false;
2439
John McCall9ba61662010-02-26 08:45:28 +00002440 return isDeclarationSpecifier();
2441
Chris Lattner166a8fc2009-01-04 23:41:41 +00002442 case tok::coloncolon: // ::foo::bar
2443 if (NextToken().is(tok::kw_new) || // ::new
2444 NextToken().is(tok::kw_delete)) // ::delete
2445 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002446
Chris Lattner166a8fc2009-01-04 23:41:41 +00002447 // Annotate typenames and C++ scope specifiers. If we get one, just
2448 // recurse to handle whatever we get.
2449 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002450 return true;
2451 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002452
Reid Spencer5f016e22007-07-11 17:01:13 +00002453 // storage-class-specifier
2454 case tok::kw_typedef:
2455 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002456 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002457 case tok::kw_static:
2458 case tok::kw_auto:
2459 case tok::kw_register:
2460 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002461
Reid Spencer5f016e22007-07-11 17:01:13 +00002462 // type-specifiers
2463 case tok::kw_short:
2464 case tok::kw_long:
2465 case tok::kw_signed:
2466 case tok::kw_unsigned:
2467 case tok::kw__Complex:
2468 case tok::kw__Imaginary:
2469 case tok::kw_void:
2470 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002471 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002472 case tok::kw_char16_t:
2473 case tok::kw_char32_t:
2474
Reid Spencer5f016e22007-07-11 17:01:13 +00002475 case tok::kw_int:
2476 case tok::kw_float:
2477 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002478 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002479 case tok::kw__Bool:
2480 case tok::kw__Decimal32:
2481 case tok::kw__Decimal64:
2482 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002483 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002484
Chris Lattner99dc9142008-04-13 18:59:07 +00002485 // struct-or-union-specifier (C99) or class-specifier (C++)
2486 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002487 case tok::kw_struct:
2488 case tok::kw_union:
2489 // enum-specifier
2490 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002491
Reid Spencer5f016e22007-07-11 17:01:13 +00002492 // type-qualifier
2493 case tok::kw_const:
2494 case tok::kw_volatile:
2495 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002496
Reid Spencer5f016e22007-07-11 17:01:13 +00002497 // function-specifier
2498 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002499 case tok::kw_virtual:
2500 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002501
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002502 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002503 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002504
Chris Lattner1ef08762007-08-09 17:01:07 +00002505 // GNU typeof support.
2506 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002507
Chris Lattner1ef08762007-08-09 17:01:07 +00002508 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002509 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002510 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002511
Chris Lattnerf3948c42008-07-26 03:38:44 +00002512 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2513 case tok::less:
2514 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002515
Steve Naroff47f52092009-01-06 19:34:12 +00002516 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002517 case tok::kw___cdecl:
2518 case tok::kw___stdcall:
2519 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002520 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002521 case tok::kw___w64:
2522 case tok::kw___ptr64:
2523 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002524 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002525 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002526 }
2527}
2528
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002529bool Parser::isConstructorDeclarator() {
2530 TentativeParsingAction TPA(*this);
2531
2532 // Parse the C++ scope specifier.
2533 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002534 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00002535 TPA.Revert();
2536 return false;
2537 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002538
2539 // Parse the constructor name.
2540 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2541 // We already know that we have a constructor name; just consume
2542 // the token.
2543 ConsumeToken();
2544 } else {
2545 TPA.Revert();
2546 return false;
2547 }
2548
2549 // Current class name must be followed by a left parentheses.
2550 if (Tok.isNot(tok::l_paren)) {
2551 TPA.Revert();
2552 return false;
2553 }
2554 ConsumeParen();
2555
2556 // A right parentheses or ellipsis signals that we have a constructor.
2557 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2558 TPA.Revert();
2559 return true;
2560 }
2561
2562 // If we need to, enter the specified scope.
2563 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002564 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002565 DeclScopeObj.EnterDeclaratorScope();
2566
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00002567 // Optionally skip Microsoft attributes.
2568 ParsedAttributes Attrs;
2569 MaybeParseMicrosoftAttributes(Attrs);
2570
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002571 // Check whether the next token(s) are part of a declaration
2572 // specifier, in which case we have the start of a parameter and,
2573 // therefore, we know that this is a constructor.
2574 bool IsConstructor = isDeclarationSpecifier();
2575 TPA.Revert();
2576 return IsConstructor;
2577}
Reid Spencer5f016e22007-07-11 17:01:13 +00002578
2579/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00002580/// type-qualifier-list: [C99 6.7.5]
2581/// type-qualifier
2582/// [vendor] attributes
2583/// [ only if VendorAttributesAllowed=true ]
2584/// type-qualifier-list type-qualifier
2585/// [vendor] type-qualifier-list attributes
2586/// [ only if VendorAttributesAllowed=true ]
2587/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2588/// [ only if CXX0XAttributesAllowed=true ]
2589/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00002590///
Dawn Perchik52fc3142010-09-03 01:29:35 +00002591void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2592 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00002593 bool CXX0XAttributesAllowed) {
2594 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2595 SourceLocation Loc = Tok.getLocation();
John McCall7f040a92010-12-24 02:08:15 +00002596 ParsedAttributesWithRange attrs;
2597 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002598 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00002599 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002600 else
2601 Diag(Loc, diag::err_attributes_not_allowed);
2602 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00002603
2604 SourceLocation EndLoc;
2605
Reid Spencer5f016e22007-07-11 17:01:13 +00002606 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002607 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002608 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002609 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002610 SourceLocation Loc = Tok.getLocation();
2611
2612 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00002613 case tok::code_completion:
2614 Actions.CodeCompleteTypeQualifiers(DS);
2615 ConsumeCodeCompletionToken();
2616 break;
2617
Reid Spencer5f016e22007-07-11 17:01:13 +00002618 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002619 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2620 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002621 break;
2622 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002623 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2624 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002625 break;
2626 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002627 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2628 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002629 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002630 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002631 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002632 case tok::kw___cdecl:
2633 case tok::kw___stdcall:
2634 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002635 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002636 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00002637 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002638 continue;
2639 }
2640 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002641 case tok::kw___pascal:
2642 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00002643 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002644 continue;
2645 }
2646 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002647 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002648 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00002649 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002650 continue; // do *not* consume the next token!
2651 }
2652 // otherwise, FALL THROUGH!
2653 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002654 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002655 // If this is not a type-qualifier token, we're done reading type
2656 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002657 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00002658 if (EndLoc.isValid())
2659 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002660 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002661 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002662
Reid Spencer5f016e22007-07-11 17:01:13 +00002663 // If the specifier combination wasn't legal, issue a diagnostic.
2664 if (isInvalid) {
2665 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002666 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002667 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00002668 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002669 }
2670}
2671
2672
2673/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2674///
2675void Parser::ParseDeclarator(Declarator &D) {
2676 /// This implements the 'declarator' production in the C grammar, then checks
2677 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002678 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002679}
2680
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002681/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2682/// is parsed by the function passed to it. Pass null, and the direct-declarator
2683/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002684/// ptr-operator production.
2685///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002686/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2687/// [C] pointer[opt] direct-declarator
2688/// [C++] direct-declarator
2689/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002690///
2691/// pointer: [C99 6.7.5]
2692/// '*' type-qualifier-list[opt]
2693/// '*' type-qualifier-list[opt] pointer
2694///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002695/// ptr-operator:
2696/// '*' cv-qualifier-seq[opt]
2697/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002698/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002699/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002700/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002701/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002702void Parser::ParseDeclaratorInternal(Declarator &D,
2703 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002704 if (Diags.hasAllExtensionsSilenced())
2705 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002706
Sebastian Redlf30208a2009-01-24 21:16:55 +00002707 // C++ member pointers start with a '::' or a nested-name.
2708 // Member pointers get special handling, since there's no place for the
2709 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002710 if (getLang().CPlusPlus &&
2711 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2712 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002713 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002714 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00002715
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002716 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002717 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002718 // The scope spec really belongs to the direct-declarator.
2719 D.getCXXScopeSpec() = SS;
2720 if (DirectDeclParser)
2721 (this->*DirectDeclParser)(D);
2722 return;
2723 }
2724
2725 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002726 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002727 DeclSpec DS;
2728 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002729 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002730
2731 // Recurse to parse whatever is left.
2732 ParseDeclaratorInternal(D, DirectDeclParser);
2733
2734 // Sema will have to catch (syntactically invalid) pointers into global
2735 // scope. It has to catch pointers into namespace scope anyway.
2736 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall7f040a92010-12-24 02:08:15 +00002737 Loc, DS.takeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002738 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002739 return;
2740 }
2741 }
2742
2743 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002744 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002745 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002746 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002747 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002748 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002749 if (DirectDeclParser)
2750 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002751 return;
2752 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002753
Sebastian Redl05532f22009-03-15 22:02:01 +00002754 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2755 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002756 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002757 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002758
Chris Lattner9af55002009-03-27 04:18:06 +00002759 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002760 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002761 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002762
Reid Spencer5f016e22007-07-11 17:01:13 +00002763 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002764 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002765
Reid Spencer5f016e22007-07-11 17:01:13 +00002766 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002767 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002768 if (Kind == tok::star)
2769 // Remember that we parsed a pointer type, and remember the type-quals.
2770 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00002771 DS.getConstSpecLoc(),
2772 DS.getVolatileSpecLoc(),
2773 DS.getRestrictSpecLoc(),
John McCall7f040a92010-12-24 02:08:15 +00002774 DS.takeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002775 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002776 else
2777 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002778 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall7f040a92010-12-24 02:08:15 +00002779 Loc, DS.takeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002780 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002781 } else {
2782 // Is a reference
2783 DeclSpec DS;
2784
Sebastian Redl743de1f2009-03-23 00:00:23 +00002785 // Complain about rvalue references in C++03, but then go on and build
2786 // the declarator.
2787 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00002788 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00002789
Reid Spencer5f016e22007-07-11 17:01:13 +00002790 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2791 // cv-qualifiers are introduced through the use of a typedef or of a
2792 // template type argument, in which case the cv-qualifiers are ignored.
2793 //
2794 // [GNU] Retricted references are allowed.
2795 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002796 // [C++0x] Attributes on references are not allowed.
2797 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002798 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002799
2800 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2801 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2802 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002803 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002804 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2805 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002806 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002807 }
2808
2809 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002810 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002811
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002812 if (D.getNumTypeObjects() > 0) {
2813 // C++ [dcl.ref]p4: There shall be no references to references.
2814 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2815 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002816 if (const IdentifierInfo *II = D.getIdentifier())
2817 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2818 << II;
2819 else
2820 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2821 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002822
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002823 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002824 // can go ahead and build the (technically ill-formed)
2825 // declarator: reference collapsing will take care of it.
2826 }
2827 }
2828
Reid Spencer5f016e22007-07-11 17:01:13 +00002829 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002830 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
John McCall7f040a92010-12-24 02:08:15 +00002831 DS.takeAttributes(),
Sebastian Redl05532f22009-03-15 22:02:01 +00002832 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002833 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002834 }
2835}
2836
2837/// ParseDirectDeclarator
2838/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002839/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002840/// '(' declarator ')'
2841/// [GNU] '(' attributes declarator ')'
2842/// [C90] direct-declarator '[' constant-expression[opt] ']'
2843/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2844/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2845/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2846/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2847/// direct-declarator '(' parameter-type-list ')'
2848/// direct-declarator '(' identifier-list[opt] ')'
2849/// [GNU] direct-declarator '(' parameter-forward-declarations
2850/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002851/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2852/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002853/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002854///
2855/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002856/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00002857/// '::'[opt] nested-name-specifier[opt] type-name
2858///
2859/// id-expression: [C++ 5.1]
2860/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002861/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002862///
2863/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002864/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002865/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002866/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002867/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002868/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002869///
Reid Spencer5f016e22007-07-11 17:01:13 +00002870void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002871 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002872
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002873 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2874 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002875 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00002876 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00002877 }
2878
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002879 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002880 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00002881 // Change the declaration context for name lookup, until this function
2882 // is exited (and the declarator has been parsed).
2883 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002884 }
2885
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002886 // C++0x [dcl.fct]p14:
2887 // There is a syntactic ambiguity when an ellipsis occurs at the end
2888 // of a parameter-declaration-clause without a preceding comma. In
2889 // this case, the ellipsis is parsed as part of the
2890 // abstract-declarator if the type of the parameter names a template
2891 // parameter pack that has not been expanded; otherwise, it is parsed
2892 // as part of the parameter-declaration-clause.
2893 if (Tok.is(tok::ellipsis) &&
2894 !((D.getContext() == Declarator::PrototypeContext ||
2895 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002896 NextToken().is(tok::r_paren) &&
2897 !Actions.containsUnexpandedParameterPacks(D)))
2898 D.setEllipsisLoc(ConsumeToken());
2899
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002900 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2901 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2902 // We found something that indicates the start of an unqualified-id.
2903 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002904 bool AllowConstructorName;
2905 if (D.getDeclSpec().hasTypeSpecifier())
2906 AllowConstructorName = false;
2907 else if (D.getCXXScopeSpec().isSet())
2908 AllowConstructorName =
2909 (D.getContext() == Declarator::FileContext ||
2910 (D.getContext() == Declarator::MemberContext &&
2911 D.getDeclSpec().isFriendSpecified()));
2912 else
2913 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2914
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002915 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2916 /*EnteringContext=*/true,
2917 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002918 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002919 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002920 D.getName()) ||
2921 // Once we're past the identifier, if the scope was bad, mark the
2922 // whole declarator bad.
2923 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002924 D.SetIdentifier(0, Tok.getLocation());
2925 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002926 } else {
2927 // Parsed the unqualified-id; update range information and move along.
2928 if (D.getSourceRange().getBegin().isInvalid())
2929 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2930 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002931 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002932 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002933 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002934 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002935 assert(!getLang().CPlusPlus &&
2936 "There's a C++-specific check for tok::identifier above");
2937 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2938 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2939 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002940 goto PastIdentifier;
2941 }
2942
2943 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002944 // direct-declarator: '(' declarator ')'
2945 // direct-declarator: '(' attributes declarator ')'
2946 // Example: 'char (*X)' or 'int (*XX)(void)'
2947 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002948
2949 // If the declarator was parenthesized, we entered the declarator
2950 // scope when parsing the parenthesized declarator, then exited
2951 // the scope already. Re-enter the scope, if we need to.
2952 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002953 // If there was an error parsing parenthesized declarator, declarator
2954 // scope may have been enterred before. Don't do it again.
2955 if (!D.isInvalidType() &&
2956 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002957 // Change the declaration context for name lookup, until this function
2958 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002959 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002960 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002961 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002962 // This could be something simple like "int" (in which case the declarator
2963 // portion is empty), if an abstract-declarator is allowed.
2964 D.SetIdentifier(0, Tok.getLocation());
2965 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002966 if (D.getContext() == Declarator::MemberContext)
2967 Diag(Tok, diag::err_expected_member_name_or_semi)
2968 << D.getDeclSpec().getSourceRange();
2969 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002970 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002971 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002972 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002973 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002974 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002975 }
Mike Stump1eb44332009-09-09 15:08:12 +00002976
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002977 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002978 assert(D.isPastIdentifier() &&
2979 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002980
Sean Huntbbd37c62009-11-21 08:43:09 +00002981 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00002982 if (D.getIdentifier())
2983 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00002984
Reid Spencer5f016e22007-07-11 17:01:13 +00002985 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002986 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002987 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2988 // In such a case, check if we actually have a function declarator; if it
2989 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002990 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2991 // When not in file scope, warn for ambiguous function declarators, just
2992 // in case the author intended it as a variable definition.
2993 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2994 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2995 break;
2996 }
John McCall7f040a92010-12-24 02:08:15 +00002997 ParsedAttributes attrs;
2998 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00002999 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003000 ParseBracketDeclarator(D);
3001 } else {
3002 break;
3003 }
3004 }
3005}
3006
Chris Lattneref4715c2008-04-06 05:45:57 +00003007/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
3008/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00003009/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00003010/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
3011///
3012/// direct-declarator:
3013/// '(' declarator ')'
3014/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00003015/// direct-declarator '(' parameter-type-list ')'
3016/// direct-declarator '(' identifier-list[opt] ')'
3017/// [GNU] direct-declarator '(' parameter-forward-declarations
3018/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00003019///
3020void Parser::ParseParenDeclarator(Declarator &D) {
3021 SourceLocation StartLoc = ConsumeParen();
3022 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00003023
Chris Lattner7399ee02008-10-20 02:05:46 +00003024 // Eat any attributes before we look at whether this is a grouping or function
3025 // declarator paren. If this is a grouping paren, the attribute applies to
3026 // the type being built up, for example:
3027 // int (__attribute__(()) *x)(long y)
3028 // If this ends up not being a grouping paren, the attribute applies to the
3029 // first argument, for example:
3030 // int (__attribute__(()) int x)
3031 // In either case, we need to eat any attributes to be able to determine what
3032 // sort of paren this is.
3033 //
John McCall7f040a92010-12-24 02:08:15 +00003034 ParsedAttributes attrs;
Chris Lattner7399ee02008-10-20 02:05:46 +00003035 bool RequiresArg = false;
3036 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00003037 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003038
Chris Lattner7399ee02008-10-20 02:05:46 +00003039 // We require that the argument list (if this is a non-grouping paren) be
3040 // present even if the attribute list was empty.
3041 RequiresArg = true;
3042 }
Steve Naroff239f0732008-12-25 14:16:32 +00003043 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003044 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003045 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3046 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00003047 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003048 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003049 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003050 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003051 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003052
Chris Lattneref4715c2008-04-06 05:45:57 +00003053 // If we haven't past the identifier yet (or where the identifier would be
3054 // stored, if this is an abstract declarator), then this is probably just
3055 // grouping parens. However, if this could be an abstract-declarator, then
3056 // this could also be the start of function arguments (consider 'void()').
3057 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003058
Chris Lattneref4715c2008-04-06 05:45:57 +00003059 if (!D.mayOmitIdentifier()) {
3060 // If this can't be an abstract-declarator, this *must* be a grouping
3061 // paren, because we haven't seen the identifier yet.
3062 isGrouping = true;
3063 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003064 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003065 isDeclarationSpecifier()) { // 'int(int)' is a function.
3066 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3067 // considered to be a type, not a K&R identifier-list.
3068 isGrouping = false;
3069 } else {
3070 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3071 isGrouping = true;
3072 }
Mike Stump1eb44332009-09-09 15:08:12 +00003073
Chris Lattneref4715c2008-04-06 05:45:57 +00003074 // If this is a grouping paren, handle:
3075 // direct-declarator: '(' declarator ')'
3076 // direct-declarator: '(' attributes declarator ')'
3077 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003078 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003079 D.setGroupingParens(true);
John McCall7f040a92010-12-24 02:08:15 +00003080 if (!attrs.empty())
3081 D.addAttributes(attrs.getList(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003082
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003083 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003084 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003085 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
3086 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc), EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003087
3088 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003089 return;
3090 }
Mike Stump1eb44332009-09-09 15:08:12 +00003091
Chris Lattneref4715c2008-04-06 05:45:57 +00003092 // Okay, if this wasn't a grouping paren, it must be the start of a function
3093 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003094 // identifier (and remember where it would have been), then call into
3095 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003096 D.SetIdentifier(0, Tok.getLocation());
3097
John McCall7f040a92010-12-24 02:08:15 +00003098 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003099}
3100
3101/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3102/// declarator D up to a paren, which indicates that we are parsing function
3103/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003104///
Chris Lattner7399ee02008-10-20 02:05:46 +00003105/// If AttrList is non-null, then the caller parsed those arguments immediately
3106/// after the open paren - they should be considered to be the first argument of
3107/// a parameter. If RequiresArg is true, then the first argument of the
3108/// function is required to be present and required to not be an identifier
3109/// list.
3110///
Reid Spencer5f016e22007-07-11 17:01:13 +00003111/// This method also handles this portion of the grammar:
3112/// parameter-type-list: [C99 6.7.5]
3113/// parameter-list
3114/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003115/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003116///
3117/// parameter-list: [C99 6.7.5]
3118/// parameter-declaration
3119/// parameter-list ',' parameter-declaration
3120///
3121/// parameter-declaration: [C99 6.7.5]
3122/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003123/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003124/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003125/// declaration-specifiers abstract-declarator[opt]
3126/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003127/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003128/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3129///
Douglas Gregor83f51722011-01-26 03:43:54 +00003130/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3131/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003132///
Sebastian Redl7acafd02011-03-05 14:45:16 +00003133/// [C++0x] exception-specification:
3134/// dynamic-exception-specification
3135/// noexcept-specification
3136///
Chris Lattner7399ee02008-10-20 02:05:46 +00003137void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall7f040a92010-12-24 02:08:15 +00003138 ParsedAttributes &attrs,
Chris Lattner7399ee02008-10-20 02:05:46 +00003139 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003140 // lparen is already consumed!
3141 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003142
Douglas Gregordab60ad2010-10-01 18:44:50 +00003143 ParsedType TrailingReturnType;
3144
Chris Lattner7399ee02008-10-20 02:05:46 +00003145 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003146 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003147 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003148 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003149
Abramo Bagnara796aa442011-03-12 11:17:06 +00003150 SourceLocation EndLoc = ConsumeParen(); // Eat the closing ')'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003151
3152 // cv-qualifier-seq[opt].
3153 DeclSpec DS;
Douglas Gregor83f51722011-01-26 03:43:54 +00003154 SourceLocation RefQualifierLoc;
3155 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003156 ExceptionSpecificationType ESpecType = EST_None;
3157 SourceRange ESpecRange;
3158 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3159 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3160 ExprResult NoexceptExpr;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003161 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003162 MaybeParseCXX0XAttributes(attrs);
3163
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003164 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003165 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003166 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003167
Douglas Gregor83f51722011-01-26 03:43:54 +00003168 // Parse ref-qualifier[opt]
3169 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3170 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003171 Diag(Tok, diag::ext_ref_qualifier);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003172
Douglas Gregor83f51722011-01-26 03:43:54 +00003173 RefQualifierIsLValueRef = Tok.is(tok::amp);
3174 RefQualifierLoc = ConsumeToken();
3175 EndLoc = RefQualifierLoc;
3176 }
Sebastian Redl7acafd02011-03-05 14:45:16 +00003177
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003178 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003179 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3180 DynamicExceptions,
3181 DynamicExceptionRanges,
3182 NoexceptExpr);
3183 if (ESpecType != EST_None)
3184 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003185
3186 // Parse trailing-return-type.
3187 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3188 TrailingReturnType = ParseTrailingReturnType().get();
3189 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003190 }
3191
Chris Lattnerf97409f2008-04-06 06:57:35 +00003192 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003193 // int() -> no prototype, no '...'.
John McCall7f040a92010-12-24 02:08:15 +00003194 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3195 /*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003196 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003197 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003198 /*arglist*/ 0, 0,
3199 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003200 RefQualifierIsLValueRef,
3201 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003202 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003203 DynamicExceptions.data(),
3204 DynamicExceptionRanges.data(),
3205 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003206 NoexceptExpr.isUsable() ?
3207 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003208 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003209 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003210 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003211 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003212 }
3213
Chris Lattner7399ee02008-10-20 02:05:46 +00003214 // Alternatively, this parameter list may be an identifier list form for a
3215 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003216 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3217 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003218 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003219 // K&R identifier lists can't have typedefs as identifiers, per
3220 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003221 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003222 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003223
Steve Naroff2d081c42009-01-28 19:16:40 +00003224 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003225 // normal declarators, not for abstract-declarators. Get the first
3226 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003227 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003228 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003229
3230 // Identifier lists follow a really simple grammar: the identifiers can
3231 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3232 // identifier lists are really rare in the brave new modern world, and it
3233 // is very common for someone to typo a type in a non-k&r style list. If
3234 // we are presented with something like: "void foo(intptr x, float y)",
3235 // we don't want to start parsing the function declarator as though it is
3236 // a K&R style declarator just because intptr is an invalid type.
3237 //
3238 // To handle this, we check to see if the token after the first identifier
3239 // is a "," or ")". Only if so, do we parse it as an identifier list.
3240 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3241 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3242 FirstTok.getIdentifierInfo(),
3243 FirstTok.getLocation(), D);
3244
3245 // If we get here, the code is invalid. Push the first identifier back
3246 // into the token stream and parse the first argument as an (invalid)
3247 // normal argument declarator.
3248 PP.EnterToken(Tok);
3249 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003250 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003251 }
Mike Stump1eb44332009-09-09 15:08:12 +00003252
Chris Lattnerf97409f2008-04-06 06:57:35 +00003253 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003254
Chris Lattnerf97409f2008-04-06 06:57:35 +00003255 // Build up an array of information about the parsed arguments.
3256 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003257
3258 // Enter function-declaration scope, limiting any declarators to the
3259 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003260 ParseScope PrototypeScope(this,
3261 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003262
Chris Lattnerf97409f2008-04-06 06:57:35 +00003263 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003264 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003265 while (1) {
3266 if (Tok.is(tok::ellipsis)) {
3267 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003268 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003269 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003270 }
Mike Stump1eb44332009-09-09 15:08:12 +00003271
Chris Lattnerf97409f2008-04-06 06:57:35 +00003272 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003273 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003274 DeclSpec DS;
John McCall7f040a92010-12-24 02:08:15 +00003275
3276 // Skip any Microsoft attributes before a param.
3277 if (getLang().Microsoft && Tok.is(tok::l_square))
3278 ParseMicrosoftAttributes(DS.getAttributes());
3279
3280 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003281
3282 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003283 // Take them so that we only apply the attributes to the first parameter.
3284 DS.takeAttributesFrom(attrs);
3285
Chris Lattnere64c5492009-02-27 18:38:20 +00003286 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003287
Chris Lattnerf97409f2008-04-06 06:57:35 +00003288 // Parse the declarator. This is "PrototypeContext", because we must
3289 // accept either 'declarator' or 'abstract-declarator' here.
3290 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3291 ParseDeclarator(ParmDecl);
3292
3293 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00003294 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003295
Chris Lattnerf97409f2008-04-06 06:57:35 +00003296 // Remember this parsed parameter in ParamInfo.
3297 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003298
Douglas Gregor72b505b2008-12-16 21:30:33 +00003299 // DefArgToks is used when the parsing of default arguments needs
3300 // to be delayed.
3301 CachedTokens *DefArgToks = 0;
3302
Chris Lattnerf97409f2008-04-06 06:57:35 +00003303 // If no parameter was specified, verify that *something* was specified,
3304 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003305 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3306 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003307 // Completely missing, emit error.
3308 Diag(DSStart, diag::err_missing_param);
3309 } else {
3310 // Otherwise, we have something. Add it and let semantic analysis try
3311 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003312
Chris Lattnerf97409f2008-04-06 06:57:35 +00003313 // Inform the actions module about the parameter declarator, so it gets
3314 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003315 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003316
3317 // Parse the default argument, if any. We parse the default
3318 // arguments in all dialects; the semantic analysis in
3319 // ActOnParamDefaultArgument will reject the default argument in
3320 // C.
3321 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003322 SourceLocation EqualLoc = Tok.getLocation();
3323
Chris Lattner04421082008-04-08 04:40:51 +00003324 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003325 if (D.getContext() == Declarator::MemberContext) {
3326 // If we're inside a class definition, cache the tokens
3327 // corresponding to the default argument. We'll actually parse
3328 // them when we see the end of the class definition.
3329 // FIXME: Templates will require something similar.
3330 // FIXME: Can we use a smart pointer for Toks?
3331 DefArgToks = new CachedTokens;
3332
Mike Stump1eb44332009-09-09 15:08:12 +00003333 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003334 /*StopAtSemi=*/true,
3335 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003336 delete DefArgToks;
3337 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003338 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003339 } else {
3340 // Mark the end of the default argument so that we know when to
3341 // stop when we parse it later on.
3342 Token DefArgEnd;
3343 DefArgEnd.startToken();
3344 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3345 DefArgEnd.setLocation(Tok.getLocation());
3346 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003347 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003348 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003349 }
Chris Lattner04421082008-04-08 04:40:51 +00003350 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003351 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003352 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003353
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003354 // The argument isn't actually potentially evaluated unless it is
3355 // used.
3356 EnterExpressionEvaluationContext Eval(Actions,
3357 Sema::PotentiallyEvaluatedIfUsed);
3358
John McCall60d7b3a2010-08-24 06:29:42 +00003359 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003360 if (DefArgResult.isInvalid()) {
3361 Actions.ActOnParamDefaultArgumentError(Param);
3362 SkipUntil(tok::comma, tok::r_paren, true, true);
3363 } else {
3364 // Inform the actions module about the default argument
3365 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003366 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003367 }
Chris Lattner04421082008-04-08 04:40:51 +00003368 }
3369 }
Mike Stump1eb44332009-09-09 15:08:12 +00003370
3371 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3372 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003373 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003374 }
3375
3376 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003377 if (Tok.isNot(tok::comma)) {
3378 if (Tok.is(tok::ellipsis)) {
3379 IsVariadic = true;
3380 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3381
3382 if (!getLang().CPlusPlus) {
3383 // We have ellipsis without a preceding ',', which is ill-formed
3384 // in C. Complain and provide the fix.
3385 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003386 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003387 }
3388 }
3389
3390 break;
3391 }
Mike Stump1eb44332009-09-09 15:08:12 +00003392
Chris Lattnerf97409f2008-04-06 06:57:35 +00003393 // Consume the comma.
3394 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003395 }
Mike Stump1eb44332009-09-09 15:08:12 +00003396
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003397 // If we have the closing ')', eat it.
Abramo Bagnara796aa442011-03-12 11:17:06 +00003398 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003399
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003400 DeclSpec DS;
Douglas Gregor83f51722011-01-26 03:43:54 +00003401 SourceLocation RefQualifierLoc;
3402 bool RefQualifierIsLValueRef = true;
Sebastian Redl7acafd02011-03-05 14:45:16 +00003403 ExceptionSpecificationType ESpecType = EST_None;
3404 SourceRange ESpecRange;
3405 llvm::SmallVector<ParsedType, 2> DynamicExceptions;
3406 llvm::SmallVector<SourceRange, 2> DynamicExceptionRanges;
3407 ExprResult NoexceptExpr;
Sean Huntbbd37c62009-11-21 08:43:09 +00003408
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003409 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003410 MaybeParseCXX0XAttributes(attrs);
3411
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003412 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003413 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003414 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003415 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003416
Douglas Gregor83f51722011-01-26 03:43:54 +00003417 // Parse ref-qualifier[opt]
3418 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3419 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003420 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor83f51722011-01-26 03:43:54 +00003421
3422 RefQualifierIsLValueRef = Tok.is(tok::amp);
3423 RefQualifierLoc = ConsumeToken();
3424 EndLoc = RefQualifierLoc;
3425 }
3426
Sebastian Redl7acafd02011-03-05 14:45:16 +00003427 // FIXME: We should leave the prototype scope before parsing the exception
3428 // specification, and then reenter it when parsing the trailing return type.
3429 // FIXMEFIXME: Why? That wouldn't be right for the noexcept clause.
3430
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003431 // Parse exception-specification[opt].
Sebastian Redl7acafd02011-03-05 14:45:16 +00003432 ESpecType = MaybeParseExceptionSpecification(ESpecRange,
3433 DynamicExceptions,
3434 DynamicExceptionRanges,
3435 NoexceptExpr);
3436 if (ESpecType != EST_None)
3437 EndLoc = ESpecRange.getEnd();
Douglas Gregordab60ad2010-10-01 18:44:50 +00003438
3439 // Parse trailing-return-type.
3440 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3441 TrailingReturnType = ParseTrailingReturnType().get();
3442 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003443 }
3444
Douglas Gregordab60ad2010-10-01 18:44:50 +00003445 // Leave prototype scope.
3446 PrototypeScope.Exit();
3447
Reid Spencer5f016e22007-07-11 17:01:13 +00003448 // Remember that we parsed a function type, and remember the attributes.
John McCall7f040a92010-12-24 02:08:15 +00003449 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3450 /*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003451 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003452 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003453 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003454 RefQualifierIsLValueRef,
3455 RefQualifierLoc,
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003456 ESpecType, ESpecRange.getBegin(),
Sebastian Redl7acafd02011-03-05 14:45:16 +00003457 DynamicExceptions.data(),
3458 DynamicExceptionRanges.data(),
3459 DynamicExceptions.size(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003460 NoexceptExpr.isUsable() ?
3461 NoexceptExpr.get() : 0,
Abramo Bagnara796aa442011-03-12 11:17:06 +00003462 LParenLoc, EndLoc, D,
Douglas Gregordab60ad2010-10-01 18:44:50 +00003463 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003464 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003465}
3466
Chris Lattner66d28652008-04-06 06:34:08 +00003467/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3468/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003469/// first identifier has already been consumed, and the current token is the
3470/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003471///
3472/// identifier-list: [C99 6.7.5]
3473/// identifier
3474/// identifier-list ',' identifier
3475///
3476void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003477 IdentifierInfo *FirstIdent,
3478 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003479 Declarator &D) {
3480 // Build up an array of information about the parsed arguments.
3481 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3482 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003483
Chris Lattner66d28652008-04-06 06:34:08 +00003484 // If there was no identifier specified for the declarator, either we are in
3485 // an abstract-declarator, or we are in a parameter declarator which was found
3486 // to be abstract. In abstract-declarators, identifier lists are not valid:
3487 // diagnose this.
3488 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003489 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003490
Chris Lattner83a94472010-05-14 17:23:36 +00003491 // The first identifier was already read, and is known to be the first
3492 // identifier in the list. Remember this identifier in ParamInfo.
3493 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003494 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003495
Chris Lattner66d28652008-04-06 06:34:08 +00003496 while (Tok.is(tok::comma)) {
3497 // Eat the comma.
3498 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003499
Chris Lattner50c64772008-04-06 06:39:19 +00003500 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003501 if (Tok.isNot(tok::identifier)) {
3502 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003503 SkipUntil(tok::r_paren);
3504 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003505 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003506
Chris Lattner66d28652008-04-06 06:34:08 +00003507 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003508
3509 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003510 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003511 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003512
Chris Lattner66d28652008-04-06 06:34:08 +00003513 // Verify that the argument identifier has not already been mentioned.
3514 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003515 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003516 } else {
3517 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003518 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003519 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00003520 0));
Chris Lattner50c64772008-04-06 06:39:19 +00003521 }
Mike Stump1eb44332009-09-09 15:08:12 +00003522
Chris Lattner66d28652008-04-06 06:34:08 +00003523 // Eat the identifier.
3524 ConsumeToken();
3525 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003526
3527 // If we have the closing ')', eat it and we're done.
3528 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3529
Chris Lattner50c64772008-04-06 06:39:19 +00003530 // Remember that we parsed a function type, and remember the attributes. This
3531 // function type is always a K&R style function type, which is not varargs and
3532 // has no prototype.
John McCall7f040a92010-12-24 02:08:15 +00003533 D.AddTypeInfo(DeclaratorChunk::getFunction(ParsedAttributes(),
3534 /*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003535 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003536 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003537 /*TypeQuals*/0,
Douglas Gregor83f51722011-01-26 03:43:54 +00003538 true, SourceLocation(),
Sebastian Redl6e5d3192011-03-05 22:42:13 +00003539 EST_None, SourceLocation(), 0, 0,
3540 0, 0, LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003541 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003542}
Chris Lattneref4715c2008-04-06 05:45:57 +00003543
Reid Spencer5f016e22007-07-11 17:01:13 +00003544/// [C90] direct-declarator '[' constant-expression[opt] ']'
3545/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3546/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3547/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3548/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3549void Parser::ParseBracketDeclarator(Declarator &D) {
3550 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003551
Chris Lattner378c7e42008-12-18 07:27:21 +00003552 // C array syntax has many features, but by-far the most common is [] and [4].
3553 // This code does a fast path to handle some of the most obvious cases.
3554 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003555 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall7f040a92010-12-24 02:08:15 +00003556 ParsedAttributes attrs;
3557 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003558
Chris Lattner378c7e42008-12-18 07:27:21 +00003559 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00003560 ExprResult NumElements;
John McCall7f040a92010-12-24 02:08:15 +00003561 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003562 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003563 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003564 return;
3565 } else if (Tok.getKind() == tok::numeric_constant &&
3566 GetLookAheadToken(1).is(tok::r_square)) {
3567 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00003568 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003569 ConsumeToken();
3570
Sebastian Redlab197ba2009-02-09 18:23:29 +00003571 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall7f040a92010-12-24 02:08:15 +00003572 ParsedAttributes attrs;
3573 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003574
Chris Lattner378c7e42008-12-18 07:27:21 +00003575 // Remember that we parsed a array type, and remember its features.
John McCall7f040a92010-12-24 02:08:15 +00003576 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, 0,
3577 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003578 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003579 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003580 return;
3581 }
Mike Stump1eb44332009-09-09 15:08:12 +00003582
Reid Spencer5f016e22007-07-11 17:01:13 +00003583 // If valid, this location is the position where we read the 'static' keyword.
3584 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003585 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003586 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003587
Reid Spencer5f016e22007-07-11 17:01:13 +00003588 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003589 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003590 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003591 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003592
Reid Spencer5f016e22007-07-11 17:01:13 +00003593 // If we haven't already read 'static', check to see if there is one after the
3594 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003595 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003596 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003597
Reid Spencer5f016e22007-07-11 17:01:13 +00003598 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3599 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00003600 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00003601
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003602 // Handle the case where we have '[*]' as the array size. However, a leading
3603 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3604 // the the token after the star is a ']'. Since stars in arrays are
3605 // infrequent, use of lookahead is not costly here.
3606 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003607 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003608
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003609 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003610 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003611 StaticLoc = SourceLocation(); // Drop the static.
3612 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003613 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003614 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003615 // Note, in C89, this production uses the constant-expr production instead
3616 // of assignment-expr. The only difference is that assignment-expr allows
3617 // things like '=' and '*='. Sema rejects these in C89 mode because they
3618 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003619
Douglas Gregore0762c92009-06-19 23:52:42 +00003620 // Parse the constant-expression or assignment-expression now (depending
3621 // on dialect).
3622 if (getLang().CPlusPlus)
3623 NumElements = ParseConstantExpression();
3624 else
3625 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003626 }
Mike Stump1eb44332009-09-09 15:08:12 +00003627
Reid Spencer5f016e22007-07-11 17:01:13 +00003628 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003629 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003630 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003631 // If the expression was invalid, skip it.
3632 SkipUntil(tok::r_square);
3633 return;
3634 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003635
3636 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3637
John McCall7f040a92010-12-24 02:08:15 +00003638 ParsedAttributes attrs;
3639 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003640
Chris Lattner378c7e42008-12-18 07:27:21 +00003641 // Remember that we parsed a array type, and remember its features.
John McCall7f040a92010-12-24 02:08:15 +00003642 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), attrs,
Reid Spencer5f016e22007-07-11 17:01:13 +00003643 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003644 NumElements.release(),
3645 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003646 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003647}
3648
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003649/// [GNU] typeof-specifier:
3650/// typeof ( expressions )
3651/// typeof ( type-name )
3652/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003653///
3654void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003655 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003656 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003657 SourceLocation StartLoc = ConsumeToken();
3658
John McCallcfb708c2010-01-13 20:03:27 +00003659 const bool hasParens = Tok.is(tok::l_paren);
3660
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003661 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00003662 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003663 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003664 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
3665 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003666 if (hasParens)
3667 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003668
3669 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003670 // FIXME: Not accurate, the range gets one token more than it should.
3671 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003672 else
3673 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003674
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003675 if (isCastExpr) {
3676 if (!CastTy) {
3677 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003678 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003679 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003680
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003681 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003682 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003683 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3684 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003685 DiagID, CastTy))
3686 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003687 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003688 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003689
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003690 // If we get here, the operand to the typeof was an expresion.
3691 if (Operand.isInvalid()) {
3692 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003693 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003694 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003695
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003696 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003697 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003698 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3699 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00003700 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00003701 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003702}
Chris Lattner1b492422010-02-28 18:33:55 +00003703
3704
3705/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3706/// from TryAltiVecVectorToken.
3707bool Parser::TryAltiVecVectorTokenOutOfLine() {
3708 Token Next = NextToken();
3709 switch (Next.getKind()) {
3710 default: return false;
3711 case tok::kw_short:
3712 case tok::kw_long:
3713 case tok::kw_signed:
3714 case tok::kw_unsigned:
3715 case tok::kw_void:
3716 case tok::kw_char:
3717 case tok::kw_int:
3718 case tok::kw_float:
3719 case tok::kw_double:
3720 case tok::kw_bool:
3721 case tok::kw___pixel:
3722 Tok.setKind(tok::kw___vector);
3723 return true;
3724 case tok::identifier:
3725 if (Next.getIdentifierInfo() == Ident_pixel) {
3726 Tok.setKind(tok::kw___vector);
3727 return true;
3728 }
3729 return false;
3730 }
3731}
3732
3733bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3734 const char *&PrevSpec, unsigned &DiagID,
3735 bool &isInvalid) {
3736 if (Tok.getIdentifierInfo() == Ident_vector) {
3737 Token Next = NextToken();
3738 switch (Next.getKind()) {
3739 case tok::kw_short:
3740 case tok::kw_long:
3741 case tok::kw_signed:
3742 case tok::kw_unsigned:
3743 case tok::kw_void:
3744 case tok::kw_char:
3745 case tok::kw_int:
3746 case tok::kw_float:
3747 case tok::kw_double:
3748 case tok::kw_bool:
3749 case tok::kw___pixel:
3750 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3751 return true;
3752 case tok::identifier:
3753 if (Next.getIdentifierInfo() == Ident_pixel) {
3754 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3755 return true;
3756 }
3757 break;
3758 default:
3759 break;
3760 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003761 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003762 DS.isTypeAltiVecVector()) {
3763 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3764 return true;
3765 }
3766 return false;
3767}