blob: bff4e184c2ae241f7cc60dc08e4ca220f4846dff [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,
395 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
Douglas Gregor1426e532009-05-12 21:31:51 +0000590 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000591 if (isTokenEqualOrMistypedEqualEqual(
592 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000593 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000594 if (Tok.is(tok::kw_delete)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000595 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000596
597 if (!getLang().CPlusPlus0x)
598 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
599
Douglas Gregor1426e532009-05-12 21:31:51 +0000600 Actions.SetDeclDeleted(ThisDecl, DelLoc);
601 } else {
John McCall731ad842009-12-19 09:28:58 +0000602 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
603 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000604 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000605 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000606
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000607 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000608 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000609 ConsumeCodeCompletionToken();
610 SkipUntil(tok::comma, true, true);
611 return ThisDecl;
612 }
613
John McCall60d7b3a2010-08-24 06:29:42 +0000614 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000615
John McCall731ad842009-12-19 09:28:58 +0000616 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000617 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000618 ExitScope();
619 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000620
Douglas Gregor1426e532009-05-12 21:31:51 +0000621 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000622 SkipUntil(tok::comma, true, true);
623 Actions.ActOnInitializerError(ThisDecl);
624 } else
John McCall9ae2f072010-08-23 23:25:46 +0000625 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor1426e532009-05-12 21:31:51 +0000626 }
627 } else if (Tok.is(tok::l_paren)) {
628 // Parse C++ direct initializer: '(' expression-list ')'
629 SourceLocation LParenLoc = ConsumeParen();
630 ExprVector Exprs(Actions);
631 CommaLocsTy CommaLocs;
632
Douglas Gregorb4debae2009-12-22 17:47:17 +0000633 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
634 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000635 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000636 }
637
Douglas Gregor1426e532009-05-12 21:31:51 +0000638 if (ParseExpressionList(Exprs, CommaLocs)) {
639 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000640
641 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000642 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000643 ExitScope();
644 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000645 } else {
646 // Match the ')'.
647 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
648
649 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
650 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000651
652 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000653 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000654 ExitScope();
655 }
656
Douglas Gregor1426e532009-05-12 21:31:51 +0000657 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
658 move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000659 RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000660 }
661 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000662 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000663 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
664 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000665 }
666
667 return ThisDecl;
668}
669
Reid Spencer5f016e22007-07-11 17:01:13 +0000670/// ParseSpecifierQualifierList
671/// specifier-qualifier-list:
672/// type-specifier specifier-qualifier-list[opt]
673/// type-qualifier specifier-qualifier-list[opt]
674/// [GNU] attributes specifier-qualifier-list[opt]
675///
676void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
677 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
678 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 // Validate declspec for type-name.
682 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000683 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +0000684 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 // Issue diagnostic and remove storage class if present.
688 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
689 if (DS.getStorageClassSpecLoc().isValid())
690 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
691 else
692 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
693 DS.ClearStorageClassSpecs();
694 }
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 // Issue diagnostic and remove function specfier if present.
697 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000698 if (DS.isInlineSpecified())
699 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
700 if (DS.isVirtualSpecified())
701 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
702 if (DS.isExplicitSpecified())
703 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 DS.ClearFunctionSpecs();
705 }
706}
707
Chris Lattnerc199ab32009-04-12 20:42:31 +0000708/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
709/// specified token is valid after the identifier in a declarator which
710/// immediately follows the declspec. For example, these things are valid:
711///
712/// int x [ 4]; // direct-declarator
713/// int x ( int y); // direct-declarator
714/// int(int x ) // direct-declarator
715/// int x ; // simple-declaration
716/// int x = 17; // init-declarator-list
717/// int x , y; // init-declarator-list
718/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000719/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000720/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000721///
722/// This is not, because 'x' does not immediately follow the declspec (though
723/// ')' happens to be valid anyway).
724/// int (x)
725///
726static bool isValidAfterIdentifierInDeclarator(const Token &T) {
727 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
728 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000729 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000730}
731
Chris Lattnere40c2952009-04-14 21:34:55 +0000732
733/// ParseImplicitInt - This method is called when we have an non-typename
734/// identifier in a declspec (which normally terminates the decl spec) when
735/// the declspec has no type specifier. In this case, the declspec is either
736/// malformed or is "implicit int" (in K&R and C89).
737///
738/// This method handles diagnosing this prettily and returns false if the
739/// declspec is done being processed. If it recovers and thinks there may be
740/// other pieces of declspec after it, it returns true.
741///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000742bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000743 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000744 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000745 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Chris Lattnere40c2952009-04-14 21:34:55 +0000747 SourceLocation Loc = Tok.getLocation();
748 // If we see an identifier that is not a type name, we normally would
749 // parse it as the identifer being declared. However, when a typename
750 // is typo'd or the definition is not included, this will incorrectly
751 // parse the typename as the identifier name and fall over misparsing
752 // later parts of the diagnostic.
753 //
754 // As such, we try to do some look-ahead in cases where this would
755 // otherwise be an "implicit-int" case to see if this is invalid. For
756 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
757 // an identifier with implicit int, we'd get a parse error because the
758 // next token is obviously invalid for a type. Parse these as a case
759 // with an invalid type specifier.
760 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Chris Lattnere40c2952009-04-14 21:34:55 +0000762 // Since we know that this either implicit int (which is rare) or an
763 // error, we'd do lookahead to try to do better recovery.
764 if (isValidAfterIdentifierInDeclarator(NextToken())) {
765 // If this token is valid for implicit int, e.g. "static x = 4", then
766 // we just avoid eating the identifier, so it will be parsed as the
767 // identifier in the declarator.
768 return false;
769 }
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Chris Lattnere40c2952009-04-14 21:34:55 +0000771 // Otherwise, if we don't consume this token, we are going to emit an
772 // error anyway. Try to recover from various common problems. Check
773 // to see if this was a reference to a tag name without a tag specified.
774 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000775 //
776 // C++ doesn't need this, and isTagName doesn't take SS.
777 if (SS == 0) {
778 const char *TagName = 0;
779 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Douglas Gregor23c94db2010-07-02 17:43:08 +0000781 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +0000782 default: break;
783 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
784 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
785 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
786 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
787 }
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Chris Lattnerf4382f52009-04-14 22:17:06 +0000789 if (TagName) {
790 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000791 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000792 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Chris Lattnerf4382f52009-04-14 22:17:06 +0000794 // Parse this as a tag as if the missing tag were present.
795 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000796 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000797 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000798 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000799 return true;
800 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Douglas Gregora786fdb2009-10-13 23:27:22 +0000803 // This is almost certainly an invalid type name. Let the action emit a
804 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +0000805 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +0000806 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +0000807 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +0000808 // The action emitted a diagnostic, so we don't have to.
809 if (T) {
810 // The action has suggested that the type T could be used. Set that as
811 // the type in the declaration specifiers, consume the would-be type
812 // name token, and we're done.
813 const char *PrevSpec;
814 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +0000815 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000816 DS.SetRangeEnd(Tok.getLocation());
817 ConsumeToken();
818
819 // There may be other declaration specifiers after this.
820 return true;
821 }
822
823 // Fall through; the action had no suggestion for us.
824 } else {
825 // The action did not emit a diagnostic, so emit one now.
826 SourceRange R;
827 if (SS) R = SS->getRange();
828 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
829 }
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Douglas Gregora786fdb2009-10-13 23:27:22 +0000831 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000832 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000833 unsigned DiagID;
834 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000835 DS.SetRangeEnd(Tok.getLocation());
836 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Chris Lattnere40c2952009-04-14 21:34:55 +0000838 // TODO: Could inject an invalid typedef decl in an enclosing scope to
839 // avoid rippling error messages on subsequent uses of the same type,
840 // could be useful if #include was forgotten.
841 return false;
842}
843
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000844/// \brief Determine the declaration specifier context from the declarator
845/// context.
846///
847/// \param Context the declarator context, which is one of the
848/// Declarator::TheContext enumerator values.
849Parser::DeclSpecContext
850Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
851 if (Context == Declarator::MemberContext)
852 return DSC_class;
853 if (Context == Declarator::FileContext)
854 return DSC_top_level;
855 return DSC_normal;
856}
857
Reid Spencer5f016e22007-07-11 17:01:13 +0000858/// ParseDeclarationSpecifiers
859/// declaration-specifiers: [C99 6.7]
860/// storage-class-specifier declaration-specifiers[opt]
861/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000862/// [C99] function-specifier declaration-specifiers[opt]
863/// [GNU] attributes declaration-specifiers[opt]
864///
865/// storage-class-specifier: [C99 6.7.1]
866/// 'typedef'
867/// 'extern'
868/// 'static'
869/// 'auto'
870/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000871/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000872/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000873/// function-specifier: [C99 6.7.4]
874/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000875/// [C++] 'virtual'
876/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +0000877/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000878/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000879/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000880
Reid Spencer5f016e22007-07-11 17:01:13 +0000881///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000882void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000883 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000884 AccessSpecifier AS,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000885 DeclSpecContext DSContext) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000886 DS.SetRangeStart(Tok.getLocation());
Chris Lattner729ad832010-11-09 20:14:26 +0000887 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000888 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000889 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000891 unsigned DiagID = 0;
892
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000894
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000896 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000897 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 // If this is not a declaration specifier token, we're done reading decl
899 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000900 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000903 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +0000904 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000905 if (DS.hasTypeSpecifier()) {
906 bool AllowNonIdentifiers
907 = (getCurScope()->getFlags() & (Scope::ControlScope |
908 Scope::BlockScope |
909 Scope::TemplateParamScope |
910 Scope::FunctionPrototypeScope |
911 Scope::AtCatchScope)) == 0;
912 bool AllowNestedNameSpecifiers
913 = DSContext == DSC_top_level ||
914 (DSContext == DSC_class && DS.isFriendSpecified());
915
Douglas Gregorc7b6d882010-09-16 15:14:18 +0000916 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
917 AllowNonIdentifiers,
918 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000919 ConsumeCodeCompletionToken();
920 return;
921 }
922
Douglas Gregor68e3c2e2011-02-15 20:33:25 +0000923 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
924 CCC = Sema::PCC_LocalDeclarationSpecifiers;
925 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +0000926 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
927 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000928 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +0000929 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000930 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +0000931 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000932
933 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
934 ConsumeCodeCompletionToken();
935 return;
936 }
937
Chris Lattner5e02c472009-01-05 00:07:25 +0000938 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000939 // C++ scope specifier. Annotate and loop, or bail out on error.
940 if (TryAnnotateCXXScopeToken(true)) {
941 if (!DS.hasTypeSpecifier())
942 DS.SetTypeSpecError();
943 goto DoneWithDeclSpec;
944 }
John McCall2e0a7152010-03-01 18:20:46 +0000945 if (Tok.is(tok::coloncolon)) // ::new or ::delete
946 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000947 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000948
949 case tok::annot_cxxscope: {
950 if (DS.hasTypeSpecifier())
951 goto DoneWithDeclSpec;
952
John McCallaa87d332009-12-12 11:40:51 +0000953 CXXScopeSpec SS;
John McCallca0408f2010-08-23 06:44:23 +0000954 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCallaa87d332009-12-12 11:40:51 +0000955 SS.setRange(Tok.getAnnotationRange());
956
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000957 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000958 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000959 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000960 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000961 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000962 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000963
964 // C++ [class.qual]p2:
965 // In a lookup in which the constructor is an acceptable lookup
966 // result and the nested-name-specifier nominates a class C:
967 //
968 // - if the name specified after the
969 // nested-name-specifier, when looked up in C, is the
970 // injected-class-name of C (Clause 9), or
971 //
972 // - if the name specified after the nested-name-specifier
973 // is the same as the identifier or the
974 // simple-template-id's template-name in the last
975 // component of the nested-name-specifier,
976 //
977 // the name is instead considered to name the constructor of
978 // class C.
979 //
980 // Thus, if the template-name is actually the constructor
981 // name, then the code is ill-formed; this interpretation is
982 // reinforced by the NAD status of core issue 635.
983 TemplateIdAnnotation *TemplateId
984 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000985 if ((DSContext == DSC_top_level ||
986 (DSContext == DSC_class && DS.isFriendSpecified())) &&
987 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000988 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000989 if (isConstructorDeclarator()) {
990 // The user meant this to be an out-of-line constructor
991 // definition, but template arguments are not allowed
992 // there. Just allow this as a constructor; we'll
993 // complain about it later.
994 goto DoneWithDeclSpec;
995 }
996
997 // The user meant this to name a type, but it actually names
998 // a constructor with some extraneous template
999 // arguments. Complain, then parse it as a type as the user
1000 // intended.
1001 Diag(TemplateId->TemplateNameLoc,
1002 diag::err_out_of_line_template_id_names_constructor)
1003 << TemplateId->Name;
1004 }
1005
John McCallaa87d332009-12-12 11:40:51 +00001006 DS.getTypeSpecScope() = SS;
1007 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001008 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001009 "ParseOptionalCXXScopeSpecifier not working");
1010 AnnotateTemplateIdTokenAsType(&SS);
1011 continue;
1012 }
1013
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001014 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001015 DS.getTypeSpecScope() = SS;
1016 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001017 if (Tok.getAnnotationValue()) {
1018 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001019 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1020 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001021 PrevSpec, DiagID, T);
1022 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001023 else
1024 DS.SetTypeSpecError();
1025 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1026 ConsumeToken(); // The typename
1027 }
1028
Douglas Gregor9135c722009-03-25 15:40:00 +00001029 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001030 goto DoneWithDeclSpec;
1031
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001032 // If we're in a context where the identifier could be a class name,
1033 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001034 if ((DSContext == DSC_top_level ||
1035 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001036 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001037 &SS)) {
1038 if (isConstructorDeclarator())
1039 goto DoneWithDeclSpec;
1040
1041 // As noted in C++ [class.qual]p2 (cited above), when the name
1042 // of the class is qualified in a context where it could name
1043 // a constructor, its a constructor name. However, we've
1044 // looked at the declarator, and the user probably meant this
1045 // to be a type. Complain that it isn't supposed to be treated
1046 // as a type, then proceed to parse it as a type.
1047 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1048 << Next.getIdentifierInfo();
1049 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001050
John McCallb3d87482010-08-24 05:47:05 +00001051 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1052 Next.getLocation(),
1053 getCurScope(), &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001054
Chris Lattnerf4382f52009-04-14 22:17:06 +00001055 // If the referenced identifier is not a type, then this declspec is
1056 // erroneous: We already checked about that it has no type specifier, and
1057 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001058 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001059 if (TypeRep == 0) {
1060 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001061 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001062 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
John McCallaa87d332009-12-12 11:40:51 +00001065 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001066 ConsumeToken(); // The C++ scope.
1067
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001068 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001069 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001070 if (isInvalid)
1071 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001073 DS.SetRangeEnd(Tok.getLocation());
1074 ConsumeToken(); // The typename.
1075
1076 continue;
1077 }
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Chris Lattner80d0c892009-01-21 19:48:37 +00001079 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001080 if (Tok.getAnnotationValue()) {
1081 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001082 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001083 DiagID, T);
1084 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001085 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001086
1087 if (isInvalid)
1088 break;
1089
Chris Lattner80d0c892009-01-21 19:48:37 +00001090 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1091 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Chris Lattner80d0c892009-01-21 19:48:37 +00001093 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1094 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001095 // Objective-C interface.
1096 if (Tok.is(tok::less) && getLang().ObjC1)
1097 ParseObjCProtocolQualifiers(DS);
1098
Chris Lattner80d0c892009-01-21 19:48:37 +00001099 continue;
1100 }
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Chris Lattner3bd934a2008-07-26 01:18:38 +00001102 // typedef-name
1103 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001104 // In C++, check to see if this is a scope specifier like foo::bar::, if
1105 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001106 if (getLang().CPlusPlus) {
1107 if (TryAnnotateCXXScopeToken(true)) {
1108 if (!DS.hasTypeSpecifier())
1109 DS.SetTypeSpecError();
1110 goto DoneWithDeclSpec;
1111 }
1112 if (!Tok.is(tok::identifier))
1113 continue;
1114 }
Mike Stump1eb44332009-09-09 15:08:12 +00001115
Chris Lattner3bd934a2008-07-26 01:18:38 +00001116 // This identifier can only be a typedef name if we haven't already seen
1117 // a type-specifier. Without this check we misparse:
1118 // typedef int X; struct Y { short X; }; as 'short int'.
1119 if (DS.hasTypeSpecifier())
1120 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001121
John Thompson82287d12010-02-05 00:12:22 +00001122 // Check for need to substitute AltiVec keyword tokens.
1123 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1124 break;
1125
Chris Lattner3bd934a2008-07-26 01:18:38 +00001126 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001127 ParsedType TypeRep =
1128 Actions.getTypeName(*Tok.getIdentifierInfo(),
1129 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001130
Chris Lattnerc199ab32009-04-12 20:42:31 +00001131 // If this is not a typedef name, don't parse it as part of the declspec,
1132 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001133 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001134 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001135 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001136 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001137
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001138 // If we're in a context where the identifier could be a class name,
1139 // check whether this is a constructor declaration.
1140 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001141 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001142 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001143 goto DoneWithDeclSpec;
1144
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001145 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001146 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001147 if (isInvalid)
1148 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Chris Lattner3bd934a2008-07-26 01:18:38 +00001150 DS.SetRangeEnd(Tok.getLocation());
1151 ConsumeToken(); // The identifier
1152
1153 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1154 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001155 // Objective-C interface.
1156 if (Tok.is(tok::less) && getLang().ObjC1)
1157 ParseObjCProtocolQualifiers(DS);
1158
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001159 // Need to support trailing type qualifiers (e.g. "id<p> const").
1160 // If a type specifier follows, it will be diagnosed elsewhere.
1161 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001162 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001163
1164 // type-name
1165 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001166 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001167 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001168 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001169 // This template-id does not refer to a type name, so we're
1170 // done with the type-specifiers.
1171 goto DoneWithDeclSpec;
1172 }
1173
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001174 // If we're in a context where the template-id could be a
1175 // constructor name or specialization, check whether this is a
1176 // constructor declaration.
1177 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001178 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001179 isConstructorDeclarator())
1180 goto DoneWithDeclSpec;
1181
Douglas Gregor39a8de12009-02-25 19:37:18 +00001182 // Turn the template-id annotation token into a type annotation
1183 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001184 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001185 continue;
1186 }
1187
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 // GNU attributes support.
1189 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001190 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001192
1193 // Microsoft declspec support.
1194 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001195 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001196 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Steve Naroff239f0732008-12-25 14:16:32 +00001198 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001199 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001200 // FIXME: Add handling here!
1201 break;
1202
1203 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001204 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001205 case tok::kw___cdecl:
1206 case tok::kw___stdcall:
1207 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001208 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001209 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001210 continue;
1211
Dawn Perchik52fc3142010-09-03 01:29:35 +00001212 // Borland single token adornments.
1213 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001214 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001215 continue;
1216
Peter Collingbournef315fa82011-02-14 01:42:53 +00001217 // OpenCL single token adornments.
1218 case tok::kw___kernel:
1219 ParseOpenCLAttributes(DS.getAttributes());
1220 continue;
1221
Reid Spencer5f016e22007-07-11 17:01:13 +00001222 // storage-class-specifier
1223 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001224 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001225 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 break;
1227 case tok::kw_extern:
1228 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001229 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001230 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001231 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001233 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001234 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001235 PrevSpec, DiagID, getLang());
Steve Naroff8d54bf22007-12-18 00:16:02 +00001236 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 case tok::kw_static:
1238 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001239 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001240 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001241 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 break;
1243 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001244 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001245 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1246 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001247 else
John McCallfec54012009-08-03 20:12:06 +00001248 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, 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_register:
John McCallfec54012009-08-03 20:12:06 +00001252 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001253 DiagID, getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001255 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001256 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
Peter Collingbournee2f82f72011-02-11 19:59:54 +00001257 DiagID, getLang());
Sebastian Redl669d5d72008-11-14 23:42:31 +00001258 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001260 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 // function-specifier
1264 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001265 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001267 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001268 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001269 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001270 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001271 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001272 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001273
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001274 // friend
1275 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001276 if (DSContext == DSC_class)
1277 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1278 else {
1279 PrevSpec = ""; // not actually used by the diagnostic
1280 DiagID = diag::err_friend_invalid_in_context;
1281 isInvalid = true;
1282 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001283 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Sebastian Redl2ac67232009-11-05 15:47:02 +00001285 // constexpr
1286 case tok::kw_constexpr:
1287 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1288 break;
1289
Chris Lattner80d0c892009-01-21 19:48:37 +00001290 // type-specifier
1291 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001292 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1293 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001294 break;
1295 case tok::kw_long:
1296 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001297 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1298 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001299 else
John McCallfec54012009-08-03 20:12:06 +00001300 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1301 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001302 break;
1303 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001304 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1305 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001306 break;
1307 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001308 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1309 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001310 break;
1311 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001312 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1313 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001314 break;
1315 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001316 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1317 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001318 break;
1319 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001320 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1321 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001322 break;
1323 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001324 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1325 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001326 break;
1327 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001328 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1329 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001330 break;
1331 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1333 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001334 break;
1335 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001336 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1337 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001338 break;
1339 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001340 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1341 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001342 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001343 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001344 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1345 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001346 break;
1347 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001348 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1349 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001350 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001351 case tok::kw_bool:
1352 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001353 if (Tok.is(tok::kw_bool) &&
1354 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1355 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1356 PrevSpec = ""; // Not used by the diagnostic.
1357 DiagID = diag::err_bool_redeclaration;
1358 isInvalid = true;
1359 } else {
1360 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1361 DiagID);
1362 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001363 break;
1364 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001365 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1366 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001367 break;
1368 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001369 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1370 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001371 break;
1372 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001373 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1374 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001375 break;
John Thompson82287d12010-02-05 00:12:22 +00001376 case tok::kw___vector:
1377 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1378 break;
1379 case tok::kw___pixel:
1380 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1381 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001382
1383 // class-specifier:
1384 case tok::kw_class:
1385 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001386 case tok::kw_union: {
1387 tok::TokenKind Kind = Tok.getKind();
1388 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001389 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001390 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001391 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001392
1393 // enum-specifier:
1394 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001395 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001396 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001397 continue;
1398
1399 // cv-qualifier:
1400 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001401 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1402 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001403 break;
1404 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001405 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1406 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001407 break;
1408 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001409 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1410 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001411 break;
1412
Douglas Gregord57959a2009-03-27 23:10:48 +00001413 // C++ typename-specifier:
1414 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001415 if (TryAnnotateTypeOrScopeToken()) {
1416 DS.SetTypeSpecError();
1417 goto DoneWithDeclSpec;
1418 }
1419 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001420 continue;
1421 break;
1422
Chris Lattner80d0c892009-01-21 19:48:37 +00001423 // GNU typeof support.
1424 case tok::kw_typeof:
1425 ParseTypeofSpecifier(DS);
1426 continue;
1427
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001428 case tok::kw_decltype:
1429 ParseDecltypeSpecifier(DS);
1430 continue;
1431
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001432 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001433 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001434 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1435 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001436 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001437 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Douglas Gregor46f936e2010-11-19 17:10:50 +00001439 if (!ParseObjCProtocolQualifiers(DS))
1440 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1441 << FixItHint::CreateInsertion(Loc, "id")
1442 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001443
1444 // Need to support trailing type qualifiers (e.g. "id<p> const").
1445 // If a type specifier follows, it will be diagnosed elsewhere.
1446 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001447 }
John McCallfec54012009-08-03 20:12:06 +00001448 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001449 if (isInvalid) {
1450 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001451 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001452
1453 if (DiagID == diag::ext_duplicate_declspec)
1454 Diag(Tok, DiagID)
1455 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1456 else
1457 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001458 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001459 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001460 ConsumeToken();
1461 }
1462}
Douglas Gregoradcac882008-12-01 23:54:00 +00001463
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001464/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001465/// primarily follow the C++ grammar with additions for C99 and GNU,
1466/// which together subsume the C grammar. Note that the C++
1467/// type-specifier also includes the C type-qualifier (for const,
1468/// volatile, and C99 restrict). Returns true if a type-specifier was
1469/// found (and parsed), false otherwise.
1470///
1471/// type-specifier: [C++ 7.1.5]
1472/// simple-type-specifier
1473/// class-specifier
1474/// enum-specifier
1475/// elaborated-type-specifier [TODO]
1476/// cv-qualifier
1477///
1478/// cv-qualifier: [C++ 7.1.5.1]
1479/// 'const'
1480/// 'volatile'
1481/// [C99] 'restrict'
1482///
1483/// simple-type-specifier: [ C++ 7.1.5.2]
1484/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1485/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1486/// 'char'
1487/// 'wchar_t'
1488/// 'bool'
1489/// 'short'
1490/// 'int'
1491/// 'long'
1492/// 'signed'
1493/// 'unsigned'
1494/// 'float'
1495/// 'double'
1496/// 'void'
1497/// [C99] '_Bool'
1498/// [C99] '_Complex'
1499/// [C99] '_Imaginary' // Removed in TC2?
1500/// [GNU] '_Decimal32'
1501/// [GNU] '_Decimal64'
1502/// [GNU] '_Decimal128'
1503/// [GNU] typeof-specifier
1504/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1505/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001506/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001507/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001508bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001509 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001510 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001511 const ParsedTemplateInfo &TemplateInfo,
1512 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001513 SourceLocation Loc = Tok.getLocation();
1514
1515 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001516 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001517 // If we already have a type specifier, this identifier is not a type.
1518 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1519 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1520 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1521 return false;
John Thompson82287d12010-02-05 00:12:22 +00001522 // Check for need to substitute AltiVec keyword tokens.
1523 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1524 break;
1525 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001526 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001527 // Annotate typenames and C++ scope specifiers. If we get one, just
1528 // recurse to handle whatever we get.
1529 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001530 return true;
1531 if (Tok.is(tok::identifier))
1532 return false;
1533 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1534 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001535 case tok::coloncolon: // ::foo::bar
1536 if (NextToken().is(tok::kw_new) || // ::new
1537 NextToken().is(tok::kw_delete)) // ::delete
1538 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Chris Lattner166a8fc2009-01-04 23:41:41 +00001540 // Annotate typenames and C++ scope specifiers. If we get one, just
1541 // recurse to handle whatever we get.
1542 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001543 return true;
1544 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1545 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Douglas Gregor12e083c2008-11-07 15:42:26 +00001547 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001548 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001549 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00001550 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1551 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001552 DiagID, T);
1553 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001554 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001555 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1556 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Douglas Gregor12e083c2008-11-07 15:42:26 +00001558 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1559 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1560 // Objective-C interface. If we don't have Objective-C or a '<', this is
1561 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001562 if (Tok.is(tok::less) && getLang().ObjC1)
1563 ParseObjCProtocolQualifiers(DS);
1564
Douglas Gregor12e083c2008-11-07 15:42:26 +00001565 return true;
1566 }
1567
1568 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001569 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001570 break;
1571 case tok::kw_long:
1572 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001573 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1574 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001575 else
John McCallfec54012009-08-03 20:12:06 +00001576 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1577 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001578 break;
1579 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001580 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001581 break;
1582 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001583 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1584 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001585 break;
1586 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001587 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1588 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001589 break;
1590 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001591 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1592 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001593 break;
1594 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001595 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001596 break;
1597 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001598 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001599 break;
1600 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001601 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001602 break;
1603 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001604 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001605 break;
1606 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001607 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001608 break;
1609 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001610 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001611 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001612 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001613 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001614 break;
1615 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001616 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001617 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001618 case tok::kw_bool:
1619 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001620 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001621 break;
1622 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001623 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1624 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001625 break;
1626 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001627 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1628 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001629 break;
1630 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001631 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1632 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001633 break;
John Thompson82287d12010-02-05 00:12:22 +00001634 case tok::kw___vector:
1635 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1636 break;
1637 case tok::kw___pixel:
1638 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1639 break;
1640
Douglas Gregor12e083c2008-11-07 15:42:26 +00001641 // class-specifier:
1642 case tok::kw_class:
1643 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001644 case tok::kw_union: {
1645 tok::TokenKind Kind = Tok.getKind();
1646 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001647 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1648 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001649 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001650 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001651
1652 // enum-specifier:
1653 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001654 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001655 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001656 return true;
1657
1658 // cv-qualifier:
1659 case tok::kw_const:
1660 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001661 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001662 break;
1663 case tok::kw_volatile:
1664 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001665 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001666 break;
1667 case tok::kw_restrict:
1668 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001669 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001670 break;
1671
1672 // GNU typeof support.
1673 case tok::kw_typeof:
1674 ParseTypeofSpecifier(DS);
1675 return true;
1676
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001677 // C++0x decltype support.
1678 case tok::kw_decltype:
1679 ParseDecltypeSpecifier(DS);
1680 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001682 // C++0x auto support.
1683 case tok::kw_auto:
1684 if (!getLang().CPlusPlus0x)
1685 return false;
1686
John McCallfec54012009-08-03 20:12:06 +00001687 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001688 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001689
Eli Friedman290eeb02009-06-08 23:27:34 +00001690 case tok::kw___ptr64:
1691 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001692 case tok::kw___cdecl:
1693 case tok::kw___stdcall:
1694 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001695 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001696 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001697 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001698
Dawn Perchik52fc3142010-09-03 01:29:35 +00001699 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001700 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001701 return true;
1702
Douglas Gregor12e083c2008-11-07 15:42:26 +00001703 default:
1704 // Not a type-specifier; do nothing.
1705 return false;
1706 }
1707
1708 // If the specifier combination wasn't legal, issue a diagnostic.
1709 if (isInvalid) {
1710 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001711 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001712 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001713 }
1714 DS.SetRangeEnd(Tok.getLocation());
1715 ConsumeToken(); // whatever we parsed above.
1716 return true;
1717}
Reid Spencer5f016e22007-07-11 17:01:13 +00001718
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001719/// ParseStructDeclaration - Parse a struct declaration without the terminating
1720/// semicolon.
1721///
Reid Spencer5f016e22007-07-11 17:01:13 +00001722/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001723/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001724/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001725/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001726/// struct-declarator-list:
1727/// struct-declarator
1728/// struct-declarator-list ',' struct-declarator
1729/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1730/// struct-declarator:
1731/// declarator
1732/// [GNU] declarator attributes[opt]
1733/// declarator[opt] ':' constant-expression
1734/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1735///
Chris Lattnere1359422008-04-10 06:46:29 +00001736void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001737ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001738 if (Tok.is(tok::kw___extension__)) {
1739 // __extension__ silences extension warnings in the subexpression.
1740 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001741 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001742 return ParseStructDeclaration(DS, Fields);
1743 }
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Steve Naroff28a7ca82007-08-20 22:28:22 +00001745 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001746 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001748 // If there are no declarators, this is a free-standing declaration
1749 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001750 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001751 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001752 return;
1753 }
1754
1755 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001756 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001757 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001758 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001759 FieldDeclarator DeclaratorInfo(DS);
1760
1761 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00001762 if (!FirstDeclarator)
1763 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00001764
Steve Naroff28a7ca82007-08-20 22:28:22 +00001765 /// struct-declarator: declarator
1766 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001767 if (Tok.isNot(tok::colon)) {
1768 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1769 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001770 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001771 }
Mike Stump1eb44332009-09-09 15:08:12 +00001772
Chris Lattner04d66662007-10-09 17:33:22 +00001773 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001774 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001775 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001776 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001777 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001778 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001779 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001780 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001781
Steve Naroff28a7ca82007-08-20 22:28:22 +00001782 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001783 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001784
John McCallbdd563e2009-11-03 02:38:08 +00001785 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00001786 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00001787 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001788
Steve Naroff28a7ca82007-08-20 22:28:22 +00001789 // If we don't have a comma, it is either the end of the list (a ';')
1790 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001791 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001792 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001793
Steve Naroff28a7ca82007-08-20 22:28:22 +00001794 // Consume the comma.
1795 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001796
John McCallbdd563e2009-11-03 02:38:08 +00001797 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001798 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001799}
1800
1801/// ParseStructUnionBody
1802/// struct-contents:
1803/// struct-declaration-list
1804/// [EXT] empty
1805/// [GNU] "struct-declaration-list" without terminatoring ';'
1806/// struct-declaration-list:
1807/// struct-declaration
1808/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001809/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001810///
Reid Spencer5f016e22007-07-11 17:01:13 +00001811void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001812 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00001813 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1814 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Reid Spencer5f016e22007-07-11 17:01:13 +00001816 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001818 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001819 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00001820
Reid Spencer5f016e22007-07-11 17:01:13 +00001821 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1822 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001823 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00001824 Diag(Tok, diag::ext_empty_struct_union)
1825 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00001826
John McCalld226f652010-08-21 09:40:31 +00001827 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001828
Reid Spencer5f016e22007-07-11 17:01:13 +00001829 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001830 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001831 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001832
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001834 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001835 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00001836 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00001837 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001838 ConsumeToken();
1839 continue;
1840 }
Chris Lattnere1359422008-04-10 06:46:29 +00001841
1842 // Parse all the comma separated declarators.
1843 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001844
John McCallbdd563e2009-11-03 02:38:08 +00001845 if (!Tok.is(tok::at)) {
1846 struct CFieldCallback : FieldCallback {
1847 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001848 Decl *TagDecl;
1849 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001850
John McCalld226f652010-08-21 09:40:31 +00001851 CFieldCallback(Parser &P, Decl *TagDecl,
1852 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001853 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1854
John McCalld226f652010-08-21 09:40:31 +00001855 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001856 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00001857 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00001858 FD.D.getDeclSpec().getSourceRange().getBegin(),
1859 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001860 FieldDecls.push_back(Field);
1861 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001862 }
John McCallbdd563e2009-11-03 02:38:08 +00001863 } Callback(*this, TagDecl, FieldDecls);
1864
1865 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001866 } else { // Handle @defs
1867 ConsumeToken();
1868 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1869 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001870 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001871 continue;
1872 }
1873 ConsumeToken();
1874 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1875 if (!Tok.is(tok::identifier)) {
1876 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001877 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001878 continue;
1879 }
John McCalld226f652010-08-21 09:40:31 +00001880 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001881 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001882 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001883 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1884 ConsumeToken();
1885 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001886 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001887
Chris Lattner04d66662007-10-09 17:33:22 +00001888 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001889 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001890 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001891 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001892 break;
1893 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001894 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1895 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001896 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001897 // If we stopped at a ';', eat it.
1898 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001899 }
1900 }
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Steve Naroff60fccee2007-10-29 21:38:07 +00001902 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001903
John McCall7f040a92010-12-24 02:08:15 +00001904 ParsedAttributes attrs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001905 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001906 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001907
Douglas Gregor23c94db2010-07-02 17:43:08 +00001908 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001909 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001910 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00001911 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00001912 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001913 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001914}
1915
Reid Spencer5f016e22007-07-11 17:01:13 +00001916/// ParseEnumSpecifier
1917/// enum-specifier: [C99 6.7.2.2]
1918/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001919///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001920/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1921/// '}' attributes[opt]
1922/// 'enum' identifier
1923/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001924///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001925/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1926/// [C++0x] enum-head '{' enumerator-list ',' '}'
1927///
1928/// enum-head: [C++0x]
1929/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1930/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1931///
1932/// enum-key: [C++0x]
1933/// 'enum'
1934/// 'enum' 'class'
1935/// 'enum' 'struct'
1936///
1937/// enum-base: [C++0x]
1938/// ':' type-specifier-seq
1939///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001940/// [C++] elaborated-type-specifier:
1941/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1942///
Chris Lattner4c97d762009-04-12 21:49:30 +00001943void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001944 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001945 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001946 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001947 if (Tok.is(tok::code_completion)) {
1948 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001949 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00001950 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00001951 }
1952
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001953 // If attributes exist after tag, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001954 ParsedAttributes attrs;
1955 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001956
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001957 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00001958 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00001959 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00001960 return;
1961
1962 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001963 Diag(Tok, diag::err_expected_ident);
1964 if (Tok.isNot(tok::l_brace)) {
1965 // Has no name and is not a definition.
1966 // Skip the rest of this declarator, up until the comma or semicolon.
1967 SkipUntil(tok::comma, true);
1968 return;
1969 }
1970 }
1971 }
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001973 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001974 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001975
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001976 if (getLang().CPlusPlus0x &&
1977 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001978 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001979 IsScopedUsingClassTag = Tok.is(tok::kw_class);
1980 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001981 }
1982
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001983 // Must have either 'enum name' or 'enum {...}'.
1984 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1985 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001986
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001987 // Skip the rest of this declarator, up until the comma or semicolon.
1988 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001989 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001990 }
Mike Stump1eb44332009-09-09 15:08:12 +00001991
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001992 // If an identifier is present, consume and remember it.
1993 IdentifierInfo *Name = 0;
1994 SourceLocation NameLoc;
1995 if (Tok.is(tok::identifier)) {
1996 Name = Tok.getIdentifierInfo();
1997 NameLoc = ConsumeToken();
1998 }
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002000 if (!Name && IsScopedEnum) {
2001 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2002 // declaration of a scoped enumeration.
2003 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2004 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002005 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002006 }
2007
2008 TypeResult BaseType;
2009
Douglas Gregora61b3e72010-12-01 17:42:47 +00002010 // Parse the fixed underlying type.
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002011 if (getLang().CPlusPlus0x && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002012 bool PossibleBitfield = false;
2013 if (getCurScope()->getFlags() & Scope::ClassScope) {
2014 // If we're in class scope, this can either be an enum declaration with
2015 // an underlying type, or a declaration of a bitfield member. We try to
2016 // use a simple disambiguation scheme first to catch the common cases
2017 // (integer literal, sizeof); if it's still ambiguous, we then consider
2018 // anything that's a simple-type-specifier followed by '(' as an
2019 // expression. This suffices because function types are not valid
2020 // underlying types anyway.
2021 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2022 // If the next token starts an expression, we know we're parsing a
2023 // bit-field. This is the common case.
2024 if (TPR == TPResult::True())
2025 PossibleBitfield = true;
2026 // If the next token starts a type-specifier-seq, it may be either a
2027 // a fixed underlying type or the start of a function-style cast in C++;
2028 // lookahead one more token to see if it's obvious that we have a
2029 // fixed underlying type.
2030 else if (TPR == TPResult::False() &&
2031 GetLookAheadToken(2).getKind() == tok::semi) {
2032 // Consume the ':'.
2033 ConsumeToken();
2034 } else {
2035 // We have the start of a type-specifier-seq, so we have to perform
2036 // tentative parsing to determine whether we have an expression or a
2037 // type.
2038 TentativeParsingAction TPA(*this);
2039
2040 // Consume the ':'.
2041 ConsumeToken();
2042
2043 if (isCXXDeclarationSpecifier() != TPResult::True()) {
2044 // We'll parse this as a bitfield later.
2045 PossibleBitfield = true;
2046 TPA.Revert();
2047 } else {
2048 // We have a type-specifier-seq.
2049 TPA.Commit();
2050 }
2051 }
2052 } else {
2053 // Consume the ':'.
2054 ConsumeToken();
2055 }
2056
2057 if (!PossibleBitfield) {
2058 SourceRange Range;
2059 BaseType = ParseTypeName(&Range);
2060 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002061 }
2062
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002063 // There are three options here. If we have 'enum foo;', then this is a
2064 // forward declaration. If we have 'enum foo {...' then this is a
2065 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2066 //
2067 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2068 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2069 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2070 //
John McCallf312b1e2010-08-26 23:41:50 +00002071 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002072 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002073 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002074 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002075 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002076 else
John McCallf312b1e2010-08-26 23:41:50 +00002077 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002078
2079 // enums cannot be templates, although they can be referenced from a
2080 // template.
2081 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002082 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002083 Diag(Tok, diag::err_enum_template);
2084
2085 // Skip the rest of this declarator, up until the comma or semicolon.
2086 SkipUntil(tok::comma, true);
2087 return;
2088 }
2089
Douglas Gregor402abb52009-05-28 23:31:59 +00002090 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002091 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002092 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2093 const char *PrevSpec = 0;
2094 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002095 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002096 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002097 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002098 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002099 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002100 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002101
Douglas Gregor48c89f42010-04-24 16:38:41 +00002102 if (IsDependent) {
2103 // This enum has a dependent nested-name-specifier. Handle it as a
2104 // dependent tag.
2105 if (!Name) {
2106 DS.SetTypeSpecError();
2107 Diag(Tok, diag::err_expected_type_name_after_typename);
2108 return;
2109 }
2110
Douglas Gregor23c94db2010-07-02 17:43:08 +00002111 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002112 TUK, SS, Name, StartLoc,
2113 NameLoc);
2114 if (Type.isInvalid()) {
2115 DS.SetTypeSpecError();
2116 return;
2117 }
2118
2119 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallb3d87482010-08-24 05:47:05 +00002120 Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002121 Diag(StartLoc, DiagID) << PrevSpec;
2122
2123 return;
2124 }
Mike Stump1eb44332009-09-09 15:08:12 +00002125
John McCalld226f652010-08-21 09:40:31 +00002126 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002127 // The action failed to produce an enumeration tag. If this is a
2128 // definition, consume the entire definition.
2129 if (Tok.is(tok::l_brace)) {
2130 ConsumeBrace();
2131 SkipUntil(tok::r_brace);
2132 }
2133
2134 DS.SetTypeSpecError();
2135 return;
2136 }
2137
Chris Lattner04d66662007-10-09 17:33:22 +00002138 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002139 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002140
John McCallb3d87482010-08-24 05:47:05 +00002141 // FIXME: The DeclSpec should keep the locations of both the keyword
2142 // and the name (if there is one).
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002143 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCalld226f652010-08-21 09:40:31 +00002144 TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002145 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002146}
2147
2148/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2149/// enumerator-list:
2150/// enumerator
2151/// enumerator-list ',' enumerator
2152/// enumerator:
2153/// enumeration-constant
2154/// enumeration-constant '=' constant-expression
2155/// enumeration-constant:
2156/// identifier
2157///
John McCalld226f652010-08-21 09:40:31 +00002158void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002159 // Enter the scope of the enum body and start the definition.
2160 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002161 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002162
Reid Spencer5f016e22007-07-11 17:01:13 +00002163 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002164
Chris Lattner7946dd32007-08-27 17:24:30 +00002165 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002166 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002167 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002168
John McCalld226f652010-08-21 09:40:31 +00002169 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002170
John McCalld226f652010-08-21 09:40:31 +00002171 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002172
Reid Spencer5f016e22007-07-11 17:01:13 +00002173 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002174 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002175 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2176 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002177
John McCall5b629aa2010-10-22 23:36:17 +00002178 // If attributes exist after the enumerator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002179 ParsedAttributes attrs;
2180 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002181
Reid Spencer5f016e22007-07-11 17:01:13 +00002182 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002183 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002184 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002185 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002186 AssignedVal = ParseConstantExpression();
2187 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002188 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002189 }
Mike Stump1eb44332009-09-09 15:08:12 +00002190
Reid Spencer5f016e22007-07-11 17:01:13 +00002191 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002192 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2193 LastEnumConstDecl,
2194 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002195 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002196 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002197 EnumConstantDecls.push_back(EnumConstDecl);
2198 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002199
Douglas Gregor751f6922010-09-07 14:51:08 +00002200 if (Tok.is(tok::identifier)) {
2201 // We're missing a comma between enumerators.
2202 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2203 Diag(Loc, diag::err_enumerator_list_missing_comma)
2204 << FixItHint::CreateInsertion(Loc, ", ");
2205 continue;
2206 }
2207
Chris Lattner04d66662007-10-09 17:33:22 +00002208 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002209 break;
2210 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002211
2212 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002213 !(getLang().C99 || getLang().CPlusPlus0x))
2214 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2215 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002216 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002217 }
Mike Stump1eb44332009-09-09 15:08:12 +00002218
Reid Spencer5f016e22007-07-11 17:01:13 +00002219 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002220 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002221
Reid Spencer5f016e22007-07-11 17:01:13 +00002222 // If attributes exist after the identifier list, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002223 ParsedAttributes attrs;
2224 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002225
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002226 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2227 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002228 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002229
Douglas Gregor72de6672009-01-08 20:45:30 +00002230 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002231 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002232}
2233
2234/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002235/// start of a type-qualifier-list.
2236bool Parser::isTypeQualifier() const {
2237 switch (Tok.getKind()) {
2238 default: return false;
2239 // type-qualifier
2240 case tok::kw_const:
2241 case tok::kw_volatile:
2242 case tok::kw_restrict:
2243 return true;
2244 }
2245}
2246
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002247/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2248/// is definitely a type-specifier. Return false if it isn't part of a type
2249/// specifier or if we're not sure.
2250bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2251 switch (Tok.getKind()) {
2252 default: return false;
2253 // type-specifiers
2254 case tok::kw_short:
2255 case tok::kw_long:
2256 case tok::kw_signed:
2257 case tok::kw_unsigned:
2258 case tok::kw__Complex:
2259 case tok::kw__Imaginary:
2260 case tok::kw_void:
2261 case tok::kw_char:
2262 case tok::kw_wchar_t:
2263 case tok::kw_char16_t:
2264 case tok::kw_char32_t:
2265 case tok::kw_int:
2266 case tok::kw_float:
2267 case tok::kw_double:
2268 case tok::kw_bool:
2269 case tok::kw__Bool:
2270 case tok::kw__Decimal32:
2271 case tok::kw__Decimal64:
2272 case tok::kw__Decimal128:
2273 case tok::kw___vector:
2274
2275 // struct-or-union-specifier (C99) or class-specifier (C++)
2276 case tok::kw_class:
2277 case tok::kw_struct:
2278 case tok::kw_union:
2279 // enum-specifier
2280 case tok::kw_enum:
2281
2282 // typedef-name
2283 case tok::annot_typename:
2284 return true;
2285 }
2286}
2287
Steve Naroff5f8aa692008-02-11 23:15:56 +00002288/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002289/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002290bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002291 switch (Tok.getKind()) {
2292 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002293
Chris Lattner166a8fc2009-01-04 23:41:41 +00002294 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002295 if (TryAltiVecVectorToken())
2296 return true;
2297 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002298 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002299 // Annotate typenames and C++ scope specifiers. If we get one, just
2300 // recurse to handle whatever we get.
2301 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002302 return true;
2303 if (Tok.is(tok::identifier))
2304 return false;
2305 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002306
Chris Lattner166a8fc2009-01-04 23:41:41 +00002307 case tok::coloncolon: // ::foo::bar
2308 if (NextToken().is(tok::kw_new) || // ::new
2309 NextToken().is(tok::kw_delete)) // ::delete
2310 return false;
2311
Chris Lattner166a8fc2009-01-04 23:41:41 +00002312 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002313 return true;
2314 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002315
Reid Spencer5f016e22007-07-11 17:01:13 +00002316 // GNU attributes support.
2317 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002318 // GNU typeof support.
2319 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002320
Reid Spencer5f016e22007-07-11 17:01:13 +00002321 // type-specifiers
2322 case tok::kw_short:
2323 case tok::kw_long:
2324 case tok::kw_signed:
2325 case tok::kw_unsigned:
2326 case tok::kw__Complex:
2327 case tok::kw__Imaginary:
2328 case tok::kw_void:
2329 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002330 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002331 case tok::kw_char16_t:
2332 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002333 case tok::kw_int:
2334 case tok::kw_float:
2335 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002336 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002337 case tok::kw__Bool:
2338 case tok::kw__Decimal32:
2339 case tok::kw__Decimal64:
2340 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002341 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002342
Chris Lattner99dc9142008-04-13 18:59:07 +00002343 // struct-or-union-specifier (C99) or class-specifier (C++)
2344 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002345 case tok::kw_struct:
2346 case tok::kw_union:
2347 // enum-specifier
2348 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002349
Reid Spencer5f016e22007-07-11 17:01:13 +00002350 // type-qualifier
2351 case tok::kw_const:
2352 case tok::kw_volatile:
2353 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002354
2355 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002356 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002357 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002358
Chris Lattner7c186be2008-10-20 00:25:30 +00002359 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2360 case tok::less:
2361 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002362
Steve Naroff239f0732008-12-25 14:16:32 +00002363 case tok::kw___cdecl:
2364 case tok::kw___stdcall:
2365 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002366 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002367 case tok::kw___w64:
2368 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002369 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002370 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002371 }
2372}
2373
2374/// isDeclarationSpecifier() - Return true if the current token is part of a
2375/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002376///
2377/// \param DisambiguatingWithExpression True to indicate that the purpose of
2378/// this check is to disambiguate between an expression and a declaration.
2379bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002380 switch (Tok.getKind()) {
2381 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002382
Chris Lattner166a8fc2009-01-04 23:41:41 +00002383 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002384 // Unfortunate hack to support "Class.factoryMethod" notation.
2385 if (getLang().ObjC1 && NextToken().is(tok::period))
2386 return false;
John Thompson82287d12010-02-05 00:12:22 +00002387 if (TryAltiVecVectorToken())
2388 return true;
2389 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002390 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002391 // Annotate typenames and C++ scope specifiers. If we get one, just
2392 // recurse to handle whatever we get.
2393 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002394 return true;
2395 if (Tok.is(tok::identifier))
2396 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002397
2398 // If we're in Objective-C and we have an Objective-C class type followed
2399 // by an identifier and then either ':' or ']', in a place where an
2400 // expression is permitted, then this is probably a class message send
2401 // missing the initial '['. In this case, we won't consider this to be
2402 // the start of a declaration.
2403 if (DisambiguatingWithExpression &&
2404 isStartOfObjCClassMessageMissingOpenBracket())
2405 return false;
2406
John McCall9ba61662010-02-26 08:45:28 +00002407 return isDeclarationSpecifier();
2408
Chris Lattner166a8fc2009-01-04 23:41:41 +00002409 case tok::coloncolon: // ::foo::bar
2410 if (NextToken().is(tok::kw_new) || // ::new
2411 NextToken().is(tok::kw_delete)) // ::delete
2412 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002413
Chris Lattner166a8fc2009-01-04 23:41:41 +00002414 // Annotate typenames and C++ scope specifiers. If we get one, just
2415 // recurse to handle whatever we get.
2416 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002417 return true;
2418 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002419
Reid Spencer5f016e22007-07-11 17:01:13 +00002420 // storage-class-specifier
2421 case tok::kw_typedef:
2422 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002423 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002424 case tok::kw_static:
2425 case tok::kw_auto:
2426 case tok::kw_register:
2427 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002428
Reid Spencer5f016e22007-07-11 17:01:13 +00002429 // type-specifiers
2430 case tok::kw_short:
2431 case tok::kw_long:
2432 case tok::kw_signed:
2433 case tok::kw_unsigned:
2434 case tok::kw__Complex:
2435 case tok::kw__Imaginary:
2436 case tok::kw_void:
2437 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002438 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002439 case tok::kw_char16_t:
2440 case tok::kw_char32_t:
2441
Reid Spencer5f016e22007-07-11 17:01:13 +00002442 case tok::kw_int:
2443 case tok::kw_float:
2444 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002445 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002446 case tok::kw__Bool:
2447 case tok::kw__Decimal32:
2448 case tok::kw__Decimal64:
2449 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002450 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002451
Chris Lattner99dc9142008-04-13 18:59:07 +00002452 // struct-or-union-specifier (C99) or class-specifier (C++)
2453 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002454 case tok::kw_struct:
2455 case tok::kw_union:
2456 // enum-specifier
2457 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002458
Reid Spencer5f016e22007-07-11 17:01:13 +00002459 // type-qualifier
2460 case tok::kw_const:
2461 case tok::kw_volatile:
2462 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002463
Reid Spencer5f016e22007-07-11 17:01:13 +00002464 // function-specifier
2465 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002466 case tok::kw_virtual:
2467 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002468
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002469 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002470 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002471
Chris Lattner1ef08762007-08-09 17:01:07 +00002472 // GNU typeof support.
2473 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002474
Chris Lattner1ef08762007-08-09 17:01:07 +00002475 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002476 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002477 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002478
Chris Lattnerf3948c42008-07-26 03:38:44 +00002479 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2480 case tok::less:
2481 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002482
Steve Naroff47f52092009-01-06 19:34:12 +00002483 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002484 case tok::kw___cdecl:
2485 case tok::kw___stdcall:
2486 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002487 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002488 case tok::kw___w64:
2489 case tok::kw___ptr64:
2490 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002491 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002492 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002493 }
2494}
2495
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002496bool Parser::isConstructorDeclarator() {
2497 TentativeParsingAction TPA(*this);
2498
2499 // Parse the C++ scope specifier.
2500 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002501 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00002502 TPA.Revert();
2503 return false;
2504 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002505
2506 // Parse the constructor name.
2507 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2508 // We already know that we have a constructor name; just consume
2509 // the token.
2510 ConsumeToken();
2511 } else {
2512 TPA.Revert();
2513 return false;
2514 }
2515
2516 // Current class name must be followed by a left parentheses.
2517 if (Tok.isNot(tok::l_paren)) {
2518 TPA.Revert();
2519 return false;
2520 }
2521 ConsumeParen();
2522
2523 // A right parentheses or ellipsis signals that we have a constructor.
2524 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2525 TPA.Revert();
2526 return true;
2527 }
2528
2529 // If we need to, enter the specified scope.
2530 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002531 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002532 DeclScopeObj.EnterDeclaratorScope();
2533
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00002534 // Optionally skip Microsoft attributes.
2535 ParsedAttributes Attrs;
2536 MaybeParseMicrosoftAttributes(Attrs);
2537
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002538 // Check whether the next token(s) are part of a declaration
2539 // specifier, in which case we have the start of a parameter and,
2540 // therefore, we know that this is a constructor.
2541 bool IsConstructor = isDeclarationSpecifier();
2542 TPA.Revert();
2543 return IsConstructor;
2544}
Reid Spencer5f016e22007-07-11 17:01:13 +00002545
2546/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00002547/// type-qualifier-list: [C99 6.7.5]
2548/// type-qualifier
2549/// [vendor] attributes
2550/// [ only if VendorAttributesAllowed=true ]
2551/// type-qualifier-list type-qualifier
2552/// [vendor] type-qualifier-list attributes
2553/// [ only if VendorAttributesAllowed=true ]
2554/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2555/// [ only if CXX0XAttributesAllowed=true ]
2556/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00002557///
Dawn Perchik52fc3142010-09-03 01:29:35 +00002558void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2559 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00002560 bool CXX0XAttributesAllowed) {
2561 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2562 SourceLocation Loc = Tok.getLocation();
John McCall7f040a92010-12-24 02:08:15 +00002563 ParsedAttributesWithRange attrs;
2564 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002565 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00002566 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002567 else
2568 Diag(Loc, diag::err_attributes_not_allowed);
2569 }
2570
Reid Spencer5f016e22007-07-11 17:01:13 +00002571 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002572 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002573 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002574 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002575 SourceLocation Loc = Tok.getLocation();
2576
2577 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00002578 case tok::code_completion:
2579 Actions.CodeCompleteTypeQualifiers(DS);
2580 ConsumeCodeCompletionToken();
2581 break;
2582
Reid Spencer5f016e22007-07-11 17:01:13 +00002583 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002584 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2585 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002586 break;
2587 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002588 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2589 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002590 break;
2591 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002592 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2593 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002594 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002595 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002596 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002597 case tok::kw___cdecl:
2598 case tok::kw___stdcall:
2599 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002600 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002601 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00002602 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002603 continue;
2604 }
2605 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002606 case tok::kw___pascal:
2607 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00002608 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002609 continue;
2610 }
2611 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002612 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002613 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00002614 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002615 continue; // do *not* consume the next token!
2616 }
2617 // otherwise, FALL THROUGH!
2618 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002619 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002620 // If this is not a type-qualifier token, we're done reading type
2621 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002622 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002623 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002624 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002625
Reid Spencer5f016e22007-07-11 17:01:13 +00002626 // If the specifier combination wasn't legal, issue a diagnostic.
2627 if (isInvalid) {
2628 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002629 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002630 }
2631 ConsumeToken();
2632 }
2633}
2634
2635
2636/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2637///
2638void Parser::ParseDeclarator(Declarator &D) {
2639 /// This implements the 'declarator' production in the C grammar, then checks
2640 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002641 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002642}
2643
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002644/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2645/// is parsed by the function passed to it. Pass null, and the direct-declarator
2646/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002647/// ptr-operator production.
2648///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002649/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2650/// [C] pointer[opt] direct-declarator
2651/// [C++] direct-declarator
2652/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002653///
2654/// pointer: [C99 6.7.5]
2655/// '*' type-qualifier-list[opt]
2656/// '*' type-qualifier-list[opt] pointer
2657///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002658/// ptr-operator:
2659/// '*' cv-qualifier-seq[opt]
2660/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002661/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002662/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002663/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002664/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002665void Parser::ParseDeclaratorInternal(Declarator &D,
2666 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002667 if (Diags.hasAllExtensionsSilenced())
2668 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002669
Sebastian Redlf30208a2009-01-24 21:16:55 +00002670 // C++ member pointers start with a '::' or a nested-name.
2671 // Member pointers get special handling, since there's no place for the
2672 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002673 if (getLang().CPlusPlus &&
2674 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2675 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002676 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002677 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00002678
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002679 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002680 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002681 // The scope spec really belongs to the direct-declarator.
2682 D.getCXXScopeSpec() = SS;
2683 if (DirectDeclParser)
2684 (this->*DirectDeclParser)(D);
2685 return;
2686 }
2687
2688 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002689 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002690 DeclSpec DS;
2691 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002692 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002693
2694 // Recurse to parse whatever is left.
2695 ParseDeclaratorInternal(D, DirectDeclParser);
2696
2697 // Sema will have to catch (syntactically invalid) pointers into global
2698 // scope. It has to catch pointers into namespace scope anyway.
2699 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall7f040a92010-12-24 02:08:15 +00002700 Loc, DS.takeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002701 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002702 return;
2703 }
2704 }
2705
2706 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002707 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002708 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002709 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002710 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002711 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002712 if (DirectDeclParser)
2713 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002714 return;
2715 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002716
Sebastian Redl05532f22009-03-15 22:02:01 +00002717 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2718 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002719 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002720 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002721
Chris Lattner9af55002009-03-27 04:18:06 +00002722 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002723 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002724 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002725
Reid Spencer5f016e22007-07-11 17:01:13 +00002726 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002727 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002728
Reid Spencer5f016e22007-07-11 17:01:13 +00002729 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002730 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002731 if (Kind == tok::star)
2732 // Remember that we parsed a pointer type, and remember the type-quals.
2733 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
John McCall7f040a92010-12-24 02:08:15 +00002734 DS.takeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002735 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002736 else
2737 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002738 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall7f040a92010-12-24 02:08:15 +00002739 Loc, DS.takeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002740 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002741 } else {
2742 // Is a reference
2743 DeclSpec DS;
2744
Sebastian Redl743de1f2009-03-23 00:00:23 +00002745 // Complain about rvalue references in C++03, but then go on and build
2746 // the declarator.
2747 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
Douglas Gregor16cf8f52011-01-25 02:17:32 +00002748 Diag(Loc, diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00002749
Reid Spencer5f016e22007-07-11 17:01:13 +00002750 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2751 // cv-qualifiers are introduced through the use of a typedef or of a
2752 // template type argument, in which case the cv-qualifiers are ignored.
2753 //
2754 // [GNU] Retricted references are allowed.
2755 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002756 // [C++0x] Attributes on references are not allowed.
2757 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002758 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002759
2760 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2761 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2762 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002763 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002764 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2765 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002766 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002767 }
2768
2769 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002770 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002771
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002772 if (D.getNumTypeObjects() > 0) {
2773 // C++ [dcl.ref]p4: There shall be no references to references.
2774 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2775 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002776 if (const IdentifierInfo *II = D.getIdentifier())
2777 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2778 << II;
2779 else
2780 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2781 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002782
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002783 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002784 // can go ahead and build the (technically ill-formed)
2785 // declarator: reference collapsing will take care of it.
2786 }
2787 }
2788
Reid Spencer5f016e22007-07-11 17:01:13 +00002789 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002790 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
John McCall7f040a92010-12-24 02:08:15 +00002791 DS.takeAttributes(),
Sebastian Redl05532f22009-03-15 22:02:01 +00002792 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002793 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002794 }
2795}
2796
2797/// ParseDirectDeclarator
2798/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002799/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002800/// '(' declarator ')'
2801/// [GNU] '(' attributes declarator ')'
2802/// [C90] direct-declarator '[' constant-expression[opt] ']'
2803/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2804/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2805/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2806/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2807/// direct-declarator '(' parameter-type-list ')'
2808/// direct-declarator '(' identifier-list[opt] ')'
2809/// [GNU] direct-declarator '(' parameter-forward-declarations
2810/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002811/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2812/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002813/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002814///
2815/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002816/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00002817/// '::'[opt] nested-name-specifier[opt] type-name
2818///
2819/// id-expression: [C++ 5.1]
2820/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002821/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002822///
2823/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002824/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002825/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002826/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002827/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002828/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002829///
Reid Spencer5f016e22007-07-11 17:01:13 +00002830void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002831 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002832
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002833 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2834 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002835 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00002836 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00002837 }
2838
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002839 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002840 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00002841 // Change the declaration context for name lookup, until this function
2842 // is exited (and the declarator has been parsed).
2843 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002844 }
2845
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002846 // C++0x [dcl.fct]p14:
2847 // There is a syntactic ambiguity when an ellipsis occurs at the end
2848 // of a parameter-declaration-clause without a preceding comma. In
2849 // this case, the ellipsis is parsed as part of the
2850 // abstract-declarator if the type of the parameter names a template
2851 // parameter pack that has not been expanded; otherwise, it is parsed
2852 // as part of the parameter-declaration-clause.
2853 if (Tok.is(tok::ellipsis) &&
2854 !((D.getContext() == Declarator::PrototypeContext ||
2855 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002856 NextToken().is(tok::r_paren) &&
2857 !Actions.containsUnexpandedParameterPacks(D)))
2858 D.setEllipsisLoc(ConsumeToken());
2859
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002860 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2861 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2862 // We found something that indicates the start of an unqualified-id.
2863 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002864 bool AllowConstructorName;
2865 if (D.getDeclSpec().hasTypeSpecifier())
2866 AllowConstructorName = false;
2867 else if (D.getCXXScopeSpec().isSet())
2868 AllowConstructorName =
2869 (D.getContext() == Declarator::FileContext ||
2870 (D.getContext() == Declarator::MemberContext &&
2871 D.getDeclSpec().isFriendSpecified()));
2872 else
2873 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2874
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002875 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2876 /*EnteringContext=*/true,
2877 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002878 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002879 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002880 D.getName()) ||
2881 // Once we're past the identifier, if the scope was bad, mark the
2882 // whole declarator bad.
2883 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002884 D.SetIdentifier(0, Tok.getLocation());
2885 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002886 } else {
2887 // Parsed the unqualified-id; update range information and move along.
2888 if (D.getSourceRange().getBegin().isInvalid())
2889 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2890 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002891 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002892 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002893 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002894 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002895 assert(!getLang().CPlusPlus &&
2896 "There's a C++-specific check for tok::identifier above");
2897 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2898 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2899 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002900 goto PastIdentifier;
2901 }
2902
2903 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002904 // direct-declarator: '(' declarator ')'
2905 // direct-declarator: '(' attributes declarator ')'
2906 // Example: 'char (*X)' or 'int (*XX)(void)'
2907 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002908
2909 // If the declarator was parenthesized, we entered the declarator
2910 // scope when parsing the parenthesized declarator, then exited
2911 // the scope already. Re-enter the scope, if we need to.
2912 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002913 // If there was an error parsing parenthesized declarator, declarator
2914 // scope may have been enterred before. Don't do it again.
2915 if (!D.isInvalidType() &&
2916 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002917 // Change the declaration context for name lookup, until this function
2918 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002919 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002920 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002921 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002922 // This could be something simple like "int" (in which case the declarator
2923 // portion is empty), if an abstract-declarator is allowed.
2924 D.SetIdentifier(0, Tok.getLocation());
2925 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002926 if (D.getContext() == Declarator::MemberContext)
2927 Diag(Tok, diag::err_expected_member_name_or_semi)
2928 << D.getDeclSpec().getSourceRange();
2929 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002930 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002931 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002932 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002933 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002934 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002935 }
Mike Stump1eb44332009-09-09 15:08:12 +00002936
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002937 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002938 assert(D.isPastIdentifier() &&
2939 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002940
Sean Huntbbd37c62009-11-21 08:43:09 +00002941 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00002942 if (D.getIdentifier())
2943 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00002944
Reid Spencer5f016e22007-07-11 17:01:13 +00002945 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002946 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002947 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2948 // In such a case, check if we actually have a function declarator; if it
2949 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002950 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2951 // When not in file scope, warn for ambiguous function declarators, just
2952 // in case the author intended it as a variable definition.
2953 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2954 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2955 break;
2956 }
John McCall7f040a92010-12-24 02:08:15 +00002957 ParsedAttributes attrs;
2958 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00002959 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002960 ParseBracketDeclarator(D);
2961 } else {
2962 break;
2963 }
2964 }
2965}
2966
Chris Lattneref4715c2008-04-06 05:45:57 +00002967/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2968/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002969/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002970/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2971///
2972/// direct-declarator:
2973/// '(' declarator ')'
2974/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002975/// direct-declarator '(' parameter-type-list ')'
2976/// direct-declarator '(' identifier-list[opt] ')'
2977/// [GNU] direct-declarator '(' parameter-forward-declarations
2978/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002979///
2980void Parser::ParseParenDeclarator(Declarator &D) {
2981 SourceLocation StartLoc = ConsumeParen();
2982 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002983
Chris Lattner7399ee02008-10-20 02:05:46 +00002984 // Eat any attributes before we look at whether this is a grouping or function
2985 // declarator paren. If this is a grouping paren, the attribute applies to
2986 // the type being built up, for example:
2987 // int (__attribute__(()) *x)(long y)
2988 // If this ends up not being a grouping paren, the attribute applies to the
2989 // first argument, for example:
2990 // int (__attribute__(()) int x)
2991 // In either case, we need to eat any attributes to be able to determine what
2992 // sort of paren this is.
2993 //
John McCall7f040a92010-12-24 02:08:15 +00002994 ParsedAttributes attrs;
Chris Lattner7399ee02008-10-20 02:05:46 +00002995 bool RequiresArg = false;
2996 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00002997 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002998
Chris Lattner7399ee02008-10-20 02:05:46 +00002999 // We require that the argument list (if this is a non-grouping paren) be
3000 // present even if the attribute list was empty.
3001 RequiresArg = true;
3002 }
Steve Naroff239f0732008-12-25 14:16:32 +00003003 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00003004 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003005 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3006 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00003007 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00003008 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003009 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003010 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00003011 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003012
Chris Lattneref4715c2008-04-06 05:45:57 +00003013 // If we haven't past the identifier yet (or where the identifier would be
3014 // stored, if this is an abstract declarator), then this is probably just
3015 // grouping parens. However, if this could be an abstract-declarator, then
3016 // this could also be the start of function arguments (consider 'void()').
3017 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003018
Chris Lattneref4715c2008-04-06 05:45:57 +00003019 if (!D.mayOmitIdentifier()) {
3020 // If this can't be an abstract-declarator, this *must* be a grouping
3021 // paren, because we haven't seen the identifier yet.
3022 isGrouping = true;
3023 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003024 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003025 isDeclarationSpecifier()) { // 'int(int)' is a function.
3026 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3027 // considered to be a type, not a K&R identifier-list.
3028 isGrouping = false;
3029 } else {
3030 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3031 isGrouping = true;
3032 }
Mike Stump1eb44332009-09-09 15:08:12 +00003033
Chris Lattneref4715c2008-04-06 05:45:57 +00003034 // If this is a grouping paren, handle:
3035 // direct-declarator: '(' declarator ')'
3036 // direct-declarator: '(' attributes declarator ')'
3037 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003038 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003039 D.setGroupingParens(true);
John McCall7f040a92010-12-24 02:08:15 +00003040 if (!attrs.empty())
3041 D.addAttributes(attrs.getList(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003042
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003043 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003044 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003045 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
3046 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc), EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003047
3048 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003049 return;
3050 }
Mike Stump1eb44332009-09-09 15:08:12 +00003051
Chris Lattneref4715c2008-04-06 05:45:57 +00003052 // Okay, if this wasn't a grouping paren, it must be the start of a function
3053 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003054 // identifier (and remember where it would have been), then call into
3055 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003056 D.SetIdentifier(0, Tok.getLocation());
3057
John McCall7f040a92010-12-24 02:08:15 +00003058 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003059}
3060
3061/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3062/// declarator D up to a paren, which indicates that we are parsing function
3063/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003064///
Chris Lattner7399ee02008-10-20 02:05:46 +00003065/// If AttrList is non-null, then the caller parsed those arguments immediately
3066/// after the open paren - they should be considered to be the first argument of
3067/// a parameter. If RequiresArg is true, then the first argument of the
3068/// function is required to be present and required to not be an identifier
3069/// list.
3070///
Reid Spencer5f016e22007-07-11 17:01:13 +00003071/// This method also handles this portion of the grammar:
3072/// parameter-type-list: [C99 6.7.5]
3073/// parameter-list
3074/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003075/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003076///
3077/// parameter-list: [C99 6.7.5]
3078/// parameter-declaration
3079/// parameter-list ',' parameter-declaration
3080///
3081/// parameter-declaration: [C99 6.7.5]
3082/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003083/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003084/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003085/// declaration-specifiers abstract-declarator[opt]
3086/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003087/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003088/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3089///
Douglas Gregor83f51722011-01-26 03:43:54 +00003090/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]",
3091/// C++0x "ref-qualifier[opt]" and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003092///
Chris Lattner7399ee02008-10-20 02:05:46 +00003093void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall7f040a92010-12-24 02:08:15 +00003094 ParsedAttributes &attrs,
Chris Lattner7399ee02008-10-20 02:05:46 +00003095 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003096 // lparen is already consumed!
3097 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003098
Douglas Gregordab60ad2010-10-01 18:44:50 +00003099 ParsedType TrailingReturnType;
3100
Chris Lattner7399ee02008-10-20 02:05:46 +00003101 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003102 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003103 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003104 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003105
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003106 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3107 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003108
3109 // cv-qualifier-seq[opt].
3110 DeclSpec DS;
Douglas Gregor83f51722011-01-26 03:43:54 +00003111 SourceLocation RefQualifierLoc;
3112 bool RefQualifierIsLValueRef = true;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003113 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003114 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003115 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003116 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003117 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003118 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003119 MaybeParseCXX0XAttributes(attrs);
3120
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003121 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003122 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003123 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003124
Douglas Gregor83f51722011-01-26 03:43:54 +00003125 // Parse ref-qualifier[opt]
3126 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3127 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003128 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor83f51722011-01-26 03:43:54 +00003129
3130 RefQualifierIsLValueRef = Tok.is(tok::amp);
3131 RefQualifierLoc = ConsumeToken();
3132 EndLoc = RefQualifierLoc;
3133 }
3134
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003135 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003136 if (Tok.is(tok::kw_throw)) {
3137 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003138 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003139 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003140 hasAnyExceptionSpec);
3141 assert(Exceptions.size() == ExceptionRanges.size() &&
3142 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003143 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00003144
3145 // Parse trailing-return-type.
3146 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3147 TrailingReturnType = ParseTrailingReturnType().get();
3148 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003149 }
3150
Chris Lattnerf97409f2008-04-06 06:57:35 +00003151 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003152 // int() -> no prototype, no '...'.
John McCall7f040a92010-12-24 02:08:15 +00003153 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3154 /*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003155 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003156 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003157 /*arglist*/ 0, 0,
3158 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003159 RefQualifierIsLValueRef,
3160 RefQualifierLoc,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003161 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003162 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003163 Exceptions.data(),
3164 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003165 Exceptions.size(),
Douglas Gregordab60ad2010-10-01 18:44:50 +00003166 LParenLoc, RParenLoc, D,
3167 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003168 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003169 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003170 }
3171
Chris Lattner7399ee02008-10-20 02:05:46 +00003172 // Alternatively, this parameter list may be an identifier list form for a
3173 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003174 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3175 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003176 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003177 // K&R identifier lists can't have typedefs as identifiers, per
3178 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003179 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003180 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003181
Steve Naroff2d081c42009-01-28 19:16:40 +00003182 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003183 // normal declarators, not for abstract-declarators. Get the first
3184 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003185 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003186 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003187
3188 // Identifier lists follow a really simple grammar: the identifiers can
3189 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3190 // identifier lists are really rare in the brave new modern world, and it
3191 // is very common for someone to typo a type in a non-k&r style list. If
3192 // we are presented with something like: "void foo(intptr x, float y)",
3193 // we don't want to start parsing the function declarator as though it is
3194 // a K&R style declarator just because intptr is an invalid type.
3195 //
3196 // To handle this, we check to see if the token after the first identifier
3197 // is a "," or ")". Only if so, do we parse it as an identifier list.
3198 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3199 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3200 FirstTok.getIdentifierInfo(),
3201 FirstTok.getLocation(), D);
3202
3203 // If we get here, the code is invalid. Push the first identifier back
3204 // into the token stream and parse the first argument as an (invalid)
3205 // normal argument declarator.
3206 PP.EnterToken(Tok);
3207 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003208 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003209 }
Mike Stump1eb44332009-09-09 15:08:12 +00003210
Chris Lattnerf97409f2008-04-06 06:57:35 +00003211 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003212
Chris Lattnerf97409f2008-04-06 06:57:35 +00003213 // Build up an array of information about the parsed arguments.
3214 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003215
3216 // Enter function-declaration scope, limiting any declarators to the
3217 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003218 ParseScope PrototypeScope(this,
3219 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003220
Chris Lattnerf97409f2008-04-06 06:57:35 +00003221 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003222 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003223 while (1) {
3224 if (Tok.is(tok::ellipsis)) {
3225 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003226 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003227 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003228 }
Mike Stump1eb44332009-09-09 15:08:12 +00003229
Chris Lattnerf97409f2008-04-06 06:57:35 +00003230 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003231 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003232 DeclSpec DS;
John McCall7f040a92010-12-24 02:08:15 +00003233
3234 // Skip any Microsoft attributes before a param.
3235 if (getLang().Microsoft && Tok.is(tok::l_square))
3236 ParseMicrosoftAttributes(DS.getAttributes());
3237
3238 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003239
3240 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003241 // Take them so that we only apply the attributes to the first parameter.
3242 DS.takeAttributesFrom(attrs);
3243
Chris Lattnere64c5492009-02-27 18:38:20 +00003244 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003245
Chris Lattnerf97409f2008-04-06 06:57:35 +00003246 // Parse the declarator. This is "PrototypeContext", because we must
3247 // accept either 'declarator' or 'abstract-declarator' here.
3248 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3249 ParseDeclarator(ParmDecl);
3250
3251 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00003252 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003253
Chris Lattnerf97409f2008-04-06 06:57:35 +00003254 // Remember this parsed parameter in ParamInfo.
3255 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003256
Douglas Gregor72b505b2008-12-16 21:30:33 +00003257 // DefArgToks is used when the parsing of default arguments needs
3258 // to be delayed.
3259 CachedTokens *DefArgToks = 0;
3260
Chris Lattnerf97409f2008-04-06 06:57:35 +00003261 // If no parameter was specified, verify that *something* was specified,
3262 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003263 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3264 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003265 // Completely missing, emit error.
3266 Diag(DSStart, diag::err_missing_param);
3267 } else {
3268 // Otherwise, we have something. Add it and let semantic analysis try
3269 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003270
Chris Lattnerf97409f2008-04-06 06:57:35 +00003271 // Inform the actions module about the parameter declarator, so it gets
3272 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003273 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003274
3275 // Parse the default argument, if any. We parse the default
3276 // arguments in all dialects; the semantic analysis in
3277 // ActOnParamDefaultArgument will reject the default argument in
3278 // C.
3279 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003280 SourceLocation EqualLoc = Tok.getLocation();
3281
Chris Lattner04421082008-04-08 04:40:51 +00003282 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003283 if (D.getContext() == Declarator::MemberContext) {
3284 // If we're inside a class definition, cache the tokens
3285 // corresponding to the default argument. We'll actually parse
3286 // them when we see the end of the class definition.
3287 // FIXME: Templates will require something similar.
3288 // FIXME: Can we use a smart pointer for Toks?
3289 DefArgToks = new CachedTokens;
3290
Mike Stump1eb44332009-09-09 15:08:12 +00003291 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003292 /*StopAtSemi=*/true,
3293 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003294 delete DefArgToks;
3295 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003296 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003297 } else {
3298 // Mark the end of the default argument so that we know when to
3299 // stop when we parse it later on.
3300 Token DefArgEnd;
3301 DefArgEnd.startToken();
3302 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3303 DefArgEnd.setLocation(Tok.getLocation());
3304 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003305 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003306 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003307 }
Chris Lattner04421082008-04-08 04:40:51 +00003308 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003309 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003310 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003311
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003312 // The argument isn't actually potentially evaluated unless it is
3313 // used.
3314 EnterExpressionEvaluationContext Eval(Actions,
3315 Sema::PotentiallyEvaluatedIfUsed);
3316
John McCall60d7b3a2010-08-24 06:29:42 +00003317 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003318 if (DefArgResult.isInvalid()) {
3319 Actions.ActOnParamDefaultArgumentError(Param);
3320 SkipUntil(tok::comma, tok::r_paren, true, true);
3321 } else {
3322 // Inform the actions module about the default argument
3323 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003324 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003325 }
Chris Lattner04421082008-04-08 04:40:51 +00003326 }
3327 }
Mike Stump1eb44332009-09-09 15:08:12 +00003328
3329 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3330 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003331 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003332 }
3333
3334 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003335 if (Tok.isNot(tok::comma)) {
3336 if (Tok.is(tok::ellipsis)) {
3337 IsVariadic = true;
3338 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3339
3340 if (!getLang().CPlusPlus) {
3341 // We have ellipsis without a preceding ',', which is ill-formed
3342 // in C. Complain and provide the fix.
3343 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003344 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003345 }
3346 }
3347
3348 break;
3349 }
Mike Stump1eb44332009-09-09 15:08:12 +00003350
Chris Lattnerf97409f2008-04-06 06:57:35 +00003351 // Consume the comma.
3352 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003353 }
Mike Stump1eb44332009-09-09 15:08:12 +00003354
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003355 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003356 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3357 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003358
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003359 DeclSpec DS;
Douglas Gregor83f51722011-01-26 03:43:54 +00003360 SourceLocation RefQualifierLoc;
3361 bool RefQualifierIsLValueRef = true;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003362 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003363 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003364 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003365 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003366 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003367
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003368 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003369 MaybeParseCXX0XAttributes(attrs);
3370
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003371 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003372 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003373 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003374 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003375
Douglas Gregor83f51722011-01-26 03:43:54 +00003376 // Parse ref-qualifier[opt]
3377 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
3378 if (!getLang().CPlusPlus0x)
Douglas Gregor1f381062011-01-26 20:35:32 +00003379 Diag(Tok, diag::ext_ref_qualifier);
Douglas Gregor83f51722011-01-26 03:43:54 +00003380
3381 RefQualifierIsLValueRef = Tok.is(tok::amp);
3382 RefQualifierLoc = ConsumeToken();
3383 EndLoc = RefQualifierLoc;
3384 }
3385
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003386 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003387 if (Tok.is(tok::kw_throw)) {
3388 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003389 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003390 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003391 hasAnyExceptionSpec);
3392 assert(Exceptions.size() == ExceptionRanges.size() &&
3393 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003394 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00003395
3396 // Parse trailing-return-type.
3397 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3398 TrailingReturnType = ParseTrailingReturnType().get();
3399 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003400 }
3401
Douglas Gregordab60ad2010-10-01 18:44:50 +00003402 // FIXME: We should leave the prototype scope before parsing the exception
3403 // specification, and then reenter it when parsing the trailing return type.
3404
3405 // Leave prototype scope.
3406 PrototypeScope.Exit();
3407
Reid Spencer5f016e22007-07-11 17:01:13 +00003408 // Remember that we parsed a function type, and remember the attributes.
John McCall7f040a92010-12-24 02:08:15 +00003409 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3410 /*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003411 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003412 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003413 DS.getTypeQualifiers(),
Douglas Gregor83f51722011-01-26 03:43:54 +00003414 RefQualifierIsLValueRef,
3415 RefQualifierLoc,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003416 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003417 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003418 Exceptions.data(),
3419 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003420 Exceptions.size(),
Douglas Gregordab60ad2010-10-01 18:44:50 +00003421 LParenLoc, RParenLoc, D,
3422 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003423 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003424}
3425
Chris Lattner66d28652008-04-06 06:34:08 +00003426/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3427/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003428/// first identifier has already been consumed, and the current token is the
3429/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003430///
3431/// identifier-list: [C99 6.7.5]
3432/// identifier
3433/// identifier-list ',' identifier
3434///
3435void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003436 IdentifierInfo *FirstIdent,
3437 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003438 Declarator &D) {
3439 // Build up an array of information about the parsed arguments.
3440 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3441 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003442
Chris Lattner66d28652008-04-06 06:34:08 +00003443 // If there was no identifier specified for the declarator, either we are in
3444 // an abstract-declarator, or we are in a parameter declarator which was found
3445 // to be abstract. In abstract-declarators, identifier lists are not valid:
3446 // diagnose this.
3447 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003448 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003449
Chris Lattner83a94472010-05-14 17:23:36 +00003450 // The first identifier was already read, and is known to be the first
3451 // identifier in the list. Remember this identifier in ParamInfo.
3452 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003453 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003454
Chris Lattner66d28652008-04-06 06:34:08 +00003455 while (Tok.is(tok::comma)) {
3456 // Eat the comma.
3457 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003458
Chris Lattner50c64772008-04-06 06:39:19 +00003459 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003460 if (Tok.isNot(tok::identifier)) {
3461 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003462 SkipUntil(tok::r_paren);
3463 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003464 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003465
Chris Lattner66d28652008-04-06 06:34:08 +00003466 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003467
3468 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003469 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003470 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003471
Chris Lattner66d28652008-04-06 06:34:08 +00003472 // Verify that the argument identifier has not already been mentioned.
3473 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003474 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003475 } else {
3476 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003477 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003478 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00003479 0));
Chris Lattner50c64772008-04-06 06:39:19 +00003480 }
Mike Stump1eb44332009-09-09 15:08:12 +00003481
Chris Lattner66d28652008-04-06 06:34:08 +00003482 // Eat the identifier.
3483 ConsumeToken();
3484 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003485
3486 // If we have the closing ')', eat it and we're done.
3487 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3488
Chris Lattner50c64772008-04-06 06:39:19 +00003489 // Remember that we parsed a function type, and remember the attributes. This
3490 // function type is always a K&R style function type, which is not varargs and
3491 // has no prototype.
John McCall7f040a92010-12-24 02:08:15 +00003492 D.AddTypeInfo(DeclaratorChunk::getFunction(ParsedAttributes(),
3493 /*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003494 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003495 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003496 /*TypeQuals*/0,
Douglas Gregor83f51722011-01-26 03:43:54 +00003497 true, SourceLocation(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003498 /*exception*/false,
3499 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003500 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003501 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003502}
Chris Lattneref4715c2008-04-06 05:45:57 +00003503
Reid Spencer5f016e22007-07-11 17:01:13 +00003504/// [C90] direct-declarator '[' constant-expression[opt] ']'
3505/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3506/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3507/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3508/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3509void Parser::ParseBracketDeclarator(Declarator &D) {
3510 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003511
Chris Lattner378c7e42008-12-18 07:27:21 +00003512 // C array syntax has many features, but by-far the most common is [] and [4].
3513 // This code does a fast path to handle some of the most obvious cases.
3514 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003515 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall7f040a92010-12-24 02:08:15 +00003516 ParsedAttributes attrs;
3517 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003518
Chris Lattner378c7e42008-12-18 07:27:21 +00003519 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00003520 ExprResult NumElements;
John McCall7f040a92010-12-24 02:08:15 +00003521 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003522 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003523 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003524 return;
3525 } else if (Tok.getKind() == tok::numeric_constant &&
3526 GetLookAheadToken(1).is(tok::r_square)) {
3527 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00003528 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003529 ConsumeToken();
3530
Sebastian Redlab197ba2009-02-09 18:23:29 +00003531 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall7f040a92010-12-24 02:08:15 +00003532 ParsedAttributes attrs;
3533 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003534
Chris Lattner378c7e42008-12-18 07:27:21 +00003535 // Remember that we parsed a array type, and remember its features.
John McCall7f040a92010-12-24 02:08:15 +00003536 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, 0,
3537 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003538 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003539 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003540 return;
3541 }
Mike Stump1eb44332009-09-09 15:08:12 +00003542
Reid Spencer5f016e22007-07-11 17:01:13 +00003543 // If valid, this location is the position where we read the 'static' keyword.
3544 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003545 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003546 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003547
Reid Spencer5f016e22007-07-11 17:01:13 +00003548 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003549 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003550 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003551 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003552
Reid Spencer5f016e22007-07-11 17:01:13 +00003553 // If we haven't already read 'static', check to see if there is one after the
3554 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003555 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003556 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003557
Reid Spencer5f016e22007-07-11 17:01:13 +00003558 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3559 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00003560 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00003561
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003562 // Handle the case where we have '[*]' as the array size. However, a leading
3563 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3564 // the the token after the star is a ']'. Since stars in arrays are
3565 // infrequent, use of lookahead is not costly here.
3566 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003567 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003568
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003569 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003570 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003571 StaticLoc = SourceLocation(); // Drop the static.
3572 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003573 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003574 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003575 // Note, in C89, this production uses the constant-expr production instead
3576 // of assignment-expr. The only difference is that assignment-expr allows
3577 // things like '=' and '*='. Sema rejects these in C89 mode because they
3578 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003579
Douglas Gregore0762c92009-06-19 23:52:42 +00003580 // Parse the constant-expression or assignment-expression now (depending
3581 // on dialect).
3582 if (getLang().CPlusPlus)
3583 NumElements = ParseConstantExpression();
3584 else
3585 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003586 }
Mike Stump1eb44332009-09-09 15:08:12 +00003587
Reid Spencer5f016e22007-07-11 17:01:13 +00003588 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003589 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003590 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003591 // If the expression was invalid, skip it.
3592 SkipUntil(tok::r_square);
3593 return;
3594 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003595
3596 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3597
John McCall7f040a92010-12-24 02:08:15 +00003598 ParsedAttributes attrs;
3599 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003600
Chris Lattner378c7e42008-12-18 07:27:21 +00003601 // Remember that we parsed a array type, and remember its features.
John McCall7f040a92010-12-24 02:08:15 +00003602 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), attrs,
Reid Spencer5f016e22007-07-11 17:01:13 +00003603 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003604 NumElements.release(),
3605 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003606 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003607}
3608
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003609/// [GNU] typeof-specifier:
3610/// typeof ( expressions )
3611/// typeof ( type-name )
3612/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003613///
3614void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003615 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003616 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003617 SourceLocation StartLoc = ConsumeToken();
3618
John McCallcfb708c2010-01-13 20:03:27 +00003619 const bool hasParens = Tok.is(tok::l_paren);
3620
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003621 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00003622 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003623 SourceRange CastRange;
John McCall60d7b3a2010-08-24 06:29:42 +00003624 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall911093e2010-08-25 02:45:51 +00003625 isCastExpr,
3626 CastTy,
3627 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003628 if (hasParens)
3629 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003630
3631 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003632 // FIXME: Not accurate, the range gets one token more than it should.
3633 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003634 else
3635 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003636
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003637 if (isCastExpr) {
3638 if (!CastTy) {
3639 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003640 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003641 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003642
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003643 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003644 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003645 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3646 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003647 DiagID, CastTy))
3648 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003649 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003650 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003651
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003652 // If we get here, the operand to the typeof was an expresion.
3653 if (Operand.isInvalid()) {
3654 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003655 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003656 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003657
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003658 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003659 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003660 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3661 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00003662 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00003663 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003664}
Chris Lattner1b492422010-02-28 18:33:55 +00003665
3666
3667/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3668/// from TryAltiVecVectorToken.
3669bool Parser::TryAltiVecVectorTokenOutOfLine() {
3670 Token Next = NextToken();
3671 switch (Next.getKind()) {
3672 default: return false;
3673 case tok::kw_short:
3674 case tok::kw_long:
3675 case tok::kw_signed:
3676 case tok::kw_unsigned:
3677 case tok::kw_void:
3678 case tok::kw_char:
3679 case tok::kw_int:
3680 case tok::kw_float:
3681 case tok::kw_double:
3682 case tok::kw_bool:
3683 case tok::kw___pixel:
3684 Tok.setKind(tok::kw___vector);
3685 return true;
3686 case tok::identifier:
3687 if (Next.getIdentifierInfo() == Ident_pixel) {
3688 Tok.setKind(tok::kw___vector);
3689 return true;
3690 }
3691 return false;
3692 }
3693}
3694
3695bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3696 const char *&PrevSpec, unsigned &DiagID,
3697 bool &isInvalid) {
3698 if (Tok.getIdentifierInfo() == Ident_vector) {
3699 Token Next = NextToken();
3700 switch (Next.getKind()) {
3701 case tok::kw_short:
3702 case tok::kw_long:
3703 case tok::kw_signed:
3704 case tok::kw_unsigned:
3705 case tok::kw_void:
3706 case tok::kw_char:
3707 case tok::kw_int:
3708 case tok::kw_float:
3709 case tok::kw_double:
3710 case tok::kw_bool:
3711 case tok::kw___pixel:
3712 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3713 return true;
3714 case tok::identifier:
3715 if (Next.getIdentifierInfo() == Ident_pixel) {
3716 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3717 return true;
3718 }
3719 break;
3720 default:
3721 break;
3722 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003723 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003724 DS.isTypeAltiVecVector()) {
3725 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3726 return true;
3727 }
3728 return false;
3729}
3730