blob: 53cccc0819731b5029edea36a558a1fa9ef12efd [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);
Nico Weber253e80b2010-11-22 10:30:56 +00001023 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1024 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00001025 PrevSpec, DiagID, T);
1026 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001027 else
1028 DS.SetTypeSpecError();
1029 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1030 ConsumeToken(); // The typename
1031 }
1032
Douglas Gregor9135c722009-03-25 15:40:00 +00001033 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001034 goto DoneWithDeclSpec;
1035
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001036 // If we're in a context where the identifier could be a class name,
1037 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001038 if ((DSContext == DSC_top_level ||
1039 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001040 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001041 &SS)) {
1042 if (isConstructorDeclarator())
1043 goto DoneWithDeclSpec;
1044
1045 // As noted in C++ [class.qual]p2 (cited above), when the name
1046 // of the class is qualified in a context where it could name
1047 // a constructor, its a constructor name. However, we've
1048 // looked at the declarator, and the user probably meant this
1049 // to be a type. Complain that it isn't supposed to be treated
1050 // as a type, then proceed to parse it as a type.
1051 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1052 << Next.getIdentifierInfo();
1053 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001054
John McCallb3d87482010-08-24 05:47:05 +00001055 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1056 Next.getLocation(),
1057 getCurScope(), &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001058
Chris Lattnerf4382f52009-04-14 22:17:06 +00001059 // If the referenced identifier is not a type, then this declspec is
1060 // erroneous: We already checked about that it has no type specifier, and
1061 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001062 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001063 if (TypeRep == 0) {
1064 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001065 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001066 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001067 }
Mike Stump1eb44332009-09-09 15:08:12 +00001068
John McCallaa87d332009-12-12 11:40:51 +00001069 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001070 ConsumeToken(); // The C++ scope.
1071
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001072 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001073 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001074 if (isInvalid)
1075 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001077 DS.SetRangeEnd(Tok.getLocation());
1078 ConsumeToken(); // The typename.
1079
1080 continue;
1081 }
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Chris Lattner80d0c892009-01-21 19:48:37 +00001083 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001084 if (Tok.getAnnotationValue()) {
1085 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00001086 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001087 DiagID, T);
1088 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001089 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001090
1091 if (isInvalid)
1092 break;
1093
Chris Lattner80d0c892009-01-21 19:48:37 +00001094 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1095 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Chris Lattner80d0c892009-01-21 19:48:37 +00001097 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1098 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001099 // Objective-C interface.
1100 if (Tok.is(tok::less) && getLang().ObjC1)
1101 ParseObjCProtocolQualifiers(DS);
1102
Chris Lattner80d0c892009-01-21 19:48:37 +00001103 continue;
1104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Chris Lattner3bd934a2008-07-26 01:18:38 +00001106 // typedef-name
1107 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001108 // In C++, check to see if this is a scope specifier like foo::bar::, if
1109 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001110 if (getLang().CPlusPlus) {
1111 if (TryAnnotateCXXScopeToken(true)) {
1112 if (!DS.hasTypeSpecifier())
1113 DS.SetTypeSpecError();
1114 goto DoneWithDeclSpec;
1115 }
1116 if (!Tok.is(tok::identifier))
1117 continue;
1118 }
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Chris Lattner3bd934a2008-07-26 01:18:38 +00001120 // This identifier can only be a typedef name if we haven't already seen
1121 // a type-specifier. Without this check we misparse:
1122 // typedef int X; struct Y { short X; }; as 'short int'.
1123 if (DS.hasTypeSpecifier())
1124 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001125
John Thompson82287d12010-02-05 00:12:22 +00001126 // Check for need to substitute AltiVec keyword tokens.
1127 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1128 break;
1129
Chris Lattner3bd934a2008-07-26 01:18:38 +00001130 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001131 ParsedType TypeRep =
1132 Actions.getTypeName(*Tok.getIdentifierInfo(),
1133 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001134
Chris Lattnerc199ab32009-04-12 20:42:31 +00001135 // If this is not a typedef name, don't parse it as part of the declspec,
1136 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001137 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001138 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001139 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001140 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001141
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001142 // If we're in a context where the identifier could be a class name,
1143 // check whether this is a constructor declaration.
1144 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001145 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001146 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001147 goto DoneWithDeclSpec;
1148
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001149 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001150 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001151 if (isInvalid)
1152 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Chris Lattner3bd934a2008-07-26 01:18:38 +00001154 DS.SetRangeEnd(Tok.getLocation());
1155 ConsumeToken(); // The identifier
1156
1157 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1158 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001159 // Objective-C interface.
1160 if (Tok.is(tok::less) && getLang().ObjC1)
1161 ParseObjCProtocolQualifiers(DS);
1162
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001163 // Need to support trailing type qualifiers (e.g. "id<p> const").
1164 // If a type specifier follows, it will be diagnosed elsewhere.
1165 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001166 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001167
1168 // type-name
1169 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001170 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001171 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001172 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001173 // This template-id does not refer to a type name, so we're
1174 // done with the type-specifiers.
1175 goto DoneWithDeclSpec;
1176 }
1177
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001178 // If we're in a context where the template-id could be a
1179 // constructor name or specialization, check whether this is a
1180 // constructor declaration.
1181 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001182 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001183 isConstructorDeclarator())
1184 goto DoneWithDeclSpec;
1185
Douglas Gregor39a8de12009-02-25 19:37:18 +00001186 // Turn the template-id annotation token into a type annotation
1187 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001188 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001189 continue;
1190 }
1191
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 // GNU attributes support.
1193 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001194 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001196
1197 // Microsoft declspec support.
1198 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001199 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001200 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Steve Naroff239f0732008-12-25 14:16:32 +00001202 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001203 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001204 // FIXME: Add handling here!
1205 break;
1206
1207 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001208 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001209 case tok::kw___cdecl:
1210 case tok::kw___stdcall:
1211 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001212 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001213 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1214 continue;
1215
Dawn Perchik52fc3142010-09-03 01:29:35 +00001216 // Borland single token adornments.
1217 case tok::kw___pascal:
1218 DS.AddAttributes(ParseBorlandTypeAttributes());
1219 continue;
1220
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 // storage-class-specifier
1222 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001223 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1224 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 break;
1226 case tok::kw_extern:
1227 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001228 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001229 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1230 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001231 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001232 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001233 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001234 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001235 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 case tok::kw_static:
1237 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001238 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001239 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1240 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 break;
1242 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001243 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001244 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1245 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001246 else
John McCallfec54012009-08-03 20:12:06 +00001247 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1248 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 break;
1250 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001251 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1252 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001254 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001255 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1256 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001257 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001259 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 // function-specifier
1263 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001264 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001266 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001267 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001268 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001269 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001270 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001271 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001272
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001273 // friend
1274 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001275 if (DSContext == DSC_class)
1276 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1277 else {
1278 PrevSpec = ""; // not actually used by the diagnostic
1279 DiagID = diag::err_friend_invalid_in_context;
1280 isInvalid = true;
1281 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001282 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Sebastian Redl2ac67232009-11-05 15:47:02 +00001284 // constexpr
1285 case tok::kw_constexpr:
1286 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1287 break;
1288
Chris Lattner80d0c892009-01-21 19:48:37 +00001289 // type-specifier
1290 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001291 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1292 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001293 break;
1294 case tok::kw_long:
1295 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001296 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1297 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001298 else
John McCallfec54012009-08-03 20:12:06 +00001299 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1300 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001301 break;
1302 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001303 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1304 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001305 break;
1306 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001307 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1308 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001309 break;
1310 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001311 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1312 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001313 break;
1314 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001315 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1316 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001317 break;
1318 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001319 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1320 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001321 break;
1322 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001323 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1324 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001325 break;
1326 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001327 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1328 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001329 break;
1330 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001331 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1332 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001333 break;
1334 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001335 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1336 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001337 break;
1338 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001339 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1340 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001341 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001342 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001343 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1344 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001345 break;
1346 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001347 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1348 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001349 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001350 case tok::kw_bool:
1351 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00001352 if (Tok.is(tok::kw_bool) &&
1353 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
1354 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1355 PrevSpec = ""; // Not used by the diagnostic.
1356 DiagID = diag::err_bool_redeclaration;
1357 isInvalid = true;
1358 } else {
1359 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1360 DiagID);
1361 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001362 break;
1363 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001364 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1365 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001366 break;
1367 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001368 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1369 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001370 break;
1371 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001372 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1373 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001374 break;
John Thompson82287d12010-02-05 00:12:22 +00001375 case tok::kw___vector:
1376 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1377 break;
1378 case tok::kw___pixel:
1379 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1380 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001381
1382 // class-specifier:
1383 case tok::kw_class:
1384 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001385 case tok::kw_union: {
1386 tok::TokenKind Kind = Tok.getKind();
1387 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001388 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001389 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001390 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001391
1392 // enum-specifier:
1393 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001394 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001395 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001396 continue;
1397
1398 // cv-qualifier:
1399 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001400 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1401 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001402 break;
1403 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001404 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1405 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001406 break;
1407 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001408 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1409 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001410 break;
1411
Douglas Gregord57959a2009-03-27 23:10:48 +00001412 // C++ typename-specifier:
1413 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001414 if (TryAnnotateTypeOrScopeToken()) {
1415 DS.SetTypeSpecError();
1416 goto DoneWithDeclSpec;
1417 }
1418 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001419 continue;
1420 break;
1421
Chris Lattner80d0c892009-01-21 19:48:37 +00001422 // GNU typeof support.
1423 case tok::kw_typeof:
1424 ParseTypeofSpecifier(DS);
1425 continue;
1426
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001427 case tok::kw_decltype:
1428 ParseDecltypeSpecifier(DS);
1429 continue;
1430
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001431 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001432 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001433 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1434 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001435 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001436 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Douglas Gregor46f936e2010-11-19 17:10:50 +00001438 if (!ParseObjCProtocolQualifiers(DS))
1439 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
1440 << FixItHint::CreateInsertion(Loc, "id")
1441 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001442
1443 // Need to support trailing type qualifiers (e.g. "id<p> const").
1444 // If a type specifier follows, it will be diagnosed elsewhere.
1445 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00001446 }
John McCallfec54012009-08-03 20:12:06 +00001447 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001448 if (isInvalid) {
1449 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001450 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001451
1452 if (DiagID == diag::ext_duplicate_declspec)
1453 Diag(Tok, DiagID)
1454 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1455 else
1456 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001457 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001458 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001459 ConsumeToken();
1460 }
1461}
Douglas Gregoradcac882008-12-01 23:54:00 +00001462
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001463/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001464/// primarily follow the C++ grammar with additions for C99 and GNU,
1465/// which together subsume the C grammar. Note that the C++
1466/// type-specifier also includes the C type-qualifier (for const,
1467/// volatile, and C99 restrict). Returns true if a type-specifier was
1468/// found (and parsed), false otherwise.
1469///
1470/// type-specifier: [C++ 7.1.5]
1471/// simple-type-specifier
1472/// class-specifier
1473/// enum-specifier
1474/// elaborated-type-specifier [TODO]
1475/// cv-qualifier
1476///
1477/// cv-qualifier: [C++ 7.1.5.1]
1478/// 'const'
1479/// 'volatile'
1480/// [C99] 'restrict'
1481///
1482/// simple-type-specifier: [ C++ 7.1.5.2]
1483/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1484/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1485/// 'char'
1486/// 'wchar_t'
1487/// 'bool'
1488/// 'short'
1489/// 'int'
1490/// 'long'
1491/// 'signed'
1492/// 'unsigned'
1493/// 'float'
1494/// 'double'
1495/// 'void'
1496/// [C99] '_Bool'
1497/// [C99] '_Complex'
1498/// [C99] '_Imaginary' // Removed in TC2?
1499/// [GNU] '_Decimal32'
1500/// [GNU] '_Decimal64'
1501/// [GNU] '_Decimal128'
1502/// [GNU] typeof-specifier
1503/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1504/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001505/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001506/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001507bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001508 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001509 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001510 const ParsedTemplateInfo &TemplateInfo,
1511 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001512 SourceLocation Loc = Tok.getLocation();
1513
1514 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001515 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001516 // If we already have a type specifier, this identifier is not a type.
1517 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1518 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1519 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1520 return false;
John Thompson82287d12010-02-05 00:12:22 +00001521 // Check for need to substitute AltiVec keyword tokens.
1522 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1523 break;
1524 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001525 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001526 // Annotate typenames and C++ scope specifiers. If we get one, just
1527 // recurse to handle whatever we get.
1528 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001529 return true;
1530 if (Tok.is(tok::identifier))
1531 return false;
1532 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1533 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001534 case tok::coloncolon: // ::foo::bar
1535 if (NextToken().is(tok::kw_new) || // ::new
1536 NextToken().is(tok::kw_delete)) // ::delete
1537 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Chris Lattner166a8fc2009-01-04 23:41:41 +00001539 // Annotate typenames and C++ scope specifiers. If we get one, just
1540 // recurse to handle whatever we get.
1541 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001542 return true;
1543 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1544 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Douglas Gregor12e083c2008-11-07 15:42:26 +00001546 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001547 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001548 if (ParsedType T = getTypeAnnotation(Tok)) {
Nico Weber253e80b2010-11-22 10:30:56 +00001549 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
1550 Tok.getAnnotationEndLoc(), PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001551 DiagID, T);
1552 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001553 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001554 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1555 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Douglas Gregor12e083c2008-11-07 15:42:26 +00001557 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1558 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1559 // Objective-C interface. If we don't have Objective-C or a '<', this is
1560 // just a normal reference to a typedef name.
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00001561 if (Tok.is(tok::less) && getLang().ObjC1)
1562 ParseObjCProtocolQualifiers(DS);
1563
Douglas Gregor12e083c2008-11-07 15:42:26 +00001564 return true;
1565 }
1566
1567 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001568 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001569 break;
1570 case tok::kw_long:
1571 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001572 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1573 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001574 else
John McCallfec54012009-08-03 20:12:06 +00001575 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1576 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001577 break;
1578 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001579 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001580 break;
1581 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001582 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1583 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001584 break;
1585 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001586 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1587 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001588 break;
1589 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001590 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1591 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001592 break;
1593 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001594 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001595 break;
1596 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001597 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001598 break;
1599 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001600 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001601 break;
1602 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001603 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001604 break;
1605 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001606 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001607 break;
1608 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001609 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001610 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001611 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001612 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001613 break;
1614 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001615 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001616 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001617 case tok::kw_bool:
1618 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001619 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001620 break;
1621 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001622 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1623 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001624 break;
1625 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001626 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1627 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001628 break;
1629 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001630 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1631 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001632 break;
John Thompson82287d12010-02-05 00:12:22 +00001633 case tok::kw___vector:
1634 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1635 break;
1636 case tok::kw___pixel:
1637 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1638 break;
1639
Douglas Gregor12e083c2008-11-07 15:42:26 +00001640 // class-specifier:
1641 case tok::kw_class:
1642 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001643 case tok::kw_union: {
1644 tok::TokenKind Kind = Tok.getKind();
1645 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001646 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1647 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001648 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001649 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001650
1651 // enum-specifier:
1652 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001653 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001654 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001655 return true;
1656
1657 // cv-qualifier:
1658 case tok::kw_const:
1659 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001660 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001661 break;
1662 case tok::kw_volatile:
1663 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001664 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001665 break;
1666 case tok::kw_restrict:
1667 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001668 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001669 break;
1670
1671 // GNU typeof support.
1672 case tok::kw_typeof:
1673 ParseTypeofSpecifier(DS);
1674 return true;
1675
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001676 // C++0x decltype support.
1677 case tok::kw_decltype:
1678 ParseDecltypeSpecifier(DS);
1679 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001681 // C++0x auto support.
1682 case tok::kw_auto:
1683 if (!getLang().CPlusPlus0x)
1684 return false;
1685
John McCallfec54012009-08-03 20:12:06 +00001686 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001687 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001688
Eli Friedman290eeb02009-06-08 23:27:34 +00001689 case tok::kw___ptr64:
1690 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001691 case tok::kw___cdecl:
1692 case tok::kw___stdcall:
1693 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001694 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001695 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001696 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001697
Dawn Perchik52fc3142010-09-03 01:29:35 +00001698 case tok::kw___pascal:
1699 DS.AddAttributes(ParseBorlandTypeAttributes());
1700 return true;
1701
Douglas Gregor12e083c2008-11-07 15:42:26 +00001702 default:
1703 // Not a type-specifier; do nothing.
1704 return false;
1705 }
1706
1707 // If the specifier combination wasn't legal, issue a diagnostic.
1708 if (isInvalid) {
1709 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001710 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001711 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001712 }
1713 DS.SetRangeEnd(Tok.getLocation());
1714 ConsumeToken(); // whatever we parsed above.
1715 return true;
1716}
Reid Spencer5f016e22007-07-11 17:01:13 +00001717
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001718/// ParseStructDeclaration - Parse a struct declaration without the terminating
1719/// semicolon.
1720///
Reid Spencer5f016e22007-07-11 17:01:13 +00001721/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001722/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001723/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001724/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001725/// struct-declarator-list:
1726/// struct-declarator
1727/// struct-declarator-list ',' struct-declarator
1728/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1729/// struct-declarator:
1730/// declarator
1731/// [GNU] declarator attributes[opt]
1732/// declarator[opt] ':' constant-expression
1733/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1734///
Chris Lattnere1359422008-04-10 06:46:29 +00001735void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001736ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001737 if (Tok.is(tok::kw___extension__)) {
1738 // __extension__ silences extension warnings in the subexpression.
1739 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001740 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001741 return ParseStructDeclaration(DS, Fields);
1742 }
Mike Stump1eb44332009-09-09 15:08:12 +00001743
Steve Naroff28a7ca82007-08-20 22:28:22 +00001744 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001745 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001746 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001748 // If there are no declarators, this is a free-standing declaration
1749 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001750 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001751 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001752 return;
1753 }
1754
1755 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001756 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001757 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001758 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001759 FieldDeclarator DeclaratorInfo(DS);
1760
1761 // Attributes are only allowed here on successive declarators.
1762 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1763 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001764 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001765 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1766 }
Mike Stump1eb44332009-09-09 15:08:12 +00001767
Steve Naroff28a7ca82007-08-20 22:28:22 +00001768 /// struct-declarator: declarator
1769 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001770 if (Tok.isNot(tok::colon)) {
1771 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1772 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001773 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001774 }
Mike Stump1eb44332009-09-09 15:08:12 +00001775
Chris Lattner04d66662007-10-09 17:33:22 +00001776 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001777 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001778 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001779 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001780 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001781 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001782 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001783 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001784
Steve Naroff28a7ca82007-08-20 22:28:22 +00001785 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001786 if (Tok.is(tok::kw___attribute)) {
1787 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001788 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001789 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1790 }
1791
John McCallbdd563e2009-11-03 02:38:08 +00001792 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00001793 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00001794 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001795
Steve Naroff28a7ca82007-08-20 22:28:22 +00001796 // If we don't have a comma, it is either the end of the list (a ';')
1797 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001798 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001799 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001800
Steve Naroff28a7ca82007-08-20 22:28:22 +00001801 // Consume the comma.
1802 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001803
John McCallbdd563e2009-11-03 02:38:08 +00001804 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001805 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001806}
1807
1808/// ParseStructUnionBody
1809/// struct-contents:
1810/// struct-declaration-list
1811/// [EXT] empty
1812/// [GNU] "struct-declaration-list" without terminatoring ';'
1813/// struct-declaration-list:
1814/// struct-declaration
1815/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001816/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001817///
Reid Spencer5f016e22007-07-11 17:01:13 +00001818void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001819 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00001820 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1821 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Reid Spencer5f016e22007-07-11 17:01:13 +00001823 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001824
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001825 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001826 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00001827
Reid Spencer5f016e22007-07-11 17:01:13 +00001828 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1829 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001830 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00001831 Diag(Tok, diag::ext_empty_struct_union)
1832 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00001833
John McCalld226f652010-08-21 09:40:31 +00001834 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001835
Reid Spencer5f016e22007-07-11 17:01:13 +00001836 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001837 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001838 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Reid Spencer5f016e22007-07-11 17:01:13 +00001840 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001841 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001842 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00001843 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00001844 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001845 ConsumeToken();
1846 continue;
1847 }
Chris Lattnere1359422008-04-10 06:46:29 +00001848
1849 // Parse all the comma separated declarators.
1850 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001851
John McCallbdd563e2009-11-03 02:38:08 +00001852 if (!Tok.is(tok::at)) {
1853 struct CFieldCallback : FieldCallback {
1854 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001855 Decl *TagDecl;
1856 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001857
John McCalld226f652010-08-21 09:40:31 +00001858 CFieldCallback(Parser &P, Decl *TagDecl,
1859 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001860 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1861
John McCalld226f652010-08-21 09:40:31 +00001862 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001863 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00001864 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00001865 FD.D.getDeclSpec().getSourceRange().getBegin(),
1866 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001867 FieldDecls.push_back(Field);
1868 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001869 }
John McCallbdd563e2009-11-03 02:38:08 +00001870 } Callback(*this, TagDecl, FieldDecls);
1871
1872 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001873 } else { // Handle @defs
1874 ConsumeToken();
1875 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1876 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001877 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001878 continue;
1879 }
1880 ConsumeToken();
1881 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1882 if (!Tok.is(tok::identifier)) {
1883 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001884 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001885 continue;
1886 }
John McCalld226f652010-08-21 09:40:31 +00001887 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001888 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001889 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001890 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1891 ConsumeToken();
1892 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001893 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001894
Chris Lattner04d66662007-10-09 17:33:22 +00001895 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001896 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001897 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001898 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001899 break;
1900 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001901 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1902 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001903 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001904 // If we stopped at a ';', eat it.
1905 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001906 }
1907 }
Mike Stump1eb44332009-09-09 15:08:12 +00001908
Steve Naroff60fccee2007-10-29 21:38:07 +00001909 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Ted Kremenek8113ecf2010-11-10 05:59:39 +00001911 AttributeList *AttrList = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001912 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001913 if (Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +00001914 AttrList = ParseGNUAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001915
Douglas Gregor23c94db2010-07-02 17:43:08 +00001916 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001917 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001918 LBraceLoc, RBraceLoc,
Ted Kremenek8113ecf2010-11-10 05:59:39 +00001919 AttrList);
Douglas Gregor72de6672009-01-08 20:45:30 +00001920 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001921 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001922}
1923
Reid Spencer5f016e22007-07-11 17:01:13 +00001924/// ParseEnumSpecifier
1925/// enum-specifier: [C99 6.7.2.2]
1926/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001927///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001928/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1929/// '}' attributes[opt]
1930/// 'enum' identifier
1931/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001932///
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001933/// [C++0x] enum-head '{' enumerator-list[opt] '}'
1934/// [C++0x] enum-head '{' enumerator-list ',' '}'
1935///
1936/// enum-head: [C++0x]
1937/// enum-key attributes[opt] identifier[opt] enum-base[opt]
1938/// enum-key attributes[opt] nested-name-specifier identifier enum-base[opt]
1939///
1940/// enum-key: [C++0x]
1941/// 'enum'
1942/// 'enum' 'class'
1943/// 'enum' 'struct'
1944///
1945/// enum-base: [C++0x]
1946/// ':' type-specifier-seq
1947///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001948/// [C++] elaborated-type-specifier:
1949/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1950///
Chris Lattner4c97d762009-04-12 21:49:30 +00001951void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001952 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001953 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001954 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001955 if (Tok.is(tok::code_completion)) {
1956 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00001957 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Douglas Gregordc845342010-05-25 05:58:43 +00001958 ConsumeCodeCompletionToken();
Douglas Gregor374929f2009-09-18 15:37:17 +00001959 }
1960
Ted Kremenek8113ecf2010-11-10 05:59:39 +00001961 AttributeList *Attr = 0;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001962 // If attributes exist after tag, parse them.
1963 if (Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +00001964 Attr = ParseGNUAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001965
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00001966 CXXScopeSpec &SS = DS.getTypeSpecScope();
John McCall9ba61662010-02-26 08:45:28 +00001967 if (getLang().CPlusPlus) {
John McCallb3d87482010-08-24 05:47:05 +00001968 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), false))
John McCall9ba61662010-02-26 08:45:28 +00001969 return;
1970
1971 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001972 Diag(Tok, diag::err_expected_ident);
1973 if (Tok.isNot(tok::l_brace)) {
1974 // Has no name and is not a definition.
1975 // Skip the rest of this declarator, up until the comma or semicolon.
1976 SkipUntil(tok::comma, true);
1977 return;
1978 }
1979 }
1980 }
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001982 bool IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001983 bool IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001984
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001985 if (getLang().CPlusPlus0x &&
1986 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001987 IsScopedEnum = true;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001988 IsScopedUsingClassTag = Tok.is(tok::kw_class);
1989 ConsumeToken();
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001990 }
1991
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001992 // Must have either 'enum name' or 'enum {...}'.
1993 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1994 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001995
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001996 // Skip the rest of this declarator, up until the comma or semicolon.
1997 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001998 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001999 }
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002001 // If an identifier is present, consume and remember it.
2002 IdentifierInfo *Name = 0;
2003 SourceLocation NameLoc;
2004 if (Tok.is(tok::identifier)) {
2005 Name = Tok.getIdentifierInfo();
2006 NameLoc = ConsumeToken();
2007 }
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002009 if (!Name && IsScopedEnum) {
2010 // C++0x 7.2p2: The optional identifier shall not be omitted in the
2011 // declaration of a scoped enumeration.
2012 Diag(Tok, diag::err_scoped_enum_missing_identifier);
2013 IsScopedEnum = false;
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002014 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002015 }
2016
2017 TypeResult BaseType;
2018
Douglas Gregora61b3e72010-12-01 17:42:47 +00002019 // Parse the fixed underlying type.
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002020 if (getLang().CPlusPlus0x && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00002021 bool PossibleBitfield = false;
2022 if (getCurScope()->getFlags() & Scope::ClassScope) {
2023 // If we're in class scope, this can either be an enum declaration with
2024 // an underlying type, or a declaration of a bitfield member. We try to
2025 // use a simple disambiguation scheme first to catch the common cases
2026 // (integer literal, sizeof); if it's still ambiguous, we then consider
2027 // anything that's a simple-type-specifier followed by '(' as an
2028 // expression. This suffices because function types are not valid
2029 // underlying types anyway.
2030 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
2031 // If the next token starts an expression, we know we're parsing a
2032 // bit-field. This is the common case.
2033 if (TPR == TPResult::True())
2034 PossibleBitfield = true;
2035 // If the next token starts a type-specifier-seq, it may be either a
2036 // a fixed underlying type or the start of a function-style cast in C++;
2037 // lookahead one more token to see if it's obvious that we have a
2038 // fixed underlying type.
2039 else if (TPR == TPResult::False() &&
2040 GetLookAheadToken(2).getKind() == tok::semi) {
2041 // Consume the ':'.
2042 ConsumeToken();
2043 } else {
2044 // We have the start of a type-specifier-seq, so we have to perform
2045 // tentative parsing to determine whether we have an expression or a
2046 // type.
2047 TentativeParsingAction TPA(*this);
2048
2049 // Consume the ':'.
2050 ConsumeToken();
2051
2052 if (isCXXDeclarationSpecifier() != TPResult::True()) {
2053 // We'll parse this as a bitfield later.
2054 PossibleBitfield = true;
2055 TPA.Revert();
2056 } else {
2057 // We have a type-specifier-seq.
2058 TPA.Commit();
2059 }
2060 }
2061 } else {
2062 // Consume the ':'.
2063 ConsumeToken();
2064 }
2065
2066 if (!PossibleBitfield) {
2067 SourceRange Range;
2068 BaseType = ParseTypeName(&Range);
2069 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002070 }
2071
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002072 // There are three options here. If we have 'enum foo;', then this is a
2073 // forward declaration. If we have 'enum foo {...' then this is a
2074 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2075 //
2076 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2077 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2078 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2079 //
John McCallf312b1e2010-08-26 23:41:50 +00002080 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002081 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002082 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002083 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002084 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002085 else
John McCallf312b1e2010-08-26 23:41:50 +00002086 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002087
2088 // enums cannot be templates, although they can be referenced from a
2089 // template.
2090 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002091 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002092 Diag(Tok, diag::err_enum_template);
2093
2094 // Skip the rest of this declarator, up until the comma or semicolon.
2095 SkipUntil(tok::comma, true);
2096 return;
2097 }
2098
Douglas Gregor402abb52009-05-28 23:31:59 +00002099 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002100 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002101 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2102 const char *PrevSpec = 0;
2103 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002104 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002105 StartLoc, SS, Name, NameLoc, Attr,
John McCalld226f652010-08-21 09:40:31 +00002106 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002107 MultiTemplateParamsArg(Actions),
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002108 Owned, IsDependent, IsScopedEnum,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002109 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002110
Douglas Gregor48c89f42010-04-24 16:38:41 +00002111 if (IsDependent) {
2112 // This enum has a dependent nested-name-specifier. Handle it as a
2113 // dependent tag.
2114 if (!Name) {
2115 DS.SetTypeSpecError();
2116 Diag(Tok, diag::err_expected_type_name_after_typename);
2117 return;
2118 }
2119
Douglas Gregor23c94db2010-07-02 17:43:08 +00002120 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002121 TUK, SS, Name, StartLoc,
2122 NameLoc);
2123 if (Type.isInvalid()) {
2124 DS.SetTypeSpecError();
2125 return;
2126 }
2127
2128 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallb3d87482010-08-24 05:47:05 +00002129 Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002130 Diag(StartLoc, DiagID) << PrevSpec;
2131
2132 return;
2133 }
Mike Stump1eb44332009-09-09 15:08:12 +00002134
John McCalld226f652010-08-21 09:40:31 +00002135 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002136 // The action failed to produce an enumeration tag. If this is a
2137 // definition, consume the entire definition.
2138 if (Tok.is(tok::l_brace)) {
2139 ConsumeBrace();
2140 SkipUntil(tok::r_brace);
2141 }
2142
2143 DS.SetTypeSpecError();
2144 return;
2145 }
2146
Chris Lattner04d66662007-10-09 17:33:22 +00002147 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002148 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002149
John McCallb3d87482010-08-24 05:47:05 +00002150 // FIXME: The DeclSpec should keep the locations of both the keyword
2151 // and the name (if there is one).
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002152 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCalld226f652010-08-21 09:40:31 +00002153 TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002154 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002155}
2156
2157/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2158/// enumerator-list:
2159/// enumerator
2160/// enumerator-list ',' enumerator
2161/// enumerator:
2162/// enumeration-constant
2163/// enumeration-constant '=' constant-expression
2164/// enumeration-constant:
2165/// identifier
2166///
John McCalld226f652010-08-21 09:40:31 +00002167void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002168 // Enter the scope of the enum body and start the definition.
2169 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002170 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002171
Reid Spencer5f016e22007-07-11 17:01:13 +00002172 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002173
Chris Lattner7946dd32007-08-27 17:24:30 +00002174 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002175 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002176 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002177
John McCalld226f652010-08-21 09:40:31 +00002178 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002179
John McCalld226f652010-08-21 09:40:31 +00002180 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002181
Reid Spencer5f016e22007-07-11 17:01:13 +00002182 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002183 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002184 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2185 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002186
John McCall5b629aa2010-10-22 23:36:17 +00002187 // If attributes exist after the enumerator, parse them.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002188 AttributeList *Attr = 0;
John McCall5b629aa2010-10-22 23:36:17 +00002189 if (Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002190 Attr = ParseGNUAttributes();
John McCall5b629aa2010-10-22 23:36:17 +00002191
Reid Spencer5f016e22007-07-11 17:01:13 +00002192 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002193 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002194 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002195 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002196 AssignedVal = ParseConstantExpression();
2197 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002198 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002199 }
Mike Stump1eb44332009-09-09 15:08:12 +00002200
Reid Spencer5f016e22007-07-11 17:01:13 +00002201 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002202 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2203 LastEnumConstDecl,
2204 IdentLoc, Ident,
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002205 Attr, EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00002206 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002207 EnumConstantDecls.push_back(EnumConstDecl);
2208 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002209
Douglas Gregor751f6922010-09-07 14:51:08 +00002210 if (Tok.is(tok::identifier)) {
2211 // We're missing a comma between enumerators.
2212 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
2213 Diag(Loc, diag::err_enumerator_list_missing_comma)
2214 << FixItHint::CreateInsertion(Loc, ", ");
2215 continue;
2216 }
2217
Chris Lattner04d66662007-10-09 17:33:22 +00002218 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002219 break;
2220 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002221
2222 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002223 !(getLang().C99 || getLang().CPlusPlus0x))
2224 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2225 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002226 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002227 }
Mike Stump1eb44332009-09-09 15:08:12 +00002228
Reid Spencer5f016e22007-07-11 17:01:13 +00002229 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002230 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002231
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002232 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002233 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00002234 if (Tok.is(tok::kw___attribute))
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002235 Attr = ParseGNUAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00002236
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002237 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2238 EnumConstantDecls.data(), EnumConstantDecls.size(),
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002239 getCurScope(), Attr);
Mike Stump1eb44332009-09-09 15:08:12 +00002240
Douglas Gregor72de6672009-01-08 20:45:30 +00002241 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002242 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002243}
2244
2245/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002246/// start of a type-qualifier-list.
2247bool Parser::isTypeQualifier() const {
2248 switch (Tok.getKind()) {
2249 default: return false;
2250 // type-qualifier
2251 case tok::kw_const:
2252 case tok::kw_volatile:
2253 case tok::kw_restrict:
2254 return true;
2255 }
2256}
2257
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002258/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2259/// is definitely a type-specifier. Return false if it isn't part of a type
2260/// specifier or if we're not sure.
2261bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2262 switch (Tok.getKind()) {
2263 default: return false;
2264 // type-specifiers
2265 case tok::kw_short:
2266 case tok::kw_long:
2267 case tok::kw_signed:
2268 case tok::kw_unsigned:
2269 case tok::kw__Complex:
2270 case tok::kw__Imaginary:
2271 case tok::kw_void:
2272 case tok::kw_char:
2273 case tok::kw_wchar_t:
2274 case tok::kw_char16_t:
2275 case tok::kw_char32_t:
2276 case tok::kw_int:
2277 case tok::kw_float:
2278 case tok::kw_double:
2279 case tok::kw_bool:
2280 case tok::kw__Bool:
2281 case tok::kw__Decimal32:
2282 case tok::kw__Decimal64:
2283 case tok::kw__Decimal128:
2284 case tok::kw___vector:
2285
2286 // struct-or-union-specifier (C99) or class-specifier (C++)
2287 case tok::kw_class:
2288 case tok::kw_struct:
2289 case tok::kw_union:
2290 // enum-specifier
2291 case tok::kw_enum:
2292
2293 // typedef-name
2294 case tok::annot_typename:
2295 return true;
2296 }
2297}
2298
Steve Naroff5f8aa692008-02-11 23:15:56 +00002299/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002300/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002301bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002302 switch (Tok.getKind()) {
2303 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002304
Chris Lattner166a8fc2009-01-04 23:41:41 +00002305 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002306 if (TryAltiVecVectorToken())
2307 return true;
2308 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002309 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002310 // Annotate typenames and C++ scope specifiers. If we get one, just
2311 // recurse to handle whatever we get.
2312 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002313 return true;
2314 if (Tok.is(tok::identifier))
2315 return false;
2316 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002317
Chris Lattner166a8fc2009-01-04 23:41:41 +00002318 case tok::coloncolon: // ::foo::bar
2319 if (NextToken().is(tok::kw_new) || // ::new
2320 NextToken().is(tok::kw_delete)) // ::delete
2321 return false;
2322
Chris Lattner166a8fc2009-01-04 23:41:41 +00002323 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002324 return true;
2325 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002326
Reid Spencer5f016e22007-07-11 17:01:13 +00002327 // GNU attributes support.
2328 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002329 // GNU typeof support.
2330 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Reid Spencer5f016e22007-07-11 17:01:13 +00002332 // type-specifiers
2333 case tok::kw_short:
2334 case tok::kw_long:
2335 case tok::kw_signed:
2336 case tok::kw_unsigned:
2337 case tok::kw__Complex:
2338 case tok::kw__Imaginary:
2339 case tok::kw_void:
2340 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002341 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002342 case tok::kw_char16_t:
2343 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002344 case tok::kw_int:
2345 case tok::kw_float:
2346 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002347 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002348 case tok::kw__Bool:
2349 case tok::kw__Decimal32:
2350 case tok::kw__Decimal64:
2351 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002352 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Chris Lattner99dc9142008-04-13 18:59:07 +00002354 // struct-or-union-specifier (C99) or class-specifier (C++)
2355 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002356 case tok::kw_struct:
2357 case tok::kw_union:
2358 // enum-specifier
2359 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002360
Reid Spencer5f016e22007-07-11 17:01:13 +00002361 // type-qualifier
2362 case tok::kw_const:
2363 case tok::kw_volatile:
2364 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002365
2366 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002367 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002368 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002369
Chris Lattner7c186be2008-10-20 00:25:30 +00002370 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2371 case tok::less:
2372 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002373
Steve Naroff239f0732008-12-25 14:16:32 +00002374 case tok::kw___cdecl:
2375 case tok::kw___stdcall:
2376 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002377 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002378 case tok::kw___w64:
2379 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002380 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002381 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002382 }
2383}
2384
2385/// isDeclarationSpecifier() - Return true if the current token is part of a
2386/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00002387///
2388/// \param DisambiguatingWithExpression True to indicate that the purpose of
2389/// this check is to disambiguate between an expression and a declaration.
2390bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002391 switch (Tok.getKind()) {
2392 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002393
Chris Lattner166a8fc2009-01-04 23:41:41 +00002394 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002395 // Unfortunate hack to support "Class.factoryMethod" notation.
2396 if (getLang().ObjC1 && NextToken().is(tok::period))
2397 return false;
John Thompson82287d12010-02-05 00:12:22 +00002398 if (TryAltiVecVectorToken())
2399 return true;
2400 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002401 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002402 // Annotate typenames and C++ scope specifiers. If we get one, just
2403 // recurse to handle whatever we get.
2404 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002405 return true;
2406 if (Tok.is(tok::identifier))
2407 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00002408
2409 // If we're in Objective-C and we have an Objective-C class type followed
2410 // by an identifier and then either ':' or ']', in a place where an
2411 // expression is permitted, then this is probably a class message send
2412 // missing the initial '['. In this case, we won't consider this to be
2413 // the start of a declaration.
2414 if (DisambiguatingWithExpression &&
2415 isStartOfObjCClassMessageMissingOpenBracket())
2416 return false;
2417
John McCall9ba61662010-02-26 08:45:28 +00002418 return isDeclarationSpecifier();
2419
Chris Lattner166a8fc2009-01-04 23:41:41 +00002420 case tok::coloncolon: // ::foo::bar
2421 if (NextToken().is(tok::kw_new) || // ::new
2422 NextToken().is(tok::kw_delete)) // ::delete
2423 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002424
Chris Lattner166a8fc2009-01-04 23:41:41 +00002425 // Annotate typenames and C++ scope specifiers. If we get one, just
2426 // recurse to handle whatever we get.
2427 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002428 return true;
2429 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002430
Reid Spencer5f016e22007-07-11 17:01:13 +00002431 // storage-class-specifier
2432 case tok::kw_typedef:
2433 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002434 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002435 case tok::kw_static:
2436 case tok::kw_auto:
2437 case tok::kw_register:
2438 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002439
Reid Spencer5f016e22007-07-11 17:01:13 +00002440 // type-specifiers
2441 case tok::kw_short:
2442 case tok::kw_long:
2443 case tok::kw_signed:
2444 case tok::kw_unsigned:
2445 case tok::kw__Complex:
2446 case tok::kw__Imaginary:
2447 case tok::kw_void:
2448 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002449 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002450 case tok::kw_char16_t:
2451 case tok::kw_char32_t:
2452
Reid Spencer5f016e22007-07-11 17:01:13 +00002453 case tok::kw_int:
2454 case tok::kw_float:
2455 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002456 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002457 case tok::kw__Bool:
2458 case tok::kw__Decimal32:
2459 case tok::kw__Decimal64:
2460 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002461 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002462
Chris Lattner99dc9142008-04-13 18:59:07 +00002463 // struct-or-union-specifier (C99) or class-specifier (C++)
2464 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002465 case tok::kw_struct:
2466 case tok::kw_union:
2467 // enum-specifier
2468 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002469
Reid Spencer5f016e22007-07-11 17:01:13 +00002470 // type-qualifier
2471 case tok::kw_const:
2472 case tok::kw_volatile:
2473 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002474
Reid Spencer5f016e22007-07-11 17:01:13 +00002475 // function-specifier
2476 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002477 case tok::kw_virtual:
2478 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002479
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002480 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002481 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002482
Chris Lattner1ef08762007-08-09 17:01:07 +00002483 // GNU typeof support.
2484 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002485
Chris Lattner1ef08762007-08-09 17:01:07 +00002486 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002487 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002488 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002489
Chris Lattnerf3948c42008-07-26 03:38:44 +00002490 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2491 case tok::less:
2492 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002493
Steve Naroff47f52092009-01-06 19:34:12 +00002494 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002495 case tok::kw___cdecl:
2496 case tok::kw___stdcall:
2497 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002498 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002499 case tok::kw___w64:
2500 case tok::kw___ptr64:
2501 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002502 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002503 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002504 }
2505}
2506
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002507bool Parser::isConstructorDeclarator() {
2508 TentativeParsingAction TPA(*this);
2509
2510 // Parse the C++ scope specifier.
2511 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002512 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00002513 TPA.Revert();
2514 return false;
2515 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002516
2517 // Parse the constructor name.
2518 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2519 // We already know that we have a constructor name; just consume
2520 // the token.
2521 ConsumeToken();
2522 } else {
2523 TPA.Revert();
2524 return false;
2525 }
2526
2527 // Current class name must be followed by a left parentheses.
2528 if (Tok.isNot(tok::l_paren)) {
2529 TPA.Revert();
2530 return false;
2531 }
2532 ConsumeParen();
2533
2534 // A right parentheses or ellipsis signals that we have a constructor.
2535 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2536 TPA.Revert();
2537 return true;
2538 }
2539
2540 // If we need to, enter the specified scope.
2541 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002542 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002543 DeclScopeObj.EnterDeclaratorScope();
2544
2545 // Check whether the next token(s) are part of a declaration
2546 // specifier, in which case we have the start of a parameter and,
2547 // therefore, we know that this is a constructor.
2548 bool IsConstructor = isDeclarationSpecifier();
2549 TPA.Revert();
2550 return IsConstructor;
2551}
Reid Spencer5f016e22007-07-11 17:01:13 +00002552
2553/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00002554/// type-qualifier-list: [C99 6.7.5]
2555/// type-qualifier
2556/// [vendor] attributes
2557/// [ only if VendorAttributesAllowed=true ]
2558/// type-qualifier-list type-qualifier
2559/// [vendor] type-qualifier-list attributes
2560/// [ only if VendorAttributesAllowed=true ]
2561/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2562/// [ only if CXX0XAttributesAllowed=true ]
2563/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00002564///
Dawn Perchik52fc3142010-09-03 01:29:35 +00002565void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2566 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00002567 bool CXX0XAttributesAllowed) {
2568 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2569 SourceLocation Loc = Tok.getLocation();
2570 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2571 if (CXX0XAttributesAllowed)
2572 DS.AddAttributes(Attr.AttrList);
2573 else
2574 Diag(Loc, diag::err_attributes_not_allowed);
2575 }
2576
Reid Spencer5f016e22007-07-11 17:01:13 +00002577 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002578 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002579 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002580 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002581 SourceLocation Loc = Tok.getLocation();
2582
2583 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00002584 case tok::code_completion:
2585 Actions.CodeCompleteTypeQualifiers(DS);
2586 ConsumeCodeCompletionToken();
2587 break;
2588
Reid Spencer5f016e22007-07-11 17:01:13 +00002589 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002590 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2591 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002592 break;
2593 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002594 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2595 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002596 break;
2597 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002598 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2599 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002600 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002601 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002602 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002603 case tok::kw___cdecl:
2604 case tok::kw___stdcall:
2605 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002606 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002607 if (VendorAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002608 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2609 continue;
2610 }
2611 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002612 case tok::kw___pascal:
2613 if (VendorAttributesAllowed) {
2614 DS.AddAttributes(ParseBorlandTypeAttributes());
2615 continue;
2616 }
2617 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002618 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002619 if (VendorAttributesAllowed) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002620 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002621 continue; // do *not* consume the next token!
2622 }
2623 // otherwise, FALL THROUGH!
2624 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002625 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002626 // If this is not a type-qualifier token, we're done reading type
2627 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002628 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002629 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002630 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002631
Reid Spencer5f016e22007-07-11 17:01:13 +00002632 // If the specifier combination wasn't legal, issue a diagnostic.
2633 if (isInvalid) {
2634 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002635 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002636 }
2637 ConsumeToken();
2638 }
2639}
2640
2641
2642/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2643///
2644void Parser::ParseDeclarator(Declarator &D) {
2645 /// This implements the 'declarator' production in the C grammar, then checks
2646 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002647 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002648}
2649
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002650/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2651/// is parsed by the function passed to it. Pass null, and the direct-declarator
2652/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002653/// ptr-operator production.
2654///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002655/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2656/// [C] pointer[opt] direct-declarator
2657/// [C++] direct-declarator
2658/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002659///
2660/// pointer: [C99 6.7.5]
2661/// '*' type-qualifier-list[opt]
2662/// '*' type-qualifier-list[opt] pointer
2663///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002664/// ptr-operator:
2665/// '*' cv-qualifier-seq[opt]
2666/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002667/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002668/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002669/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002670/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002671void Parser::ParseDeclaratorInternal(Declarator &D,
2672 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002673 if (Diags.hasAllExtensionsSilenced())
2674 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002675
Sebastian Redlf30208a2009-01-24 21:16:55 +00002676 // C++ member pointers start with a '::' or a nested-name.
2677 // Member pointers get special handling, since there's no place for the
2678 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002679 if (getLang().CPlusPlus &&
2680 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2681 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002682 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002683 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00002684
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002685 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002686 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002687 // The scope spec really belongs to the direct-declarator.
2688 D.getCXXScopeSpec() = SS;
2689 if (DirectDeclParser)
2690 (this->*DirectDeclParser)(D);
2691 return;
2692 }
2693
2694 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002695 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002696 DeclSpec DS;
2697 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002698 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002699
2700 // Recurse to parse whatever is left.
2701 ParseDeclaratorInternal(D, DirectDeclParser);
2702
2703 // Sema will have to catch (syntactically invalid) pointers into global
2704 // scope. It has to catch pointers into namespace scope anyway.
2705 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002706 Loc, DS.TakeAttributes()),
2707 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002708 return;
2709 }
2710 }
2711
2712 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002713 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002714 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002715 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002716 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002717 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002718 if (DirectDeclParser)
2719 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002720 return;
2721 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002722
Sebastian Redl05532f22009-03-15 22:02:01 +00002723 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2724 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002725 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002726 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002727
Chris Lattner9af55002009-03-27 04:18:06 +00002728 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002729 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002730 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002731
Reid Spencer5f016e22007-07-11 17:01:13 +00002732 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002733 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002734
Reid Spencer5f016e22007-07-11 17:01:13 +00002735 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002736 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002737 if (Kind == tok::star)
2738 // Remember that we parsed a pointer type, and remember the type-quals.
2739 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002740 DS.TakeAttributes()),
2741 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002742 else
2743 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002744 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002745 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002746 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002747 } else {
2748 // Is a reference
2749 DeclSpec DS;
2750
Sebastian Redl743de1f2009-03-23 00:00:23 +00002751 // Complain about rvalue references in C++03, but then go on and build
2752 // the declarator.
2753 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2754 Diag(Loc, diag::err_rvalue_reference);
2755
Reid Spencer5f016e22007-07-11 17:01:13 +00002756 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2757 // cv-qualifiers are introduced through the use of a typedef or of a
2758 // template type argument, in which case the cv-qualifiers are ignored.
2759 //
2760 // [GNU] Retricted references are allowed.
2761 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002762 // [C++0x] Attributes on references are not allowed.
2763 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002764 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002765
2766 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2767 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2768 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002769 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002770 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2771 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002772 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002773 }
2774
2775 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002776 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002777
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002778 if (D.getNumTypeObjects() > 0) {
2779 // C++ [dcl.ref]p4: There shall be no references to references.
2780 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2781 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002782 if (const IdentifierInfo *II = D.getIdentifier())
2783 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2784 << II;
2785 else
2786 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2787 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002788
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002789 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002790 // can go ahead and build the (technically ill-formed)
2791 // declarator: reference collapsing will take care of it.
2792 }
2793 }
2794
Reid Spencer5f016e22007-07-11 17:01:13 +00002795 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002796 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002797 DS.TakeAttributes(),
2798 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002799 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002800 }
2801}
2802
2803/// ParseDirectDeclarator
2804/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002805/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002806/// '(' declarator ')'
2807/// [GNU] '(' attributes declarator ')'
2808/// [C90] direct-declarator '[' constant-expression[opt] ']'
2809/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2810/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2811/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2812/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2813/// direct-declarator '(' parameter-type-list ')'
2814/// direct-declarator '(' identifier-list[opt] ')'
2815/// [GNU] direct-declarator '(' parameter-forward-declarations
2816/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002817/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2818/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002819/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002820///
2821/// declarator-id: [C++ 8]
2822/// id-expression
2823/// '::'[opt] nested-name-specifier[opt] type-name
2824///
2825/// id-expression: [C++ 5.1]
2826/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002827/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002828///
2829/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002830/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002831/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002832/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002833/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002834/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002835///
Reid Spencer5f016e22007-07-11 17:01:13 +00002836void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002837 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002838
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002839 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2840 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002841 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00002842 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00002843 }
2844
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002845 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002846 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00002847 // Change the declaration context for name lookup, until this function
2848 // is exited (and the declarator has been parsed).
2849 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002850 }
2851
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002852 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2853 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2854 // We found something that indicates the start of an unqualified-id.
2855 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002856 bool AllowConstructorName;
2857 if (D.getDeclSpec().hasTypeSpecifier())
2858 AllowConstructorName = false;
2859 else if (D.getCXXScopeSpec().isSet())
2860 AllowConstructorName =
2861 (D.getContext() == Declarator::FileContext ||
2862 (D.getContext() == Declarator::MemberContext &&
2863 D.getDeclSpec().isFriendSpecified()));
2864 else
2865 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2866
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002867 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2868 /*EnteringContext=*/true,
2869 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002870 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002871 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002872 D.getName()) ||
2873 // Once we're past the identifier, if the scope was bad, mark the
2874 // whole declarator bad.
2875 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002876 D.SetIdentifier(0, Tok.getLocation());
2877 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002878 } else {
2879 // Parsed the unqualified-id; update range information and move along.
2880 if (D.getSourceRange().getBegin().isInvalid())
2881 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2882 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002883 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002884 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002885 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002886 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002887 assert(!getLang().CPlusPlus &&
2888 "There's a C++-specific check for tok::identifier above");
2889 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2890 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2891 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002892 goto PastIdentifier;
2893 }
2894
2895 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002896 // direct-declarator: '(' declarator ')'
2897 // direct-declarator: '(' attributes declarator ')'
2898 // Example: 'char (*X)' or 'int (*XX)(void)'
2899 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002900
2901 // If the declarator was parenthesized, we entered the declarator
2902 // scope when parsing the parenthesized declarator, then exited
2903 // the scope already. Re-enter the scope, if we need to.
2904 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002905 // If there was an error parsing parenthesized declarator, declarator
2906 // scope may have been enterred before. Don't do it again.
2907 if (!D.isInvalidType() &&
2908 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002909 // Change the declaration context for name lookup, until this function
2910 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002911 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002912 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002913 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002914 // This could be something simple like "int" (in which case the declarator
2915 // portion is empty), if an abstract-declarator is allowed.
2916 D.SetIdentifier(0, Tok.getLocation());
2917 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002918 if (D.getContext() == Declarator::MemberContext)
2919 Diag(Tok, diag::err_expected_member_name_or_semi)
2920 << D.getDeclSpec().getSourceRange();
2921 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002922 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002923 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002924 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002925 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002926 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002927 }
Mike Stump1eb44332009-09-09 15:08:12 +00002928
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002929 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002930 assert(D.isPastIdentifier() &&
2931 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002932
Sean Huntbbd37c62009-11-21 08:43:09 +00002933 // Don't parse attributes unless we have an identifier.
Douglas Gregor3c3aaf92010-02-19 16:47:56 +00002934 if (D.getIdentifier() && getLang().CPlusPlus0x
Sean Huntbbd37c62009-11-21 08:43:09 +00002935 && isCXX0XAttributeSpecifier(true)) {
2936 SourceLocation AttrEndLoc;
2937 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2938 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2939 }
2940
Reid Spencer5f016e22007-07-11 17:01:13 +00002941 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002942 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002943 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2944 // In such a case, check if we actually have a function declarator; if it
2945 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002946 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2947 // When not in file scope, warn for ambiguous function declarators, just
2948 // in case the author intended it as a variable definition.
2949 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2950 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2951 break;
2952 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002953 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002954 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002955 ParseBracketDeclarator(D);
2956 } else {
2957 break;
2958 }
2959 }
2960}
2961
Chris Lattneref4715c2008-04-06 05:45:57 +00002962/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2963/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002964/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002965/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2966///
2967/// direct-declarator:
2968/// '(' declarator ')'
2969/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002970/// direct-declarator '(' parameter-type-list ')'
2971/// direct-declarator '(' identifier-list[opt] ')'
2972/// [GNU] direct-declarator '(' parameter-forward-declarations
2973/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002974///
2975void Parser::ParseParenDeclarator(Declarator &D) {
2976 SourceLocation StartLoc = ConsumeParen();
2977 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002978
Chris Lattner7399ee02008-10-20 02:05:46 +00002979 // Eat any attributes before we look at whether this is a grouping or function
2980 // declarator paren. If this is a grouping paren, the attribute applies to
2981 // the type being built up, for example:
2982 // int (__attribute__(()) *x)(long y)
2983 // If this ends up not being a grouping paren, the attribute applies to the
2984 // first argument, for example:
2985 // int (__attribute__(()) int x)
2986 // In either case, we need to eat any attributes to be able to determine what
2987 // sort of paren this is.
2988 //
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002989 AttributeList *AttrList = 0;
Chris Lattner7399ee02008-10-20 02:05:46 +00002990 bool RequiresArg = false;
2991 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00002992 AttrList = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00002993
Chris Lattner7399ee02008-10-20 02:05:46 +00002994 // We require that the argument list (if this is a non-grouping paren) be
2995 // present even if the attribute list was empty.
2996 RequiresArg = true;
2997 }
Steve Naroff239f0732008-12-25 14:16:32 +00002998 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002999 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003000 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
3001 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003002 AttrList = ParseMicrosoftTypeAttributes(AttrList);
Eli Friedman290eeb02009-06-08 23:27:34 +00003003 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00003004 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003005 if (Tok.is(tok::kw___pascal))
3006 AttrList = ParseBorlandTypeAttributes(AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00003007
Chris Lattneref4715c2008-04-06 05:45:57 +00003008 // If we haven't past the identifier yet (or where the identifier would be
3009 // stored, if this is an abstract declarator), then this is probably just
3010 // grouping parens. However, if this could be an abstract-declarator, then
3011 // this could also be the start of function arguments (consider 'void()').
3012 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00003013
Chris Lattneref4715c2008-04-06 05:45:57 +00003014 if (!D.mayOmitIdentifier()) {
3015 // If this can't be an abstract-declarator, this *must* be a grouping
3016 // paren, because we haven't seen the identifier yet.
3017 isGrouping = true;
3018 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00003019 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00003020 isDeclarationSpecifier()) { // 'int(int)' is a function.
3021 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
3022 // considered to be a type, not a K&R identifier-list.
3023 isGrouping = false;
3024 } else {
3025 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
3026 isGrouping = true;
3027 }
Mike Stump1eb44332009-09-09 15:08:12 +00003028
Chris Lattneref4715c2008-04-06 05:45:57 +00003029 // If this is a grouping paren, handle:
3030 // direct-declarator: '(' declarator ')'
3031 // direct-declarator: '(' attributes declarator ')'
3032 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003033 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003034 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00003035 if (AttrList)
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003036 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00003037
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003038 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00003039 // Match the ')'.
Abramo Bagnara075f8f12010-12-10 16:29:40 +00003040 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_paren, StartLoc);
3041 D.AddTypeInfo(DeclaratorChunk::getParen(StartLoc, EndLoc), EndLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00003042
3043 D.setGroupingParens(hadGroupingParens);
Chris Lattneref4715c2008-04-06 05:45:57 +00003044 return;
3045 }
Mike Stump1eb44332009-09-09 15:08:12 +00003046
Chris Lattneref4715c2008-04-06 05:45:57 +00003047 // Okay, if this wasn't a grouping paren, it must be the start of a function
3048 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00003049 // identifier (and remember where it would have been), then call into
3050 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00003051 D.SetIdentifier(0, Tok.getLocation());
3052
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003053 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00003054}
3055
3056/// ParseFunctionDeclarator - We are after the identifier and have parsed the
3057/// declarator D up to a paren, which indicates that we are parsing function
3058/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00003059///
Chris Lattner7399ee02008-10-20 02:05:46 +00003060/// If AttrList is non-null, then the caller parsed those arguments immediately
3061/// after the open paren - they should be considered to be the first argument of
3062/// a parameter. If RequiresArg is true, then the first argument of the
3063/// function is required to be present and required to not be an identifier
3064/// list.
3065///
Reid Spencer5f016e22007-07-11 17:01:13 +00003066/// This method also handles this portion of the grammar:
3067/// parameter-type-list: [C99 6.7.5]
3068/// parameter-list
3069/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00003070/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00003071///
3072/// parameter-list: [C99 6.7.5]
3073/// parameter-declaration
3074/// parameter-list ',' parameter-declaration
3075///
3076/// parameter-declaration: [C99 6.7.5]
3077/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00003078/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003079/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00003080/// declaration-specifiers abstract-declarator[opt]
3081/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00003082/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00003083/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
3084///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003085/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00003086/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003087///
Chris Lattner7399ee02008-10-20 02:05:46 +00003088void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
3089 AttributeList *AttrList,
3090 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00003091 // lparen is already consumed!
3092 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00003093
Douglas Gregordab60ad2010-10-01 18:44:50 +00003094 ParsedType TrailingReturnType;
3095
Chris Lattner7399ee02008-10-20 02:05:46 +00003096 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00003097 if (Tok.is(tok::r_paren)) {
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003098 if (RequiresArg)
Chris Lattner1ab3b962008-11-18 07:48:38 +00003099 Diag(Tok, diag::err_argument_required_after_attribute);
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003100
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003101 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3102 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003103
3104 // cv-qualifier-seq[opt].
3105 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003106 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003107 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003108 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003109 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003110 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003111 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003112 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003113 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003114 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003115
3116 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003117 if (Tok.is(tok::kw_throw)) {
3118 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003119 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003120 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003121 hasAnyExceptionSpec);
3122 assert(Exceptions.size() == ExceptionRanges.size() &&
3123 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003124 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00003125
3126 // Parse trailing-return-type.
3127 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3128 TrailingReturnType = ParseTrailingReturnType().get();
3129 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003130 }
3131
Chris Lattnerf97409f2008-04-06 06:57:35 +00003132 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003133 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003134 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003135 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003136 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003137 /*arglist*/ 0, 0,
3138 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003139 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003140 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003141 Exceptions.data(),
3142 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003143 Exceptions.size(),
Douglas Gregordab60ad2010-10-01 18:44:50 +00003144 LParenLoc, RParenLoc, D,
3145 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003146 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003147 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003148 }
3149
Chris Lattner7399ee02008-10-20 02:05:46 +00003150 // Alternatively, this parameter list may be an identifier list form for a
3151 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003152 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3153 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003154 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003155 // K&R identifier lists can't have typedefs as identifiers, per
3156 // C99 6.7.5.3p11.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00003157 if (RequiresArg)
Steve Naroff2d081c42009-01-28 19:16:40 +00003158 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner83a94472010-05-14 17:23:36 +00003159
Steve Naroff2d081c42009-01-28 19:16:40 +00003160 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003161 // normal declarators, not for abstract-declarators. Get the first
3162 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003163 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003164 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003165
3166 // Identifier lists follow a really simple grammar: the identifiers can
3167 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3168 // identifier lists are really rare in the brave new modern world, and it
3169 // is very common for someone to typo a type in a non-k&r style list. If
3170 // we are presented with something like: "void foo(intptr x, float y)",
3171 // we don't want to start parsing the function declarator as though it is
3172 // a K&R style declarator just because intptr is an invalid type.
3173 //
3174 // To handle this, we check to see if the token after the first identifier
3175 // is a "," or ")". Only if so, do we parse it as an identifier list.
3176 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3177 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3178 FirstTok.getIdentifierInfo(),
3179 FirstTok.getLocation(), D);
3180
3181 // If we get here, the code is invalid. Push the first identifier back
3182 // into the token stream and parse the first argument as an (invalid)
3183 // normal argument declarator.
3184 PP.EnterToken(Tok);
3185 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003186 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003187 }
Mike Stump1eb44332009-09-09 15:08:12 +00003188
Chris Lattnerf97409f2008-04-06 06:57:35 +00003189 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003190
Chris Lattnerf97409f2008-04-06 06:57:35 +00003191 // Build up an array of information about the parsed arguments.
3192 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003193
3194 // Enter function-declaration scope, limiting any declarators to the
3195 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003196 ParseScope PrototypeScope(this,
3197 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003198
Chris Lattnerf97409f2008-04-06 06:57:35 +00003199 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003200 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003201 while (1) {
3202 if (Tok.is(tok::ellipsis)) {
3203 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003204 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003205 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003206 }
Francois Pichet334d47e2010-10-11 12:59:39 +00003207
3208 // Skip any Microsoft attributes before a param.
3209 if (getLang().Microsoft && Tok.is(tok::l_square))
3210 ParseMicrosoftAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00003211
Chris Lattnerf97409f2008-04-06 06:57:35 +00003212 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00003213
Chris Lattnerf97409f2008-04-06 06:57:35 +00003214 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003215 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003216 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00003217
3218 // If the caller parsed attributes for the first argument, add them now.
3219 if (AttrList) {
3220 DS.AddAttributes(AttrList);
3221 AttrList = 0; // Only apply the attributes to the first parameter.
3222 }
Chris Lattnere64c5492009-02-27 18:38:20 +00003223 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003224
Chris Lattnerf97409f2008-04-06 06:57:35 +00003225 // Parse the declarator. This is "PrototypeContext", because we must
3226 // accept either 'declarator' or 'abstract-declarator' here.
3227 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3228 ParseDeclarator(ParmDecl);
3229
3230 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003231 if (Tok.is(tok::kw___attribute)) {
3232 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00003233 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003234 ParmDecl.AddAttributes(AttrList, Loc);
3235 }
Mike Stump1eb44332009-09-09 15:08:12 +00003236
Chris Lattnerf97409f2008-04-06 06:57:35 +00003237 // Remember this parsed parameter in ParamInfo.
3238 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003239
Douglas Gregor72b505b2008-12-16 21:30:33 +00003240 // DefArgToks is used when the parsing of default arguments needs
3241 // to be delayed.
3242 CachedTokens *DefArgToks = 0;
3243
Chris Lattnerf97409f2008-04-06 06:57:35 +00003244 // If no parameter was specified, verify that *something* was specified,
3245 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003246 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3247 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003248 // Completely missing, emit error.
3249 Diag(DSStart, diag::err_missing_param);
3250 } else {
3251 // Otherwise, we have something. Add it and let semantic analysis try
3252 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003253
Chris Lattnerf97409f2008-04-06 06:57:35 +00003254 // Inform the actions module about the parameter declarator, so it gets
3255 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003256 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003257
3258 // Parse the default argument, if any. We parse the default
3259 // arguments in all dialects; the semantic analysis in
3260 // ActOnParamDefaultArgument will reject the default argument in
3261 // C.
3262 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003263 SourceLocation EqualLoc = Tok.getLocation();
3264
Chris Lattner04421082008-04-08 04:40:51 +00003265 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003266 if (D.getContext() == Declarator::MemberContext) {
3267 // If we're inside a class definition, cache the tokens
3268 // corresponding to the default argument. We'll actually parse
3269 // them when we see the end of the class definition.
3270 // FIXME: Templates will require something similar.
3271 // FIXME: Can we use a smart pointer for Toks?
3272 DefArgToks = new CachedTokens;
3273
Mike Stump1eb44332009-09-09 15:08:12 +00003274 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003275 /*StopAtSemi=*/true,
3276 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003277 delete DefArgToks;
3278 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003279 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003280 } else {
3281 // Mark the end of the default argument so that we know when to
3282 // stop when we parse it later on.
3283 Token DefArgEnd;
3284 DefArgEnd.startToken();
3285 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3286 DefArgEnd.setLocation(Tok.getLocation());
3287 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003288 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003289 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003290 }
Chris Lattner04421082008-04-08 04:40:51 +00003291 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003292 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003293 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003294
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00003295 // The argument isn't actually potentially evaluated unless it is
3296 // used.
3297 EnterExpressionEvaluationContext Eval(Actions,
3298 Sema::PotentiallyEvaluatedIfUsed);
3299
John McCall60d7b3a2010-08-24 06:29:42 +00003300 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003301 if (DefArgResult.isInvalid()) {
3302 Actions.ActOnParamDefaultArgumentError(Param);
3303 SkipUntil(tok::comma, tok::r_paren, true, true);
3304 } else {
3305 // Inform the actions module about the default argument
3306 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003307 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003308 }
Chris Lattner04421082008-04-08 04:40:51 +00003309 }
3310 }
Mike Stump1eb44332009-09-09 15:08:12 +00003311
3312 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3313 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003314 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003315 }
3316
3317 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003318 if (Tok.isNot(tok::comma)) {
3319 if (Tok.is(tok::ellipsis)) {
3320 IsVariadic = true;
3321 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3322
3323 if (!getLang().CPlusPlus) {
3324 // We have ellipsis without a preceding ',', which is ill-formed
3325 // in C. Complain and provide the fix.
3326 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003327 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003328 }
3329 }
3330
3331 break;
3332 }
Mike Stump1eb44332009-09-09 15:08:12 +00003333
Chris Lattnerf97409f2008-04-06 06:57:35 +00003334 // Consume the comma.
3335 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003336 }
Mike Stump1eb44332009-09-09 15:08:12 +00003337
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003338 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003339 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3340 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003341
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003342 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003343 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003344 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003345 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003346 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003347 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003348
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003349 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003350 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003351 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003352 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003353 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003354
3355 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003356 if (Tok.is(tok::kw_throw)) {
3357 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003358 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003359 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003360 hasAnyExceptionSpec);
3361 assert(Exceptions.size() == ExceptionRanges.size() &&
3362 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003363 }
Douglas Gregordab60ad2010-10-01 18:44:50 +00003364
3365 // Parse trailing-return-type.
3366 if (getLang().CPlusPlus0x && Tok.is(tok::arrow)) {
3367 TrailingReturnType = ParseTrailingReturnType().get();
3368 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003369 }
3370
Douglas Gregordab60ad2010-10-01 18:44:50 +00003371 // FIXME: We should leave the prototype scope before parsing the exception
3372 // specification, and then reenter it when parsing the trailing return type.
3373
3374 // Leave prototype scope.
3375 PrototypeScope.Exit();
3376
Reid Spencer5f016e22007-07-11 17:01:13 +00003377 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003378 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003379 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003380 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003381 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003382 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003383 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003384 Exceptions.data(),
3385 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003386 Exceptions.size(),
Douglas Gregordab60ad2010-10-01 18:44:50 +00003387 LParenLoc, RParenLoc, D,
3388 TrailingReturnType),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003389 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003390}
3391
Chris Lattner66d28652008-04-06 06:34:08 +00003392/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3393/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003394/// first identifier has already been consumed, and the current token is the
3395/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003396///
3397/// identifier-list: [C99 6.7.5]
3398/// identifier
3399/// identifier-list ',' identifier
3400///
3401void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003402 IdentifierInfo *FirstIdent,
3403 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003404 Declarator &D) {
3405 // Build up an array of information about the parsed arguments.
3406 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3407 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003408
Chris Lattner66d28652008-04-06 06:34:08 +00003409 // If there was no identifier specified for the declarator, either we are in
3410 // an abstract-declarator, or we are in a parameter declarator which was found
3411 // to be abstract. In abstract-declarators, identifier lists are not valid:
3412 // diagnose this.
3413 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003414 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003415
Chris Lattner83a94472010-05-14 17:23:36 +00003416 // The first identifier was already read, and is known to be the first
3417 // identifier in the list. Remember this identifier in ParamInfo.
3418 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003419 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003420
Chris Lattner66d28652008-04-06 06:34:08 +00003421 while (Tok.is(tok::comma)) {
3422 // Eat the comma.
3423 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003424
Chris Lattner50c64772008-04-06 06:39:19 +00003425 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003426 if (Tok.isNot(tok::identifier)) {
3427 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003428 SkipUntil(tok::r_paren);
3429 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003430 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003431
Chris Lattner66d28652008-04-06 06:34:08 +00003432 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003433
3434 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003435 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003436 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003437
Chris Lattner66d28652008-04-06 06:34:08 +00003438 // Verify that the argument identifier has not already been mentioned.
3439 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003440 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003441 } else {
3442 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003443 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003444 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00003445 0));
Chris Lattner50c64772008-04-06 06:39:19 +00003446 }
Mike Stump1eb44332009-09-09 15:08:12 +00003447
Chris Lattner66d28652008-04-06 06:34:08 +00003448 // Eat the identifier.
3449 ConsumeToken();
3450 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003451
3452 // If we have the closing ')', eat it and we're done.
3453 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3454
Chris Lattner50c64772008-04-06 06:39:19 +00003455 // Remember that we parsed a function type, and remember the attributes. This
3456 // function type is always a K&R style function type, which is not varargs and
3457 // has no prototype.
3458 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003459 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003460 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003461 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003462 /*exception*/false,
3463 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003464 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003465 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003466}
Chris Lattneref4715c2008-04-06 05:45:57 +00003467
Reid Spencer5f016e22007-07-11 17:01:13 +00003468/// [C90] direct-declarator '[' constant-expression[opt] ']'
3469/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3470/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3471/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3472/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3473void Parser::ParseBracketDeclarator(Declarator &D) {
3474 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003475
Chris Lattner378c7e42008-12-18 07:27:21 +00003476 // C array syntax has many features, but by-far the most common is [] and [4].
3477 // This code does a fast path to handle some of the most obvious cases.
3478 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003479 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003480 //FIXME: Use these
3481 CXX0XAttributeList Attr;
3482 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3483 Attr = ParseCXX0XAttributes();
3484 }
3485
Chris Lattner378c7e42008-12-18 07:27:21 +00003486 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00003487 ExprResult NumElements;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003488 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3489 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003490 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003491 return;
3492 } else if (Tok.getKind() == tok::numeric_constant &&
3493 GetLookAheadToken(1).is(tok::r_square)) {
3494 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00003495 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003496 ConsumeToken();
3497
Sebastian Redlab197ba2009-02-09 18:23:29 +00003498 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003499 //FIXME: Use these
3500 CXX0XAttributeList Attr;
3501 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3502 Attr = ParseCXX0XAttributes();
3503 }
Chris Lattner378c7e42008-12-18 07:27:21 +00003504
3505 // If there was an error parsing the assignment-expression, recover.
3506 if (ExprRes.isInvalid())
3507 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00003508
Chris Lattner378c7e42008-12-18 07:27:21 +00003509 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003510 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3511 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003512 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003513 return;
3514 }
Mike Stump1eb44332009-09-09 15:08:12 +00003515
Reid Spencer5f016e22007-07-11 17:01:13 +00003516 // If valid, this location is the position where we read the 'static' keyword.
3517 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003518 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003519 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003520
Reid Spencer5f016e22007-07-11 17:01:13 +00003521 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003522 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003523 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003524 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003525
Reid Spencer5f016e22007-07-11 17:01:13 +00003526 // If we haven't already read 'static', check to see if there is one after the
3527 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003528 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003529 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003530
Reid Spencer5f016e22007-07-11 17:01:13 +00003531 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3532 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00003533 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00003534
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003535 // Handle the case where we have '[*]' as the array size. However, a leading
3536 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3537 // the the token after the star is a ']'. Since stars in arrays are
3538 // infrequent, use of lookahead is not costly here.
3539 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003540 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003541
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003542 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003543 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003544 StaticLoc = SourceLocation(); // Drop the static.
3545 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003546 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003547 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003548 // Note, in C89, this production uses the constant-expr production instead
3549 // of assignment-expr. The only difference is that assignment-expr allows
3550 // things like '=' and '*='. Sema rejects these in C89 mode because they
3551 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003552
Douglas Gregore0762c92009-06-19 23:52:42 +00003553 // Parse the constant-expression or assignment-expression now (depending
3554 // on dialect).
3555 if (getLang().CPlusPlus)
3556 NumElements = ParseConstantExpression();
3557 else
3558 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003559 }
Mike Stump1eb44332009-09-09 15:08:12 +00003560
Reid Spencer5f016e22007-07-11 17:01:13 +00003561 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003562 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003563 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003564 // If the expression was invalid, skip it.
3565 SkipUntil(tok::r_square);
3566 return;
3567 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003568
3569 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3570
Sean Huntbbd37c62009-11-21 08:43:09 +00003571 //FIXME: Use these
3572 CXX0XAttributeList Attr;
3573 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3574 Attr = ParseCXX0XAttributes();
3575 }
3576
Chris Lattner378c7e42008-12-18 07:27:21 +00003577 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003578 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3579 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003580 NumElements.release(),
3581 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003582 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003583}
3584
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003585/// [GNU] typeof-specifier:
3586/// typeof ( expressions )
3587/// typeof ( type-name )
3588/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003589///
3590void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003591 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003592 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003593 SourceLocation StartLoc = ConsumeToken();
3594
John McCallcfb708c2010-01-13 20:03:27 +00003595 const bool hasParens = Tok.is(tok::l_paren);
3596
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003597 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00003598 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003599 SourceRange CastRange;
John McCall60d7b3a2010-08-24 06:29:42 +00003600 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall911093e2010-08-25 02:45:51 +00003601 isCastExpr,
3602 CastTy,
3603 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003604 if (hasParens)
3605 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003606
3607 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003608 // FIXME: Not accurate, the range gets one token more than it should.
3609 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003610 else
3611 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003612
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003613 if (isCastExpr) {
3614 if (!CastTy) {
3615 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003616 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003617 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003618
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003619 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003620 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003621 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3622 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003623 DiagID, CastTy))
3624 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003625 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003626 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003627
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003628 // If we get here, the operand to the typeof was an expresion.
3629 if (Operand.isInvalid()) {
3630 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003631 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003632 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003633
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003634 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003635 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003636 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3637 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00003638 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00003639 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003640}
Chris Lattner1b492422010-02-28 18:33:55 +00003641
3642
3643/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3644/// from TryAltiVecVectorToken.
3645bool Parser::TryAltiVecVectorTokenOutOfLine() {
3646 Token Next = NextToken();
3647 switch (Next.getKind()) {
3648 default: return false;
3649 case tok::kw_short:
3650 case tok::kw_long:
3651 case tok::kw_signed:
3652 case tok::kw_unsigned:
3653 case tok::kw_void:
3654 case tok::kw_char:
3655 case tok::kw_int:
3656 case tok::kw_float:
3657 case tok::kw_double:
3658 case tok::kw_bool:
3659 case tok::kw___pixel:
3660 Tok.setKind(tok::kw___vector);
3661 return true;
3662 case tok::identifier:
3663 if (Next.getIdentifierInfo() == Ident_pixel) {
3664 Tok.setKind(tok::kw___vector);
3665 return true;
3666 }
3667 return false;
3668 }
3669}
3670
3671bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3672 const char *&PrevSpec, unsigned &DiagID,
3673 bool &isInvalid) {
3674 if (Tok.getIdentifierInfo() == Ident_vector) {
3675 Token Next = NextToken();
3676 switch (Next.getKind()) {
3677 case tok::kw_short:
3678 case tok::kw_long:
3679 case tok::kw_signed:
3680 case tok::kw_unsigned:
3681 case tok::kw_void:
3682 case tok::kw_char:
3683 case tok::kw_int:
3684 case tok::kw_float:
3685 case tok::kw_double:
3686 case tok::kw_bool:
3687 case tok::kw___pixel:
3688 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3689 return true;
3690 case tok::identifier:
3691 if (Next.getIdentifierInfo() == Ident_pixel) {
3692 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3693 return true;
3694 }
3695 break;
3696 default:
3697 break;
3698 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003699 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003700 DS.isTypeAltiVecVector()) {
3701 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3702 return true;
3703 }
3704 return false;
3705}
3706