blob: 8e334c3069742e3cf89092ac79229ae1a4fb01bc [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++.
John McCallf312b1e2010-08-26 23:41:50 +000032TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 // Parse the common declaration-specifiers piece.
34 DeclSpec DS;
35 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000036
Reid Spencer5f016e22007-07-11 17:01:13 +000037 // Parse the abstract-declarator, if present.
38 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
39 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000040 if (Range)
41 *Range = DeclaratorInfo.getSourceRange();
42
Chris Lattnereaaebc72009-04-25 08:06:05 +000043 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000044 return true;
45
Douglas Gregor23c94db2010-07-02 17:43:08 +000046 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000047}
48
Sean Huntbbd37c62009-11-21 08:43:09 +000049/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000050///
51/// [GNU] attributes:
52/// attribute
53/// attributes attribute
54///
55/// [GNU] attribute:
56/// '__attribute__' '(' '(' attribute-list ')' ')'
57///
58/// [GNU] attribute-list:
59/// attrib
60/// attribute_list ',' attrib
61///
62/// [GNU] attrib:
63/// empty
64/// attrib-name
65/// attrib-name '(' identifier ')'
66/// attrib-name '(' identifier ',' nonempty-expr-list ')'
67/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
68///
69/// [GNU] attrib-name:
70/// identifier
71/// typespec
72/// typequal
73/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000074///
Reid Spencer5f016e22007-07-11 17:01:13 +000075/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000076/// token lookahead. Comment from gcc: "If they start with an identifier
77/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000078/// start with that identifier; otherwise they are an expression list."
79///
80/// At the moment, I am not doing 2 token lookahead. I am also unaware of
81/// any attributes that don't work (based on my limited testing). Most
82/// attributes are very simple in practice. Until we find a bug, I don't see
83/// a pressing need to implement the 2 token lookahead.
84
John McCall7f040a92010-12-24 02:08:15 +000085void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
86 SourceLocation *endLoc) {
Sean Huntbbd37c62009-11-21 08:43:09 +000087 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner04d66662007-10-09 17:33:22 +000089 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000090 ConsumeToken();
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
92 "attribute")) {
93 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +000094 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000095 }
96 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
97 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +000098 return;
Reid Spencer5f016e22007-07-11 17:01:13 +000099 }
100 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000101 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
102 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000103
104 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
106 ConsumeToken();
107 continue;
108 }
109 // we have an identifier or declaration specifier (const, int, etc.)
110 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
111 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000113 // check if we have a "parameterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000114 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattner04d66662007-10-09 17:33:22 +0000117 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
119 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000120
121 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 // __attribute__(( mode(byte) ))
123 ConsumeParen(); // ignore the right paren loc for now
John McCall7f040a92010-12-24 02:08:15 +0000124 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
125 ParmName, ParmLoc, 0, 0));
Chris Lattner04d66662007-10-09 17:33:22 +0000126 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 ConsumeToken();
128 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000129 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 // now parse the non-empty comma separated list of expressions
133 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000134 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000135 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 ArgExprsOk = false;
137 SkipUntil(tok::r_paren);
138 break;
139 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000140 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 }
Chris Lattner04d66662007-10-09 17:33:22 +0000142 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 break;
144 ConsumeToken(); // Eat the comma, move to the next argument
145 }
Chris Lattner04d66662007-10-09 17:33:22 +0000146 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 ConsumeParen(); // ignore the right paren loc for now
John McCall7f040a92010-12-24 02:08:15 +0000148 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0,
149 AttrNameLoc, ParmName, ParmLoc,
150 ArgExprs.take(), ArgExprs.size()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 }
152 }
153 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000154 switch (Tok.getKind()) {
155 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // __attribute__(( nonnull() ))
158 ConsumeParen(); // ignore the right paren loc for now
John McCall7f040a92010-12-24 02:08:15 +0000159 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
160 0, SourceLocation(), 0, 0));
Nate Begeman6f3d8382009-06-26 06:32:41 +0000161 break;
162 case tok::kw_char:
163 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000164 case tok::kw_char16_t:
165 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000166 case tok::kw_bool:
167 case tok::kw_short:
168 case tok::kw_int:
169 case tok::kw_long:
170 case tok::kw_signed:
171 case tok::kw_unsigned:
172 case tok::kw_float:
173 case tok::kw_double:
174 case tok::kw_void:
John McCall7f040a92010-12-24 02:08:15 +0000175 case tok::kw_typeof: {
176 AttributeList *attr
177 = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
178 0, SourceLocation(), 0, 0);
179 attrs.add(attr);
180 if (attr->getKind() == AttributeList::AT_IBOutletCollection)
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000181 Diag(Tok, diag::err_iboutletcollection_builtintype);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000182 // If it's a builtin type name, eat it and expect a rparen
183 // __attribute__(( vec_type_hint(char) ))
184 ConsumeToken();
Nate Begeman6f3d8382009-06-26 06:32:41 +0000185 if (Tok.is(tok::r_paren))
186 ConsumeParen();
187 break;
John McCall7f040a92010-12-24 02:08:15 +0000188 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000189 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000191 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 // now parse the list of expressions
195 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000196 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000197 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 ArgExprsOk = false;
199 SkipUntil(tok::r_paren);
200 break;
201 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000202 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 }
Chris Lattner04d66662007-10-09 17:33:22 +0000204 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 break;
206 ConsumeToken(); // Eat the comma, move to the next argument
207 }
208 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000209 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 ConsumeParen(); // ignore the right paren loc for now
John McCall7f040a92010-12-24 02:08:15 +0000211 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0,
212 AttrNameLoc, 0, SourceLocation(),
213 ArgExprs.take(), ArgExprs.size()));
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000215 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 }
217 }
218 } else {
John McCall7f040a92010-12-24 02:08:15 +0000219 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
220 0, SourceLocation(), 0, 0));
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 }
222 }
223 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000225 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000226 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
227 SkipUntil(tok::r_paren, false);
228 }
John McCall7f040a92010-12-24 02:08:15 +0000229 if (endLoc)
230 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000232}
233
Eli Friedmana23b4852009-06-08 07:21:15 +0000234/// ParseMicrosoftDeclSpec - Parse an __declspec construct
235///
236/// [MS] decl-specifier:
237/// __declspec ( extended-decl-modifier-seq )
238///
239/// [MS] extended-decl-modifier-seq:
240/// extended-decl-modifier[opt]
241/// extended-decl-modifier extended-decl-modifier-seq
242
John McCall7f040a92010-12-24 02:08:15 +0000243void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000244 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000245
Steve Narofff59e17e2008-12-24 20:59:21 +0000246 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000247 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
248 "declspec")) {
249 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000250 return;
Eli Friedmana23b4852009-06-08 07:21:15 +0000251 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000252 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000253 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
254 SourceLocation AttrNameLoc = ConsumeToken();
255 if (Tok.is(tok::l_paren)) {
256 ConsumeParen();
257 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
258 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000259 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000260 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000261 Expr *ExprList = ArgExpr.take();
John McCall7f040a92010-12-24 02:08:15 +0000262 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
263 SourceLocation(), &ExprList, 1, true));
Eli Friedmana23b4852009-06-08 07:21:15 +0000264 }
265 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
266 SkipUntil(tok::r_paren, false);
267 } else {
John McCall7f040a92010-12-24 02:08:15 +0000268 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
269 0, SourceLocation(), 0, 0, true));
Eli Friedmana23b4852009-06-08 07:21:15 +0000270 }
271 }
272 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
273 SkipUntil(tok::r_paren, false);
John McCall7f040a92010-12-24 02:08:15 +0000274 return;
Eli Friedman290eeb02009-06-08 23:27:34 +0000275}
276
John McCall7f040a92010-12-24 02:08:15 +0000277void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000278 // Treat these like attributes
279 // FIXME: Allow Sema to distinguish between these and real attributes!
280 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000281 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
282 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000283 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
284 SourceLocation AttrNameLoc = ConsumeToken();
285 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
286 // FIXME: Support these properly!
287 continue;
John McCall7f040a92010-12-24 02:08:15 +0000288 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
289 SourceLocation(), 0, 0, true));
Eli Friedman290eeb02009-06-08 23:27:34 +0000290 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000291}
292
John McCall7f040a92010-12-24 02:08:15 +0000293void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000294 // Treat these like attributes
295 while (Tok.is(tok::kw___pascal)) {
296 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
297 SourceLocation AttrNameLoc = ConsumeToken();
John McCall7f040a92010-12-24 02:08:15 +0000298 attrs.add(AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
299 SourceLocation(), 0, 0, true));
Dawn Perchik52fc3142010-09-03 01:29:35 +0000300 }
John McCall7f040a92010-12-24 02:08:15 +0000301}
302
303void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
304 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
305 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +0000306}
307
Reid Spencer5f016e22007-07-11 17:01:13 +0000308/// ParseDeclaration - Parse a full 'declaration', which consists of
309/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000310/// 'Context' should be a Declarator::TheContext value. This returns the
311/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000312///
313/// declaration: [C99 6.7]
314/// block-declaration ->
315/// simple-declaration
316/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000317/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000318/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000319/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000320/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000321/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000322/// others... [FIXME]
323///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000324Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
325 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000326 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000327 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000328 ParenBraceBracketBalancer BalancerRAIIObj(*this);
329
John McCalld226f652010-08-21 09:40:31 +0000330 Decl *SingleDecl = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000331 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000332 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000333 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +0000334 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000335 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000336 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000337 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000338 // Could be the start of an inline namespace. Allowed as an ext in C++03.
339 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +0000340 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +0000341 SourceLocation InlineLoc = ConsumeToken();
342 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
343 break;
344 }
John McCall7f040a92010-12-24 02:08:15 +0000345 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000346 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000347 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +0000348 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000349 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000350 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000351 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000352 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
John McCall7f040a92010-12-24 02:08:15 +0000353 DeclEnd, attrs);
Chris Lattner682bf922009-03-29 16:50:03 +0000354 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000355 case tok::kw_static_assert:
John McCall7f040a92010-12-24 02:08:15 +0000356 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +0000357 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000358 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000359 default:
John McCall7f040a92010-12-24 02:08:15 +0000360 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000361 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000362
Chris Lattner682bf922009-03-29 16:50:03 +0000363 // This routine returns a DeclGroup, if the thing we parsed only contains a
364 // single decl, convert it now.
365 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000366}
367
368/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
369/// declaration-specifiers init-declarator-list[opt] ';'
370///[C90/C++]init-declarator-list ';' [TODO]
371/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000372///
373/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000374/// declaration. If it is true, it checks for and eats it.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000375Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
376 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000377 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +0000378 ParsedAttributes &attrs,
Chris Lattner5c5db552010-04-05 18:18:31 +0000379 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000381 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +0000382 DS.takeAttributesFrom(attrs);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000383 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
384 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000385 StmtResult R = Actions.ActOnVlaStmt(DS);
386 if (R.isUsable())
387 Stmts.push_back(R.release());
Mike Stump1eb44332009-09-09 15:08:12 +0000388
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
390 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000391 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000392 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000393 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallaec03712010-05-21 20:45:30 +0000394 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000395 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000396 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 }
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Chris Lattner5c5db552010-04-05 18:18:31 +0000399 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000400}
Mike Stump1eb44332009-09-09 15:08:12 +0000401
John McCalld8ac0572009-11-03 19:26:08 +0000402/// ParseDeclGroup - Having concluded that this is either a function
403/// definition or a group of object declarations, actually parse the
404/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000405Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
406 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000407 bool AllowFunctionDefinitions,
408 SourceLocation *DeclEnd) {
409 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000410 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000411 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000412
John McCalld8ac0572009-11-03 19:26:08 +0000413 // Bail out if the first declarator didn't seem well-formed.
414 if (!D.hasName() && !D.mayOmitIdentifier()) {
415 // Skip until ; or }.
416 SkipUntil(tok::r_brace, true, true);
417 if (Tok.is(tok::semi))
418 ConsumeToken();
419 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000420 }
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattnerc82daef2010-07-11 22:24:20 +0000422 // Check to see if we have a function *definition* which must have a body.
423 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
424 // Look at the next token to make sure that this isn't a function
425 // declaration. We have to check this because __attribute__ might be the
426 // start of a function definition in GCC-extended K&R C.
427 !isDeclarationAfterDeclarator()) {
428
Chris Lattner004659a2010-07-11 22:42:07 +0000429 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000430 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
431 Diag(Tok, diag::err_function_declared_typedef);
432
433 // Recover by treating the 'typedef' as spurious.
434 DS.ClearStorageClassSpecs();
435 }
436
John McCalld226f652010-08-21 09:40:31 +0000437 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000438 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000439 }
440
441 if (isDeclarationSpecifier()) {
442 // If there is an invalid declaration specifier right after the function
443 // prototype, then we must be in a missing semicolon case where this isn't
444 // actually a body. Just fall through into the code that handles it as a
445 // prototype, and let the top-level code handle the erroneous declspec
446 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000447 } else {
448 Diag(Tok, diag::err_expected_fn_body);
449 SkipUntil(tok::semi);
450 return DeclGroupPtrTy();
451 }
452 }
453
John McCalld226f652010-08-21 09:40:31 +0000454 llvm::SmallVector<Decl *, 8> DeclsInGroup;
455 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000456 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000457 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000458 DeclsInGroup.push_back(FirstDecl);
459
460 // If we don't have a comma, it is either the end of the list (a ';') or an
461 // error, bail out.
462 while (Tok.is(tok::comma)) {
463 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000464 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000465
466 // Parse the next declarator.
467 D.clear();
468
469 // Accept attributes in an init-declarator. In the first declarator in a
470 // declaration, these would be part of the declspec. In subsequent
471 // declarators, they become part of the declarator itself, so that they
472 // don't apply to declarators after *this* one. Examples:
473 // short __attribute__((common)) var; -> declspec
474 // short var __attribute__((common)); -> declarator
475 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +0000476 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +0000477
478 ParseDeclarator(D);
479
John McCalld226f652010-08-21 09:40:31 +0000480 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000481 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000482 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000483 DeclsInGroup.push_back(ThisDecl);
484 }
485
486 if (DeclEnd)
487 *DeclEnd = Tok.getLocation();
488
489 if (Context != Declarator::ForContext &&
490 ExpectAndConsume(tok::semi,
491 Context == Declarator::FileContext
492 ? diag::err_invalid_token_after_toplevel_declarator
493 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000494 // Okay, there was no semicolon and one was expected. If we see a
495 // declaration specifier, just assume it was missing and continue parsing.
496 // Otherwise things are very confused and we skip to recover.
497 if (!isDeclarationSpecifier()) {
498 SkipUntil(tok::r_brace, true, true);
499 if (Tok.is(tok::semi))
500 ConsumeToken();
501 }
John McCalld8ac0572009-11-03 19:26:08 +0000502 }
503
Douglas Gregor23c94db2010-07-02 17:43:08 +0000504 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000505 DeclsInGroup.data(),
506 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000507}
508
Douglas Gregor1426e532009-05-12 21:31:51 +0000509/// \brief Parse 'declaration' after parsing 'declaration-specifiers
510/// declarator'. This method parses the remainder of the declaration
511/// (including any attributes or initializer, among other things) and
512/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000513///
Reid Spencer5f016e22007-07-11 17:01:13 +0000514/// init-declarator: [C99 6.7]
515/// declarator
516/// declarator '=' initializer
517/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
518/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000519/// [C++] declarator initializer[opt]
520///
521/// [C++] initializer:
522/// [C++] '=' initializer-clause
523/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000524/// [C++0x] '=' 'default' [TODO]
525/// [C++0x] '=' 'delete'
526///
527/// According to the standard grammar, =default and =delete are function
528/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000529///
John McCalld226f652010-08-21 09:40:31 +0000530Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000531 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000532 // If a simple-asm-expr is present, parse it.
533 if (Tok.is(tok::kw_asm)) {
534 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000535 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor1426e532009-05-12 21:31:51 +0000536 if (AsmLabel.isInvalid()) {
537 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000538 return 0;
Douglas Gregor1426e532009-05-12 21:31:51 +0000539 }
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Douglas Gregor1426e532009-05-12 21:31:51 +0000541 D.setAsmLabel(AsmLabel.release());
542 D.SetRangeEnd(Loc);
543 }
Mike Stump1eb44332009-09-09 15:08:12 +0000544
John McCall7f040a92010-12-24 02:08:15 +0000545 MaybeParseGNUAttributes(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Douglas Gregor1426e532009-05-12 21:31:51 +0000547 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000548 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000549 switch (TemplateInfo.Kind) {
550 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000551 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000552 break;
553
554 case ParsedTemplateInfo::Template:
555 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000556 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000557 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000558 TemplateInfo.TemplateParams->data(),
559 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000560 D);
561 break;
562
563 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000564 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000565 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000566 TemplateInfo.ExternLoc,
567 TemplateInfo.TemplateLoc,
568 D);
569 if (ThisRes.isInvalid()) {
570 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000571 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000572 }
573
574 ThisDecl = ThisRes.get();
575 break;
576 }
577 }
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Douglas Gregor1426e532009-05-12 21:31:51 +0000579 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000580 if (isTokenEqualOrMistypedEqualEqual(
581 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000582 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000583 if (Tok.is(tok::kw_delete)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000584 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000585
586 if (!getLang().CPlusPlus0x)
587 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
588
Douglas Gregor1426e532009-05-12 21:31:51 +0000589 Actions.SetDeclDeleted(ThisDecl, DelLoc);
590 } else {
John McCall731ad842009-12-19 09:28:58 +0000591 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
592 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000593 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000594 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000595
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000596 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000597 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000598 ConsumeCodeCompletionToken();
599 SkipUntil(tok::comma, true, true);
600 return ThisDecl;
601 }
602
John McCall60d7b3a2010-08-24 06:29:42 +0000603 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000604
John McCall731ad842009-12-19 09:28:58 +0000605 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000606 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000607 ExitScope();
608 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000609
Douglas Gregor1426e532009-05-12 21:31:51 +0000610 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000611 SkipUntil(tok::comma, true, true);
612 Actions.ActOnInitializerError(ThisDecl);
613 } else
John McCall9ae2f072010-08-23 23:25:46 +0000614 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor1426e532009-05-12 21:31:51 +0000615 }
616 } else if (Tok.is(tok::l_paren)) {
617 // Parse C++ direct initializer: '(' expression-list ')'
618 SourceLocation LParenLoc = ConsumeParen();
619 ExprVector Exprs(Actions);
620 CommaLocsTy CommaLocs;
621
Douglas Gregorb4debae2009-12-22 17:47:17 +0000622 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
623 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000624 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000625 }
626
Douglas Gregor1426e532009-05-12 21:31:51 +0000627 if (ParseExpressionList(Exprs, CommaLocs)) {
628 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000629
630 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000631 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000632 ExitScope();
633 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000634 } else {
635 // Match the ')'.
636 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
637
638 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
639 "Unexpected number of commas!");
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 }
645
Douglas Gregor1426e532009-05-12 21:31:51 +0000646 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
647 move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000648 RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000649 }
650 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000651 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000652 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
653 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000654 }
655
656 return ThisDecl;
657}
658
Reid Spencer5f016e22007-07-11 17:01:13 +0000659/// ParseSpecifierQualifierList
660/// specifier-qualifier-list:
661/// type-specifier specifier-qualifier-list[opt]
662/// type-qualifier specifier-qualifier-list[opt]
663/// [GNU] attributes specifier-qualifier-list[opt]
664///
665void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
666 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
667 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 // Validate declspec for type-name.
671 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000672 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
John McCall7f040a92010-12-24 02:08:15 +0000673 !DS.hasAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Reid Spencer5f016e22007-07-11 17:01:13 +0000676 // Issue diagnostic and remove storage class if present.
677 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
678 if (DS.getStorageClassSpecLoc().isValid())
679 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
680 else
681 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
682 DS.ClearStorageClassSpecs();
683 }
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 // Issue diagnostic and remove function specfier if present.
686 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000687 if (DS.isInlineSpecified())
688 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
689 if (DS.isVirtualSpecified())
690 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
691 if (DS.isExplicitSpecified())
692 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 DS.ClearFunctionSpecs();
694 }
695}
696
Chris Lattnerc199ab32009-04-12 20:42:31 +0000697/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
698/// specified token is valid after the identifier in a declarator which
699/// immediately follows the declspec. For example, these things are valid:
700///
701/// int x [ 4]; // direct-declarator
702/// int x ( int y); // direct-declarator
703/// int(int x ) // direct-declarator
704/// int x ; // simple-declaration
705/// int x = 17; // init-declarator-list
706/// int x , y; // init-declarator-list
707/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000708/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000709/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000710///
711/// This is not, because 'x' does not immediately follow the declspec (though
712/// ')' happens to be valid anyway).
713/// int (x)
714///
715static bool isValidAfterIdentifierInDeclarator(const Token &T) {
716 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
717 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000718 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000719}
720
Chris Lattnere40c2952009-04-14 21:34:55 +0000721
722/// ParseImplicitInt - This method is called when we have an non-typename
723/// identifier in a declspec (which normally terminates the decl spec) when
724/// the declspec has no type specifier. In this case, the declspec is either
725/// malformed or is "implicit int" (in K&R and C89).
726///
727/// This method handles diagnosing this prettily and returns false if the
728/// declspec is done being processed. If it recovers and thinks there may be
729/// other pieces of declspec after it, it returns true.
730///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000731bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000732 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000733 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000734 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Chris Lattnere40c2952009-04-14 21:34:55 +0000736 SourceLocation Loc = Tok.getLocation();
737 // If we see an identifier that is not a type name, we normally would
738 // parse it as the identifer being declared. However, when a typename
739 // is typo'd or the definition is not included, this will incorrectly
740 // parse the typename as the identifier name and fall over misparsing
741 // later parts of the diagnostic.
742 //
743 // As such, we try to do some look-ahead in cases where this would
744 // otherwise be an "implicit-int" case to see if this is invalid. For
745 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
746 // an identifier with implicit int, we'd get a parse error because the
747 // next token is obviously invalid for a type. Parse these as a case
748 // with an invalid type specifier.
749 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Chris Lattnere40c2952009-04-14 21:34:55 +0000751 // Since we know that this either implicit int (which is rare) or an
752 // error, we'd do lookahead to try to do better recovery.
753 if (isValidAfterIdentifierInDeclarator(NextToken())) {
754 // If this token is valid for implicit int, e.g. "static x = 4", then
755 // we just avoid eating the identifier, so it will be parsed as the
756 // identifier in the declarator.
757 return false;
758 }
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Chris Lattnere40c2952009-04-14 21:34:55 +0000760 // Otherwise, if we don't consume this token, we are going to emit an
761 // error anyway. Try to recover from various common problems. Check
762 // to see if this was a reference to a tag name without a tag specified.
763 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000764 //
765 // C++ doesn't need this, and isTagName doesn't take SS.
766 if (SS == 0) {
767 const char *TagName = 0;
768 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Douglas Gregor23c94db2010-07-02 17:43:08 +0000770 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +0000771 default: break;
772 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
773 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
774 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
775 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
776 }
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Chris Lattnerf4382f52009-04-14 22:17:06 +0000778 if (TagName) {
779 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000780 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000781 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattnerf4382f52009-04-14 22:17:06 +0000783 // Parse this as a tag as if the missing tag were present.
784 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000785 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000786 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000787 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000788 return true;
789 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000790 }
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Douglas Gregora786fdb2009-10-13 23:27:22 +0000792 // This is almost certainly an invalid type name. Let the action emit a
793 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +0000794 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +0000795 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +0000796 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +0000797 // The action emitted a diagnostic, so we don't have to.
798 if (T) {
799 // The action has suggested that the type T could be used. Set that as
800 // the type in the declaration specifiers, consume the would-be type
801 // name token, and we're done.
802 const char *PrevSpec;
803 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +0000804 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000805 DS.SetRangeEnd(Tok.getLocation());
806 ConsumeToken();
807
808 // There may be other declaration specifiers after this.
809 return true;
810 }
811
812 // Fall through; the action had no suggestion for us.
813 } else {
814 // The action did not emit a diagnostic, so emit one now.
815 SourceRange R;
816 if (SS) R = SS->getRange();
817 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
818 }
Mike Stump1eb44332009-09-09 15:08:12 +0000819
Douglas Gregora786fdb2009-10-13 23:27:22 +0000820 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000821 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000822 unsigned DiagID;
823 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000824 DS.SetRangeEnd(Tok.getLocation());
825 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Chris Lattnere40c2952009-04-14 21:34:55 +0000827 // TODO: Could inject an invalid typedef decl in an enclosing scope to
828 // avoid rippling error messages on subsequent uses of the same type,
829 // could be useful if #include was forgotten.
830 return false;
831}
832
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000833/// \brief Determine the declaration specifier context from the declarator
834/// context.
835///
836/// \param Context the declarator context, which is one of the
837/// Declarator::TheContext enumerator values.
838Parser::DeclSpecContext
839Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
840 if (Context == Declarator::MemberContext)
841 return DSC_class;
842 if (Context == Declarator::FileContext)
843 return DSC_top_level;
844 return DSC_normal;
845}
846
Reid Spencer5f016e22007-07-11 17:01:13 +0000847/// ParseDeclarationSpecifiers
848/// declaration-specifiers: [C99 6.7]
849/// storage-class-specifier declaration-specifiers[opt]
850/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000851/// [C99] function-specifier declaration-specifiers[opt]
852/// [GNU] attributes declaration-specifiers[opt]
853///
854/// storage-class-specifier: [C99 6.7.1]
855/// 'typedef'
856/// 'extern'
857/// 'static'
858/// 'auto'
859/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000860/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000861/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000862/// function-specifier: [C99 6.7.4]
863/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000864/// [C++] 'virtual'
865/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000866/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000867/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000868
Reid Spencer5f016e22007-07-11 17:01:13 +0000869///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000870void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000871 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000872 AccessSpecifier AS,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000873 DeclSpecContext DSContext) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000874 DS.SetRangeStart(Tok.getLocation());
Chris Lattner729ad832010-11-09 20:14:26 +0000875 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000877 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000879 unsigned DiagID = 0;
880
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000882
Reid Spencer5f016e22007-07-11 17:01:13 +0000883 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000884 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000885 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 // If this is not a declaration specifier token, we're done reading decl
887 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000888 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000891 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +0000892 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000893 if (DS.hasTypeSpecifier()) {
894 bool AllowNonIdentifiers
895 = (getCurScope()->getFlags() & (Scope::ControlScope |
896 Scope::BlockScope |
897 Scope::TemplateParamScope |
898 Scope::FunctionPrototypeScope |
899 Scope::AtCatchScope)) == 0;
900 bool AllowNestedNameSpecifiers
901 = DSContext == DSC_top_level ||
902 (DSContext == DSC_class && DS.isFriendSpecified());
903
Douglas Gregorc7b6d882010-09-16 15:14:18 +0000904 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
905 AllowNonIdentifiers,
906 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000907 ConsumeCodeCompletionToken();
908 return;
909 }
910
911 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +0000912 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
913 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000914 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +0000915 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000916 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +0000917 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000918
919 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
920 ConsumeCodeCompletionToken();
921 return;
922 }
923
Chris Lattner5e02c472009-01-05 00:07:25 +0000924 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000925 // C++ scope specifier. Annotate and loop, or bail out on error.
926 if (TryAnnotateCXXScopeToken(true)) {
927 if (!DS.hasTypeSpecifier())
928 DS.SetTypeSpecError();
929 goto DoneWithDeclSpec;
930 }
John McCall2e0a7152010-03-01 18:20:46 +0000931 if (Tok.is(tok::coloncolon)) // ::new or ::delete
932 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000933 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000934
935 case tok::annot_cxxscope: {
936 if (DS.hasTypeSpecifier())
937 goto DoneWithDeclSpec;
938
John McCallaa87d332009-12-12 11:40:51 +0000939 CXXScopeSpec SS;
John McCallca0408f2010-08-23 06:44:23 +0000940 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCallaa87d332009-12-12 11:40:51 +0000941 SS.setRange(Tok.getAnnotationRange());
942
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000943 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000944 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000945 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000946 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000947 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000948 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000949
950 // C++ [class.qual]p2:
951 // In a lookup in which the constructor is an acceptable lookup
952 // result and the nested-name-specifier nominates a class C:
953 //
954 // - if the name specified after the
955 // nested-name-specifier, when looked up in C, is the
956 // injected-class-name of C (Clause 9), or
957 //
958 // - if the name specified after the nested-name-specifier
959 // is the same as the identifier or the
960 // simple-template-id's template-name in the last
961 // component of the nested-name-specifier,
962 //
963 // the name is instead considered to name the constructor of
964 // class C.
965 //
966 // Thus, if the template-name is actually the constructor
967 // name, then the code is ill-formed; this interpretation is
968 // reinforced by the NAD status of core issue 635.
969 TemplateIdAnnotation *TemplateId
970 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000971 if ((DSContext == DSC_top_level ||
972 (DSContext == DSC_class && DS.isFriendSpecified())) &&
973 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000974 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000975 if (isConstructorDeclarator()) {
976 // The user meant this to be an out-of-line constructor
977 // definition, but template arguments are not allowed
978 // there. Just allow this as a constructor; we'll
979 // complain about it later.
980 goto DoneWithDeclSpec;
981 }
982
983 // The user meant this to name a type, but it actually names
984 // a constructor with some extraneous template
985 // arguments. Complain, then parse it as a type as the user
986 // intended.
987 Diag(TemplateId->TemplateNameLoc,
988 diag::err_out_of_line_template_id_names_constructor)
989 << TemplateId->Name;
990 }
991
John McCallaa87d332009-12-12 11:40:51 +0000992 DS.getTypeSpecScope() = SS;
993 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +0000994 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000995 "ParseOptionalCXXScopeSpecifier not working");
996 AnnotateTemplateIdTokenAsType(&SS);
997 continue;
998 }
999
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001000 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001001 DS.getTypeSpecScope() = SS;
1002 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001003 if (Tok.getAnnotationValue()) {
1004 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00001005 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1006 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001007 PrevSpec, DiagID, T);
1008 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001009 else
1010 DS.SetTypeSpecError();
1011 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1012 ConsumeToken(); // The typename
1013 }
1014
Douglas Gregor9135c722009-03-25 15:40:00 +00001015 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001016 goto DoneWithDeclSpec;
1017
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001018 // If we're in a context where the identifier could be a class name,
1019 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001020 if ((DSContext == DSC_top_level ||
1021 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001022 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001023 &SS)) {
1024 if (isConstructorDeclarator())
1025 goto DoneWithDeclSpec;
1026
1027 // As noted in C++ [class.qual]p2 (cited above), when the name
1028 // of the class is qualified in a context where it could name
1029 // a constructor, its a constructor name. However, we've
1030 // looked at the declarator, and the user probably meant this
1031 // to be a type. Complain that it isn't supposed to be treated
1032 // as a type, then proceed to parse it as a type.
1033 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1034 << Next.getIdentifierInfo();
1035 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001036
John McCallb3d87482010-08-24 05:47:05 +00001037 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1038 Next.getLocation(),
1039 getCurScope(), &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001040
Chris Lattnerf4382f52009-04-14 22:17:06 +00001041 // If the referenced identifier is not a type, then this declspec is
1042 // erroneous: We already checked about that it has no type specifier, and
1043 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001044 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001045 if (TypeRep == 0) {
1046 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001047 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001048 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001049 }
Mike Stump1eb44332009-09-09 15:08:12 +00001050
John McCallaa87d332009-12-12 11:40:51 +00001051 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001052 ConsumeToken(); // The C++ scope.
1053
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001054 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001055 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001056 if (isInvalid)
1057 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001058
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001059 DS.SetRangeEnd(Tok.getLocation());
1060 ConsumeToken(); // The typename.
1061
1062 continue;
1063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Chris Lattner80d0c892009-01-21 19:48:37 +00001065 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001066 if (Tok.getAnnotationValue()) {
1067 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001068 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001069 DiagID, T);
1070 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001071 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001072
1073 if (isInvalid)
1074 break;
1075
Chris Lattner80d0c892009-01-21 19:48:37 +00001076 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1077 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Chris Lattner80d0c892009-01-21 19:48:37 +00001079 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1080 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001081 // Objective-C interface.
1082 if (Tok.is(tok::less) && getLang().ObjC1)
1083 ParseObjCProtocolQualifiers(DS);
1084
Chris Lattner80d0c892009-01-21 19:48:37 +00001085 continue;
1086 }
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Chris Lattner3bd934a2008-07-26 01:18:38 +00001088 // typedef-name
1089 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001090 // In C++, check to see if this is a scope specifier like foo::bar::, if
1091 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001092 if (getLang().CPlusPlus) {
1093 if (TryAnnotateCXXScopeToken(true)) {
1094 if (!DS.hasTypeSpecifier())
1095 DS.SetTypeSpecError();
1096 goto DoneWithDeclSpec;
1097 }
1098 if (!Tok.is(tok::identifier))
1099 continue;
1100 }
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Chris Lattner3bd934a2008-07-26 01:18:38 +00001102 // This identifier can only be a typedef name if we haven't already seen
1103 // a type-specifier. Without this check we misparse:
1104 // typedef int X; struct Y { short X; }; as 'short int'.
1105 if (DS.hasTypeSpecifier())
1106 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001107
John Thompson82287d12010-02-05 00:12:22 +00001108 // Check for need to substitute AltiVec keyword tokens.
1109 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1110 break;
1111
Chris Lattner3bd934a2008-07-26 01:18:38 +00001112 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001113 ParsedType TypeRep =
1114 Actions.getTypeName(*Tok.getIdentifierInfo(),
1115 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001116
Chris Lattnerc199ab32009-04-12 20:42:31 +00001117 // If this is not a typedef name, don't parse it as part of the declspec,
1118 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001119 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001120 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001121 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001122 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001123
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001124 // If we're in a context where the identifier could be a class name,
1125 // check whether this is a constructor declaration.
1126 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001127 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001128 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001129 goto DoneWithDeclSpec;
1130
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001131 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001132 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001133 if (isInvalid)
1134 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Chris Lattner3bd934a2008-07-26 01:18:38 +00001136 DS.SetRangeEnd(Tok.getLocation());
1137 ConsumeToken(); // The identifier
1138
1139 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1140 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001141 // Objective-C interface.
1142 if (Tok.is(tok::less) && getLang().ObjC1)
1143 ParseObjCProtocolQualifiers(DS);
1144
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001145 // Need to support trailing type qualifiers (e.g. "id<p> const").
1146 // If a type specifier follows, it will be diagnosed elsewhere.
1147 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001148 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001149
1150 // type-name
1151 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001152 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001153 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001154 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001155 // This template-id does not refer to a type name, so we're
1156 // done with the type-specifiers.
1157 goto DoneWithDeclSpec;
1158 }
1159
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001160 // If we're in a context where the template-id could be a
1161 // constructor name or specialization, check whether this is a
1162 // constructor declaration.
1163 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001164 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001165 isConstructorDeclarator())
1166 goto DoneWithDeclSpec;
1167
Douglas Gregor39a8de12009-02-25 19:37:18 +00001168 // Turn the template-id annotation token into a type annotation
1169 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001170 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001171 continue;
1172 }
1173
Reid Spencer5f016e22007-07-11 17:01:13 +00001174 // GNU attributes support.
1175 case tok::kw___attribute:
John McCall7f040a92010-12-24 02:08:15 +00001176 ParseGNUAttributes(DS.getAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001178
1179 // Microsoft declspec support.
1180 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00001181 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00001182 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Steve Naroff239f0732008-12-25 14:16:32 +00001184 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001185 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001186 // FIXME: Add handling here!
1187 break;
1188
1189 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001190 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001191 case tok::kw___cdecl:
1192 case tok::kw___stdcall:
1193 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001194 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001195 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00001196 continue;
1197
Dawn Perchik52fc3142010-09-03 01:29:35 +00001198 // Borland single token adornments.
1199 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001200 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001201 continue;
1202
Reid Spencer5f016e22007-07-11 17:01:13 +00001203 // storage-class-specifier
1204 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001205 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1206 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 break;
1208 case tok::kw_extern:
1209 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001210 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001211 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1212 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001213 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001214 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001215 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001216 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001217 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001218 case tok::kw_static:
1219 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001220 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001221 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1222 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 break;
1224 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001225 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001226 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1227 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001228 else
John McCallfec54012009-08-03 20:12:06 +00001229 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1230 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 break;
1232 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001233 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1234 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001236 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001237 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1238 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001239 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001241 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 // function-specifier
1245 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001246 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001248 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001249 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001250 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001251 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001252 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001253 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001254
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001255 // friend
1256 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001257 if (DSContext == DSC_class)
1258 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1259 else {
1260 PrevSpec = ""; // not actually used by the diagnostic
1261 DiagID = diag::err_friend_invalid_in_context;
1262 isInvalid = true;
1263 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001264 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001265
Sebastian Redl2ac67232009-11-05 15:47:02 +00001266 // constexpr
1267 case tok::kw_constexpr:
1268 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1269 break;
1270
Chris Lattner80d0c892009-01-21 19:48:37 +00001271 // type-specifier
1272 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001273 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1274 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001275 break;
1276 case tok::kw_long:
1277 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001278 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1279 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001280 else
John McCallfec54012009-08-03 20:12:06 +00001281 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1282 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001283 break;
1284 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001285 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1286 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001287 break;
1288 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001289 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1290 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001291 break;
1292 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001293 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1294 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001295 break;
1296 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001297 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1298 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001299 break;
1300 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001301 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1302 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001303 break;
1304 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001305 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1306 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001307 break;
1308 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001309 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1310 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001311 break;
1312 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001313 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1314 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001315 break;
1316 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001317 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1318 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001319 break;
1320 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001321 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1322 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001323 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001324 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001325 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1326 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001327 break;
1328 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001329 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1330 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001331 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001332 case tok::kw_bool:
1333 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001334 if (Tok.is(tok::kw_bool) &&
1335 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1336 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1337 PrevSpec = ""; // Not used by the diagnostic.
1338 DiagID = diag::err_bool_redeclaration;
1339 isInvalid = true;
1340 } else {
1341 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1342 DiagID);
1343 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001344 break;
1345 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001346 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1347 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001348 break;
1349 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001350 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1351 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001352 break;
1353 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001354 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1355 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001356 break;
John Thompson82287d12010-02-05 00:12:22 +00001357 case tok::kw___vector:
1358 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1359 break;
1360 case tok::kw___pixel:
1361 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1362 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001363
1364 // class-specifier:
1365 case tok::kw_class:
1366 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001367 case tok::kw_union: {
1368 tok::TokenKind Kind = Tok.getKind();
1369 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001370 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001371 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001372 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001373
1374 // enum-specifier:
1375 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001376 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001377 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001378 continue;
1379
1380 // cv-qualifier:
1381 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001382 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1383 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001384 break;
1385 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001386 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1387 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001388 break;
1389 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001390 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1391 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001392 break;
1393
Douglas Gregord57959a2009-03-27 23:10:48 +00001394 // C++ typename-specifier:
1395 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001396 if (TryAnnotateTypeOrScopeToken()) {
1397 DS.SetTypeSpecError();
1398 goto DoneWithDeclSpec;
1399 }
1400 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001401 continue;
1402 break;
1403
Chris Lattner80d0c892009-01-21 19:48:37 +00001404 // GNU typeof support.
1405 case tok::kw_typeof:
1406 ParseTypeofSpecifier(DS);
1407 continue;
1408
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001409 case tok::kw_decltype:
1410 ParseDecltypeSpecifier(DS);
1411 continue;
1412
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001413 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001414 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001415 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1416 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001417 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001418 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Douglas Gregor46f936e2010-11-19 17:10:50 +00001420 if (!ParseObjCProtocolQualifiers(DS))
1421 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1422 << FixItHint::CreateInsertion(Loc, "id")
1423 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001424
1425 // Need to support trailing type qualifiers (e.g. "id<p> const").
1426 // If a type specifier follows, it will be diagnosed elsewhere.
1427 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001428 }
John McCallfec54012009-08-03 20:12:06 +00001429 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 if (isInvalid) {
1431 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001432 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001433
1434 if (DiagID == diag::ext_duplicate_declspec)
1435 Diag(Tok, DiagID)
1436 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1437 else
1438 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001439 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001440 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001441 ConsumeToken();
1442 }
1443}
Douglas Gregoradcac882008-12-01 23:54:00 +00001444
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001445/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001446/// primarily follow the C++ grammar with additions for C99 and GNU,
1447/// which together subsume the C grammar. Note that the C++
1448/// type-specifier also includes the C type-qualifier (for const,
1449/// volatile, and C99 restrict). Returns true if a type-specifier was
1450/// found (and parsed), false otherwise.
1451///
1452/// type-specifier: [C++ 7.1.5]
1453/// simple-type-specifier
1454/// class-specifier
1455/// enum-specifier
1456/// elaborated-type-specifier [TODO]
1457/// cv-qualifier
1458///
1459/// cv-qualifier: [C++ 7.1.5.1]
1460/// 'const'
1461/// 'volatile'
1462/// [C99] 'restrict'
1463///
1464/// simple-type-specifier: [ C++ 7.1.5.2]
1465/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1466/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1467/// 'char'
1468/// 'wchar_t'
1469/// 'bool'
1470/// 'short'
1471/// 'int'
1472/// 'long'
1473/// 'signed'
1474/// 'unsigned'
1475/// 'float'
1476/// 'double'
1477/// 'void'
1478/// [C99] '_Bool'
1479/// [C99] '_Complex'
1480/// [C99] '_Imaginary' // Removed in TC2?
1481/// [GNU] '_Decimal32'
1482/// [GNU] '_Decimal64'
1483/// [GNU] '_Decimal128'
1484/// [GNU] typeof-specifier
1485/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1486/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001487/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001488/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001489bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001490 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001491 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001492 const ParsedTemplateInfo &TemplateInfo,
1493 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001494 SourceLocation Loc = Tok.getLocation();
1495
1496 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001497 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001498 // If we already have a type specifier, this identifier is not a type.
1499 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1500 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1501 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1502 return false;
John Thompson82287d12010-02-05 00:12:22 +00001503 // Check for need to substitute AltiVec keyword tokens.
1504 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1505 break;
1506 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001507 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001508 // Annotate typenames and C++ scope specifiers. If we get one, just
1509 // recurse to handle whatever we get.
1510 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001511 return true;
1512 if (Tok.is(tok::identifier))
1513 return false;
1514 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1515 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001516 case tok::coloncolon: // ::foo::bar
1517 if (NextToken().is(tok::kw_new) || // ::new
1518 NextToken().is(tok::kw_delete)) // ::delete
1519 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Chris Lattner166a8fc2009-01-04 23:41:41 +00001521 // Annotate typenames and C++ scope specifiers. If we get one, just
1522 // recurse to handle whatever we get.
1523 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001524 return true;
1525 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1526 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Douglas Gregor12e083c2008-11-07 15:42:26 +00001528 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001529 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001530 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00001531 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1532 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001533 DiagID, T);
1534 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001535 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001536 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1537 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Douglas Gregor12e083c2008-11-07 15:42:26 +00001539 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1540 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1541 // Objective-C interface. If we don't have Objective-C or a '<', this is
1542 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001543 if (Tok.is(tok::less) && getLang().ObjC1)
1544 ParseObjCProtocolQualifiers(DS);
1545
Douglas Gregor12e083c2008-11-07 15:42:26 +00001546 return true;
1547 }
1548
1549 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001550 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001551 break;
1552 case tok::kw_long:
1553 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001554 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1555 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001556 else
John McCallfec54012009-08-03 20:12:06 +00001557 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1558 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001559 break;
1560 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001561 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001562 break;
1563 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001564 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1565 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001566 break;
1567 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001568 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1569 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001570 break;
1571 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001572 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1573 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001574 break;
1575 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001576 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001577 break;
1578 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001579 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001580 break;
1581 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001582 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001583 break;
1584 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001585 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001586 break;
1587 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001588 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001589 break;
1590 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001591 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001592 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001593 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001594 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001595 break;
1596 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001597 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001598 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001599 case tok::kw_bool:
1600 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001601 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001602 break;
1603 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001604 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1605 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001606 break;
1607 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001608 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1609 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001610 break;
1611 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001612 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1613 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001614 break;
John Thompson82287d12010-02-05 00:12:22 +00001615 case tok::kw___vector:
1616 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1617 break;
1618 case tok::kw___pixel:
1619 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1620 break;
1621
Douglas Gregor12e083c2008-11-07 15:42:26 +00001622 // class-specifier:
1623 case tok::kw_class:
1624 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001625 case tok::kw_union: {
1626 tok::TokenKind Kind = Tok.getKind();
1627 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001628 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1629 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001630 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001631 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001632
1633 // enum-specifier:
1634 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001635 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001636 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001637 return true;
1638
1639 // cv-qualifier:
1640 case tok::kw_const:
1641 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001642 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001643 break;
1644 case tok::kw_volatile:
1645 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001646 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001647 break;
1648 case tok::kw_restrict:
1649 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001650 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001651 break;
1652
1653 // GNU typeof support.
1654 case tok::kw_typeof:
1655 ParseTypeofSpecifier(DS);
1656 return true;
1657
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001658 // C++0x decltype support.
1659 case tok::kw_decltype:
1660 ParseDecltypeSpecifier(DS);
1661 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001663 // C++0x auto support.
1664 case tok::kw_auto:
1665 if (!getLang().CPlusPlus0x)
1666 return false;
1667
John McCallfec54012009-08-03 20:12:06 +00001668 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001669 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001670
Eli Friedman290eeb02009-06-08 23:27:34 +00001671 case tok::kw___ptr64:
1672 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001673 case tok::kw___cdecl:
1674 case tok::kw___stdcall:
1675 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001676 case tok::kw___thiscall:
John McCall7f040a92010-12-24 02:08:15 +00001677 ParseMicrosoftTypeAttributes(DS.getAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001678 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001679
Dawn Perchik52fc3142010-09-03 01:29:35 +00001680 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00001681 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00001682 return true;
1683
Douglas Gregor12e083c2008-11-07 15:42:26 +00001684 default:
1685 // Not a type-specifier; do nothing.
1686 return false;
1687 }
1688
1689 // If the specifier combination wasn't legal, issue a diagnostic.
1690 if (isInvalid) {
1691 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001692 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001693 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001694 }
1695 DS.SetRangeEnd(Tok.getLocation());
1696 ConsumeToken(); // whatever we parsed above.
1697 return true;
1698}
Reid Spencer5f016e22007-07-11 17:01:13 +00001699
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001700/// ParseStructDeclaration - Parse a struct declaration without the terminating
1701/// semicolon.
1702///
Reid Spencer5f016e22007-07-11 17:01:13 +00001703/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001704/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001705/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001706/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001707/// struct-declarator-list:
1708/// struct-declarator
1709/// struct-declarator-list ',' struct-declarator
1710/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1711/// struct-declarator:
1712/// declarator
1713/// [GNU] declarator attributes[opt]
1714/// declarator[opt] ':' constant-expression
1715/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1716///
Chris Lattnere1359422008-04-10 06:46:29 +00001717void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001718ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001719 if (Tok.is(tok::kw___extension__)) {
1720 // __extension__ silences extension warnings in the subexpression.
1721 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001722 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001723 return ParseStructDeclaration(DS, Fields);
1724 }
Mike Stump1eb44332009-09-09 15:08:12 +00001725
Steve Naroff28a7ca82007-08-20 22:28:22 +00001726 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001727 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001728 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001730 // If there are no declarators, this is a free-standing declaration
1731 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001732 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001733 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001734 return;
1735 }
1736
1737 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001738 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001739 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001740 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001741 FieldDeclarator DeclaratorInfo(DS);
1742
1743 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00001744 if (!FirstDeclarator)
1745 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00001746
Steve Naroff28a7ca82007-08-20 22:28:22 +00001747 /// struct-declarator: declarator
1748 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001749 if (Tok.isNot(tok::colon)) {
1750 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1751 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001752 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001753 }
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Chris Lattner04d66662007-10-09 17:33:22 +00001755 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001756 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001757 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001758 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001759 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001760 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001761 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001762 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001763
Steve Naroff28a7ca82007-08-20 22:28:22 +00001764 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001765 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001766
John McCallbdd563e2009-11-03 02:38:08 +00001767 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00001768 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00001769 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001770
Steve Naroff28a7ca82007-08-20 22:28:22 +00001771 // If we don't have a comma, it is either the end of the list (a ';')
1772 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001773 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001774 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001775
Steve Naroff28a7ca82007-08-20 22:28:22 +00001776 // Consume the comma.
1777 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001778
John McCallbdd563e2009-11-03 02:38:08 +00001779 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001780 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001781}
1782
1783/// ParseStructUnionBody
1784/// struct-contents:
1785/// struct-declaration-list
1786/// [EXT] empty
1787/// [GNU] "struct-declaration-list" without terminatoring ';'
1788/// struct-declaration-list:
1789/// struct-declaration
1790/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001791/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001792///
Reid Spencer5f016e22007-07-11 17:01:13 +00001793void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001794 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00001795 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1796 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001797
Reid Spencer5f016e22007-07-11 17:01:13 +00001798 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001800 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001801 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00001802
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1804 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001805 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00001806 Diag(Tok, diag::ext_empty_struct_union)
1807 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00001808
John McCalld226f652010-08-21 09:40:31 +00001809 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001810
Reid Spencer5f016e22007-07-11 17:01:13 +00001811 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001812 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001813 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001816 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001817 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00001818 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00001819 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001820 ConsumeToken();
1821 continue;
1822 }
Chris Lattnere1359422008-04-10 06:46:29 +00001823
1824 // Parse all the comma separated declarators.
1825 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001826
John McCallbdd563e2009-11-03 02:38:08 +00001827 if (!Tok.is(tok::at)) {
1828 struct CFieldCallback : FieldCallback {
1829 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001830 Decl *TagDecl;
1831 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001832
John McCalld226f652010-08-21 09:40:31 +00001833 CFieldCallback(Parser &P, Decl *TagDecl,
1834 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001835 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1836
John McCalld226f652010-08-21 09:40:31 +00001837 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001838 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00001839 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00001840 FD.D.getDeclSpec().getSourceRange().getBegin(),
1841 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001842 FieldDecls.push_back(Field);
1843 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001844 }
John McCallbdd563e2009-11-03 02:38:08 +00001845 } Callback(*this, TagDecl, FieldDecls);
1846
1847 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001848 } else { // Handle @defs
1849 ConsumeToken();
1850 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1851 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001852 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001853 continue;
1854 }
1855 ConsumeToken();
1856 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1857 if (!Tok.is(tok::identifier)) {
1858 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001859 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001860 continue;
1861 }
John McCalld226f652010-08-21 09:40:31 +00001862 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001863 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001864 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001865 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1866 ConsumeToken();
1867 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001868 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001869
Chris Lattner04d66662007-10-09 17:33:22 +00001870 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001871 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001872 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001873 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 break;
1875 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001876 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1877 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001878 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001879 // If we stopped at a ';', eat it.
1880 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001881 }
1882 }
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Steve Naroff60fccee2007-10-29 21:38:07 +00001884 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001885
John McCall7f040a92010-12-24 02:08:15 +00001886 ParsedAttributes attrs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001887 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001888 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001889
Douglas Gregor23c94db2010-07-02 17:43:08 +00001890 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001891 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001892 LBraceLoc, RBraceLoc,
John McCall7f040a92010-12-24 02:08:15 +00001893 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00001894 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001895 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001896}
1897
Reid Spencer5f016e22007-07-11 17:01:13 +00001898/// ParseEnumSpecifier
1899/// enum-specifier: [C99 6.7.2.2]
1900/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001901///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001902/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1903/// '}' attributes[opt]
1904/// 'enum' identifier
1905/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001906///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001907/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1908/// [C++0x] enum-head '{' enumerator-list ',' '}'
1909///
1910/// enum-head: [C++0x]
1911/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1912/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1913///
1914/// enum-key: [C++0x]
1915/// 'enum'
1916/// 'enum' 'class'
1917/// 'enum' 'struct'
1918///
1919/// enum-base: [C++0x]
1920/// ':' type-specifier-seq
1921///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001922/// [C++] elaborated-type-specifier:
1923/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1924///
Chris Lattner4c97d762009-04-12 21:49:30 +00001925void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001926 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001927 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001928 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001929 if (Tok.is(tok::code_completion)) {
1930 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001931 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00001932 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00001933 }
1934
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001935 // If attributes exist after tag, parse them.
John McCall7f040a92010-12-24 02:08:15 +00001936 ParsedAttributes attrs;
1937 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001938
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001939 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00001940 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00001941 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00001942 return;
1943
1944 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001945 Diag(Tok, diag::err_expected_ident);
1946 if (Tok.isNot(tok::l_brace)) {
1947 // Has no name and is not a definition.
1948 // Skip the rest of this declarator, up until the comma or semicolon.
1949 SkipUntil(tok::comma, true);
1950 return;
1951 }
1952 }
1953 }
Mike Stump1eb44332009-09-09 15:08:12 +00001954
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001955 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001956 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001957
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001958 if (getLang().CPlusPlus0x &&
1959 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001960 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001961 IsScopedUsingClassTag = Tok.is(tok::kw_class);
1962 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001963 }
1964
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001965 // Must have either 'enum name' or 'enum {...}'.
1966 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1967 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001969 // Skip the rest of this declarator, up until the comma or semicolon.
1970 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001971 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001972 }
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001974 // If an identifier is present, consume and remember it.
1975 IdentifierInfo *Name = 0;
1976 SourceLocation NameLoc;
1977 if (Tok.is(tok::identifier)) {
1978 Name = Tok.getIdentifierInfo();
1979 NameLoc = ConsumeToken();
1980 }
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001982 if (!Name && IsScopedEnum) {
1983 // C++0x 7.2p2: The optional identifier shall not be omitted in the
1984 // declaration of a scoped enumeration.
1985 Diag(Tok, diag::err_scoped_enum_missing_identifier);
1986 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001987 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001988 }
1989
1990 TypeResult BaseType;
1991
Douglas Gregora61b3e72010-12-01 17:42:47 +00001992 // Parse the fixed underlying type.
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001993 if (getLang().CPlusPlus0x && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00001994 bool PossibleBitfield = false;
1995 if (getCurScope()->getFlags() & Scope::ClassScope) {
1996 // If we're in class scope, this can either be an enum declaration with
1997 // an underlying type, or a declaration of a bitfield member. We try to
1998 // use a simple disambiguation scheme first to catch the common cases
1999 // (integer literal, sizeof); if it's still ambiguous, we then consider
2000 // anything that's a simple-type-specifier followed by '(' as an
2001 // expression. This suffices because function types are not valid
2002 // underlying types anyway.
2003 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2004 // If the next token starts an expression, we know we're parsing a
2005 // bit-field. This is the common case.
2006 if (TPR == TPResult::True())
2007 PossibleBitfield = true;
2008 // If the next token starts a type-specifier-seq, it may be either a
2009 // a fixed underlying type or the start of a function-style cast in C++;
2010 // lookahead one more token to see if it's obvious that we have a
2011 // fixed underlying type.
2012 else if (TPR == TPResult::False() &&
2013 GetLookAheadToken(2).getKind() == tok::semi) {
2014 // Consume the ':'.
2015 ConsumeToken();
2016 } else {
2017 // We have the start of a type-specifier-seq, so we have to perform
2018 // tentative parsing to determine whether we have an expression or a
2019 // type.
2020 TentativeParsingAction TPA(*this);
2021
2022 // Consume the ':'.
2023 ConsumeToken();
2024
2025 if (isCXXDeclarationSpecifier() != TPResult::True()) {
2026 // We'll parse this as a bitfield later.
2027 PossibleBitfield = true;
2028 TPA.Revert();
2029 } else {
2030 // We have a type-specifier-seq.
2031 TPA.Commit();
2032 }
2033 }
2034 } else {
2035 // Consume the ':'.
2036 ConsumeToken();
2037 }
2038
2039 if (!PossibleBitfield) {
2040 SourceRange Range;
2041 BaseType = ParseTypeName(&Range);
2042 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002043 }
2044
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002045 // There are three options here. If we have 'enum foo;', then this is a
2046 // forward declaration. If we have 'enum foo {...' then this is a
2047 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2048 //
2049 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2050 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2051 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2052 //
John McCallf312b1e2010-08-26 23:41:50 +00002053 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002054 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002055 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002056 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002057 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002058 else
John McCallf312b1e2010-08-26 23:41:50 +00002059 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002060
2061 // enums cannot be templates, although they can be referenced from a
2062 // template.
2063 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002064 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002065 Diag(Tok, diag::err_enum_template);
2066
2067 // Skip the rest of this declarator, up until the comma or semicolon.
2068 SkipUntil(tok::comma, true);
2069 return;
2070 }
2071
Douglas Gregor402abb52009-05-28 23:31:59 +00002072 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002073 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002074 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2075 const char *PrevSpec = 0;
2076 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002077 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00002078 StartLoc, SS, Name, NameLoc, attrs.getList(),
John McCalld226f652010-08-21 09:40:31 +00002079 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002080 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002081 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002082 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002083
Douglas Gregor48c89f42010-04-24 16:38:41 +00002084 if (IsDependent) {
2085 // This enum has a dependent nested-name-specifier. Handle it as a
2086 // dependent tag.
2087 if (!Name) {
2088 DS.SetTypeSpecError();
2089 Diag(Tok, diag::err_expected_type_name_after_typename);
2090 return;
2091 }
2092
Douglas Gregor23c94db2010-07-02 17:43:08 +00002093 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002094 TUK, SS, Name, StartLoc,
2095 NameLoc);
2096 if (Type.isInvalid()) {
2097 DS.SetTypeSpecError();
2098 return;
2099 }
2100
2101 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallb3d87482010-08-24 05:47:05 +00002102 Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002103 Diag(StartLoc, DiagID) << PrevSpec;
2104
2105 return;
2106 }
Mike Stump1eb44332009-09-09 15:08:12 +00002107
John McCalld226f652010-08-21 09:40:31 +00002108 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002109 // The action failed to produce an enumeration tag. If this is a
2110 // definition, consume the entire definition.
2111 if (Tok.is(tok::l_brace)) {
2112 ConsumeBrace();
2113 SkipUntil(tok::r_brace);
2114 }
2115
2116 DS.SetTypeSpecError();
2117 return;
2118 }
2119
Chris Lattner04d66662007-10-09 17:33:22 +00002120 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002121 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002122
John McCallb3d87482010-08-24 05:47:05 +00002123 // FIXME: The DeclSpec should keep the locations of both the keyword
2124 // and the name (if there is one).
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002125 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCalld226f652010-08-21 09:40:31 +00002126 TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002127 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002128}
2129
2130/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2131/// enumerator-list:
2132/// enumerator
2133/// enumerator-list ',' enumerator
2134/// enumerator:
2135/// enumeration-constant
2136/// enumeration-constant '=' constant-expression
2137/// enumeration-constant:
2138/// identifier
2139///
John McCalld226f652010-08-21 09:40:31 +00002140void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002141 // Enter the scope of the enum body and start the definition.
2142 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002143 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002144
Reid Spencer5f016e22007-07-11 17:01:13 +00002145 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002146
Chris Lattner7946dd32007-08-27 17:24:30 +00002147 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002148 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002149 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002150
John McCalld226f652010-08-21 09:40:31 +00002151 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002152
John McCalld226f652010-08-21 09:40:31 +00002153 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002154
Reid Spencer5f016e22007-07-11 17:01:13 +00002155 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002156 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002157 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2158 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002159
John McCall5b629aa2010-10-22 23:36:17 +00002160 // If attributes exist after the enumerator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002161 ParsedAttributes attrs;
2162 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00002163
Reid Spencer5f016e22007-07-11 17:01:13 +00002164 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002165 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002166 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002167 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002168 AssignedVal = ParseConstantExpression();
2169 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002170 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002171 }
Mike Stump1eb44332009-09-09 15:08:12 +00002172
Reid Spencer5f016e22007-07-11 17:01:13 +00002173 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002174 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2175 LastEnumConstDecl,
2176 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00002177 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002178 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002179 EnumConstantDecls.push_back(EnumConstDecl);
2180 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002181
Douglas Gregor751f6922010-09-07 14:51:08 +00002182 if (Tok.is(tok::identifier)) {
2183 // We're missing a comma between enumerators.
2184 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2185 Diag(Loc, diag::err_enumerator_list_missing_comma)
2186 << FixItHint::CreateInsertion(Loc, ", ");
2187 continue;
2188 }
2189
Chris Lattner04d66662007-10-09 17:33:22 +00002190 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002191 break;
2192 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002193
2194 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002195 !(getLang().C99 || getLang().CPlusPlus0x))
2196 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2197 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002198 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002199 }
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Reid Spencer5f016e22007-07-11 17:01:13 +00002201 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002202 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002203
Reid Spencer5f016e22007-07-11 17:01:13 +00002204 // If attributes exist after the identifier list, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002205 ParsedAttributes attrs;
2206 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00002207
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002208 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2209 EnumConstantDecls.data(), EnumConstantDecls.size(),
John McCall7f040a92010-12-24 02:08:15 +00002210 getCurScope(), attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Douglas Gregor72de6672009-01-08 20:45:30 +00002212 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002213 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002214}
2215
2216/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002217/// start of a type-qualifier-list.
2218bool Parser::isTypeQualifier() const {
2219 switch (Tok.getKind()) {
2220 default: return false;
2221 // type-qualifier
2222 case tok::kw_const:
2223 case tok::kw_volatile:
2224 case tok::kw_restrict:
2225 return true;
2226 }
2227}
2228
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002229/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2230/// is definitely a type-specifier. Return false if it isn't part of a type
2231/// specifier or if we're not sure.
2232bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2233 switch (Tok.getKind()) {
2234 default: return false;
2235 // type-specifiers
2236 case tok::kw_short:
2237 case tok::kw_long:
2238 case tok::kw_signed:
2239 case tok::kw_unsigned:
2240 case tok::kw__Complex:
2241 case tok::kw__Imaginary:
2242 case tok::kw_void:
2243 case tok::kw_char:
2244 case tok::kw_wchar_t:
2245 case tok::kw_char16_t:
2246 case tok::kw_char32_t:
2247 case tok::kw_int:
2248 case tok::kw_float:
2249 case tok::kw_double:
2250 case tok::kw_bool:
2251 case tok::kw__Bool:
2252 case tok::kw__Decimal32:
2253 case tok::kw__Decimal64:
2254 case tok::kw__Decimal128:
2255 case tok::kw___vector:
2256
2257 // struct-or-union-specifier (C99) or class-specifier (C++)
2258 case tok::kw_class:
2259 case tok::kw_struct:
2260 case tok::kw_union:
2261 // enum-specifier
2262 case tok::kw_enum:
2263
2264 // typedef-name
2265 case tok::annot_typename:
2266 return true;
2267 }
2268}
2269
Steve Naroff5f8aa692008-02-11 23:15:56 +00002270/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002271/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002272bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002273 switch (Tok.getKind()) {
2274 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002275
Chris Lattner166a8fc2009-01-04 23:41:41 +00002276 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002277 if (TryAltiVecVectorToken())
2278 return true;
2279 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002280 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002281 // Annotate typenames and C++ scope specifiers. If we get one, just
2282 // recurse to handle whatever we get.
2283 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002284 return true;
2285 if (Tok.is(tok::identifier))
2286 return false;
2287 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002288
Chris Lattner166a8fc2009-01-04 23:41:41 +00002289 case tok::coloncolon: // ::foo::bar
2290 if (NextToken().is(tok::kw_new) || // ::new
2291 NextToken().is(tok::kw_delete)) // ::delete
2292 return false;
2293
Chris Lattner166a8fc2009-01-04 23:41:41 +00002294 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002295 return true;
2296 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002297
Reid Spencer5f016e22007-07-11 17:01:13 +00002298 // GNU attributes support.
2299 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002300 // GNU typeof support.
2301 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002302
Reid Spencer5f016e22007-07-11 17:01:13 +00002303 // type-specifiers
2304 case tok::kw_short:
2305 case tok::kw_long:
2306 case tok::kw_signed:
2307 case tok::kw_unsigned:
2308 case tok::kw__Complex:
2309 case tok::kw__Imaginary:
2310 case tok::kw_void:
2311 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002312 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002313 case tok::kw_char16_t:
2314 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002315 case tok::kw_int:
2316 case tok::kw_float:
2317 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002318 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002319 case tok::kw__Bool:
2320 case tok::kw__Decimal32:
2321 case tok::kw__Decimal64:
2322 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002323 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002324
Chris Lattner99dc9142008-04-13 18:59:07 +00002325 // struct-or-union-specifier (C99) or class-specifier (C++)
2326 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002327 case tok::kw_struct:
2328 case tok::kw_union:
2329 // enum-specifier
2330 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Reid Spencer5f016e22007-07-11 17:01:13 +00002332 // type-qualifier
2333 case tok::kw_const:
2334 case tok::kw_volatile:
2335 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002336
2337 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002338 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002339 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002340
Chris Lattner7c186be2008-10-20 00:25:30 +00002341 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2342 case tok::less:
2343 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002344
Steve Naroff239f0732008-12-25 14:16:32 +00002345 case tok::kw___cdecl:
2346 case tok::kw___stdcall:
2347 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002348 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002349 case tok::kw___w64:
2350 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002351 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002352 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002353 }
2354}
2355
2356/// isDeclarationSpecifier() - Return true if the current token is part of a
2357/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002358///
2359/// \param DisambiguatingWithExpression True to indicate that the purpose of
2360/// this check is to disambiguate between an expression and a declaration.
2361bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002362 switch (Tok.getKind()) {
2363 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002364
Chris Lattner166a8fc2009-01-04 23:41:41 +00002365 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002366 // Unfortunate hack to support "Class.factoryMethod" notation.
2367 if (getLang().ObjC1 && NextToken().is(tok::period))
2368 return false;
John Thompson82287d12010-02-05 00:12:22 +00002369 if (TryAltiVecVectorToken())
2370 return true;
2371 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002372 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002373 // Annotate typenames and C++ scope specifiers. If we get one, just
2374 // recurse to handle whatever we get.
2375 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002376 return true;
2377 if (Tok.is(tok::identifier))
2378 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002379
2380 // If we're in Objective-C and we have an Objective-C class type followed
2381 // by an identifier and then either ':' or ']', in a place where an
2382 // expression is permitted, then this is probably a class message send
2383 // missing the initial '['. In this case, we won't consider this to be
2384 // the start of a declaration.
2385 if (DisambiguatingWithExpression &&
2386 isStartOfObjCClassMessageMissingOpenBracket())
2387 return false;
2388
John McCall9ba61662010-02-26 08:45:28 +00002389 return isDeclarationSpecifier();
2390
Chris Lattner166a8fc2009-01-04 23:41:41 +00002391 case tok::coloncolon: // ::foo::bar
2392 if (NextToken().is(tok::kw_new) || // ::new
2393 NextToken().is(tok::kw_delete)) // ::delete
2394 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002395
Chris Lattner166a8fc2009-01-04 23:41:41 +00002396 // Annotate typenames and C++ scope specifiers. If we get one, just
2397 // recurse to handle whatever we get.
2398 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002399 return true;
2400 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002401
Reid Spencer5f016e22007-07-11 17:01:13 +00002402 // storage-class-specifier
2403 case tok::kw_typedef:
2404 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002405 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002406 case tok::kw_static:
2407 case tok::kw_auto:
2408 case tok::kw_register:
2409 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002410
Reid Spencer5f016e22007-07-11 17:01:13 +00002411 // type-specifiers
2412 case tok::kw_short:
2413 case tok::kw_long:
2414 case tok::kw_signed:
2415 case tok::kw_unsigned:
2416 case tok::kw__Complex:
2417 case tok::kw__Imaginary:
2418 case tok::kw_void:
2419 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002420 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002421 case tok::kw_char16_t:
2422 case tok::kw_char32_t:
2423
Reid Spencer5f016e22007-07-11 17:01:13 +00002424 case tok::kw_int:
2425 case tok::kw_float:
2426 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002427 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002428 case tok::kw__Bool:
2429 case tok::kw__Decimal32:
2430 case tok::kw__Decimal64:
2431 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002432 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002433
Chris Lattner99dc9142008-04-13 18:59:07 +00002434 // struct-or-union-specifier (C99) or class-specifier (C++)
2435 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002436 case tok::kw_struct:
2437 case tok::kw_union:
2438 // enum-specifier
2439 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002440
Reid Spencer5f016e22007-07-11 17:01:13 +00002441 // type-qualifier
2442 case tok::kw_const:
2443 case tok::kw_volatile:
2444 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002445
Reid Spencer5f016e22007-07-11 17:01:13 +00002446 // function-specifier
2447 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002448 case tok::kw_virtual:
2449 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002450
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002451 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002452 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002453
Chris Lattner1ef08762007-08-09 17:01:07 +00002454 // GNU typeof support.
2455 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002456
Chris Lattner1ef08762007-08-09 17:01:07 +00002457 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002458 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002459 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002460
Chris Lattnerf3948c42008-07-26 03:38:44 +00002461 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2462 case tok::less:
2463 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002464
Steve Naroff47f52092009-01-06 19:34:12 +00002465 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002466 case tok::kw___cdecl:
2467 case tok::kw___stdcall:
2468 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002469 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002470 case tok::kw___w64:
2471 case tok::kw___ptr64:
2472 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002473 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002474 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002475 }
2476}
2477
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002478bool Parser::isConstructorDeclarator() {
2479 TentativeParsingAction TPA(*this);
2480
2481 // Parse the C++ scope specifier.
2482 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002483 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00002484 TPA.Revert();
2485 return false;
2486 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002487
2488 // Parse the constructor name.
2489 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2490 // We already know that we have a constructor name; just consume
2491 // the token.
2492 ConsumeToken();
2493 } else {
2494 TPA.Revert();
2495 return false;
2496 }
2497
2498 // Current class name must be followed by a left parentheses.
2499 if (Tok.isNot(tok::l_paren)) {
2500 TPA.Revert();
2501 return false;
2502 }
2503 ConsumeParen();
2504
2505 // A right parentheses or ellipsis signals that we have a constructor.
2506 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2507 TPA.Revert();
2508 return true;
2509 }
2510
2511 // If we need to, enter the specified scope.
2512 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002513 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002514 DeclScopeObj.EnterDeclaratorScope();
2515
2516 // Check whether the next token(s) are part of a declaration
2517 // specifier, in which case we have the start of a parameter and,
2518 // therefore, we know that this is a constructor.
2519 bool IsConstructor = isDeclarationSpecifier();
2520 TPA.Revert();
2521 return IsConstructor;
2522}
Reid Spencer5f016e22007-07-11 17:01:13 +00002523
2524/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00002525/// type-qualifier-list: [C99 6.7.5]
2526/// type-qualifier
2527/// [vendor] attributes
2528/// [ only if VendorAttributesAllowed=true ]
2529/// type-qualifier-list type-qualifier
2530/// [vendor] type-qualifier-list attributes
2531/// [ only if VendorAttributesAllowed=true ]
2532/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2533/// [ only if CXX0XAttributesAllowed=true ]
2534/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00002535///
Dawn Perchik52fc3142010-09-03 01:29:35 +00002536void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2537 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00002538 bool CXX0XAttributesAllowed) {
2539 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2540 SourceLocation Loc = Tok.getLocation();
John McCall7f040a92010-12-24 02:08:15 +00002541 ParsedAttributesWithRange attrs;
2542 ParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002543 if (CXX0XAttributesAllowed)
John McCall7f040a92010-12-24 02:08:15 +00002544 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00002545 else
2546 Diag(Loc, diag::err_attributes_not_allowed);
2547 }
2548
Reid Spencer5f016e22007-07-11 17:01:13 +00002549 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002550 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002551 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002552 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002553 SourceLocation Loc = Tok.getLocation();
2554
2555 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00002556 case tok::code_completion:
2557 Actions.CodeCompleteTypeQualifiers(DS);
2558 ConsumeCodeCompletionToken();
2559 break;
2560
Reid Spencer5f016e22007-07-11 17:01:13 +00002561 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002562 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2563 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002564 break;
2565 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002566 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2567 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002568 break;
2569 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002570 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2571 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002572 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002573 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002574 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002575 case tok::kw___cdecl:
2576 case tok::kw___stdcall:
2577 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002578 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002579 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00002580 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002581 continue;
2582 }
2583 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002584 case tok::kw___pascal:
2585 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00002586 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002587 continue;
2588 }
2589 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002590 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002591 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00002592 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002593 continue; // do *not* consume the next token!
2594 }
2595 // otherwise, FALL THROUGH!
2596 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002597 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002598 // If this is not a type-qualifier token, we're done reading type
2599 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002600 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002601 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002602 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002603
Reid Spencer5f016e22007-07-11 17:01:13 +00002604 // If the specifier combination wasn't legal, issue a diagnostic.
2605 if (isInvalid) {
2606 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002607 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002608 }
2609 ConsumeToken();
2610 }
2611}
2612
2613
2614/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2615///
2616void Parser::ParseDeclarator(Declarator &D) {
2617 /// This implements the 'declarator' production in the C grammar, then checks
2618 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002619 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002620}
2621
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002622/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2623/// is parsed by the function passed to it. Pass null, and the direct-declarator
2624/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002625/// ptr-operator production.
2626///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002627/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2628/// [C] pointer[opt] direct-declarator
2629/// [C++] direct-declarator
2630/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002631///
2632/// pointer: [C99 6.7.5]
2633/// '*' type-qualifier-list[opt]
2634/// '*' type-qualifier-list[opt] pointer
2635///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002636/// ptr-operator:
2637/// '*' cv-qualifier-seq[opt]
2638/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002639/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002640/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002641/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002642/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002643void Parser::ParseDeclaratorInternal(Declarator &D,
2644 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002645 if (Diags.hasAllExtensionsSilenced())
2646 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002647
Sebastian Redlf30208a2009-01-24 21:16:55 +00002648 // C++ member pointers start with a '::' or a nested-name.
2649 // Member pointers get special handling, since there's no place for the
2650 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002651 if (getLang().CPlusPlus &&
2652 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2653 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002654 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002655 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00002656
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002657 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002658 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002659 // The scope spec really belongs to the direct-declarator.
2660 D.getCXXScopeSpec() = SS;
2661 if (DirectDeclParser)
2662 (this->*DirectDeclParser)(D);
2663 return;
2664 }
2665
2666 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002667 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002668 DeclSpec DS;
2669 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002670 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002671
2672 // Recurse to parse whatever is left.
2673 ParseDeclaratorInternal(D, DirectDeclParser);
2674
2675 // Sema will have to catch (syntactically invalid) pointers into global
2676 // scope. It has to catch pointers into namespace scope anyway.
2677 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall7f040a92010-12-24 02:08:15 +00002678 Loc, DS.takeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002679 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002680 return;
2681 }
2682 }
2683
2684 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002685 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002686 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002687 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002688 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002689 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002690 if (DirectDeclParser)
2691 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002692 return;
2693 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002694
Sebastian Redl05532f22009-03-15 22:02:01 +00002695 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2696 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002697 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002698 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002699
Chris Lattner9af55002009-03-27 04:18:06 +00002700 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002701 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002702 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002703
Reid Spencer5f016e22007-07-11 17:01:13 +00002704 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002705 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002706
Reid Spencer5f016e22007-07-11 17:01:13 +00002707 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002708 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002709 if (Kind == tok::star)
2710 // Remember that we parsed a pointer type, and remember the type-quals.
2711 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
John McCall7f040a92010-12-24 02:08:15 +00002712 DS.takeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002713 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002714 else
2715 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002716 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall7f040a92010-12-24 02:08:15 +00002717 Loc, DS.takeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002718 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002719 } else {
2720 // Is a reference
2721 DeclSpec DS;
2722
Sebastian Redl743de1f2009-03-23 00:00:23 +00002723 // Complain about rvalue references in C++03, but then go on and build
2724 // the declarator.
2725 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2726 Diag(Loc, diag::err_rvalue_reference);
2727
Reid Spencer5f016e22007-07-11 17:01:13 +00002728 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2729 // cv-qualifiers are introduced through the use of a typedef or of a
2730 // template type argument, in which case the cv-qualifiers are ignored.
2731 //
2732 // [GNU] Retricted references are allowed.
2733 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002734 // [C++0x] Attributes on references are not allowed.
2735 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002736 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002737
2738 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2739 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2740 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002741 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002742 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2743 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002744 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002745 }
2746
2747 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002748 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002749
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002750 if (D.getNumTypeObjects() > 0) {
2751 // C++ [dcl.ref]p4: There shall be no references to references.
2752 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2753 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002754 if (const IdentifierInfo *II = D.getIdentifier())
2755 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2756 << II;
2757 else
2758 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2759 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002760
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002761 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002762 // can go ahead and build the (technically ill-formed)
2763 // declarator: reference collapsing will take care of it.
2764 }
2765 }
2766
Reid Spencer5f016e22007-07-11 17:01:13 +00002767 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002768 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
John McCall7f040a92010-12-24 02:08:15 +00002769 DS.takeAttributes(),
Sebastian Redl05532f22009-03-15 22:02:01 +00002770 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002771 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002772 }
2773}
2774
2775/// ParseDirectDeclarator
2776/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002777/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002778/// '(' declarator ')'
2779/// [GNU] '(' attributes declarator ')'
2780/// [C90] direct-declarator '[' constant-expression[opt] ']'
2781/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2782/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2783/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2784/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2785/// direct-declarator '(' parameter-type-list ')'
2786/// direct-declarator '(' identifier-list[opt] ')'
2787/// [GNU] direct-declarator '(' parameter-forward-declarations
2788/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002789/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2790/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002791/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002792///
2793/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002794/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00002795/// '::'[opt] nested-name-specifier[opt] type-name
2796///
2797/// id-expression: [C++ 5.1]
2798/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002799/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002800///
2801/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002802/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002803/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002804/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002805/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002806/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002807///
Reid Spencer5f016e22007-07-11 17:01:13 +00002808void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002809 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002810
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002811 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2812 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002813 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00002814 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00002815 }
2816
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002817 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002818 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00002819 // Change the declaration context for name lookup, until this function
2820 // is exited (and the declarator has been parsed).
2821 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002822 }
2823
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002824 // C++0x [dcl.fct]p14:
2825 // There is a syntactic ambiguity when an ellipsis occurs at the end
2826 // of a parameter-declaration-clause without a preceding comma. In
2827 // this case, the ellipsis is parsed as part of the
2828 // abstract-declarator if the type of the parameter names a template
2829 // parameter pack that has not been expanded; otherwise, it is parsed
2830 // as part of the parameter-declaration-clause.
2831 if (Tok.is(tok::ellipsis) &&
2832 !((D.getContext() == Declarator::PrototypeContext ||
2833 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00002834 NextToken().is(tok::r_paren) &&
2835 !Actions.containsUnexpandedParameterPacks(D)))
2836 D.setEllipsisLoc(ConsumeToken());
2837
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002838 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2839 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2840 // We found something that indicates the start of an unqualified-id.
2841 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002842 bool AllowConstructorName;
2843 if (D.getDeclSpec().hasTypeSpecifier())
2844 AllowConstructorName = false;
2845 else if (D.getCXXScopeSpec().isSet())
2846 AllowConstructorName =
2847 (D.getContext() == Declarator::FileContext ||
2848 (D.getContext() == Declarator::MemberContext &&
2849 D.getDeclSpec().isFriendSpecified()));
2850 else
2851 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2852
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002853 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2854 /*EnteringContext=*/true,
2855 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002856 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002857 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002858 D.getName()) ||
2859 // Once we're past the identifier, if the scope was bad, mark the
2860 // whole declarator bad.
2861 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002862 D.SetIdentifier(0, Tok.getLocation());
2863 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002864 } else {
2865 // Parsed the unqualified-id; update range information and move along.
2866 if (D.getSourceRange().getBegin().isInvalid())
2867 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2868 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002869 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002870 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002871 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002872 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002873 assert(!getLang().CPlusPlus &&
2874 "There's a C++-specific check for tok::identifier above");
2875 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2876 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2877 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002878 goto PastIdentifier;
2879 }
2880
2881 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002882 // direct-declarator: '(' declarator ')'
2883 // direct-declarator: '(' attributes declarator ')'
2884 // Example: 'char (*X)' or 'int (*XX)(void)'
2885 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002886
2887 // If the declarator was parenthesized, we entered the declarator
2888 // scope when parsing the parenthesized declarator, then exited
2889 // the scope already. Re-enter the scope, if we need to.
2890 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002891 // If there was an error parsing parenthesized declarator, declarator
2892 // scope may have been enterred before. Don't do it again.
2893 if (!D.isInvalidType() &&
2894 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002895 // Change the declaration context for name lookup, until this function
2896 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002897 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002898 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002899 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002900 // This could be something simple like "int" (in which case the declarator
2901 // portion is empty), if an abstract-declarator is allowed.
2902 D.SetIdentifier(0, Tok.getLocation());
2903 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002904 if (D.getContext() == Declarator::MemberContext)
2905 Diag(Tok, diag::err_expected_member_name_or_semi)
2906 << D.getDeclSpec().getSourceRange();
2907 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002908 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002909 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002910 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002911 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002912 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002913 }
Mike Stump1eb44332009-09-09 15:08:12 +00002914
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002915 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002916 assert(D.isPastIdentifier() &&
2917 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002918
Sean Huntbbd37c62009-11-21 08:43:09 +00002919 // Don't parse attributes unless we have an identifier.
John McCall7f040a92010-12-24 02:08:15 +00002920 if (D.getIdentifier())
2921 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00002922
Reid Spencer5f016e22007-07-11 17:01:13 +00002923 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002924 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002925 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2926 // In such a case, check if we actually have a function declarator; if it
2927 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002928 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2929 // When not in file scope, warn for ambiguous function declarators, just
2930 // in case the author intended it as a variable definition.
2931 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2932 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2933 break;
2934 }
John McCall7f040a92010-12-24 02:08:15 +00002935 ParsedAttributes attrs;
2936 ParseFunctionDeclarator(ConsumeParen(), D, attrs);
Chris Lattner04d66662007-10-09 17:33:22 +00002937 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002938 ParseBracketDeclarator(D);
2939 } else {
2940 break;
2941 }
2942 }
2943}
2944
Chris Lattneref4715c2008-04-06 05:45:57 +00002945/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2946/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002947/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002948/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2949///
2950/// direct-declarator:
2951/// '(' declarator ')'
2952/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002953/// direct-declarator '(' parameter-type-list ')'
2954/// direct-declarator '(' identifier-list[opt] ')'
2955/// [GNU] direct-declarator '(' parameter-forward-declarations
2956/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002957///
2958void Parser::ParseParenDeclarator(Declarator &D) {
2959 SourceLocation StartLoc = ConsumeParen();
2960 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002961
Chris Lattner7399ee02008-10-20 02:05:46 +00002962 // Eat any attributes before we look at whether this is a grouping or function
2963 // declarator paren. If this is a grouping paren, the attribute applies to
2964 // the type being built up, for example:
2965 // int (__attribute__(()) *x)(long y)
2966 // If this ends up not being a grouping paren, the attribute applies to the
2967 // first argument, for example:
2968 // int (__attribute__(()) int x)
2969 // In either case, we need to eat any attributes to be able to determine what
2970 // sort of paren this is.
2971 //
John McCall7f040a92010-12-24 02:08:15 +00002972 ParsedAttributes attrs;
Chris Lattner7399ee02008-10-20 02:05:46 +00002973 bool RequiresArg = false;
2974 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00002975 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002976
Chris Lattner7399ee02008-10-20 02:05:46 +00002977 // We require that the argument list (if this is a non-grouping paren) be
2978 // present even if the attribute list was empty.
2979 RequiresArg = true;
2980 }
Steve Naroff239f0732008-12-25 14:16:32 +00002981 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002982 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002983 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2984 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
John McCall7f040a92010-12-24 02:08:15 +00002985 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00002986 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00002987 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002988 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00002989 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002990
Chris Lattneref4715c2008-04-06 05:45:57 +00002991 // If we haven't past the identifier yet (or where the identifier would be
2992 // stored, if this is an abstract declarator), then this is probably just
2993 // grouping parens. However, if this could be an abstract-declarator, then
2994 // this could also be the start of function arguments (consider 'void()').
2995 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002996
Chris Lattneref4715c2008-04-06 05:45:57 +00002997 if (!D.mayOmitIdentifier()) {
2998 // If this can't be an abstract-declarator, this *must* be a grouping
2999 // paren, because we haven't seen the identifier yet.
3000 isGrouping = true;
3001 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003002 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003003 isDeclarationSpecifier()) { // 'int(int)' is a function.
3004 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3005 // considered to be a type, not a K&R identifier-list.
3006 isGrouping = false;
3007 } else {
3008 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3009 isGrouping = true;
3010 }
Mike Stump1eb44332009-09-09 15:08:12 +00003011
Chris Lattneref4715c2008-04-06 05:45:57 +00003012 // If this is a grouping paren, handle:
3013 // direct-declarator: '(' declarator ')'
3014 // direct-declarator: '(' attributes declarator ')'
3015 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003016 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003017 D.setGroupingParens(true);
John McCall7f040a92010-12-24 02:08:15 +00003018 if (!attrs.empty())
3019 D.addAttributes(attrs.getList(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003020
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003021 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003022 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003023 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
3024 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc), EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003025
3026 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003027 return;
3028 }
Mike Stump1eb44332009-09-09 15:08:12 +00003029
Chris Lattneref4715c2008-04-06 05:45:57 +00003030 // Okay, if this wasn't a grouping paren, it must be the start of a function
3031 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003032 // identifier (and remember where it would have been), then call into
3033 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003034 D.SetIdentifier(0, Tok.getLocation());
3035
John McCall7f040a92010-12-24 02:08:15 +00003036 ParseFunctionDeclarator(StartLoc, D, attrs, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003037}
3038
3039/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3040/// declarator D up to a paren, which indicates that we are parsing function
3041/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003042///
Chris Lattner7399ee02008-10-20 02:05:46 +00003043/// If AttrList is non-null, then the caller parsed those arguments immediately
3044/// after the open paren - they should be considered to be the first argument of
3045/// a parameter. If RequiresArg is true, then the first argument of the
3046/// function is required to be present and required to not be an identifier
3047/// list.
3048///
Reid Spencer5f016e22007-07-11 17:01:13 +00003049/// This method also handles this portion of the grammar:
3050/// parameter-type-list: [C99 6.7.5]
3051/// parameter-list
3052/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003053/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003054///
3055/// parameter-list: [C99 6.7.5]
3056/// parameter-declaration
3057/// parameter-list ',' parameter-declaration
3058///
3059/// parameter-declaration: [C99 6.7.5]
3060/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003061/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003062/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003063/// declaration-specifiers abstract-declarator[opt]
3064/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003065/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003066/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3067///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003068/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00003069/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003070///
Chris Lattner7399ee02008-10-20 02:05:46 +00003071void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
John McCall7f040a92010-12-24 02:08:15 +00003072 ParsedAttributes &attrs,
Chris Lattner7399ee02008-10-20 02:05:46 +00003073 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003074 // lparen is already consumed!
3075 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003076
Douglas Gregordab60ad2010-10-01 18:44:50 +00003077 ParsedType TrailingReturnType;
3078
Chris Lattner7399ee02008-10-20 02:05:46 +00003079 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003080 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003081 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003082 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003083
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003084 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3085 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003086
3087 // cv-qualifier-seq[opt].
3088 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003089 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003090 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003091 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003092 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003093 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003094 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003095 MaybeParseCXX0XAttributes(attrs);
3096
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003097 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003098 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003099 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003100
3101 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003102 if (Tok.is(tok::kw_throw)) {
3103 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003104 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003105 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003106 hasAnyExceptionSpec);
3107 assert(Exceptions.size() == ExceptionRanges.size() &&
3108 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003109 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00003110
3111 // Parse trailing-return-type.
3112 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3113 TrailingReturnType = ParseTrailingReturnType().get();
3114 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003115 }
3116
Chris Lattnerf97409f2008-04-06 06:57:35 +00003117 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003118 // int() -> no prototype, no '...'.
John McCall7f040a92010-12-24 02:08:15 +00003119 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3120 /*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003121 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003122 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003123 /*arglist*/ 0, 0,
3124 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003125 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003126 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003127 Exceptions.data(),
3128 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003129 Exceptions.size(),
Douglas Gregordab60ad2010-10-01 18:44:50 +00003130 LParenLoc, RParenLoc, D,
3131 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003132 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003133 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003134 }
3135
Chris Lattner7399ee02008-10-20 02:05:46 +00003136 // Alternatively, this parameter list may be an identifier list form for a
3137 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003138 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3139 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003140 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003141 // K&R identifier lists can't have typedefs as identifiers, per
3142 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003143 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003144 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003145
Steve Naroff2d081c42009-01-28 19:16:40 +00003146 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003147 // normal declarators, not for abstract-declarators. Get the first
3148 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003149 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003150 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003151
3152 // Identifier lists follow a really simple grammar: the identifiers can
3153 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3154 // identifier lists are really rare in the brave new modern world, and it
3155 // is very common for someone to typo a type in a non-k&r style list. If
3156 // we are presented with something like: "void foo(intptr x, float y)",
3157 // we don't want to start parsing the function declarator as though it is
3158 // a K&R style declarator just because intptr is an invalid type.
3159 //
3160 // To handle this, we check to see if the token after the first identifier
3161 // is a "," or ")". Only if so, do we parse it as an identifier list.
3162 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3163 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3164 FirstTok.getIdentifierInfo(),
3165 FirstTok.getLocation(), D);
3166
3167 // If we get here, the code is invalid. Push the first identifier back
3168 // into the token stream and parse the first argument as an (invalid)
3169 // normal argument declarator.
3170 PP.EnterToken(Tok);
3171 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003172 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003173 }
Mike Stump1eb44332009-09-09 15:08:12 +00003174
Chris Lattnerf97409f2008-04-06 06:57:35 +00003175 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003176
Chris Lattnerf97409f2008-04-06 06:57:35 +00003177 // Build up an array of information about the parsed arguments.
3178 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003179
3180 // Enter function-declaration scope, limiting any declarators to the
3181 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003182 ParseScope PrototypeScope(this,
3183 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003184
Chris Lattnerf97409f2008-04-06 06:57:35 +00003185 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003186 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003187 while (1) {
3188 if (Tok.is(tok::ellipsis)) {
3189 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003190 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003191 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003192 }
Mike Stump1eb44332009-09-09 15:08:12 +00003193
Chris Lattnerf97409f2008-04-06 06:57:35 +00003194 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003195 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003196 DeclSpec DS;
John McCall7f040a92010-12-24 02:08:15 +00003197
3198 // Skip any Microsoft attributes before a param.
3199 if (getLang().Microsoft && Tok.is(tok::l_square))
3200 ParseMicrosoftAttributes(DS.getAttributes());
3201
3202 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00003203
3204 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00003205 // Take them so that we only apply the attributes to the first parameter.
3206 DS.takeAttributesFrom(attrs);
3207
Chris Lattnere64c5492009-02-27 18:38:20 +00003208 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003209
Chris Lattnerf97409f2008-04-06 06:57:35 +00003210 // Parse the declarator. This is "PrototypeContext", because we must
3211 // accept either 'declarator' or 'abstract-declarator' here.
3212 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3213 ParseDeclarator(ParmDecl);
3214
3215 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00003216 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003217
Chris Lattnerf97409f2008-04-06 06:57:35 +00003218 // Remember this parsed parameter in ParamInfo.
3219 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003220
Douglas Gregor72b505b2008-12-16 21:30:33 +00003221 // DefArgToks is used when the parsing of default arguments needs
3222 // to be delayed.
3223 CachedTokens *DefArgToks = 0;
3224
Chris Lattnerf97409f2008-04-06 06:57:35 +00003225 // If no parameter was specified, verify that *something* was specified,
3226 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003227 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3228 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003229 // Completely missing, emit error.
3230 Diag(DSStart, diag::err_missing_param);
3231 } else {
3232 // Otherwise, we have something. Add it and let semantic analysis try
3233 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003234
Chris Lattnerf97409f2008-04-06 06:57:35 +00003235 // Inform the actions module about the parameter declarator, so it gets
3236 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003237 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003238
3239 // Parse the default argument, if any. We parse the default
3240 // arguments in all dialects; the semantic analysis in
3241 // ActOnParamDefaultArgument will reject the default argument in
3242 // C.
3243 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003244 SourceLocation EqualLoc = Tok.getLocation();
3245
Chris Lattner04421082008-04-08 04:40:51 +00003246 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003247 if (D.getContext() == Declarator::MemberContext) {
3248 // If we're inside a class definition, cache the tokens
3249 // corresponding to the default argument. We'll actually parse
3250 // them when we see the end of the class definition.
3251 // FIXME: Templates will require something similar.
3252 // FIXME: Can we use a smart pointer for Toks?
3253 DefArgToks = new CachedTokens;
3254
Mike Stump1eb44332009-09-09 15:08:12 +00003255 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003256 /*StopAtSemi=*/true,
3257 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003258 delete DefArgToks;
3259 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003260 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003261 } else {
3262 // Mark the end of the default argument so that we know when to
3263 // stop when we parse it later on.
3264 Token DefArgEnd;
3265 DefArgEnd.startToken();
3266 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3267 DefArgEnd.setLocation(Tok.getLocation());
3268 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003269 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003270 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003271 }
Chris Lattner04421082008-04-08 04:40:51 +00003272 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003273 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003274 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003275
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003276 // The argument isn't actually potentially evaluated unless it is
3277 // used.
3278 EnterExpressionEvaluationContext Eval(Actions,
3279 Sema::PotentiallyEvaluatedIfUsed);
3280
John McCall60d7b3a2010-08-24 06:29:42 +00003281 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003282 if (DefArgResult.isInvalid()) {
3283 Actions.ActOnParamDefaultArgumentError(Param);
3284 SkipUntil(tok::comma, tok::r_paren, true, true);
3285 } else {
3286 // Inform the actions module about the default argument
3287 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003288 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003289 }
Chris Lattner04421082008-04-08 04:40:51 +00003290 }
3291 }
Mike Stump1eb44332009-09-09 15:08:12 +00003292
3293 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3294 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003295 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003296 }
3297
3298 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003299 if (Tok.isNot(tok::comma)) {
3300 if (Tok.is(tok::ellipsis)) {
3301 IsVariadic = true;
3302 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3303
3304 if (!getLang().CPlusPlus) {
3305 // We have ellipsis without a preceding ',', which is ill-formed
3306 // in C. Complain and provide the fix.
3307 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003308 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003309 }
3310 }
3311
3312 break;
3313 }
Mike Stump1eb44332009-09-09 15:08:12 +00003314
Chris Lattnerf97409f2008-04-06 06:57:35 +00003315 // Consume the comma.
3316 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003317 }
Mike Stump1eb44332009-09-09 15:08:12 +00003318
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003319 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003320 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3321 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003322
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003323 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003324 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003325 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003326 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003327 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003328 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003329
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003330 if (getLang().CPlusPlus) {
John McCall7f040a92010-12-24 02:08:15 +00003331 MaybeParseCXX0XAttributes(attrs);
3332
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003333 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003334 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003335 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003336 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003337
3338 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003339 if (Tok.is(tok::kw_throw)) {
3340 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003341 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003342 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003343 hasAnyExceptionSpec);
3344 assert(Exceptions.size() == ExceptionRanges.size() &&
3345 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003346 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00003347
3348 // Parse trailing-return-type.
3349 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3350 TrailingReturnType = ParseTrailingReturnType().get();
3351 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003352 }
3353
Douglas Gregordab60ad2010-10-01 18:44:50 +00003354 // FIXME: We should leave the prototype scope before parsing the exception
3355 // specification, and then reenter it when parsing the trailing return type.
3356
3357 // Leave prototype scope.
3358 PrototypeScope.Exit();
3359
Reid Spencer5f016e22007-07-11 17:01:13 +00003360 // Remember that we parsed a function type, and remember the attributes.
John McCall7f040a92010-12-24 02:08:15 +00003361 D.AddTypeInfo(DeclaratorChunk::getFunction(attrs,
3362 /*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003363 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003364 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003365 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003366 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003367 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003368 Exceptions.data(),
3369 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003370 Exceptions.size(),
Douglas Gregordab60ad2010-10-01 18:44:50 +00003371 LParenLoc, RParenLoc, D,
3372 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003373 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003374}
3375
Chris Lattner66d28652008-04-06 06:34:08 +00003376/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3377/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003378/// first identifier has already been consumed, and the current token is the
3379/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003380///
3381/// identifier-list: [C99 6.7.5]
3382/// identifier
3383/// identifier-list ',' identifier
3384///
3385void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003386 IdentifierInfo *FirstIdent,
3387 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003388 Declarator &D) {
3389 // Build up an array of information about the parsed arguments.
3390 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3391 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003392
Chris Lattner66d28652008-04-06 06:34:08 +00003393 // If there was no identifier specified for the declarator, either we are in
3394 // an abstract-declarator, or we are in a parameter declarator which was found
3395 // to be abstract. In abstract-declarators, identifier lists are not valid:
3396 // diagnose this.
3397 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003398 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003399
Chris Lattner83a94472010-05-14 17:23:36 +00003400 // The first identifier was already read, and is known to be the first
3401 // identifier in the list. Remember this identifier in ParamInfo.
3402 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003403 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003404
Chris Lattner66d28652008-04-06 06:34:08 +00003405 while (Tok.is(tok::comma)) {
3406 // Eat the comma.
3407 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003408
Chris Lattner50c64772008-04-06 06:39:19 +00003409 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003410 if (Tok.isNot(tok::identifier)) {
3411 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003412 SkipUntil(tok::r_paren);
3413 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003414 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003415
Chris Lattner66d28652008-04-06 06:34:08 +00003416 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003417
3418 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003419 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003420 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003421
Chris Lattner66d28652008-04-06 06:34:08 +00003422 // Verify that the argument identifier has not already been mentioned.
3423 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003424 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003425 } else {
3426 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003427 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003428 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00003429 0));
Chris Lattner50c64772008-04-06 06:39:19 +00003430 }
Mike Stump1eb44332009-09-09 15:08:12 +00003431
Chris Lattner66d28652008-04-06 06:34:08 +00003432 // Eat the identifier.
3433 ConsumeToken();
3434 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003435
3436 // If we have the closing ')', eat it and we're done.
3437 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3438
Chris Lattner50c64772008-04-06 06:39:19 +00003439 // Remember that we parsed a function type, and remember the attributes. This
3440 // function type is always a K&R style function type, which is not varargs and
3441 // has no prototype.
John McCall7f040a92010-12-24 02:08:15 +00003442 D.AddTypeInfo(DeclaratorChunk::getFunction(ParsedAttributes(),
3443 /*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003444 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003445 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003446 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003447 /*exception*/false,
3448 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003449 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003450 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003451}
Chris Lattneref4715c2008-04-06 05:45:57 +00003452
Reid Spencer5f016e22007-07-11 17:01:13 +00003453/// [C90] direct-declarator '[' constant-expression[opt] ']'
3454/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3455/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3456/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3457/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3458void Parser::ParseBracketDeclarator(Declarator &D) {
3459 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003460
Chris Lattner378c7e42008-12-18 07:27:21 +00003461 // C array syntax has many features, but by-far the most common is [] and [4].
3462 // This code does a fast path to handle some of the most obvious cases.
3463 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003464 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall7f040a92010-12-24 02:08:15 +00003465 ParsedAttributes attrs;
3466 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003467
Chris Lattner378c7e42008-12-18 07:27:21 +00003468 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00003469 ExprResult NumElements;
John McCall7f040a92010-12-24 02:08:15 +00003470 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, false, 0,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003471 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003472 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003473 return;
3474 } else if (Tok.getKind() == tok::numeric_constant &&
3475 GetLookAheadToken(1).is(tok::r_square)) {
3476 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00003477 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003478 ConsumeToken();
3479
Sebastian Redlab197ba2009-02-09 18:23:29 +00003480 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
John McCall7f040a92010-12-24 02:08:15 +00003481 ParsedAttributes attrs;
3482 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003483
Chris Lattner378c7e42008-12-18 07:27:21 +00003484 // Remember that we parsed a array type, and remember its features.
John McCall7f040a92010-12-24 02:08:15 +00003485 D.AddTypeInfo(DeclaratorChunk::getArray(0, attrs, false, 0,
3486 ExprRes.release(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003487 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003488 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003489 return;
3490 }
Mike Stump1eb44332009-09-09 15:08:12 +00003491
Reid Spencer5f016e22007-07-11 17:01:13 +00003492 // If valid, this location is the position where we read the 'static' keyword.
3493 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003494 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003495 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003496
Reid Spencer5f016e22007-07-11 17:01:13 +00003497 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003498 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003499 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003500 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003501
Reid Spencer5f016e22007-07-11 17:01:13 +00003502 // If we haven't already read 'static', check to see if there is one after the
3503 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003504 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003505 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003506
Reid Spencer5f016e22007-07-11 17:01:13 +00003507 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3508 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00003509 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00003510
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003511 // Handle the case where we have '[*]' as the array size. However, a leading
3512 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3513 // the the token after the star is a ']'. Since stars in arrays are
3514 // infrequent, use of lookahead is not costly here.
3515 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003516 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003517
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003518 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003519 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003520 StaticLoc = SourceLocation(); // Drop the static.
3521 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003522 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003523 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003524 // Note, in C89, this production uses the constant-expr production instead
3525 // of assignment-expr. The only difference is that assignment-expr allows
3526 // things like '=' and '*='. Sema rejects these in C89 mode because they
3527 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003528
Douglas Gregore0762c92009-06-19 23:52:42 +00003529 // Parse the constant-expression or assignment-expression now (depending
3530 // on dialect).
3531 if (getLang().CPlusPlus)
3532 NumElements = ParseConstantExpression();
3533 else
3534 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003535 }
Mike Stump1eb44332009-09-09 15:08:12 +00003536
Reid Spencer5f016e22007-07-11 17:01:13 +00003537 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003538 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003539 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003540 // If the expression was invalid, skip it.
3541 SkipUntil(tok::r_square);
3542 return;
3543 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003544
3545 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3546
John McCall7f040a92010-12-24 02:08:15 +00003547 ParsedAttributes attrs;
3548 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003549
Chris Lattner378c7e42008-12-18 07:27:21 +00003550 // Remember that we parsed a array type, and remember its features.
John McCall7f040a92010-12-24 02:08:15 +00003551 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), attrs,
Reid Spencer5f016e22007-07-11 17:01:13 +00003552 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003553 NumElements.release(),
3554 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003555 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003556}
3557
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003558/// [GNU] typeof-specifier:
3559/// typeof ( expressions )
3560/// typeof ( type-name )
3561/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003562///
3563void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003564 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003565 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003566 SourceLocation StartLoc = ConsumeToken();
3567
John McCallcfb708c2010-01-13 20:03:27 +00003568 const bool hasParens = Tok.is(tok::l_paren);
3569
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003570 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00003571 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003572 SourceRange CastRange;
John McCall60d7b3a2010-08-24 06:29:42 +00003573 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall911093e2010-08-25 02:45:51 +00003574 isCastExpr,
3575 CastTy,
3576 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003577 if (hasParens)
3578 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003579
3580 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003581 // FIXME: Not accurate, the range gets one token more than it should.
3582 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003583 else
3584 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003585
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003586 if (isCastExpr) {
3587 if (!CastTy) {
3588 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003589 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003590 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003591
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003592 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003593 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003594 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3595 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003596 DiagID, CastTy))
3597 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003598 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003599 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003600
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003601 // If we get here, the operand to the typeof was an expresion.
3602 if (Operand.isInvalid()) {
3603 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003604 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003605 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003606
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003607 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003608 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003609 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3610 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00003611 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00003612 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003613}
Chris Lattner1b492422010-02-28 18:33:55 +00003614
3615
3616/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3617/// from TryAltiVecVectorToken.
3618bool Parser::TryAltiVecVectorTokenOutOfLine() {
3619 Token Next = NextToken();
3620 switch (Next.getKind()) {
3621 default: return false;
3622 case tok::kw_short:
3623 case tok::kw_long:
3624 case tok::kw_signed:
3625 case tok::kw_unsigned:
3626 case tok::kw_void:
3627 case tok::kw_char:
3628 case tok::kw_int:
3629 case tok::kw_float:
3630 case tok::kw_double:
3631 case tok::kw_bool:
3632 case tok::kw___pixel:
3633 Tok.setKind(tok::kw___vector);
3634 return true;
3635 case tok::identifier:
3636 if (Next.getIdentifierInfo() == Ident_pixel) {
3637 Tok.setKind(tok::kw___vector);
3638 return true;
3639 }
3640 return false;
3641 }
3642}
3643
3644bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3645 const char *&PrevSpec, unsigned &DiagID,
3646 bool &isInvalid) {
3647 if (Tok.getIdentifierInfo() == Ident_vector) {
3648 Token Next = NextToken();
3649 switch (Next.getKind()) {
3650 case tok::kw_short:
3651 case tok::kw_long:
3652 case tok::kw_signed:
3653 case tok::kw_unsigned:
3654 case tok::kw_void:
3655 case tok::kw_char:
3656 case tok::kw_int:
3657 case tok::kw_float:
3658 case tok::kw_double:
3659 case tok::kw_bool:
3660 case tok::kw___pixel:
3661 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3662 return true;
3663 case tok::identifier:
3664 if (Next.getIdentifierInfo() == Ident_pixel) {
3665 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3666 return true;
3667 }
3668 break;
3669 default:
3670 break;
3671 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003672 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003673 DS.isTypeAltiVecVector()) {
3674 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3675 return true;
3676 }
3677 return false;
3678}
3679