blob: 121289696db8f69aef17dcfd3bb500a69dab6223 [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
Sean Huntbbd37c62009-11-21 08:43:09 +000085AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
86 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000087
Reid Spencer5f016e22007-07-11 17:01:13 +000088 AttributeList *CurrAttr = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner04d66662007-10-09 17:33:22 +000090 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000091 ConsumeToken();
92 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
93 "attribute")) {
94 SkipUntil(tok::r_paren, true); // skip until ) or ;
95 return CurrAttr;
96 }
97 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
98 SkipUntil(tok::r_paren, true); // skip until ) or ;
99 return CurrAttr;
100 }
101 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000102 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
103 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000104
105 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
107 ConsumeToken();
108 continue;
109 }
110 // we have an identifier or declaration specifier (const, int, etc.)
111 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
112 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000114 // check if we have a "parameterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000115 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattner04d66662007-10-09 17:33:22 +0000118 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
120 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000121
122 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 // __attribute__(( mode(byte) ))
124 ConsumeParen(); // ignore the right paren loc for now
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000125 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
126 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000127 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 ConsumeToken();
129 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000130 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 // now parse the non-empty comma separated list of expressions
134 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000135 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000136 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 ArgExprsOk = false;
138 SkipUntil(tok::r_paren);
139 break;
140 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000141 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 }
Chris Lattner04d66662007-10-09 17:33:22 +0000143 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 break;
145 ConsumeToken(); // Eat the comma, move to the next argument
146 }
Chris Lattner04d66662007-10-09 17:33:22 +0000147 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 ConsumeParen(); // ignore the right paren loc for now
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000149 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0,
150 AttrNameLoc, ParmName, ParmLoc,
151 ArgExprs.take(), ArgExprs.size(),
152 CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 }
154 }
155 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000156 switch (Tok.getKind()) {
157 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 // __attribute__(( nonnull() ))
160 ConsumeParen(); // ignore the right paren loc for now
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000161 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
162 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000163 break;
164 case tok::kw_char:
165 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000166 case tok::kw_char16_t:
167 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000168 case tok::kw_bool:
169 case tok::kw_short:
170 case tok::kw_int:
171 case tok::kw_long:
172 case tok::kw_signed:
173 case tok::kw_unsigned:
174 case tok::kw_float:
175 case tok::kw_double:
176 case tok::kw_void:
177 case tok::kw_typeof:
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000178 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
179 0, SourceLocation(), 0, 0, CurrAttr);
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000180 if (CurrAttr->getKind() == AttributeList::AT_IBOutletCollection)
181 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;
188 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000190 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 // now parse the list of expressions
194 while (1) {
John McCall60d7b3a2010-08-24 06:29:42 +0000195 ExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000196 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 ArgExprsOk = false;
198 SkipUntil(tok::r_paren);
199 break;
200 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000201 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 }
Chris Lattner04d66662007-10-09 17:33:22 +0000203 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 break;
205 ConsumeToken(); // Eat the comma, move to the next argument
206 }
207 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000208 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 ConsumeParen(); // ignore the right paren loc for now
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000210 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0,
Sean Huntbbd37c62009-11-21 08:43:09 +0000211 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
212 ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 CurrAttr);
214 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000215 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000216 }
217 }
218 } else {
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000219 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
220 0, SourceLocation(), 0, 0, CurrAttr);
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 }
229 if (EndLoc)
230 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000231 }
232 return CurrAttr;
233}
234
Eli Friedmana23b4852009-06-08 07:21:15 +0000235/// ParseMicrosoftDeclSpec - Parse an __declspec construct
236///
237/// [MS] decl-specifier:
238/// __declspec ( extended-decl-modifier-seq )
239///
240/// [MS] extended-decl-modifier-seq:
241/// extended-decl-modifier[opt]
242/// extended-decl-modifier extended-decl-modifier-seq
243
Eli Friedman290eeb02009-06-08 23:27:34 +0000244AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000245 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000246
Steve Narofff59e17e2008-12-24 20:59:21 +0000247 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000248 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
249 "declspec")) {
250 SkipUntil(tok::r_paren, true); // skip until ) or ;
251 return CurrAttr;
252 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000253 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000254 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
255 SourceLocation AttrNameLoc = ConsumeToken();
256 if (Tok.is(tok::l_paren)) {
257 ConsumeParen();
258 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
259 // correctly.
John McCall60d7b3a2010-08-24 06:29:42 +0000260 ExprResult ArgExpr(ParseAssignmentExpression());
Eli Friedmana23b4852009-06-08 07:21:15 +0000261 if (!ArgExpr.isInvalid()) {
John McCallca0408f2010-08-23 06:44:23 +0000262 Expr *ExprList = ArgExpr.take();
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000263 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
264 SourceLocation(), &ExprList, 1,
265 CurrAttr, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000266 }
267 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
268 SkipUntil(tok::r_paren, false);
269 } else {
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000270 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc,
271 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000272 }
273 }
274 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
275 SkipUntil(tok::r_paren, false);
Eli Friedman290eeb02009-06-08 23:27:34 +0000276 return CurrAttr;
277}
278
279AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
280 // Treat these like attributes
281 // FIXME: Allow Sema to distinguish between these and real attributes!
282 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000283 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
284 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000285 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
286 SourceLocation AttrNameLoc = ConsumeToken();
287 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
288 // FIXME: Support these properly!
289 continue;
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000290 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
291 SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedman290eeb02009-06-08 23:27:34 +0000292 }
293 return CurrAttr;
Steve Narofff59e17e2008-12-24 20:59:21 +0000294}
295
Dawn Perchik52fc3142010-09-03 01:29:35 +0000296AttributeList* Parser::ParseBorlandTypeAttributes(AttributeList *CurrAttr) {
297 // Treat these like attributes
298 while (Tok.is(tok::kw___pascal)) {
299 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
300 SourceLocation AttrNameLoc = ConsumeToken();
Ted Kremenek8113ecf2010-11-10 05:59:39 +0000301 CurrAttr = AttrFactory.Create(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
302 SourceLocation(), 0, 0, CurrAttr, true);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000303 }
304 return CurrAttr;
305}
306
Reid Spencer5f016e22007-07-11 17:01:13 +0000307/// ParseDeclaration - Parse a full 'declaration', which consists of
308/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000309/// 'Context' should be a Declarator::TheContext value. This returns the
310/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000311///
312/// declaration: [C99 6.7]
313/// block-declaration ->
314/// simple-declaration
315/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000316/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000317/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000318/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000319/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000320/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000321/// others... [FIXME]
322///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000323Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
324 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000325 SourceLocation &DeclEnd,
326 CXX0XAttributeList Attr) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000327 ParenBraceBracketBalancer BalancerRAIIObj(*this);
328
John McCalld226f652010-08-21 09:40:31 +0000329 Decl *SingleDecl = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000330 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000331 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000332 case tok::kw_export:
Sean Huntbbd37c62009-11-21 08:43:09 +0000333 if (Attr.HasAttr)
334 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
335 << Attr.Range;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000336 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000337 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000338 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000339 // Could be the start of an inline namespace. Allowed as an ext in C++03.
340 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
Sebastian Redld078e642010-08-27 23:12:46 +0000341 if (Attr.HasAttr)
342 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
343 << Attr.Range;
344 SourceLocation InlineLoc = ConsumeToken();
345 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
346 break;
347 }
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000348 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, Attr.AttrList,
349 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000350 case tok::kw_namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +0000351 if (Attr.HasAttr)
352 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
353 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000354 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000355 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000356 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +0000357 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
358 DeclEnd, Attr);
Chris Lattner682bf922009-03-29 16:50:03 +0000359 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000360 case tok::kw_static_assert:
Sean Huntbbd37c62009-11-21 08:43:09 +0000361 if (Attr.HasAttr)
362 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
363 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000364 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000365 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000366 default:
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000367 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, Attr.AttrList,
368 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000369 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000370
Chris Lattner682bf922009-03-29 16:50:03 +0000371 // This routine returns a DeclGroup, if the thing we parsed only contains a
372 // single decl, convert it now.
373 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000374}
375
376/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
377/// declaration-specifiers init-declarator-list[opt] ';'
378///[C90/C++]init-declarator-list ';' [TODO]
379/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000380///
381/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000382/// declaration. If it is true, it checks for and eats it.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000383Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
384 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000385 SourceLocation &DeclEnd,
Chris Lattner5c5db552010-04-05 18:18:31 +0000386 AttributeList *Attr,
387 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000389 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000390 if (Attr)
391 DS.AddAttributes(Attr);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000392 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
393 getDeclSpecContextFromDeclaratorContext(Context));
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +0000394 StmtResult R = Actions.ActOnVlaStmt(DS);
395 if (R.isUsable())
396 Stmts.push_back(R.release());
Mike Stump1eb44332009-09-09 15:08:12 +0000397
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
399 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000400 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000401 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000402 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallaec03712010-05-21 20:45:30 +0000403 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000404 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000405 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000406 }
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Chris Lattner5c5db552010-04-05 18:18:31 +0000408 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000409}
Mike Stump1eb44332009-09-09 15:08:12 +0000410
John McCalld8ac0572009-11-03 19:26:08 +0000411/// ParseDeclGroup - Having concluded that this is either a function
412/// definition or a group of object declarations, actually parse the
413/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000414Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
415 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000416 bool AllowFunctionDefinitions,
417 SourceLocation *DeclEnd) {
418 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000419 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000420 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000421
John McCalld8ac0572009-11-03 19:26:08 +0000422 // Bail out if the first declarator didn't seem well-formed.
423 if (!D.hasName() && !D.mayOmitIdentifier()) {
424 // Skip until ; or }.
425 SkipUntil(tok::r_brace, true, true);
426 if (Tok.is(tok::semi))
427 ConsumeToken();
428 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Chris Lattnerc82daef2010-07-11 22:24:20 +0000431 // Check to see if we have a function *definition* which must have a body.
432 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
433 // Look at the next token to make sure that this isn't a function
434 // declaration. We have to check this because __attribute__ might be the
435 // start of a function definition in GCC-extended K&R C.
436 !isDeclarationAfterDeclarator()) {
437
Chris Lattner004659a2010-07-11 22:42:07 +0000438 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000439 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
440 Diag(Tok, diag::err_function_declared_typedef);
441
442 // Recover by treating the 'typedef' as spurious.
443 DS.ClearStorageClassSpecs();
444 }
445
John McCalld226f652010-08-21 09:40:31 +0000446 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000447 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000448 }
449
450 if (isDeclarationSpecifier()) {
451 // If there is an invalid declaration specifier right after the function
452 // prototype, then we must be in a missing semicolon case where this isn't
453 // actually a body. Just fall through into the code that handles it as a
454 // prototype, and let the top-level code handle the erroneous declspec
455 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000456 } else {
457 Diag(Tok, diag::err_expected_fn_body);
458 SkipUntil(tok::semi);
459 return DeclGroupPtrTy();
460 }
461 }
462
John McCalld226f652010-08-21 09:40:31 +0000463 llvm::SmallVector<Decl *, 8> DeclsInGroup;
464 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000465 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000466 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000467 DeclsInGroup.push_back(FirstDecl);
468
469 // If we don't have a comma, it is either the end of the list (a ';') or an
470 // error, bail out.
471 while (Tok.is(tok::comma)) {
472 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000473 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000474
475 // Parse the next declarator.
476 D.clear();
477
478 // Accept attributes in an init-declarator. In the first declarator in a
479 // declaration, these would be part of the declspec. In subsequent
480 // declarators, they become part of the declarator itself, so that they
481 // don't apply to declarators after *this* one. Examples:
482 // short __attribute__((common)) var; -> declspec
483 // short var __attribute__((common)); -> declarator
484 // short x, __attribute__((common)) var; -> declarator
485 if (Tok.is(tok::kw___attribute)) {
486 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000487 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000488 D.AddAttributes(AttrList, Loc);
489 }
490
491 ParseDeclarator(D);
492
John McCalld226f652010-08-21 09:40:31 +0000493 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000494 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000495 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000496 DeclsInGroup.push_back(ThisDecl);
497 }
498
499 if (DeclEnd)
500 *DeclEnd = Tok.getLocation();
501
502 if (Context != Declarator::ForContext &&
503 ExpectAndConsume(tok::semi,
504 Context == Declarator::FileContext
505 ? diag::err_invalid_token_after_toplevel_declarator
506 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000507 // Okay, there was no semicolon and one was expected. If we see a
508 // declaration specifier, just assume it was missing and continue parsing.
509 // Otherwise things are very confused and we skip to recover.
510 if (!isDeclarationSpecifier()) {
511 SkipUntil(tok::r_brace, true, true);
512 if (Tok.is(tok::semi))
513 ConsumeToken();
514 }
John McCalld8ac0572009-11-03 19:26:08 +0000515 }
516
Douglas Gregor23c94db2010-07-02 17:43:08 +0000517 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000518 DeclsInGroup.data(),
519 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000520}
521
Douglas Gregor1426e532009-05-12 21:31:51 +0000522/// \brief Parse 'declaration' after parsing 'declaration-specifiers
523/// declarator'. This method parses the remainder of the declaration
524/// (including any attributes or initializer, among other things) and
525/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000526///
Reid Spencer5f016e22007-07-11 17:01:13 +0000527/// init-declarator: [C99 6.7]
528/// declarator
529/// declarator '=' initializer
530/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
531/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000532/// [C++] declarator initializer[opt]
533///
534/// [C++] initializer:
535/// [C++] '=' initializer-clause
536/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000537/// [C++0x] '=' 'default' [TODO]
538/// [C++0x] '=' 'delete'
539///
540/// According to the standard grammar, =default and =delete are function
541/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000542///
John McCalld226f652010-08-21 09:40:31 +0000543Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000544 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000545 // If a simple-asm-expr is present, parse it.
546 if (Tok.is(tok::kw_asm)) {
547 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000548 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor1426e532009-05-12 21:31:51 +0000549 if (AsmLabel.isInvalid()) {
550 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000551 return 0;
Douglas Gregor1426e532009-05-12 21:31:51 +0000552 }
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Douglas Gregor1426e532009-05-12 21:31:51 +0000554 D.setAsmLabel(AsmLabel.release());
555 D.SetRangeEnd(Loc);
556 }
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Douglas Gregor1426e532009-05-12 21:31:51 +0000558 // If attributes are present, parse them.
559 if (Tok.is(tok::kw___attribute)) {
560 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000561 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000562 D.AddAttributes(AttrList, Loc);
563 }
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Douglas Gregor1426e532009-05-12 21:31:51 +0000565 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000566 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000567 switch (TemplateInfo.Kind) {
568 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000569 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000570 break;
571
572 case ParsedTemplateInfo::Template:
573 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000574 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000575 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000576 TemplateInfo.TemplateParams->data(),
577 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000578 D);
579 break;
580
581 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000582 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000583 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000584 TemplateInfo.ExternLoc,
585 TemplateInfo.TemplateLoc,
586 D);
587 if (ThisRes.isInvalid()) {
588 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000589 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000590 }
591
592 ThisDecl = ThisRes.get();
593 break;
594 }
595 }
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Douglas Gregor1426e532009-05-12 21:31:51 +0000597 // Parse declarator '=' initializer.
Argyrios Kyrtzidisa6eb5f82010-10-08 02:39:23 +0000598 if (isTokenEqualOrMistypedEqualEqual(
599 diag::err_invalid_equalequal_after_declarator)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000600 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000601 if (Tok.is(tok::kw_delete)) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000602 SourceLocation DelLoc = ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +0000603
604 if (!getLang().CPlusPlus0x)
605 Diag(DelLoc, diag::warn_deleted_function_accepted_as_extension);
606
Douglas Gregor1426e532009-05-12 21:31:51 +0000607 Actions.SetDeclDeleted(ThisDecl, DelLoc);
608 } else {
John McCall731ad842009-12-19 09:28:58 +0000609 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
610 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000611 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000612 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000613
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000614 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000615 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000616 ConsumeCodeCompletionToken();
617 SkipUntil(tok::comma, true, true);
618 return ThisDecl;
619 }
620
John McCall60d7b3a2010-08-24 06:29:42 +0000621 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000622
John McCall731ad842009-12-19 09:28:58 +0000623 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000624 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000625 ExitScope();
626 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000627
Douglas Gregor1426e532009-05-12 21:31:51 +0000628 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000629 SkipUntil(tok::comma, true, true);
630 Actions.ActOnInitializerError(ThisDecl);
631 } else
John McCall9ae2f072010-08-23 23:25:46 +0000632 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor1426e532009-05-12 21:31:51 +0000633 }
634 } else if (Tok.is(tok::l_paren)) {
635 // Parse C++ direct initializer: '(' expression-list ')'
636 SourceLocation LParenLoc = ConsumeParen();
637 ExprVector Exprs(Actions);
638 CommaLocsTy CommaLocs;
639
Douglas Gregorb4debae2009-12-22 17:47:17 +0000640 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
641 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000642 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000643 }
644
Douglas Gregor1426e532009-05-12 21:31:51 +0000645 if (ParseExpressionList(Exprs, CommaLocs)) {
646 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000647
648 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000649 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000650 ExitScope();
651 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000652 } else {
653 // Match the ')'.
654 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
655
656 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
657 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000658
659 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000660 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000661 ExitScope();
662 }
663
Douglas Gregor1426e532009-05-12 21:31:51 +0000664 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
665 move_arg(Exprs),
Douglas Gregora1a04782010-09-09 16:33:13 +0000666 RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000667 }
668 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000669 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000670 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
671 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000672 }
673
674 return ThisDecl;
675}
676
Reid Spencer5f016e22007-07-11 17:01:13 +0000677/// ParseSpecifierQualifierList
678/// specifier-qualifier-list:
679/// type-specifier specifier-qualifier-list[opt]
680/// type-qualifier specifier-qualifier-list[opt]
681/// [GNU] attributes specifier-qualifier-list[opt]
682///
683void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
684 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
685 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 // Validate declspec for type-name.
689 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000690 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
691 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 // Issue diagnostic and remove storage class if present.
695 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
696 if (DS.getStorageClassSpecLoc().isValid())
697 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
698 else
699 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
700 DS.ClearStorageClassSpecs();
701 }
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 // Issue diagnostic and remove function specfier if present.
704 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000705 if (DS.isInlineSpecified())
706 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
707 if (DS.isVirtualSpecified())
708 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
709 if (DS.isExplicitSpecified())
710 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 DS.ClearFunctionSpecs();
712 }
713}
714
Chris Lattnerc199ab32009-04-12 20:42:31 +0000715/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
716/// specified token is valid after the identifier in a declarator which
717/// immediately follows the declspec. For example, these things are valid:
718///
719/// int x [ 4]; // direct-declarator
720/// int x ( int y); // direct-declarator
721/// int(int x ) // direct-declarator
722/// int x ; // simple-declaration
723/// int x = 17; // init-declarator-list
724/// int x , y; // init-declarator-list
725/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000726/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000727/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000728///
729/// This is not, because 'x' does not immediately follow the declspec (though
730/// ')' happens to be valid anyway).
731/// int (x)
732///
733static bool isValidAfterIdentifierInDeclarator(const Token &T) {
734 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
735 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000736 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000737}
738
Chris Lattnere40c2952009-04-14 21:34:55 +0000739
740/// ParseImplicitInt - This method is called when we have an non-typename
741/// identifier in a declspec (which normally terminates the decl spec) when
742/// the declspec has no type specifier. In this case, the declspec is either
743/// malformed or is "implicit int" (in K&R and C89).
744///
745/// This method handles diagnosing this prettily and returns false if the
746/// declspec is done being processed. If it recovers and thinks there may be
747/// other pieces of declspec after it, it returns true.
748///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000749bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000750 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000751 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000752 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Chris Lattnere40c2952009-04-14 21:34:55 +0000754 SourceLocation Loc = Tok.getLocation();
755 // If we see an identifier that is not a type name, we normally would
756 // parse it as the identifer being declared. However, when a typename
757 // is typo'd or the definition is not included, this will incorrectly
758 // parse the typename as the identifier name and fall over misparsing
759 // later parts of the diagnostic.
760 //
761 // As such, we try to do some look-ahead in cases where this would
762 // otherwise be an "implicit-int" case to see if this is invalid. For
763 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
764 // an identifier with implicit int, we'd get a parse error because the
765 // next token is obviously invalid for a type. Parse these as a case
766 // with an invalid type specifier.
767 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Chris Lattnere40c2952009-04-14 21:34:55 +0000769 // Since we know that this either implicit int (which is rare) or an
770 // error, we'd do lookahead to try to do better recovery.
771 if (isValidAfterIdentifierInDeclarator(NextToken())) {
772 // If this token is valid for implicit int, e.g. "static x = 4", then
773 // we just avoid eating the identifier, so it will be parsed as the
774 // identifier in the declarator.
775 return false;
776 }
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Chris Lattnere40c2952009-04-14 21:34:55 +0000778 // Otherwise, if we don't consume this token, we are going to emit an
779 // error anyway. Try to recover from various common problems. Check
780 // to see if this was a reference to a tag name without a tag specified.
781 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000782 //
783 // C++ doesn't need this, and isTagName doesn't take SS.
784 if (SS == 0) {
785 const char *TagName = 0;
786 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Douglas Gregor23c94db2010-07-02 17:43:08 +0000788 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +0000789 default: break;
790 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
791 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
792 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
793 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
794 }
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Chris Lattnerf4382f52009-04-14 22:17:06 +0000796 if (TagName) {
797 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000798 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000799 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Chris Lattnerf4382f52009-04-14 22:17:06 +0000801 // Parse this as a tag as if the missing tag were present.
802 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000803 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000804 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000805 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000806 return true;
807 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000808 }
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Douglas Gregora786fdb2009-10-13 23:27:22 +0000810 // This is almost certainly an invalid type name. Let the action emit a
811 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +0000812 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +0000813 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +0000814 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +0000815 // The action emitted a diagnostic, so we don't have to.
816 if (T) {
817 // The action has suggested that the type T could be used. Set that as
818 // the type in the declaration specifiers, consume the would-be type
819 // name token, and we're done.
820 const char *PrevSpec;
821 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +0000822 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000823 DS.SetRangeEnd(Tok.getLocation());
824 ConsumeToken();
825
826 // There may be other declaration specifiers after this.
827 return true;
828 }
829
830 // Fall through; the action had no suggestion for us.
831 } else {
832 // The action did not emit a diagnostic, so emit one now.
833 SourceRange R;
834 if (SS) R = SS->getRange();
835 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
836 }
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Douglas Gregora786fdb2009-10-13 23:27:22 +0000838 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000839 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000840 unsigned DiagID;
841 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000842 DS.SetRangeEnd(Tok.getLocation());
843 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Chris Lattnere40c2952009-04-14 21:34:55 +0000845 // TODO: Could inject an invalid typedef decl in an enclosing scope to
846 // avoid rippling error messages on subsequent uses of the same type,
847 // could be useful if #include was forgotten.
848 return false;
849}
850
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000851/// \brief Determine the declaration specifier context from the declarator
852/// context.
853///
854/// \param Context the declarator context, which is one of the
855/// Declarator::TheContext enumerator values.
856Parser::DeclSpecContext
857Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
858 if (Context == Declarator::MemberContext)
859 return DSC_class;
860 if (Context == Declarator::FileContext)
861 return DSC_top_level;
862 return DSC_normal;
863}
864
Reid Spencer5f016e22007-07-11 17:01:13 +0000865/// ParseDeclarationSpecifiers
866/// declaration-specifiers: [C99 6.7]
867/// storage-class-specifier declaration-specifiers[opt]
868/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000869/// [C99] function-specifier declaration-specifiers[opt]
870/// [GNU] attributes declaration-specifiers[opt]
871///
872/// storage-class-specifier: [C99 6.7.1]
873/// 'typedef'
874/// 'extern'
875/// 'static'
876/// 'auto'
877/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000878/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000879/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000880/// function-specifier: [C99 6.7.4]
881/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000882/// [C++] 'virtual'
883/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000884/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000885/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000886
Reid Spencer5f016e22007-07-11 17:01:13 +0000887///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000888void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000889 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000890 AccessSpecifier AS,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000891 DeclSpecContext DSContext) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000892 DS.SetRangeStart(Tok.getLocation());
Chris Lattner729ad832010-11-09 20:14:26 +0000893 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000895 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000896 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000897 unsigned DiagID = 0;
898
Reid Spencer5f016e22007-07-11 17:01:13 +0000899 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000900
Reid Spencer5f016e22007-07-11 17:01:13 +0000901 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000902 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000903 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000904 // If this is not a declaration specifier token, we're done reading decl
905 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000906 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000909 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +0000910 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000911 if (DS.hasTypeSpecifier()) {
912 bool AllowNonIdentifiers
913 = (getCurScope()->getFlags() & (Scope::ControlScope |
914 Scope::BlockScope |
915 Scope::TemplateParamScope |
916 Scope::FunctionPrototypeScope |
917 Scope::AtCatchScope)) == 0;
918 bool AllowNestedNameSpecifiers
919 = DSContext == DSC_top_level ||
920 (DSContext == DSC_class && DS.isFriendSpecified());
921
Douglas Gregorc7b6d882010-09-16 15:14:18 +0000922 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
923 AllowNonIdentifiers,
924 AllowNestedNameSpecifiers);
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000925 ConsumeCodeCompletionToken();
926 return;
927 }
928
929 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +0000930 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
931 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000932 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +0000933 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000934 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +0000935 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000936
937 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
938 ConsumeCodeCompletionToken();
939 return;
940 }
941
Chris Lattner5e02c472009-01-05 00:07:25 +0000942 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000943 // C++ scope specifier. Annotate and loop, or bail out on error.
944 if (TryAnnotateCXXScopeToken(true)) {
945 if (!DS.hasTypeSpecifier())
946 DS.SetTypeSpecError();
947 goto DoneWithDeclSpec;
948 }
John McCall2e0a7152010-03-01 18:20:46 +0000949 if (Tok.is(tok::coloncolon)) // ::new or ::delete
950 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000951 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000952
953 case tok::annot_cxxscope: {
954 if (DS.hasTypeSpecifier())
955 goto DoneWithDeclSpec;
956
John McCallaa87d332009-12-12 11:40:51 +0000957 CXXScopeSpec SS;
John McCallca0408f2010-08-23 06:44:23 +0000958 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCallaa87d332009-12-12 11:40:51 +0000959 SS.setRange(Tok.getAnnotationRange());
960
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000961 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000962 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000963 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000964 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000965 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000966 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000967
968 // C++ [class.qual]p2:
969 // In a lookup in which the constructor is an acceptable lookup
970 // result and the nested-name-specifier nominates a class C:
971 //
972 // - if the name specified after the
973 // nested-name-specifier, when looked up in C, is the
974 // injected-class-name of C (Clause 9), or
975 //
976 // - if the name specified after the nested-name-specifier
977 // is the same as the identifier or the
978 // simple-template-id's template-name in the last
979 // component of the nested-name-specifier,
980 //
981 // the name is instead considered to name the constructor of
982 // class C.
983 //
984 // Thus, if the template-name is actually the constructor
985 // name, then the code is ill-formed; this interpretation is
986 // reinforced by the NAD status of core issue 635.
987 TemplateIdAnnotation *TemplateId
988 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000989 if ((DSContext == DSC_top_level ||
990 (DSContext == DSC_class && DS.isFriendSpecified())) &&
991 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000992 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000993 if (isConstructorDeclarator()) {
994 // The user meant this to be an out-of-line constructor
995 // definition, but template arguments are not allowed
996 // there. Just allow this as a constructor; we'll
997 // complain about it later.
998 goto DoneWithDeclSpec;
999 }
1000
1001 // The user meant this to name a type, but it actually names
1002 // a constructor with some extraneous template
1003 // arguments. Complain, then parse it as a type as the user
1004 // intended.
1005 Diag(TemplateId->TemplateNameLoc,
1006 diag::err_out_of_line_template_id_names_constructor)
1007 << TemplateId->Name;
1008 }
1009
John McCallaa87d332009-12-12 11:40:51 +00001010 DS.getTypeSpecScope() = SS;
1011 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00001012 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00001013 "ParseOptionalCXXScopeSpecifier not working");
1014 AnnotateTemplateIdTokenAsType(&SS);
1015 continue;
1016 }
1017
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001018 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001019 DS.getTypeSpecScope() = SS;
1020 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001021 if (Tok.getAnnotationValue()) {
1022 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001023 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
John McCallb3d87482010-08-24 05:47:05 +00001024 PrevSpec, DiagID, T);
1025 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001026 else
1027 DS.SetTypeSpecError();
1028 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1029 ConsumeToken(); // The typename
1030 }
1031
Douglas Gregor9135c722009-03-25 15:40:00 +00001032 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001033 goto DoneWithDeclSpec;
1034
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001035 // If we're in a context where the identifier could be a class name,
1036 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001037 if ((DSContext == DSC_top_level ||
1038 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001039 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001040 &SS)) {
1041 if (isConstructorDeclarator())
1042 goto DoneWithDeclSpec;
1043
1044 // As noted in C++ [class.qual]p2 (cited above), when the name
1045 // of the class is qualified in a context where it could name
1046 // a constructor, its a constructor name. However, we've
1047 // looked at the declarator, and the user probably meant this
1048 // to be a type. Complain that it isn't supposed to be treated
1049 // as a type, then proceed to parse it as a type.
1050 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1051 << Next.getIdentifierInfo();
1052 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001053
John McCallb3d87482010-08-24 05:47:05 +00001054 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1055 Next.getLocation(),
1056 getCurScope(), &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001057
Chris Lattnerf4382f52009-04-14 22:17:06 +00001058 // If the referenced identifier is not a type, then this declspec is
1059 // erroneous: We already checked about that it has no type specifier, and
1060 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001061 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001062 if (TypeRep == 0) {
1063 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001064 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001065 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001066 }
Mike Stump1eb44332009-09-09 15:08:12 +00001067
John McCallaa87d332009-12-12 11:40:51 +00001068 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001069 ConsumeToken(); // The C++ scope.
1070
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001071 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001072 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001073 if (isInvalid)
1074 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001075
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001076 DS.SetRangeEnd(Tok.getLocation());
1077 ConsumeToken(); // The typename.
1078
1079 continue;
1080 }
Mike Stump1eb44332009-09-09 15:08:12 +00001081
Chris Lattner80d0c892009-01-21 19:48:37 +00001082 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001083 if (Tok.getAnnotationValue()) {
1084 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001085 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001086 DiagID, T);
1087 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001088 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001089
1090 if (isInvalid)
1091 break;
1092
Chris Lattner80d0c892009-01-21 19:48:37 +00001093 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1094 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Chris Lattner80d0c892009-01-21 19:48:37 +00001096 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1097 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001098 // Objective-C interface.
1099 if (Tok.is(tok::less) && getLang().ObjC1)
1100 ParseObjCProtocolQualifiers(DS);
1101
Chris Lattner80d0c892009-01-21 19:48:37 +00001102 continue;
1103 }
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Chris Lattner3bd934a2008-07-26 01:18:38 +00001105 // typedef-name
1106 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001107 // In C++, check to see if this is a scope specifier like foo::bar::, if
1108 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001109 if (getLang().CPlusPlus) {
1110 if (TryAnnotateCXXScopeToken(true)) {
1111 if (!DS.hasTypeSpecifier())
1112 DS.SetTypeSpecError();
1113 goto DoneWithDeclSpec;
1114 }
1115 if (!Tok.is(tok::identifier))
1116 continue;
1117 }
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Chris Lattner3bd934a2008-07-26 01:18:38 +00001119 // This identifier can only be a typedef name if we haven't already seen
1120 // a type-specifier. Without this check we misparse:
1121 // typedef int X; struct Y { short X; }; as 'short int'.
1122 if (DS.hasTypeSpecifier())
1123 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001124
John Thompson82287d12010-02-05 00:12:22 +00001125 // Check for need to substitute AltiVec keyword tokens.
1126 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1127 break;
1128
Chris Lattner3bd934a2008-07-26 01:18:38 +00001129 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001130 ParsedType TypeRep =
1131 Actions.getTypeName(*Tok.getIdentifierInfo(),
1132 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001133
Chris Lattnerc199ab32009-04-12 20:42:31 +00001134 // If this is not a typedef name, don't parse it as part of the declspec,
1135 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001136 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001137 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001138 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001139 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001140
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001141 // If we're in a context where the identifier could be a class name,
1142 // check whether this is a constructor declaration.
1143 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001144 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001145 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001146 goto DoneWithDeclSpec;
1147
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001148 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001149 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001150 if (isInvalid)
1151 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Chris Lattner3bd934a2008-07-26 01:18:38 +00001153 DS.SetRangeEnd(Tok.getLocation());
1154 ConsumeToken(); // The identifier
1155
1156 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1157 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001158 // Objective-C interface.
1159 if (Tok.is(tok::less) && getLang().ObjC1)
1160 ParseObjCProtocolQualifiers(DS);
1161
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001162 // Need to support trailing type qualifiers (e.g. "id<p> const").
1163 // If a type specifier follows, it will be diagnosed elsewhere.
1164 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001165 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001166
1167 // type-name
1168 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001169 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001170 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001171 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001172 // This template-id does not refer to a type name, so we're
1173 // done with the type-specifiers.
1174 goto DoneWithDeclSpec;
1175 }
1176
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001177 // If we're in a context where the template-id could be a
1178 // constructor name or specialization, check whether this is a
1179 // constructor declaration.
1180 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001181 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001182 isConstructorDeclarator())
1183 goto DoneWithDeclSpec;
1184
Douglas Gregor39a8de12009-02-25 19:37:18 +00001185 // Turn the template-id annotation token into a type annotation
1186 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001187 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001188 continue;
1189 }
1190
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 // GNU attributes support.
1192 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001193 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001195
1196 // Microsoft declspec support.
1197 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001198 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001199 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Steve Naroff239f0732008-12-25 14:16:32 +00001201 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001202 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001203 // FIXME: Add handling here!
1204 break;
1205
1206 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001207 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001208 case tok::kw___cdecl:
1209 case tok::kw___stdcall:
1210 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001211 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001212 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1213 continue;
1214
Dawn Perchik52fc3142010-09-03 01:29:35 +00001215 // Borland single token adornments.
1216 case tok::kw___pascal:
1217 DS.AddAttributes(ParseBorlandTypeAttributes());
1218 continue;
1219
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 // storage-class-specifier
1221 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001222 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1223 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 break;
1225 case tok::kw_extern:
1226 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001227 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001228 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1229 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001231 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001232 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001233 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001234 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 case tok::kw_static:
1236 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001237 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001238 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1239 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 break;
1241 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001242 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001243 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1244 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001245 else
John McCallfec54012009-08-03 20:12:06 +00001246 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1247 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 break;
1249 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001250 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1251 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001253 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001254 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1255 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001256 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001258 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 // function-specifier
1262 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001263 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001265 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001266 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001267 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001268 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001269 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001270 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001271
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001272 // friend
1273 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001274 if (DSContext == DSC_class)
1275 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1276 else {
1277 PrevSpec = ""; // not actually used by the diagnostic
1278 DiagID = diag::err_friend_invalid_in_context;
1279 isInvalid = true;
1280 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001281 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Sebastian Redl2ac67232009-11-05 15:47:02 +00001283 // constexpr
1284 case tok::kw_constexpr:
1285 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1286 break;
1287
Chris Lattner80d0c892009-01-21 19:48:37 +00001288 // type-specifier
1289 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001290 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1291 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001292 break;
1293 case tok::kw_long:
1294 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001295 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1296 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001297 else
John McCallfec54012009-08-03 20:12:06 +00001298 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1299 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001300 break;
1301 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001302 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1303 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001304 break;
1305 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001306 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1307 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001308 break;
1309 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001310 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1311 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001312 break;
1313 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001314 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1315 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001316 break;
1317 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001318 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1319 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001320 break;
1321 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001322 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1323 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001324 break;
1325 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1327 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001328 break;
1329 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001330 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1331 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001332 break;
1333 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001334 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1335 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001336 break;
1337 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001338 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1339 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001340 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001341 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001342 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1343 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001344 break;
1345 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001346 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1347 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001348 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001349 case tok::kw_bool:
1350 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001351 if (Tok.is(tok::kw_bool) &&
1352 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1353 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1354 PrevSpec = ""; // Not used by the diagnostic.
1355 DiagID = diag::err_bool_redeclaration;
1356 isInvalid = true;
1357 } else {
1358 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1359 DiagID);
1360 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001361 break;
1362 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001363 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1364 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001365 break;
1366 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001367 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1368 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001369 break;
1370 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001371 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1372 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001373 break;
John Thompson82287d12010-02-05 00:12:22 +00001374 case tok::kw___vector:
1375 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1376 break;
1377 case tok::kw___pixel:
1378 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1379 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001380
1381 // class-specifier:
1382 case tok::kw_class:
1383 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001384 case tok::kw_union: {
1385 tok::TokenKind Kind = Tok.getKind();
1386 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001387 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001388 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001389 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001390
1391 // enum-specifier:
1392 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001393 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001394 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001395 continue;
1396
1397 // cv-qualifier:
1398 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001399 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1400 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001401 break;
1402 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001403 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1404 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001405 break;
1406 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001407 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1408 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001409 break;
1410
Douglas Gregord57959a2009-03-27 23:10:48 +00001411 // C++ typename-specifier:
1412 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001413 if (TryAnnotateTypeOrScopeToken()) {
1414 DS.SetTypeSpecError();
1415 goto DoneWithDeclSpec;
1416 }
1417 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001418 continue;
1419 break;
1420
Chris Lattner80d0c892009-01-21 19:48:37 +00001421 // GNU typeof support.
1422 case tok::kw_typeof:
1423 ParseTypeofSpecifier(DS);
1424 continue;
1425
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001426 case tok::kw_decltype:
1427 ParseDecltypeSpecifier(DS);
1428 continue;
1429
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001430 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001431 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001432 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1433 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001434 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001435 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Douglas Gregor46f936e2010-11-19 17:10:50 +00001437 if (!ParseObjCProtocolQualifiers(DS))
1438 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1439 << FixItHint::CreateInsertion(Loc, "id")
1440 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001441
1442 // Need to support trailing type qualifiers (e.g. "id<p> const").
1443 // If a type specifier follows, it will be diagnosed elsewhere.
1444 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001445 }
John McCallfec54012009-08-03 20:12:06 +00001446 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001447 if (isInvalid) {
1448 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001449 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001450
1451 if (DiagID == diag::ext_duplicate_declspec)
1452 Diag(Tok, DiagID)
1453 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1454 else
1455 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001456 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001457 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001458 ConsumeToken();
1459 }
1460}
Douglas Gregoradcac882008-12-01 23:54:00 +00001461
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001462/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001463/// primarily follow the C++ grammar with additions for C99 and GNU,
1464/// which together subsume the C grammar. Note that the C++
1465/// type-specifier also includes the C type-qualifier (for const,
1466/// volatile, and C99 restrict). Returns true if a type-specifier was
1467/// found (and parsed), false otherwise.
1468///
1469/// type-specifier: [C++ 7.1.5]
1470/// simple-type-specifier
1471/// class-specifier
1472/// enum-specifier
1473/// elaborated-type-specifier [TODO]
1474/// cv-qualifier
1475///
1476/// cv-qualifier: [C++ 7.1.5.1]
1477/// 'const'
1478/// 'volatile'
1479/// [C99] 'restrict'
1480///
1481/// simple-type-specifier: [ C++ 7.1.5.2]
1482/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1483/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1484/// 'char'
1485/// 'wchar_t'
1486/// 'bool'
1487/// 'short'
1488/// 'int'
1489/// 'long'
1490/// 'signed'
1491/// 'unsigned'
1492/// 'float'
1493/// 'double'
1494/// 'void'
1495/// [C99] '_Bool'
1496/// [C99] '_Complex'
1497/// [C99] '_Imaginary' // Removed in TC2?
1498/// [GNU] '_Decimal32'
1499/// [GNU] '_Decimal64'
1500/// [GNU] '_Decimal128'
1501/// [GNU] typeof-specifier
1502/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1503/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001504/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001505/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001506bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001507 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001508 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001509 const ParsedTemplateInfo &TemplateInfo,
1510 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001511 SourceLocation Loc = Tok.getLocation();
1512
1513 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001514 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001515 // If we already have a type specifier, this identifier is not a type.
1516 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1517 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1518 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1519 return false;
John Thompson82287d12010-02-05 00:12:22 +00001520 // Check for need to substitute AltiVec keyword tokens.
1521 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1522 break;
1523 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001524 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001525 // Annotate typenames and C++ scope specifiers. If we get one, just
1526 // recurse to handle whatever we get.
1527 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001528 return true;
1529 if (Tok.is(tok::identifier))
1530 return false;
1531 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1532 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001533 case tok::coloncolon: // ::foo::bar
1534 if (NextToken().is(tok::kw_new) || // ::new
1535 NextToken().is(tok::kw_delete)) // ::delete
1536 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001537
Chris Lattner166a8fc2009-01-04 23:41:41 +00001538 // Annotate typenames and C++ scope specifiers. If we get one, just
1539 // recurse to handle whatever we get.
1540 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001541 return true;
1542 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1543 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Douglas Gregor12e083c2008-11-07 15:42:26 +00001545 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001546 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001547 if (ParsedType T = getTypeAnnotation(Tok)) {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001548 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001549 DiagID, T);
1550 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001551 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001552 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1553 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Douglas Gregor12e083c2008-11-07 15:42:26 +00001555 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1556 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1557 // Objective-C interface. If we don't have Objective-C or a '<', this is
1558 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001559 if (Tok.is(tok::less) && getLang().ObjC1)
1560 ParseObjCProtocolQualifiers(DS);
1561
Douglas Gregor12e083c2008-11-07 15:42:26 +00001562 return true;
1563 }
1564
1565 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001566 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001567 break;
1568 case tok::kw_long:
1569 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001570 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1571 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001572 else
John McCallfec54012009-08-03 20:12:06 +00001573 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1574 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001575 break;
1576 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001577 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001578 break;
1579 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001580 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1581 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001582 break;
1583 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001584 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1585 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001586 break;
1587 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001588 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1589 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001590 break;
1591 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001592 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001593 break;
1594 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001595 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001596 break;
1597 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001598 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001599 break;
1600 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001601 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001602 break;
1603 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001604 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001605 break;
1606 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001607 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001608 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001609 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001610 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001611 break;
1612 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001613 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001614 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001615 case tok::kw_bool:
1616 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001617 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001618 break;
1619 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001620 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1621 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001622 break;
1623 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001624 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1625 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001626 break;
1627 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001628 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1629 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001630 break;
John Thompson82287d12010-02-05 00:12:22 +00001631 case tok::kw___vector:
1632 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1633 break;
1634 case tok::kw___pixel:
1635 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1636 break;
1637
Douglas Gregor12e083c2008-11-07 15:42:26 +00001638 // class-specifier:
1639 case tok::kw_class:
1640 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001641 case tok::kw_union: {
1642 tok::TokenKind Kind = Tok.getKind();
1643 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001644 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1645 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001646 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001647 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001648
1649 // enum-specifier:
1650 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001651 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001652 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001653 return true;
1654
1655 // cv-qualifier:
1656 case tok::kw_const:
1657 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001658 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001659 break;
1660 case tok::kw_volatile:
1661 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001662 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001663 break;
1664 case tok::kw_restrict:
1665 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001666 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001667 break;
1668
1669 // GNU typeof support.
1670 case tok::kw_typeof:
1671 ParseTypeofSpecifier(DS);
1672 return true;
1673
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001674 // C++0x decltype support.
1675 case tok::kw_decltype:
1676 ParseDecltypeSpecifier(DS);
1677 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001679 // C++0x auto support.
1680 case tok::kw_auto:
1681 if (!getLang().CPlusPlus0x)
1682 return false;
1683
John McCallfec54012009-08-03 20:12:06 +00001684 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001685 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001686
Eli Friedman290eeb02009-06-08 23:27:34 +00001687 case tok::kw___ptr64:
1688 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001689 case tok::kw___cdecl:
1690 case tok::kw___stdcall:
1691 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001692 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001693 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001694 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001695
Dawn Perchik52fc3142010-09-03 01:29:35 +00001696 case tok::kw___pascal:
1697 DS.AddAttributes(ParseBorlandTypeAttributes());
1698 return true;
1699
Douglas Gregor12e083c2008-11-07 15:42:26 +00001700 default:
1701 // Not a type-specifier; do nothing.
1702 return false;
1703 }
1704
1705 // If the specifier combination wasn't legal, issue a diagnostic.
1706 if (isInvalid) {
1707 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001708 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001709 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001710 }
1711 DS.SetRangeEnd(Tok.getLocation());
1712 ConsumeToken(); // whatever we parsed above.
1713 return true;
1714}
Reid Spencer5f016e22007-07-11 17:01:13 +00001715
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001716/// ParseStructDeclaration - Parse a struct declaration without the terminating
1717/// semicolon.
1718///
Reid Spencer5f016e22007-07-11 17:01:13 +00001719/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001720/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001721/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001722/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001723/// struct-declarator-list:
1724/// struct-declarator
1725/// struct-declarator-list ',' struct-declarator
1726/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1727/// struct-declarator:
1728/// declarator
1729/// [GNU] declarator attributes[opt]
1730/// declarator[opt] ':' constant-expression
1731/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1732///
Chris Lattnere1359422008-04-10 06:46:29 +00001733void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001734ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001735 if (Tok.is(tok::kw___extension__)) {
1736 // __extension__ silences extension warnings in the subexpression.
1737 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001738 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001739 return ParseStructDeclaration(DS, Fields);
1740 }
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Steve Naroff28a7ca82007-08-20 22:28:22 +00001742 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001743 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001744 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001746 // If there are no declarators, this is a free-standing declaration
1747 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001748 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001749 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001750 return;
1751 }
1752
1753 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001754 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001755 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001756 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001757 FieldDeclarator DeclaratorInfo(DS);
1758
1759 // Attributes are only allowed here on successive declarators.
1760 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1761 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001762 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001763 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1764 }
Mike Stump1eb44332009-09-09 15:08:12 +00001765
Steve Naroff28a7ca82007-08-20 22:28:22 +00001766 /// struct-declarator: declarator
1767 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001768 if (Tok.isNot(tok::colon)) {
1769 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1770 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001771 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001772 }
Mike Stump1eb44332009-09-09 15:08:12 +00001773
Chris Lattner04d66662007-10-09 17:33:22 +00001774 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001775 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001776 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001777 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001778 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001779 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001780 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001781 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001782
Steve Naroff28a7ca82007-08-20 22:28:22 +00001783 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001784 if (Tok.is(tok::kw___attribute)) {
1785 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001786 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001787 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1788 }
1789
John McCallbdd563e2009-11-03 02:38:08 +00001790 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00001791 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00001792 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001793
Steve Naroff28a7ca82007-08-20 22:28:22 +00001794 // If we don't have a comma, it is either the end of the list (a ';')
1795 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001796 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001797 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001798
Steve Naroff28a7ca82007-08-20 22:28:22 +00001799 // Consume the comma.
1800 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001801
John McCallbdd563e2009-11-03 02:38:08 +00001802 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001803 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001804}
1805
1806/// ParseStructUnionBody
1807/// struct-contents:
1808/// struct-declaration-list
1809/// [EXT] empty
1810/// [GNU] "struct-declaration-list" without terminatoring ';'
1811/// struct-declaration-list:
1812/// struct-declaration
1813/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001814/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001815///
Reid Spencer5f016e22007-07-11 17:01:13 +00001816void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001817 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00001818 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1819 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Reid Spencer5f016e22007-07-11 17:01:13 +00001821 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001823 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001824 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00001825
Reid Spencer5f016e22007-07-11 17:01:13 +00001826 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1827 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001828 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00001829 Diag(Tok, diag::ext_empty_struct_union)
1830 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00001831
John McCalld226f652010-08-21 09:40:31 +00001832 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001833
Reid Spencer5f016e22007-07-11 17:01:13 +00001834 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001835 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001836 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Reid Spencer5f016e22007-07-11 17:01:13 +00001838 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001839 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001840 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00001841 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00001842 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001843 ConsumeToken();
1844 continue;
1845 }
Chris Lattnere1359422008-04-10 06:46:29 +00001846
1847 // Parse all the comma separated declarators.
1848 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001849
John McCallbdd563e2009-11-03 02:38:08 +00001850 if (!Tok.is(tok::at)) {
1851 struct CFieldCallback : FieldCallback {
1852 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001853 Decl *TagDecl;
1854 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001855
John McCalld226f652010-08-21 09:40:31 +00001856 CFieldCallback(Parser &P, Decl *TagDecl,
1857 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001858 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1859
John McCalld226f652010-08-21 09:40:31 +00001860 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001861 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00001862 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00001863 FD.D.getDeclSpec().getSourceRange().getBegin(),
1864 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001865 FieldDecls.push_back(Field);
1866 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001867 }
John McCallbdd563e2009-11-03 02:38:08 +00001868 } Callback(*this, TagDecl, FieldDecls);
1869
1870 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001871 } else { // Handle @defs
1872 ConsumeToken();
1873 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1874 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001875 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001876 continue;
1877 }
1878 ConsumeToken();
1879 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1880 if (!Tok.is(tok::identifier)) {
1881 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001882 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001883 continue;
1884 }
John McCalld226f652010-08-21 09:40:31 +00001885 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001886 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001887 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001888 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1889 ConsumeToken();
1890 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001891 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001892
Chris Lattner04d66662007-10-09 17:33:22 +00001893 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001894 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001895 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001896 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001897 break;
1898 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001899 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1900 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001901 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001902 // If we stopped at a ';', eat it.
1903 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001904 }
1905 }
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Steve Naroff60fccee2007-10-29 21:38:07 +00001907 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Ted Kremenek8113ecf2010-11-10 05:59:39 +00001909 AttributeList *AttrList = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001910 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001911 if (Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +00001912 AttrList = ParseGNUAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001913
Douglas Gregor23c94db2010-07-02 17:43:08 +00001914 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001915 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001916 LBraceLoc, RBraceLoc,
Ted Kremenek8113ecf2010-11-10 05:59:39 +00001917 AttrList);
Douglas Gregor72de6672009-01-08 20:45:30 +00001918 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001919 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001920}
1921
1922
1923/// ParseEnumSpecifier
1924/// enum-specifier: [C99 6.7.2.2]
1925/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001926///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001927/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1928/// '}' attributes[opt]
1929/// 'enum' identifier
1930/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001931///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001932/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1933/// [C++0x] enum-head '{' enumerator-list ',' '}'
1934///
1935/// enum-head: [C++0x]
1936/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1937/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1938///
1939/// enum-key: [C++0x]
1940/// 'enum'
1941/// 'enum' 'class'
1942/// 'enum' 'struct'
1943///
1944/// enum-base: [C++0x]
1945/// ':' type-specifier-seq
1946///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001947/// [C++] elaborated-type-specifier:
1948/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1949///
Chris Lattner4c97d762009-04-12 21:49:30 +00001950void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001951 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001952 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001953 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001954 if (Tok.is(tok::code_completion)) {
1955 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001956 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00001957 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00001958 }
1959
Ted Kremenek8113ecf2010-11-10 05:59:39 +00001960 AttributeList *Attr = 0;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001961 // If attributes exist after tag, parse them.
1962 if (Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +00001963 Attr = ParseGNUAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001964
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001965 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00001966 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00001967 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00001968 return;
1969
1970 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001971 Diag(Tok, diag::err_expected_ident);
1972 if (Tok.isNot(tok::l_brace)) {
1973 // Has no name and is not a definition.
1974 // Skip the rest of this declarator, up until the comma or semicolon.
1975 SkipUntil(tok::comma, true);
1976 return;
1977 }
1978 }
1979 }
Mike Stump1eb44332009-09-09 15:08:12 +00001980
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001981 bool IsScopedEnum = false;
1982
1983 if (getLang().CPlusPlus0x && (Tok.is(tok::kw_class)
1984 || Tok.is(tok::kw_struct))) {
1985 ConsumeToken();
1986 IsScopedEnum = true;
1987 }
1988
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001989 // Must have either 'enum name' or 'enum {...}'.
1990 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1991 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001992
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001993 // Skip the rest of this declarator, up until the comma or semicolon.
1994 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001995 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001996 }
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001998 // If an identifier is present, consume and remember it.
1999 IdentifierInfo *Name = 0;
2000 SourceLocation NameLoc;
2001 if (Tok.is(tok::identifier)) {
2002 Name = Tok.getIdentifierInfo();
2003 NameLoc = ConsumeToken();
2004 }
Mike Stump1eb44332009-09-09 15:08:12 +00002005
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002006 if (!Name && IsScopedEnum) {
2007 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2008 // declaration of a scoped enumeration.
2009 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2010 IsScopedEnum = false;
2011 }
2012
2013 TypeResult BaseType;
2014
2015 if (getLang().CPlusPlus0x && Tok.is(tok::colon)) {
2016 ConsumeToken();
2017 SourceRange Range;
2018 BaseType = ParseTypeName(&Range);
2019 }
2020
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002021 // There are three options here. If we have 'enum foo;', then this is a
2022 // forward declaration. If we have 'enum foo {...' then this is a
2023 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2024 //
2025 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2026 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2027 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2028 //
John McCallf312b1e2010-08-26 23:41:50 +00002029 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002030 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002031 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002032 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002033 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002034 else
John McCallf312b1e2010-08-26 23:41:50 +00002035 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002036
2037 // enums cannot be templates, although they can be referenced from a
2038 // template.
2039 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002040 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002041 Diag(Tok, diag::err_enum_template);
2042
2043 // Skip the rest of this declarator, up until the comma or semicolon.
2044 SkipUntil(tok::comma, true);
2045 return;
2046 }
2047
Douglas Gregor402abb52009-05-28 23:31:59 +00002048 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002049 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002050 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2051 const char *PrevSpec = 0;
2052 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002053 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002054 StartLoc, SS, Name, NameLoc, Attr,
John McCalld226f652010-08-21 09:40:31 +00002055 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002056 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002057 Owned, IsDependent, IsScopedEnum,
2058 BaseType);
2059
Douglas Gregor48c89f42010-04-24 16:38:41 +00002060 if (IsDependent) {
2061 // This enum has a dependent nested-name-specifier. Handle it as a
2062 // dependent tag.
2063 if (!Name) {
2064 DS.SetTypeSpecError();
2065 Diag(Tok, diag::err_expected_type_name_after_typename);
2066 return;
2067 }
2068
Douglas Gregor23c94db2010-07-02 17:43:08 +00002069 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002070 TUK, SS, Name, StartLoc,
2071 NameLoc);
2072 if (Type.isInvalid()) {
2073 DS.SetTypeSpecError();
2074 return;
2075 }
2076
2077 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallb3d87482010-08-24 05:47:05 +00002078 Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002079 Diag(StartLoc, DiagID) << PrevSpec;
2080
2081 return;
2082 }
Mike Stump1eb44332009-09-09 15:08:12 +00002083
John McCalld226f652010-08-21 09:40:31 +00002084 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002085 // The action failed to produce an enumeration tag. If this is a
2086 // definition, consume the entire definition.
2087 if (Tok.is(tok::l_brace)) {
2088 ConsumeBrace();
2089 SkipUntil(tok::r_brace);
2090 }
2091
2092 DS.SetTypeSpecError();
2093 return;
2094 }
2095
Chris Lattner04d66662007-10-09 17:33:22 +00002096 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002097 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002098
John McCallb3d87482010-08-24 05:47:05 +00002099 // FIXME: The DeclSpec should keep the locations of both the keyword
2100 // and the name (if there is one).
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002101 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCalld226f652010-08-21 09:40:31 +00002102 TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002103 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002104}
2105
2106/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2107/// enumerator-list:
2108/// enumerator
2109/// enumerator-list ',' enumerator
2110/// enumerator:
2111/// enumeration-constant
2112/// enumeration-constant '=' constant-expression
2113/// enumeration-constant:
2114/// identifier
2115///
John McCalld226f652010-08-21 09:40:31 +00002116void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002117 // Enter the scope of the enum body and start the definition.
2118 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002119 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002120
Reid Spencer5f016e22007-07-11 17:01:13 +00002121 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002122
Chris Lattner7946dd32007-08-27 17:24:30 +00002123 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002124 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002125 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002126
John McCalld226f652010-08-21 09:40:31 +00002127 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002128
John McCalld226f652010-08-21 09:40:31 +00002129 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002130
Reid Spencer5f016e22007-07-11 17:01:13 +00002131 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002132 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002133 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2134 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002135
John McCall5b629aa2010-10-22 23:36:17 +00002136 // If attributes exist after the enumerator, parse them.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002137 AttributeList *Attr = 0;
John McCall5b629aa2010-10-22 23:36:17 +00002138 if (Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002139 Attr = ParseGNUAttributes();
John McCall5b629aa2010-10-22 23:36:17 +00002140
Reid Spencer5f016e22007-07-11 17:01:13 +00002141 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002142 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002143 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002144 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002145 AssignedVal = ParseConstantExpression();
2146 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002147 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002148 }
Mike Stump1eb44332009-09-09 15:08:12 +00002149
Reid Spencer5f016e22007-07-11 17:01:13 +00002150 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002151 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2152 LastEnumConstDecl,
2153 IdentLoc, Ident,
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002154 Attr, EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002155 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002156 EnumConstantDecls.push_back(EnumConstDecl);
2157 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002158
Douglas Gregor751f6922010-09-07 14:51:08 +00002159 if (Tok.is(tok::identifier)) {
2160 // We're missing a comma between enumerators.
2161 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2162 Diag(Loc, diag::err_enumerator_list_missing_comma)
2163 << FixItHint::CreateInsertion(Loc, ", ");
2164 continue;
2165 }
2166
Chris Lattner04d66662007-10-09 17:33:22 +00002167 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002168 break;
2169 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002170
2171 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002172 !(getLang().C99 || getLang().CPlusPlus0x))
2173 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2174 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002175 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002176 }
Mike Stump1eb44332009-09-09 15:08:12 +00002177
Reid Spencer5f016e22007-07-11 17:01:13 +00002178 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002179 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002180
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002181 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002182 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00002183 if (Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002184 Attr = ParseGNUAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00002185
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002186 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2187 EnumConstantDecls.data(), EnumConstantDecls.size(),
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002188 getCurScope(), Attr);
Mike Stump1eb44332009-09-09 15:08:12 +00002189
Douglas Gregor72de6672009-01-08 20:45:30 +00002190 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002191 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002192}
2193
2194/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002195/// start of a type-qualifier-list.
2196bool Parser::isTypeQualifier() const {
2197 switch (Tok.getKind()) {
2198 default: return false;
2199 // type-qualifier
2200 case tok::kw_const:
2201 case tok::kw_volatile:
2202 case tok::kw_restrict:
2203 return true;
2204 }
2205}
2206
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002207/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2208/// is definitely a type-specifier. Return false if it isn't part of a type
2209/// specifier or if we're not sure.
2210bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2211 switch (Tok.getKind()) {
2212 default: return false;
2213 // type-specifiers
2214 case tok::kw_short:
2215 case tok::kw_long:
2216 case tok::kw_signed:
2217 case tok::kw_unsigned:
2218 case tok::kw__Complex:
2219 case tok::kw__Imaginary:
2220 case tok::kw_void:
2221 case tok::kw_char:
2222 case tok::kw_wchar_t:
2223 case tok::kw_char16_t:
2224 case tok::kw_char32_t:
2225 case tok::kw_int:
2226 case tok::kw_float:
2227 case tok::kw_double:
2228 case tok::kw_bool:
2229 case tok::kw__Bool:
2230 case tok::kw__Decimal32:
2231 case tok::kw__Decimal64:
2232 case tok::kw__Decimal128:
2233 case tok::kw___vector:
2234
2235 // struct-or-union-specifier (C99) or class-specifier (C++)
2236 case tok::kw_class:
2237 case tok::kw_struct:
2238 case tok::kw_union:
2239 // enum-specifier
2240 case tok::kw_enum:
2241
2242 // typedef-name
2243 case tok::annot_typename:
2244 return true;
2245 }
2246}
2247
Steve Naroff5f8aa692008-02-11 23:15:56 +00002248/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002249/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002250bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002251 switch (Tok.getKind()) {
2252 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002253
Chris Lattner166a8fc2009-01-04 23:41:41 +00002254 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002255 if (TryAltiVecVectorToken())
2256 return true;
2257 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002258 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002259 // Annotate typenames and C++ scope specifiers. If we get one, just
2260 // recurse to handle whatever we get.
2261 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002262 return true;
2263 if (Tok.is(tok::identifier))
2264 return false;
2265 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002266
Chris Lattner166a8fc2009-01-04 23:41:41 +00002267 case tok::coloncolon: // ::foo::bar
2268 if (NextToken().is(tok::kw_new) || // ::new
2269 NextToken().is(tok::kw_delete)) // ::delete
2270 return false;
2271
Chris Lattner166a8fc2009-01-04 23:41:41 +00002272 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002273 return true;
2274 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002275
Reid Spencer5f016e22007-07-11 17:01:13 +00002276 // GNU attributes support.
2277 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002278 // GNU typeof support.
2279 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002280
Reid Spencer5f016e22007-07-11 17:01:13 +00002281 // type-specifiers
2282 case tok::kw_short:
2283 case tok::kw_long:
2284 case tok::kw_signed:
2285 case tok::kw_unsigned:
2286 case tok::kw__Complex:
2287 case tok::kw__Imaginary:
2288 case tok::kw_void:
2289 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002290 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002291 case tok::kw_char16_t:
2292 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002293 case tok::kw_int:
2294 case tok::kw_float:
2295 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002296 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002297 case tok::kw__Bool:
2298 case tok::kw__Decimal32:
2299 case tok::kw__Decimal64:
2300 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002301 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002302
Chris Lattner99dc9142008-04-13 18:59:07 +00002303 // struct-or-union-specifier (C99) or class-specifier (C++)
2304 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002305 case tok::kw_struct:
2306 case tok::kw_union:
2307 // enum-specifier
2308 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002309
Reid Spencer5f016e22007-07-11 17:01:13 +00002310 // type-qualifier
2311 case tok::kw_const:
2312 case tok::kw_volatile:
2313 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002314
2315 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002316 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002317 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002318
Chris Lattner7c186be2008-10-20 00:25:30 +00002319 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2320 case tok::less:
2321 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002322
Steve Naroff239f0732008-12-25 14:16:32 +00002323 case tok::kw___cdecl:
2324 case tok::kw___stdcall:
2325 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002326 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002327 case tok::kw___w64:
2328 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002329 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002330 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002331 }
2332}
2333
2334/// isDeclarationSpecifier() - Return true if the current token is part of a
2335/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002336///
2337/// \param DisambiguatingWithExpression True to indicate that the purpose of
2338/// this check is to disambiguate between an expression and a declaration.
2339bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002340 switch (Tok.getKind()) {
2341 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002342
Chris Lattner166a8fc2009-01-04 23:41:41 +00002343 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002344 // Unfortunate hack to support "Class.factoryMethod" notation.
2345 if (getLang().ObjC1 && NextToken().is(tok::period))
2346 return false;
John Thompson82287d12010-02-05 00:12:22 +00002347 if (TryAltiVecVectorToken())
2348 return true;
2349 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002350 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002351 // Annotate typenames and C++ scope specifiers. If we get one, just
2352 // recurse to handle whatever we get.
2353 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002354 return true;
2355 if (Tok.is(tok::identifier))
2356 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002357
2358 // If we're in Objective-C and we have an Objective-C class type followed
2359 // by an identifier and then either ':' or ']', in a place where an
2360 // expression is permitted, then this is probably a class message send
2361 // missing the initial '['. In this case, we won't consider this to be
2362 // the start of a declaration.
2363 if (DisambiguatingWithExpression &&
2364 isStartOfObjCClassMessageMissingOpenBracket())
2365 return false;
2366
John McCall9ba61662010-02-26 08:45:28 +00002367 return isDeclarationSpecifier();
2368
Chris Lattner166a8fc2009-01-04 23:41:41 +00002369 case tok::coloncolon: // ::foo::bar
2370 if (NextToken().is(tok::kw_new) || // ::new
2371 NextToken().is(tok::kw_delete)) // ::delete
2372 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002373
Chris Lattner166a8fc2009-01-04 23:41:41 +00002374 // Annotate typenames and C++ scope specifiers. If we get one, just
2375 // recurse to handle whatever we get.
2376 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002377 return true;
2378 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002379
Reid Spencer5f016e22007-07-11 17:01:13 +00002380 // storage-class-specifier
2381 case tok::kw_typedef:
2382 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002383 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002384 case tok::kw_static:
2385 case tok::kw_auto:
2386 case tok::kw_register:
2387 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002388
Reid Spencer5f016e22007-07-11 17:01:13 +00002389 // type-specifiers
2390 case tok::kw_short:
2391 case tok::kw_long:
2392 case tok::kw_signed:
2393 case tok::kw_unsigned:
2394 case tok::kw__Complex:
2395 case tok::kw__Imaginary:
2396 case tok::kw_void:
2397 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002398 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002399 case tok::kw_char16_t:
2400 case tok::kw_char32_t:
2401
Reid Spencer5f016e22007-07-11 17:01:13 +00002402 case tok::kw_int:
2403 case tok::kw_float:
2404 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002405 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002406 case tok::kw__Bool:
2407 case tok::kw__Decimal32:
2408 case tok::kw__Decimal64:
2409 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002410 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002411
Chris Lattner99dc9142008-04-13 18:59:07 +00002412 // struct-or-union-specifier (C99) or class-specifier (C++)
2413 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002414 case tok::kw_struct:
2415 case tok::kw_union:
2416 // enum-specifier
2417 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002418
Reid Spencer5f016e22007-07-11 17:01:13 +00002419 // type-qualifier
2420 case tok::kw_const:
2421 case tok::kw_volatile:
2422 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002423
Reid Spencer5f016e22007-07-11 17:01:13 +00002424 // function-specifier
2425 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002426 case tok::kw_virtual:
2427 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002428
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002429 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002430 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002431
Chris Lattner1ef08762007-08-09 17:01:07 +00002432 // GNU typeof support.
2433 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002434
Chris Lattner1ef08762007-08-09 17:01:07 +00002435 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002436 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002437 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002438
Chris Lattnerf3948c42008-07-26 03:38:44 +00002439 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2440 case tok::less:
2441 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002442
Steve Naroff47f52092009-01-06 19:34:12 +00002443 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002444 case tok::kw___cdecl:
2445 case tok::kw___stdcall:
2446 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002447 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002448 case tok::kw___w64:
2449 case tok::kw___ptr64:
2450 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002451 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002452 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002453 }
2454}
2455
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002456bool Parser::isConstructorDeclarator() {
2457 TentativeParsingAction TPA(*this);
2458
2459 // Parse the C++ scope specifier.
2460 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002461 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00002462 TPA.Revert();
2463 return false;
2464 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002465
2466 // Parse the constructor name.
2467 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2468 // We already know that we have a constructor name; just consume
2469 // the token.
2470 ConsumeToken();
2471 } else {
2472 TPA.Revert();
2473 return false;
2474 }
2475
2476 // Current class name must be followed by a left parentheses.
2477 if (Tok.isNot(tok::l_paren)) {
2478 TPA.Revert();
2479 return false;
2480 }
2481 ConsumeParen();
2482
2483 // A right parentheses or ellipsis signals that we have a constructor.
2484 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2485 TPA.Revert();
2486 return true;
2487 }
2488
2489 // If we need to, enter the specified scope.
2490 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002491 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002492 DeclScopeObj.EnterDeclaratorScope();
2493
2494 // Check whether the next token(s) are part of a declaration
2495 // specifier, in which case we have the start of a parameter and,
2496 // therefore, we know that this is a constructor.
2497 bool IsConstructor = isDeclarationSpecifier();
2498 TPA.Revert();
2499 return IsConstructor;
2500}
Reid Spencer5f016e22007-07-11 17:01:13 +00002501
2502/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00002503/// type-qualifier-list: [C99 6.7.5]
2504/// type-qualifier
2505/// [vendor] attributes
2506/// [ only if VendorAttributesAllowed=true ]
2507/// type-qualifier-list type-qualifier
2508/// [vendor] type-qualifier-list attributes
2509/// [ only if VendorAttributesAllowed=true ]
2510/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2511/// [ only if CXX0XAttributesAllowed=true ]
2512/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00002513///
Dawn Perchik52fc3142010-09-03 01:29:35 +00002514void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2515 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00002516 bool CXX0XAttributesAllowed) {
2517 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2518 SourceLocation Loc = Tok.getLocation();
2519 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2520 if (CXX0XAttributesAllowed)
2521 DS.AddAttributes(Attr.AttrList);
2522 else
2523 Diag(Loc, diag::err_attributes_not_allowed);
2524 }
2525
Reid Spencer5f016e22007-07-11 17:01:13 +00002526 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002527 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002528 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002529 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002530 SourceLocation Loc = Tok.getLocation();
2531
2532 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00002533 case tok::code_completion:
2534 Actions.CodeCompleteTypeQualifiers(DS);
2535 ConsumeCodeCompletionToken();
2536 break;
2537
Reid Spencer5f016e22007-07-11 17:01:13 +00002538 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002539 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2540 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002541 break;
2542 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002543 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2544 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002545 break;
2546 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002547 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2548 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002549 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002550 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002551 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002552 case tok::kw___cdecl:
2553 case tok::kw___stdcall:
2554 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002555 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002556 if (VendorAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002557 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2558 continue;
2559 }
2560 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002561 case tok::kw___pascal:
2562 if (VendorAttributesAllowed) {
2563 DS.AddAttributes(ParseBorlandTypeAttributes());
2564 continue;
2565 }
2566 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002567 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002568 if (VendorAttributesAllowed) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002569 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002570 continue; // do *not* consume the next token!
2571 }
2572 // otherwise, FALL THROUGH!
2573 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002574 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002575 // If this is not a type-qualifier token, we're done reading type
2576 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002577 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002578 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002579 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002580
Reid Spencer5f016e22007-07-11 17:01:13 +00002581 // If the specifier combination wasn't legal, issue a diagnostic.
2582 if (isInvalid) {
2583 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002584 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002585 }
2586 ConsumeToken();
2587 }
2588}
2589
2590
2591/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2592///
2593void Parser::ParseDeclarator(Declarator &D) {
2594 /// This implements the 'declarator' production in the C grammar, then checks
2595 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002596 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002597}
2598
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002599/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2600/// is parsed by the function passed to it. Pass null, and the direct-declarator
2601/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002602/// ptr-operator production.
2603///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002604/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2605/// [C] pointer[opt] direct-declarator
2606/// [C++] direct-declarator
2607/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002608///
2609/// pointer: [C99 6.7.5]
2610/// '*' type-qualifier-list[opt]
2611/// '*' type-qualifier-list[opt] pointer
2612///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002613/// ptr-operator:
2614/// '*' cv-qualifier-seq[opt]
2615/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002616/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002617/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002618/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002619/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002620void Parser::ParseDeclaratorInternal(Declarator &D,
2621 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002622 if (Diags.hasAllExtensionsSilenced())
2623 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002624
Sebastian Redlf30208a2009-01-24 21:16:55 +00002625 // C++ member pointers start with a '::' or a nested-name.
2626 // Member pointers get special handling, since there's no place for the
2627 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002628 if (getLang().CPlusPlus &&
2629 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2630 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002631 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002632 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00002633
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002634 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002635 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002636 // The scope spec really belongs to the direct-declarator.
2637 D.getCXXScopeSpec() = SS;
2638 if (DirectDeclParser)
2639 (this->*DirectDeclParser)(D);
2640 return;
2641 }
2642
2643 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002644 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002645 DeclSpec DS;
2646 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002647 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002648
2649 // Recurse to parse whatever is left.
2650 ParseDeclaratorInternal(D, DirectDeclParser);
2651
2652 // Sema will have to catch (syntactically invalid) pointers into global
2653 // scope. It has to catch pointers into namespace scope anyway.
2654 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002655 Loc, DS.TakeAttributes()),
2656 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002657 return;
2658 }
2659 }
2660
2661 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002662 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002663 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002664 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002665 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002666 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002667 if (DirectDeclParser)
2668 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002669 return;
2670 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002671
Sebastian Redl05532f22009-03-15 22:02:01 +00002672 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2673 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002674 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002675 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002676
Chris Lattner9af55002009-03-27 04:18:06 +00002677 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002678 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002679 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002680
Reid Spencer5f016e22007-07-11 17:01:13 +00002681 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002682 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002683
Reid Spencer5f016e22007-07-11 17:01:13 +00002684 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002685 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002686 if (Kind == tok::star)
2687 // Remember that we parsed a pointer type, and remember the type-quals.
2688 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002689 DS.TakeAttributes()),
2690 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002691 else
2692 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002693 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002694 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002695 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002696 } else {
2697 // Is a reference
2698 DeclSpec DS;
2699
Sebastian Redl743de1f2009-03-23 00:00:23 +00002700 // Complain about rvalue references in C++03, but then go on and build
2701 // the declarator.
2702 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2703 Diag(Loc, diag::err_rvalue_reference);
2704
Reid Spencer5f016e22007-07-11 17:01:13 +00002705 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2706 // cv-qualifiers are introduced through the use of a typedef or of a
2707 // template type argument, in which case the cv-qualifiers are ignored.
2708 //
2709 // [GNU] Retricted references are allowed.
2710 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002711 // [C++0x] Attributes on references are not allowed.
2712 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002713 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002714
2715 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2716 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2717 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002718 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002719 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2720 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002721 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002722 }
2723
2724 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002725 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002726
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002727 if (D.getNumTypeObjects() > 0) {
2728 // C++ [dcl.ref]p4: There shall be no references to references.
2729 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2730 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002731 if (const IdentifierInfo *II = D.getIdentifier())
2732 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2733 << II;
2734 else
2735 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2736 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002737
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002738 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002739 // can go ahead and build the (technically ill-formed)
2740 // declarator: reference collapsing will take care of it.
2741 }
2742 }
2743
Reid Spencer5f016e22007-07-11 17:01:13 +00002744 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002745 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002746 DS.TakeAttributes(),
2747 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002748 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002749 }
2750}
2751
2752/// ParseDirectDeclarator
2753/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002754/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002755/// '(' declarator ')'
2756/// [GNU] '(' attributes declarator ')'
2757/// [C90] direct-declarator '[' constant-expression[opt] ']'
2758/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2759/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2760/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2761/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2762/// direct-declarator '(' parameter-type-list ')'
2763/// direct-declarator '(' identifier-list[opt] ')'
2764/// [GNU] direct-declarator '(' parameter-forward-declarations
2765/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002766/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2767/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002768/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002769///
2770/// declarator-id: [C++ 8]
2771/// id-expression
2772/// '::'[opt] nested-name-specifier[opt] type-name
2773///
2774/// id-expression: [C++ 5.1]
2775/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002776/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002777///
2778/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002779/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002780/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002781/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002782/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002783/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002784///
Reid Spencer5f016e22007-07-11 17:01:13 +00002785void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002786 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002787
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002788 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2789 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002790 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00002791 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00002792 }
2793
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002794 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002795 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00002796 // Change the declaration context for name lookup, until this function
2797 // is exited (and the declarator has been parsed).
2798 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002799 }
2800
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002801 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2802 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2803 // We found something that indicates the start of an unqualified-id.
2804 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002805 bool AllowConstructorName;
2806 if (D.getDeclSpec().hasTypeSpecifier())
2807 AllowConstructorName = false;
2808 else if (D.getCXXScopeSpec().isSet())
2809 AllowConstructorName =
2810 (D.getContext() == Declarator::FileContext ||
2811 (D.getContext() == Declarator::MemberContext &&
2812 D.getDeclSpec().isFriendSpecified()));
2813 else
2814 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2815
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002816 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2817 /*EnteringContext=*/true,
2818 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002819 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002820 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002821 D.getName()) ||
2822 // Once we're past the identifier, if the scope was bad, mark the
2823 // whole declarator bad.
2824 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002825 D.SetIdentifier(0, Tok.getLocation());
2826 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002827 } else {
2828 // Parsed the unqualified-id; update range information and move along.
2829 if (D.getSourceRange().getBegin().isInvalid())
2830 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2831 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002832 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002833 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002834 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002835 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002836 assert(!getLang().CPlusPlus &&
2837 "There's a C++-specific check for tok::identifier above");
2838 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2839 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2840 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002841 goto PastIdentifier;
2842 }
2843
2844 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002845 // direct-declarator: '(' declarator ')'
2846 // direct-declarator: '(' attributes declarator ')'
2847 // Example: 'char (*X)' or 'int (*XX)(void)'
2848 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002849
2850 // If the declarator was parenthesized, we entered the declarator
2851 // scope when parsing the parenthesized declarator, then exited
2852 // the scope already. Re-enter the scope, if we need to.
2853 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002854 // If there was an error parsing parenthesized declarator, declarator
2855 // scope may have been enterred before. Don't do it again.
2856 if (!D.isInvalidType() &&
2857 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002858 // Change the declaration context for name lookup, until this function
2859 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002860 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002861 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002862 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002863 // This could be something simple like "int" (in which case the declarator
2864 // portion is empty), if an abstract-declarator is allowed.
2865 D.SetIdentifier(0, Tok.getLocation());
2866 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002867 if (D.getContext() == Declarator::MemberContext)
2868 Diag(Tok, diag::err_expected_member_name_or_semi)
2869 << D.getDeclSpec().getSourceRange();
2870 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002871 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002872 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002873 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002874 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002875 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002876 }
Mike Stump1eb44332009-09-09 15:08:12 +00002877
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002878 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002879 assert(D.isPastIdentifier() &&
2880 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002881
Sean Huntbbd37c62009-11-21 08:43:09 +00002882 // Don't parse attributes unless we have an identifier.
Douglas Gregor3c3aaf92010-02-19 16:47:56 +00002883 if (D.getIdentifier() && getLang().CPlusPlus0x
Sean Huntbbd37c62009-11-21 08:43:09 +00002884 && isCXX0XAttributeSpecifier(true)) {
2885 SourceLocation AttrEndLoc;
2886 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2887 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2888 }
2889
Reid Spencer5f016e22007-07-11 17:01:13 +00002890 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002891 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002892 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2893 // In such a case, check if we actually have a function declarator; if it
2894 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002895 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2896 // When not in file scope, warn for ambiguous function declarators, just
2897 // in case the author intended it as a variable definition.
2898 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2899 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2900 break;
2901 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002902 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002903 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002904 ParseBracketDeclarator(D);
2905 } else {
2906 break;
2907 }
2908 }
2909}
2910
Chris Lattneref4715c2008-04-06 05:45:57 +00002911/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2912/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002913/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002914/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2915///
2916/// direct-declarator:
2917/// '(' declarator ')'
2918/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002919/// direct-declarator '(' parameter-type-list ')'
2920/// direct-declarator '(' identifier-list[opt] ')'
2921/// [GNU] direct-declarator '(' parameter-forward-declarations
2922/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002923///
2924void Parser::ParseParenDeclarator(Declarator &D) {
2925 SourceLocation StartLoc = ConsumeParen();
2926 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002927
Chris Lattner7399ee02008-10-20 02:05:46 +00002928 // Eat any attributes before we look at whether this is a grouping or function
2929 // declarator paren. If this is a grouping paren, the attribute applies to
2930 // the type being built up, for example:
2931 // int (__attribute__(()) *x)(long y)
2932 // If this ends up not being a grouping paren, the attribute applies to the
2933 // first argument, for example:
2934 // int (__attribute__(()) int x)
2935 // In either case, we need to eat any attributes to be able to determine what
2936 // sort of paren this is.
2937 //
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002938 AttributeList *AttrList = 0;
Chris Lattner7399ee02008-10-20 02:05:46 +00002939 bool RequiresArg = false;
2940 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002941 AttrList = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00002942
Chris Lattner7399ee02008-10-20 02:05:46 +00002943 // We require that the argument list (if this is a non-grouping paren) be
2944 // present even if the attribute list was empty.
2945 RequiresArg = true;
2946 }
Steve Naroff239f0732008-12-25 14:16:32 +00002947 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002948 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002949 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2950 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002951 AttrList = ParseMicrosoftTypeAttributes(AttrList);
Eli Friedman290eeb02009-06-08 23:27:34 +00002952 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00002953 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002954 if (Tok.is(tok::kw___pascal))
2955 AttrList = ParseBorlandTypeAttributes(AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002956
Chris Lattneref4715c2008-04-06 05:45:57 +00002957 // If we haven't past the identifier yet (or where the identifier would be
2958 // stored, if this is an abstract declarator), then this is probably just
2959 // grouping parens. However, if this could be an abstract-declarator, then
2960 // this could also be the start of function arguments (consider 'void()').
2961 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002962
Chris Lattneref4715c2008-04-06 05:45:57 +00002963 if (!D.mayOmitIdentifier()) {
2964 // If this can't be an abstract-declarator, this *must* be a grouping
2965 // paren, because we haven't seen the identifier yet.
2966 isGrouping = true;
2967 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002968 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002969 isDeclarationSpecifier()) { // 'int(int)' is a function.
2970 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2971 // considered to be a type, not a K&R identifier-list.
2972 isGrouping = false;
2973 } else {
2974 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2975 isGrouping = true;
2976 }
Mike Stump1eb44332009-09-09 15:08:12 +00002977
Chris Lattneref4715c2008-04-06 05:45:57 +00002978 // If this is a grouping paren, handle:
2979 // direct-declarator: '(' declarator ')'
2980 // direct-declarator: '(' attributes declarator ')'
2981 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002982 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002983 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002984 if (AttrList)
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002985 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002986
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002987 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002988 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002989 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002990
2991 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002992 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002993 return;
2994 }
Mike Stump1eb44332009-09-09 15:08:12 +00002995
Chris Lattneref4715c2008-04-06 05:45:57 +00002996 // Okay, if this wasn't a grouping paren, it must be the start of a function
2997 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002998 // identifier (and remember where it would have been), then call into
2999 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003000 D.SetIdentifier(0, Tok.getLocation());
3001
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003002 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003003}
3004
3005/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3006/// declarator D up to a paren, which indicates that we are parsing function
3007/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003008///
Chris Lattner7399ee02008-10-20 02:05:46 +00003009/// If AttrList is non-null, then the caller parsed those arguments immediately
3010/// after the open paren - they should be considered to be the first argument of
3011/// a parameter. If RequiresArg is true, then the first argument of the
3012/// function is required to be present and required to not be an identifier
3013/// list.
3014///
Reid Spencer5f016e22007-07-11 17:01:13 +00003015/// This method also handles this portion of the grammar:
3016/// parameter-type-list: [C99 6.7.5]
3017/// parameter-list
3018/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003019/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003020///
3021/// parameter-list: [C99 6.7.5]
3022/// parameter-declaration
3023/// parameter-list ',' parameter-declaration
3024///
3025/// parameter-declaration: [C99 6.7.5]
3026/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003027/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003028/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003029/// declaration-specifiers abstract-declarator[opt]
3030/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003031/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003032/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3033///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003034/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00003035/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003036///
Chris Lattner7399ee02008-10-20 02:05:46 +00003037void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3038 AttributeList *AttrList,
3039 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003040 // lparen is already consumed!
3041 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003042
Douglas Gregordab60ad2010-10-01 18:44:50 +00003043 ParsedType TrailingReturnType;
3044
Chris Lattner7399ee02008-10-20 02:05:46 +00003045 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003046 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003047 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003048 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003049
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003050 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3051 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003052
3053 // cv-qualifier-seq[opt].
3054 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003055 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003056 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003057 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003058 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003059 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003060 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003061 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003062 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003063 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003064
3065 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003066 if (Tok.is(tok::kw_throw)) {
3067 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003068 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003069 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003070 hasAnyExceptionSpec);
3071 assert(Exceptions.size() == ExceptionRanges.size() &&
3072 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003073 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00003074
3075 // Parse trailing-return-type.
3076 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3077 TrailingReturnType = ParseTrailingReturnType().get();
3078 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003079 }
3080
Chris Lattnerf97409f2008-04-06 06:57:35 +00003081 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003082 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003083 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003084 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003085 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003086 /*arglist*/ 0, 0,
3087 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003088 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003089 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003090 Exceptions.data(),
3091 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003092 Exceptions.size(),
Douglas Gregordab60ad2010-10-01 18:44:50 +00003093 LParenLoc, RParenLoc, D,
3094 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003095 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003096 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003097 }
3098
Chris Lattner7399ee02008-10-20 02:05:46 +00003099 // Alternatively, this parameter list may be an identifier list form for a
3100 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003101 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3102 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003103 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003104 // K&R identifier lists can't have typedefs as identifiers, per
3105 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003106 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003107 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003108
Steve Naroff2d081c42009-01-28 19:16:40 +00003109 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003110 // normal declarators, not for abstract-declarators. Get the first
3111 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003112 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003113 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003114
3115 // Identifier lists follow a really simple grammar: the identifiers can
3116 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3117 // identifier lists are really rare in the brave new modern world, and it
3118 // is very common for someone to typo a type in a non-k&r style list. If
3119 // we are presented with something like: "void foo(intptr x, float y)",
3120 // we don't want to start parsing the function declarator as though it is
3121 // a K&R style declarator just because intptr is an invalid type.
3122 //
3123 // To handle this, we check to see if the token after the first identifier
3124 // is a "," or ")". Only if so, do we parse it as an identifier list.
3125 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3126 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3127 FirstTok.getIdentifierInfo(),
3128 FirstTok.getLocation(), D);
3129
3130 // If we get here, the code is invalid. Push the first identifier back
3131 // into the token stream and parse the first argument as an (invalid)
3132 // normal argument declarator.
3133 PP.EnterToken(Tok);
3134 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003135 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003136 }
Mike Stump1eb44332009-09-09 15:08:12 +00003137
Chris Lattnerf97409f2008-04-06 06:57:35 +00003138 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003139
Chris Lattnerf97409f2008-04-06 06:57:35 +00003140 // Build up an array of information about the parsed arguments.
3141 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003142
3143 // Enter function-declaration scope, limiting any declarators to the
3144 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003145 ParseScope PrototypeScope(this,
3146 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003147
Chris Lattnerf97409f2008-04-06 06:57:35 +00003148 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003149 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003150 while (1) {
3151 if (Tok.is(tok::ellipsis)) {
3152 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003153 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003154 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003155 }
Francois Pichet334d47e2010-10-11 12:59:39 +00003156
3157 // Skip any Microsoft attributes before a param.
3158 if (getLang().Microsoft && Tok.is(tok::l_square))
3159 ParseMicrosoftAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00003160
Chris Lattnerf97409f2008-04-06 06:57:35 +00003161 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00003162
Chris Lattnerf97409f2008-04-06 06:57:35 +00003163 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003164 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003165 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00003166
3167 // If the caller parsed attributes for the first argument, add them now.
3168 if (AttrList) {
3169 DS.AddAttributes(AttrList);
3170 AttrList = 0; // Only apply the attributes to the first parameter.
3171 }
Chris Lattnere64c5492009-02-27 18:38:20 +00003172 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003173
Chris Lattnerf97409f2008-04-06 06:57:35 +00003174 // Parse the declarator. This is "PrototypeContext", because we must
3175 // accept either 'declarator' or 'abstract-declarator' here.
3176 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3177 ParseDeclarator(ParmDecl);
3178
3179 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003180 if (Tok.is(tok::kw___attribute)) {
3181 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00003182 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003183 ParmDecl.AddAttributes(AttrList, Loc);
3184 }
Mike Stump1eb44332009-09-09 15:08:12 +00003185
Chris Lattnerf97409f2008-04-06 06:57:35 +00003186 // Remember this parsed parameter in ParamInfo.
3187 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003188
Douglas Gregor72b505b2008-12-16 21:30:33 +00003189 // DefArgToks is used when the parsing of default arguments needs
3190 // to be delayed.
3191 CachedTokens *DefArgToks = 0;
3192
Chris Lattnerf97409f2008-04-06 06:57:35 +00003193 // If no parameter was specified, verify that *something* was specified,
3194 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003195 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3196 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003197 // Completely missing, emit error.
3198 Diag(DSStart, diag::err_missing_param);
3199 } else {
3200 // Otherwise, we have something. Add it and let semantic analysis try
3201 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003202
Chris Lattnerf97409f2008-04-06 06:57:35 +00003203 // Inform the actions module about the parameter declarator, so it gets
3204 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003205 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003206
3207 // Parse the default argument, if any. We parse the default
3208 // arguments in all dialects; the semantic analysis in
3209 // ActOnParamDefaultArgument will reject the default argument in
3210 // C.
3211 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003212 SourceLocation EqualLoc = Tok.getLocation();
3213
Chris Lattner04421082008-04-08 04:40:51 +00003214 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003215 if (D.getContext() == Declarator::MemberContext) {
3216 // If we're inside a class definition, cache the tokens
3217 // corresponding to the default argument. We'll actually parse
3218 // them when we see the end of the class definition.
3219 // FIXME: Templates will require something similar.
3220 // FIXME: Can we use a smart pointer for Toks?
3221 DefArgToks = new CachedTokens;
3222
Mike Stump1eb44332009-09-09 15:08:12 +00003223 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003224 /*StopAtSemi=*/true,
3225 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003226 delete DefArgToks;
3227 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003228 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003229 } else {
3230 // Mark the end of the default argument so that we know when to
3231 // stop when we parse it later on.
3232 Token DefArgEnd;
3233 DefArgEnd.startToken();
3234 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3235 DefArgEnd.setLocation(Tok.getLocation());
3236 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003237 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003238 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003239 }
Chris Lattner04421082008-04-08 04:40:51 +00003240 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003241 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003242 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003243
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003244 // The argument isn't actually potentially evaluated unless it is
3245 // used.
3246 EnterExpressionEvaluationContext Eval(Actions,
3247 Sema::PotentiallyEvaluatedIfUsed);
3248
John McCall60d7b3a2010-08-24 06:29:42 +00003249 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003250 if (DefArgResult.isInvalid()) {
3251 Actions.ActOnParamDefaultArgumentError(Param);
3252 SkipUntil(tok::comma, tok::r_paren, true, true);
3253 } else {
3254 // Inform the actions module about the default argument
3255 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003256 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003257 }
Chris Lattner04421082008-04-08 04:40:51 +00003258 }
3259 }
Mike Stump1eb44332009-09-09 15:08:12 +00003260
3261 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3262 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003263 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003264 }
3265
3266 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003267 if (Tok.isNot(tok::comma)) {
3268 if (Tok.is(tok::ellipsis)) {
3269 IsVariadic = true;
3270 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3271
3272 if (!getLang().CPlusPlus) {
3273 // We have ellipsis without a preceding ',', which is ill-formed
3274 // in C. Complain and provide the fix.
3275 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003276 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003277 }
3278 }
3279
3280 break;
3281 }
Mike Stump1eb44332009-09-09 15:08:12 +00003282
Chris Lattnerf97409f2008-04-06 06:57:35 +00003283 // Consume the comma.
3284 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003285 }
Mike Stump1eb44332009-09-09 15:08:12 +00003286
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003287 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003288 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3289 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003290
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003291 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003292 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003293 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003294 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003295 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003296 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003297
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003298 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003299 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003300 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003301 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003302 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003303
3304 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003305 if (Tok.is(tok::kw_throw)) {
3306 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003307 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003308 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003309 hasAnyExceptionSpec);
3310 assert(Exceptions.size() == ExceptionRanges.size() &&
3311 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003312 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00003313
3314 // Parse trailing-return-type.
3315 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3316 TrailingReturnType = ParseTrailingReturnType().get();
3317 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003318 }
3319
Douglas Gregordab60ad2010-10-01 18:44:50 +00003320 // FIXME: We should leave the prototype scope before parsing the exception
3321 // specification, and then reenter it when parsing the trailing return type.
3322
3323 // Leave prototype scope.
3324 PrototypeScope.Exit();
3325
Reid Spencer5f016e22007-07-11 17:01:13 +00003326 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003327 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003328 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003329 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003330 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003331 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003332 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003333 Exceptions.data(),
3334 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003335 Exceptions.size(),
Douglas Gregordab60ad2010-10-01 18:44:50 +00003336 LParenLoc, RParenLoc, D,
3337 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003338 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003339}
3340
Chris Lattner66d28652008-04-06 06:34:08 +00003341/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3342/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003343/// first identifier has already been consumed, and the current token is the
3344/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003345///
3346/// identifier-list: [C99 6.7.5]
3347/// identifier
3348/// identifier-list ',' identifier
3349///
3350void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003351 IdentifierInfo *FirstIdent,
3352 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003353 Declarator &D) {
3354 // Build up an array of information about the parsed arguments.
3355 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3356 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003357
Chris Lattner66d28652008-04-06 06:34:08 +00003358 // If there was no identifier specified for the declarator, either we are in
3359 // an abstract-declarator, or we are in a parameter declarator which was found
3360 // to be abstract. In abstract-declarators, identifier lists are not valid:
3361 // diagnose this.
3362 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003363 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003364
Chris Lattner83a94472010-05-14 17:23:36 +00003365 // The first identifier was already read, and is known to be the first
3366 // identifier in the list. Remember this identifier in ParamInfo.
3367 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003368 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003369
Chris Lattner66d28652008-04-06 06:34:08 +00003370 while (Tok.is(tok::comma)) {
3371 // Eat the comma.
3372 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003373
Chris Lattner50c64772008-04-06 06:39:19 +00003374 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003375 if (Tok.isNot(tok::identifier)) {
3376 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003377 SkipUntil(tok::r_paren);
3378 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003379 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003380
Chris Lattner66d28652008-04-06 06:34:08 +00003381 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003382
3383 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003384 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003385 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003386
Chris Lattner66d28652008-04-06 06:34:08 +00003387 // Verify that the argument identifier has not already been mentioned.
3388 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003389 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003390 } else {
3391 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003392 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003393 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00003394 0));
Chris Lattner50c64772008-04-06 06:39:19 +00003395 }
Mike Stump1eb44332009-09-09 15:08:12 +00003396
Chris Lattner66d28652008-04-06 06:34:08 +00003397 // Eat the identifier.
3398 ConsumeToken();
3399 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003400
3401 // If we have the closing ')', eat it and we're done.
3402 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3403
Chris Lattner50c64772008-04-06 06:39:19 +00003404 // Remember that we parsed a function type, and remember the attributes. This
3405 // function type is always a K&R style function type, which is not varargs and
3406 // has no prototype.
3407 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003408 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003409 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003410 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003411 /*exception*/false,
3412 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003413 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003414 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003415}
Chris Lattneref4715c2008-04-06 05:45:57 +00003416
Reid Spencer5f016e22007-07-11 17:01:13 +00003417/// [C90] direct-declarator '[' constant-expression[opt] ']'
3418/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3419/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3420/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3421/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3422void Parser::ParseBracketDeclarator(Declarator &D) {
3423 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003424
Chris Lattner378c7e42008-12-18 07:27:21 +00003425 // C array syntax has many features, but by-far the most common is [] and [4].
3426 // This code does a fast path to handle some of the most obvious cases.
3427 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003428 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003429 //FIXME: Use these
3430 CXX0XAttributeList Attr;
3431 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3432 Attr = ParseCXX0XAttributes();
3433 }
3434
Chris Lattner378c7e42008-12-18 07:27:21 +00003435 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00003436 ExprResult NumElements;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003437 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3438 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003439 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003440 return;
3441 } else if (Tok.getKind() == tok::numeric_constant &&
3442 GetLookAheadToken(1).is(tok::r_square)) {
3443 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00003444 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003445 ConsumeToken();
3446
Sebastian Redlab197ba2009-02-09 18:23:29 +00003447 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003448 //FIXME: Use these
3449 CXX0XAttributeList Attr;
3450 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3451 Attr = ParseCXX0XAttributes();
3452 }
Chris Lattner378c7e42008-12-18 07:27:21 +00003453
3454 // If there was an error parsing the assignment-expression, recover.
3455 if (ExprRes.isInvalid())
3456 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00003457
Chris Lattner378c7e42008-12-18 07:27:21 +00003458 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003459 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3460 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003461 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003462 return;
3463 }
Mike Stump1eb44332009-09-09 15:08:12 +00003464
Reid Spencer5f016e22007-07-11 17:01:13 +00003465 // If valid, this location is the position where we read the 'static' keyword.
3466 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003467 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003468 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003469
Reid Spencer5f016e22007-07-11 17:01:13 +00003470 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003471 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003472 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003473 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003474
Reid Spencer5f016e22007-07-11 17:01:13 +00003475 // If we haven't already read 'static', check to see if there is one after the
3476 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003477 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003478 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003479
Reid Spencer5f016e22007-07-11 17:01:13 +00003480 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3481 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00003482 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00003483
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003484 // Handle the case where we have '[*]' as the array size. However, a leading
3485 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3486 // the the token after the star is a ']'. Since stars in arrays are
3487 // infrequent, use of lookahead is not costly here.
3488 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003489 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003490
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003491 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003492 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003493 StaticLoc = SourceLocation(); // Drop the static.
3494 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003495 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003496 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003497 // Note, in C89, this production uses the constant-expr production instead
3498 // of assignment-expr. The only difference is that assignment-expr allows
3499 // things like '=' and '*='. Sema rejects these in C89 mode because they
3500 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003501
Douglas Gregore0762c92009-06-19 23:52:42 +00003502 // Parse the constant-expression or assignment-expression now (depending
3503 // on dialect).
3504 if (getLang().CPlusPlus)
3505 NumElements = ParseConstantExpression();
3506 else
3507 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003508 }
Mike Stump1eb44332009-09-09 15:08:12 +00003509
Reid Spencer5f016e22007-07-11 17:01:13 +00003510 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003511 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003512 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003513 // If the expression was invalid, skip it.
3514 SkipUntil(tok::r_square);
3515 return;
3516 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003517
3518 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3519
Sean Huntbbd37c62009-11-21 08:43:09 +00003520 //FIXME: Use these
3521 CXX0XAttributeList Attr;
3522 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3523 Attr = ParseCXX0XAttributes();
3524 }
3525
Chris Lattner378c7e42008-12-18 07:27:21 +00003526 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003527 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3528 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003529 NumElements.release(),
3530 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003531 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003532}
3533
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003534/// [GNU] typeof-specifier:
3535/// typeof ( expressions )
3536/// typeof ( type-name )
3537/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003538///
3539void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003540 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003541 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003542 SourceLocation StartLoc = ConsumeToken();
3543
John McCallcfb708c2010-01-13 20:03:27 +00003544 const bool hasParens = Tok.is(tok::l_paren);
3545
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003546 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00003547 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003548 SourceRange CastRange;
John McCall60d7b3a2010-08-24 06:29:42 +00003549 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall911093e2010-08-25 02:45:51 +00003550 isCastExpr,
3551 CastTy,
3552 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003553 if (hasParens)
3554 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003555
3556 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003557 // FIXME: Not accurate, the range gets one token more than it should.
3558 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003559 else
3560 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003561
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003562 if (isCastExpr) {
3563 if (!CastTy) {
3564 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003565 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003566 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003567
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003568 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003569 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003570 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3571 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003572 DiagID, CastTy))
3573 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003574 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003575 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003576
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003577 // If we get here, the operand to the typeof was an expresion.
3578 if (Operand.isInvalid()) {
3579 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003580 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003581 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003582
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003583 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003584 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003585 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3586 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00003587 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00003588 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003589}
Chris Lattner1b492422010-02-28 18:33:55 +00003590
3591
3592/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3593/// from TryAltiVecVectorToken.
3594bool Parser::TryAltiVecVectorTokenOutOfLine() {
3595 Token Next = NextToken();
3596 switch (Next.getKind()) {
3597 default: return false;
3598 case tok::kw_short:
3599 case tok::kw_long:
3600 case tok::kw_signed:
3601 case tok::kw_unsigned:
3602 case tok::kw_void:
3603 case tok::kw_char:
3604 case tok::kw_int:
3605 case tok::kw_float:
3606 case tok::kw_double:
3607 case tok::kw_bool:
3608 case tok::kw___pixel:
3609 Tok.setKind(tok::kw___vector);
3610 return true;
3611 case tok::identifier:
3612 if (Next.getIdentifierInfo() == Ident_pixel) {
3613 Tok.setKind(tok::kw___vector);
3614 return true;
3615 }
3616 return false;
3617 }
3618}
3619
3620bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3621 const char *&PrevSpec, unsigned &DiagID,
3622 bool &isInvalid) {
3623 if (Tok.getIdentifierInfo() == Ident_vector) {
3624 Token Next = NextToken();
3625 switch (Next.getKind()) {
3626 case tok::kw_short:
3627 case tok::kw_long:
3628 case tok::kw_signed:
3629 case tok::kw_unsigned:
3630 case tok::kw_void:
3631 case tok::kw_char:
3632 case tok::kw_int:
3633 case tok::kw_float:
3634 case tok::kw_double:
3635 case tok::kw_bool:
3636 case tok::kw___pixel:
3637 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3638 return true;
3639 case tok::identifier:
3640 if (Next.getIdentifierInfo() == Ident_pixel) {
3641 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3642 return true;
3643 }
3644 break;
3645 default:
3646 break;
3647 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003648 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003649 DS.isTypeAltiVecVector()) {
3650 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3651 return true;
3652 }
3653 return false;
3654}
3655