blob: 555fcf0dec558164e2230877c05a959a923cd4a6 [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
Sean Huntbbd37c62009-11-21 08:43:09 +0000125 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 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
Sean Huntbbd37c62009-11-21 08:43:09 +0000149 CurrAttr = new AttributeList(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
Sean Huntbbd37c62009-11-21 08:43:09 +0000161 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 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:
Fariborz Jahanian1b72fa72010-08-17 23:19:16 +0000178 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
179 0, SourceLocation(), 0, 0, CurrAttr);
180 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
Sebastian Redla55e52c2008-11-25 22:21:31 +0000210 CurrAttr = new AttributeList(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 {
Sean Huntbbd37c62009-11-21 08:43:09 +0000219 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 0, SourceLocation(), 0, 0, CurrAttr);
221 }
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();
Sean Huntbbd37c62009-11-21 08:43:09 +0000263 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedmana23b4852009-06-08 07:21:15 +0000264 SourceLocation(), &ExprList, 1,
265 CurrAttr, true);
266 }
267 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
268 SkipUntil(tok::r_paren, false);
269 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000270 CurrAttr = new AttributeList(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;
Sean Huntbbd37c62009-11-21 08:43:09 +0000290 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman290eeb02009-06-08 23:27:34 +0000291 SourceLocation(), 0, 0, CurrAttr, true);
292 }
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();
301 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
302 SourceLocation(), 0, 0, CurrAttr, true);
303 }
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///
Chris Lattner97144fc2009-04-02 04:16:50 +0000323Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000324 SourceLocation &DeclEnd,
325 CXX0XAttributeList Attr) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +0000326 ParenBraceBracketBalancer BalancerRAIIObj(*this);
327
John McCalld226f652010-08-21 09:40:31 +0000328 Decl *SingleDecl = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000329 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000330 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000331 case tok::kw_export:
Sean Huntbbd37c62009-11-21 08:43:09 +0000332 if (Attr.HasAttr)
333 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
334 << Attr.Range;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000335 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000336 break;
Sebastian Redld078e642010-08-27 23:12:46 +0000337 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +0000338 // Could be the start of an inline namespace. Allowed as an ext in C++03.
339 if (getLang().CPlusPlus && NextToken().is(tok::kw_namespace)) {
Sebastian Redld078e642010-08-27 23:12:46 +0000340 if (Attr.HasAttr)
341 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
342 << Attr.Range;
343 SourceLocation InlineLoc = ConsumeToken();
344 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
345 break;
346 }
347 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000348 case tok::kw_namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +0000349 if (Attr.HasAttr)
350 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
351 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000352 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000353 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000354 case tok::kw_using:
Sean Huntbbd37c62009-11-21 08:43:09 +0000355 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner682bf922009-03-29 16:50:03 +0000356 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000357 case tok::kw_static_assert:
Sean Huntbbd37c62009-11-21 08:43:09 +0000358 if (Attr.HasAttr)
359 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
360 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000361 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000362 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000363 default:
Chris Lattner5c5db552010-04-05 18:18:31 +0000364 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000365 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000366
Chris Lattner682bf922009-03-29 16:50:03 +0000367 // This routine returns a DeclGroup, if the thing we parsed only contains a
368 // single decl, convert it now.
369 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000370}
371
372/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
373/// declaration-specifiers init-declarator-list[opt] ';'
374///[C90/C++]init-declarator-list ';' [TODO]
375/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000376///
377/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000378/// declaration. If it is true, it checks for and eats it.
Chris Lattnercd147752009-03-29 17:27:48 +0000379Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000380 SourceLocation &DeclEnd,
Chris Lattner5c5db552010-04-05 18:18:31 +0000381 AttributeList *Attr,
382 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000384 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000385 if (Attr)
386 DS.AddAttributes(Attr);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000387 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
388 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
391 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000392 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000393 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +0000394 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
John McCallaec03712010-05-21 20:45:30 +0000395 DS);
John McCall54abf7d2009-11-04 02:18:39 +0000396 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000397 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattner5c5db552010-04-05 18:18:31 +0000400 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000401}
Mike Stump1eb44332009-09-09 15:08:12 +0000402
John McCalld8ac0572009-11-03 19:26:08 +0000403/// ParseDeclGroup - Having concluded that this is either a function
404/// definition or a group of object declarations, actually parse the
405/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000406Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
407 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000408 bool AllowFunctionDefinitions,
409 SourceLocation *DeclEnd) {
410 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000411 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000412 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000413
John McCalld8ac0572009-11-03 19:26:08 +0000414 // Bail out if the first declarator didn't seem well-formed.
415 if (!D.hasName() && !D.mayOmitIdentifier()) {
416 // Skip until ; or }.
417 SkipUntil(tok::r_brace, true, true);
418 if (Tok.is(tok::semi))
419 ConsumeToken();
420 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattnerc82daef2010-07-11 22:24:20 +0000423 // Check to see if we have a function *definition* which must have a body.
424 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
425 // Look at the next token to make sure that this isn't a function
426 // declaration. We have to check this because __attribute__ might be the
427 // start of a function definition in GCC-extended K&R C.
428 !isDeclarationAfterDeclarator()) {
429
Chris Lattner004659a2010-07-11 22:42:07 +0000430 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +0000431 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
432 Diag(Tok, diag::err_function_declared_typedef);
433
434 // Recover by treating the 'typedef' as spurious.
435 DS.ClearStorageClassSpecs();
436 }
437
John McCalld226f652010-08-21 09:40:31 +0000438 Decl *TheDecl = ParseFunctionDefinition(D);
John McCalld8ac0572009-11-03 19:26:08 +0000439 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +0000440 }
441
442 if (isDeclarationSpecifier()) {
443 // If there is an invalid declaration specifier right after the function
444 // prototype, then we must be in a missing semicolon case where this isn't
445 // actually a body. Just fall through into the code that handles it as a
446 // prototype, and let the top-level code handle the erroneous declspec
447 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +0000448 } else {
449 Diag(Tok, diag::err_expected_fn_body);
450 SkipUntil(tok::semi);
451 return DeclGroupPtrTy();
452 }
453 }
454
John McCalld226f652010-08-21 09:40:31 +0000455 llvm::SmallVector<Decl *, 8> DeclsInGroup;
456 Decl *FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000457 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +0000458 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000459 DeclsInGroup.push_back(FirstDecl);
460
461 // If we don't have a comma, it is either the end of the list (a ';') or an
462 // error, bail out.
463 while (Tok.is(tok::comma)) {
464 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000465 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000466
467 // Parse the next declarator.
468 D.clear();
469
470 // Accept attributes in an init-declarator. In the first declarator in a
471 // declaration, these would be part of the declspec. In subsequent
472 // declarators, they become part of the declarator itself, so that they
473 // don't apply to declarators after *this* one. Examples:
474 // short __attribute__((common)) var; -> declspec
475 // short var __attribute__((common)); -> declarator
476 // short x, __attribute__((common)) var; -> declarator
477 if (Tok.is(tok::kw___attribute)) {
478 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000479 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000480 D.AddAttributes(AttrList, Loc);
481 }
482
483 ParseDeclarator(D);
484
John McCalld226f652010-08-21 09:40:31 +0000485 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000486 D.complete(ThisDecl);
John McCalld226f652010-08-21 09:40:31 +0000487 if (ThisDecl)
John McCalld8ac0572009-11-03 19:26:08 +0000488 DeclsInGroup.push_back(ThisDecl);
489 }
490
491 if (DeclEnd)
492 *DeclEnd = Tok.getLocation();
493
494 if (Context != Declarator::ForContext &&
495 ExpectAndConsume(tok::semi,
496 Context == Declarator::FileContext
497 ? diag::err_invalid_token_after_toplevel_declarator
498 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +0000499 // Okay, there was no semicolon and one was expected. If we see a
500 // declaration specifier, just assume it was missing and continue parsing.
501 // Otherwise things are very confused and we skip to recover.
502 if (!isDeclarationSpecifier()) {
503 SkipUntil(tok::r_brace, true, true);
504 if (Tok.is(tok::semi))
505 ConsumeToken();
506 }
John McCalld8ac0572009-11-03 19:26:08 +0000507 }
508
Douglas Gregor23c94db2010-07-02 17:43:08 +0000509 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +0000510 DeclsInGroup.data(),
511 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000512}
513
Douglas Gregor1426e532009-05-12 21:31:51 +0000514/// \brief Parse 'declaration' after parsing 'declaration-specifiers
515/// declarator'. This method parses the remainder of the declaration
516/// (including any attributes or initializer, among other things) and
517/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000518///
Reid Spencer5f016e22007-07-11 17:01:13 +0000519/// init-declarator: [C99 6.7]
520/// declarator
521/// declarator '=' initializer
522/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
523/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000524/// [C++] declarator initializer[opt]
525///
526/// [C++] initializer:
527/// [C++] '=' initializer-clause
528/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000529/// [C++0x] '=' 'default' [TODO]
530/// [C++0x] '=' 'delete'
531///
532/// According to the standard grammar, =default and =delete are function
533/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000534///
John McCalld226f652010-08-21 09:40:31 +0000535Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +0000536 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000537 // If a simple-asm-expr is present, parse it.
538 if (Tok.is(tok::kw_asm)) {
539 SourceLocation Loc;
John McCall60d7b3a2010-08-24 06:29:42 +0000540 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
Douglas Gregor1426e532009-05-12 21:31:51 +0000541 if (AsmLabel.isInvalid()) {
542 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000543 return 0;
Douglas Gregor1426e532009-05-12 21:31:51 +0000544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Douglas Gregor1426e532009-05-12 21:31:51 +0000546 D.setAsmLabel(AsmLabel.release());
547 D.SetRangeEnd(Loc);
548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Douglas Gregor1426e532009-05-12 21:31:51 +0000550 // If attributes are present, parse them.
551 if (Tok.is(tok::kw___attribute)) {
552 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000553 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000554 D.AddAttributes(AttrList, Loc);
555 }
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Douglas Gregor1426e532009-05-12 21:31:51 +0000557 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +0000558 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000559 switch (TemplateInfo.Kind) {
560 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000561 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +0000562 break;
563
564 case ParsedTemplateInfo::Template:
565 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +0000566 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +0000567 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +0000568 TemplateInfo.TemplateParams->data(),
569 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000570 D);
571 break;
572
573 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +0000574 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +0000575 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000576 TemplateInfo.ExternLoc,
577 TemplateInfo.TemplateLoc,
578 D);
579 if (ThisRes.isInvalid()) {
580 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +0000581 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +0000582 }
583
584 ThisDecl = ThisRes.get();
585 break;
586 }
587 }
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Douglas Gregor1426e532009-05-12 21:31:51 +0000589 // Parse declarator '=' initializer.
590 if (Tok.is(tok::equal)) {
591 ConsumeToken();
592 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
593 SourceLocation DelLoc = ConsumeToken();
594 Actions.SetDeclDeleted(ThisDecl, DelLoc);
595 } else {
John McCall731ad842009-12-19 09:28:58 +0000596 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
597 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000598 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000599 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000600
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000601 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000602 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +0000603 ConsumeCodeCompletionToken();
604 SkipUntil(tok::comma, true, true);
605 return ThisDecl;
606 }
607
John McCall60d7b3a2010-08-24 06:29:42 +0000608 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000609
John McCall731ad842009-12-19 09:28:58 +0000610 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000611 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000612 ExitScope();
613 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000614
Douglas Gregor1426e532009-05-12 21:31:51 +0000615 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000616 SkipUntil(tok::comma, true, true);
617 Actions.ActOnInitializerError(ThisDecl);
618 } else
John McCall9ae2f072010-08-23 23:25:46 +0000619 Actions.AddInitializerToDecl(ThisDecl, Init.take());
Douglas Gregor1426e532009-05-12 21:31:51 +0000620 }
621 } else if (Tok.is(tok::l_paren)) {
622 // Parse C++ direct initializer: '(' expression-list ')'
623 SourceLocation LParenLoc = ConsumeParen();
624 ExprVector Exprs(Actions);
625 CommaLocsTy CommaLocs;
626
Douglas Gregorb4debae2009-12-22 17:47:17 +0000627 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
628 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +0000629 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000630 }
631
Douglas Gregor1426e532009-05-12 21:31:51 +0000632 if (ParseExpressionList(Exprs, CommaLocs)) {
633 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000634
635 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000636 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000637 ExitScope();
638 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000639 } else {
640 // Match the ')'.
641 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
642
643 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
644 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000645
646 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +0000647 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000648 ExitScope();
649 }
650
Douglas Gregor1426e532009-05-12 21:31:51 +0000651 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
652 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000653 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000654 }
655 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000656 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000657 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
658 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000659 }
660
661 return ThisDecl;
662}
663
Reid Spencer5f016e22007-07-11 17:01:13 +0000664/// ParseSpecifierQualifierList
665/// specifier-qualifier-list:
666/// type-specifier specifier-qualifier-list[opt]
667/// type-qualifier specifier-qualifier-list[opt]
668/// [GNU] attributes specifier-qualifier-list[opt]
669///
670void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
671 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
672 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000673 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 // Validate declspec for type-name.
676 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000677 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
678 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 // Issue diagnostic and remove storage class if present.
682 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
683 if (DS.getStorageClassSpecLoc().isValid())
684 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
685 else
686 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
687 DS.ClearStorageClassSpecs();
688 }
Mike Stump1eb44332009-09-09 15:08:12 +0000689
Reid Spencer5f016e22007-07-11 17:01:13 +0000690 // Issue diagnostic and remove function specfier if present.
691 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000692 if (DS.isInlineSpecified())
693 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
694 if (DS.isVirtualSpecified())
695 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
696 if (DS.isExplicitSpecified())
697 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000698 DS.ClearFunctionSpecs();
699 }
700}
701
Chris Lattnerc199ab32009-04-12 20:42:31 +0000702/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
703/// specified token is valid after the identifier in a declarator which
704/// immediately follows the declspec. For example, these things are valid:
705///
706/// int x [ 4]; // direct-declarator
707/// int x ( int y); // direct-declarator
708/// int(int x ) // direct-declarator
709/// int x ; // simple-declaration
710/// int x = 17; // init-declarator-list
711/// int x , y; // init-declarator-list
712/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000713/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000714/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000715///
716/// This is not, because 'x' does not immediately follow the declspec (though
717/// ')' happens to be valid anyway).
718/// int (x)
719///
720static bool isValidAfterIdentifierInDeclarator(const Token &T) {
721 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
722 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000723 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000724}
725
Chris Lattnere40c2952009-04-14 21:34:55 +0000726
727/// ParseImplicitInt - This method is called when we have an non-typename
728/// identifier in a declspec (which normally terminates the decl spec) when
729/// the declspec has no type specifier. In this case, the declspec is either
730/// malformed or is "implicit int" (in K&R and C89).
731///
732/// This method handles diagnosing this prettily and returns false if the
733/// declspec is done being processed. If it recovers and thinks there may be
734/// other pieces of declspec after it, it returns true.
735///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000736bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000737 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000738 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000739 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Chris Lattnere40c2952009-04-14 21:34:55 +0000741 SourceLocation Loc = Tok.getLocation();
742 // If we see an identifier that is not a type name, we normally would
743 // parse it as the identifer being declared. However, when a typename
744 // is typo'd or the definition is not included, this will incorrectly
745 // parse the typename as the identifier name and fall over misparsing
746 // later parts of the diagnostic.
747 //
748 // As such, we try to do some look-ahead in cases where this would
749 // otherwise be an "implicit-int" case to see if this is invalid. For
750 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
751 // an identifier with implicit int, we'd get a parse error because the
752 // next token is obviously invalid for a type. Parse these as a case
753 // with an invalid type specifier.
754 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Chris Lattnere40c2952009-04-14 21:34:55 +0000756 // Since we know that this either implicit int (which is rare) or an
757 // error, we'd do lookahead to try to do better recovery.
758 if (isValidAfterIdentifierInDeclarator(NextToken())) {
759 // If this token is valid for implicit int, e.g. "static x = 4", then
760 // we just avoid eating the identifier, so it will be parsed as the
761 // identifier in the declarator.
762 return false;
763 }
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Chris Lattnere40c2952009-04-14 21:34:55 +0000765 // Otherwise, if we don't consume this token, we are going to emit an
766 // error anyway. Try to recover from various common problems. Check
767 // to see if this was a reference to a tag name without a tag specified.
768 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000769 //
770 // C++ doesn't need this, and isTagName doesn't take SS.
771 if (SS == 0) {
772 const char *TagName = 0;
773 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Douglas Gregor23c94db2010-07-02 17:43:08 +0000775 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +0000776 default: break;
777 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
778 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
779 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
780 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattnerf4382f52009-04-14 22:17:06 +0000783 if (TagName) {
784 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000785 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000786 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Chris Lattnerf4382f52009-04-14 22:17:06 +0000788 // Parse this as a tag as if the missing tag were present.
789 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000790 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000791 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000792 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000793 return true;
794 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000795 }
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Douglas Gregora786fdb2009-10-13 23:27:22 +0000797 // This is almost certainly an invalid type name. Let the action emit a
798 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +0000799 ParsedType T;
Douglas Gregora786fdb2009-10-13 23:27:22 +0000800 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
Douglas Gregor23c94db2010-07-02 17:43:08 +0000801 getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +0000802 // The action emitted a diagnostic, so we don't have to.
803 if (T) {
804 // The action has suggested that the type T could be used. Set that as
805 // the type in the declaration specifiers, consume the would-be type
806 // name token, and we're done.
807 const char *PrevSpec;
808 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +0000809 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +0000810 DS.SetRangeEnd(Tok.getLocation());
811 ConsumeToken();
812
813 // There may be other declaration specifiers after this.
814 return true;
815 }
816
817 // Fall through; the action had no suggestion for us.
818 } else {
819 // The action did not emit a diagnostic, so emit one now.
820 SourceRange R;
821 if (SS) R = SS->getRange();
822 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Douglas Gregora786fdb2009-10-13 23:27:22 +0000825 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000826 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000827 unsigned DiagID;
828 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000829 DS.SetRangeEnd(Tok.getLocation());
830 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Chris Lattnere40c2952009-04-14 21:34:55 +0000832 // TODO: Could inject an invalid typedef decl in an enclosing scope to
833 // avoid rippling error messages on subsequent uses of the same type,
834 // could be useful if #include was forgotten.
835 return false;
836}
837
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000838/// \brief Determine the declaration specifier context from the declarator
839/// context.
840///
841/// \param Context the declarator context, which is one of the
842/// Declarator::TheContext enumerator values.
843Parser::DeclSpecContext
844Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
845 if (Context == Declarator::MemberContext)
846 return DSC_class;
847 if (Context == Declarator::FileContext)
848 return DSC_top_level;
849 return DSC_normal;
850}
851
Reid Spencer5f016e22007-07-11 17:01:13 +0000852/// ParseDeclarationSpecifiers
853/// declaration-specifiers: [C99 6.7]
854/// storage-class-specifier declaration-specifiers[opt]
855/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000856/// [C99] function-specifier declaration-specifiers[opt]
857/// [GNU] attributes declaration-specifiers[opt]
858///
859/// storage-class-specifier: [C99 6.7.1]
860/// 'typedef'
861/// 'extern'
862/// 'static'
863/// 'auto'
864/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000865/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000866/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000867/// function-specifier: [C99 6.7.4]
868/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000869/// [C++] 'virtual'
870/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000871/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000872/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000873
Reid Spencer5f016e22007-07-11 17:01:13 +0000874///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000875void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000876 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000877 AccessSpecifier AS,
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000878 DeclSpecContext DSContext) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000879 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000881 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000883 unsigned DiagID = 0;
884
Reid Spencer5f016e22007-07-11 17:01:13 +0000885 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000886
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000888 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000889 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 // If this is not a declaration specifier token, we're done reading decl
891 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000892 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000895 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +0000896 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000897 if (DS.hasTypeSpecifier()) {
898 bool AllowNonIdentifiers
899 = (getCurScope()->getFlags() & (Scope::ControlScope |
900 Scope::BlockScope |
901 Scope::TemplateParamScope |
902 Scope::FunctionPrototypeScope |
903 Scope::AtCatchScope)) == 0;
904 bool AllowNestedNameSpecifiers
905 = DSContext == DSC_top_level ||
906 (DSContext == DSC_class && DS.isFriendSpecified());
907
908 Actions.CodeCompleteDeclarator(getCurScope(), AllowNonIdentifiers,
909 AllowNestedNameSpecifiers);
910 ConsumeCodeCompletionToken();
911 return;
912 }
913
914 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +0000915 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
916 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000917 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +0000918 CCC = Sema::PCC_Class;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000919 else if (ObjCImpDecl)
John McCallf312b1e2010-08-26 23:41:50 +0000920 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +0000921
922 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
923 ConsumeCodeCompletionToken();
924 return;
925 }
926
Chris Lattner5e02c472009-01-05 00:07:25 +0000927 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000928 // C++ scope specifier. Annotate and loop, or bail out on error.
929 if (TryAnnotateCXXScopeToken(true)) {
930 if (!DS.hasTypeSpecifier())
931 DS.SetTypeSpecError();
932 goto DoneWithDeclSpec;
933 }
John McCall2e0a7152010-03-01 18:20:46 +0000934 if (Tok.is(tok::coloncolon)) // ::new or ::delete
935 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000936 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000937
938 case tok::annot_cxxscope: {
939 if (DS.hasTypeSpecifier())
940 goto DoneWithDeclSpec;
941
John McCallaa87d332009-12-12 11:40:51 +0000942 CXXScopeSpec SS;
John McCallca0408f2010-08-23 06:44:23 +0000943 SS.setScopeRep((NestedNameSpecifier*) Tok.getAnnotationValue());
John McCallaa87d332009-12-12 11:40:51 +0000944 SS.setRange(Tok.getAnnotationRange());
945
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000946 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000947 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000948 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000949 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000950 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000951 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000952
953 // C++ [class.qual]p2:
954 // In a lookup in which the constructor is an acceptable lookup
955 // result and the nested-name-specifier nominates a class C:
956 //
957 // - if the name specified after the
958 // nested-name-specifier, when looked up in C, is the
959 // injected-class-name of C (Clause 9), or
960 //
961 // - if the name specified after the nested-name-specifier
962 // is the same as the identifier or the
963 // simple-template-id's template-name in the last
964 // component of the nested-name-specifier,
965 //
966 // the name is instead considered to name the constructor of
967 // class C.
968 //
969 // Thus, if the template-name is actually the constructor
970 // name, then the code is ill-formed; this interpretation is
971 // reinforced by the NAD status of core issue 635.
972 TemplateIdAnnotation *TemplateId
973 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000974 if ((DSContext == DSC_top_level ||
975 (DSContext == DSC_class && DS.isFriendSpecified())) &&
976 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +0000977 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000978 if (isConstructorDeclarator()) {
979 // The user meant this to be an out-of-line constructor
980 // definition, but template arguments are not allowed
981 // there. Just allow this as a constructor; we'll
982 // complain about it later.
983 goto DoneWithDeclSpec;
984 }
985
986 // The user meant this to name a type, but it actually names
987 // a constructor with some extraneous template
988 // arguments. Complain, then parse it as a type as the user
989 // intended.
990 Diag(TemplateId->TemplateNameLoc,
991 diag::err_out_of_line_template_id_names_constructor)
992 << TemplateId->Name;
993 }
994
John McCallaa87d332009-12-12 11:40:51 +0000995 DS.getTypeSpecScope() = SS;
996 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +0000997 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000998 "ParseOptionalCXXScopeSpecifier not working");
999 AnnotateTemplateIdTokenAsType(&SS);
1000 continue;
1001 }
1002
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001003 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00001004 DS.getTypeSpecScope() = SS;
1005 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00001006 if (Tok.getAnnotationValue()) {
1007 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001008 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
John McCallb3d87482010-08-24 05:47:05 +00001009 PrevSpec, DiagID, T);
1010 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00001011 else
1012 DS.SetTypeSpecError();
1013 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1014 ConsumeToken(); // The typename
1015 }
1016
Douglas Gregor9135c722009-03-25 15:40:00 +00001017 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001018 goto DoneWithDeclSpec;
1019
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001020 // If we're in a context where the identifier could be a class name,
1021 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00001022 if ((DSContext == DSC_top_level ||
1023 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001024 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001025 &SS)) {
1026 if (isConstructorDeclarator())
1027 goto DoneWithDeclSpec;
1028
1029 // As noted in C++ [class.qual]p2 (cited above), when the name
1030 // of the class is qualified in a context where it could name
1031 // a constructor, its a constructor name. However, we've
1032 // looked at the declarator, and the user probably meant this
1033 // to be a type. Complain that it isn't supposed to be treated
1034 // as a type, then proceed to parse it as a type.
1035 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
1036 << Next.getIdentifierInfo();
1037 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001038
John McCallb3d87482010-08-24 05:47:05 +00001039 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
1040 Next.getLocation(),
1041 getCurScope(), &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001042
Chris Lattnerf4382f52009-04-14 22:17:06 +00001043 // If the referenced identifier is not a type, then this declspec is
1044 // erroneous: We already checked about that it has no type specifier, and
1045 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00001046 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00001047 if (TypeRep == 0) {
1048 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001049 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001050 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001051 }
Mike Stump1eb44332009-09-09 15:08:12 +00001052
John McCallaa87d332009-12-12 11:40:51 +00001053 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001054 ConsumeToken(); // The C++ scope.
1055
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001056 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001057 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001058 if (isInvalid)
1059 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001061 DS.SetRangeEnd(Tok.getLocation());
1062 ConsumeToken(); // The typename.
1063
1064 continue;
1065 }
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Chris Lattner80d0c892009-01-21 19:48:37 +00001067 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001068 if (Tok.getAnnotationValue()) {
1069 ParsedType T = getTypeAnnotation(Tok);
Douglas Gregor31a19b62009-04-01 21:51:26 +00001070 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001071 DiagID, T);
1072 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001073 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001074
1075 if (isInvalid)
1076 break;
1077
Chris Lattner80d0c892009-01-21 19:48:37 +00001078 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1079 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Chris Lattner80d0c892009-01-21 19:48:37 +00001081 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1082 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1083 // Objective-C interface. If we don't have Objective-C or a '<', this is
1084 // just a normal reference to a typedef name.
1085 if (!Tok.is(tok::less) || !getLang().ObjC1)
1086 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001087
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001088 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001089 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001090 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1091 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1092 LAngleLoc, EndProtoLoc);
1093 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1094 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Chris Lattner80d0c892009-01-21 19:48:37 +00001096 DS.SetRangeEnd(EndProtoLoc);
1097 continue;
1098 }
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Chris Lattner3bd934a2008-07-26 01:18:38 +00001100 // typedef-name
1101 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001102 // In C++, check to see if this is a scope specifier like foo::bar::, if
1103 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001104 if (getLang().CPlusPlus) {
1105 if (TryAnnotateCXXScopeToken(true)) {
1106 if (!DS.hasTypeSpecifier())
1107 DS.SetTypeSpecError();
1108 goto DoneWithDeclSpec;
1109 }
1110 if (!Tok.is(tok::identifier))
1111 continue;
1112 }
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Chris Lattner3bd934a2008-07-26 01:18:38 +00001114 // This identifier can only be a typedef name if we haven't already seen
1115 // a type-specifier. Without this check we misparse:
1116 // typedef int X; struct Y { short X; }; as 'short int'.
1117 if (DS.hasTypeSpecifier())
1118 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001119
John Thompson82287d12010-02-05 00:12:22 +00001120 // Check for need to substitute AltiVec keyword tokens.
1121 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1122 break;
1123
Chris Lattner3bd934a2008-07-26 01:18:38 +00001124 // It has to be available as a typedef too!
John McCallb3d87482010-08-24 05:47:05 +00001125 ParsedType TypeRep =
1126 Actions.getTypeName(*Tok.getIdentifierInfo(),
1127 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00001128
Chris Lattnerc199ab32009-04-12 20:42:31 +00001129 // If this is not a typedef name, don't parse it as part of the declspec,
1130 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00001131 if (!TypeRep) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001132 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001133 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001134 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001135
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001136 // If we're in a context where the identifier could be a class name,
1137 // check whether this is a constructor declaration.
1138 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001139 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001140 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001141 goto DoneWithDeclSpec;
1142
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001143 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001144 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001145 if (isInvalid)
1146 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Chris Lattner3bd934a2008-07-26 01:18:38 +00001148 DS.SetRangeEnd(Tok.getLocation());
1149 ConsumeToken(); // The identifier
1150
1151 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1152 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1153 // Objective-C interface. If we don't have Objective-C or a '<', this is
1154 // just a normal reference to a typedef name.
1155 if (!Tok.is(tok::less) || !getLang().ObjC1)
1156 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001158 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001159 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001160 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1161 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1162 LAngleLoc, EndProtoLoc);
1163 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1164 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Chris Lattner3bd934a2008-07-26 01:18:38 +00001166 DS.SetRangeEnd(EndProtoLoc);
1167
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001168 // Need to support trailing type qualifiers (e.g. "id<p> const").
1169 // If a type specifier follows, it will be diagnosed elsewhere.
1170 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001171 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001172
1173 // type-name
1174 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001175 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001176 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001177 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001178 // This template-id does not refer to a type name, so we're
1179 // done with the type-specifiers.
1180 goto DoneWithDeclSpec;
1181 }
1182
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001183 // If we're in a context where the template-id could be a
1184 // constructor name or specialization, check whether this is a
1185 // constructor declaration.
1186 if (getLang().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00001187 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001188 isConstructorDeclarator())
1189 goto DoneWithDeclSpec;
1190
Douglas Gregor39a8de12009-02-25 19:37:18 +00001191 // Turn the template-id annotation token into a type annotation
1192 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001193 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001194 continue;
1195 }
1196
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 // GNU attributes support.
1198 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001199 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001201
1202 // Microsoft declspec support.
1203 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001204 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001205 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001206
Steve Naroff239f0732008-12-25 14:16:32 +00001207 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001208 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001209 // FIXME: Add handling here!
1210 break;
1211
1212 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001213 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001214 case tok::kw___cdecl:
1215 case tok::kw___stdcall:
1216 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001217 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001218 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1219 continue;
1220
Dawn Perchik52fc3142010-09-03 01:29:35 +00001221 // Borland single token adornments.
1222 case tok::kw___pascal:
1223 DS.AddAttributes(ParseBorlandTypeAttributes());
1224 continue;
1225
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 // storage-class-specifier
1227 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001228 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1229 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 break;
1231 case tok::kw_extern:
1232 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001233 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001234 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1235 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001237 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001238 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001239 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001240 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 case tok::kw_static:
1242 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001243 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001244 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1245 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 break;
1247 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001248 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001249 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1250 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001251 else
John McCallfec54012009-08-03 20:12:06 +00001252 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1253 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 break;
1255 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001256 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1257 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001259 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001260 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1261 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001262 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001264 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 // function-specifier
1268 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001269 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001271 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001272 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001273 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001274 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001275 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001276 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001277
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001278 // friend
1279 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001280 if (DSContext == DSC_class)
1281 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1282 else {
1283 PrevSpec = ""; // not actually used by the diagnostic
1284 DiagID = diag::err_friend_invalid_in_context;
1285 isInvalid = true;
1286 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001287 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Sebastian Redl2ac67232009-11-05 15:47:02 +00001289 // constexpr
1290 case tok::kw_constexpr:
1291 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1292 break;
1293
Chris Lattner80d0c892009-01-21 19:48:37 +00001294 // type-specifier
1295 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001296 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1297 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001298 break;
1299 case tok::kw_long:
1300 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001301 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1302 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001303 else
John McCallfec54012009-08-03 20:12:06 +00001304 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1305 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001306 break;
1307 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001308 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1309 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001310 break;
1311 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001312 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1313 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001314 break;
1315 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001316 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1317 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001318 break;
1319 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001320 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1321 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001322 break;
1323 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001324 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1325 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001326 break;
1327 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001328 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1329 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001330 break;
1331 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1333 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001334 break;
1335 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001336 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1337 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001338 break;
1339 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001340 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1341 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001342 break;
1343 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001344 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1345 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001346 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001347 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001348 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1349 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001350 break;
1351 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001352 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1353 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001354 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001355 case tok::kw_bool:
1356 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001357 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1358 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001359 break;
1360 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001361 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1362 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001363 break;
1364 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001365 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1366 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001367 break;
1368 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001369 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1370 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001371 break;
John Thompson82287d12010-02-05 00:12:22 +00001372 case tok::kw___vector:
1373 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1374 break;
1375 case tok::kw___pixel:
1376 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1377 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001378
1379 // class-specifier:
1380 case tok::kw_class:
1381 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001382 case tok::kw_union: {
1383 tok::TokenKind Kind = Tok.getKind();
1384 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001385 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001386 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001387 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001388
1389 // enum-specifier:
1390 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001391 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001392 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001393 continue;
1394
1395 // cv-qualifier:
1396 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001397 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1398 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001399 break;
1400 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001401 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1402 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001403 break;
1404 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001405 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1406 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001407 break;
1408
Douglas Gregord57959a2009-03-27 23:10:48 +00001409 // C++ typename-specifier:
1410 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001411 if (TryAnnotateTypeOrScopeToken()) {
1412 DS.SetTypeSpecError();
1413 goto DoneWithDeclSpec;
1414 }
1415 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001416 continue;
1417 break;
1418
Chris Lattner80d0c892009-01-21 19:48:37 +00001419 // GNU typeof support.
1420 case tok::kw_typeof:
1421 ParseTypeofSpecifier(DS);
1422 continue;
1423
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001424 case tok::kw_decltype:
1425 ParseDecltypeSpecifier(DS);
1426 continue;
1427
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001428 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001429 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001430 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1431 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001432 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001433 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Chris Lattnerbce61352008-07-26 00:20:22 +00001435 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001436 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001437 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001438 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1439 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1440 LAngleLoc, EndProtoLoc);
1441 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1442 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001443 DS.SetRangeEnd(EndProtoLoc);
1444
Chris Lattner1ab3b962008-11-18 07:48:38 +00001445 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregor849b2432010-03-31 17:46:05 +00001446 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001447 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001448 // Need to support trailing type qualifiers (e.g. "id<p> const").
1449 // If a type specifier follows, it will be diagnosed elsewhere.
1450 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001451 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001452 }
John McCallfec54012009-08-03 20:12:06 +00001453 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001454 if (isInvalid) {
1455 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001456 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00001457
1458 if (DiagID == diag::ext_duplicate_declspec)
1459 Diag(Tok, DiagID)
1460 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
1461 else
1462 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001463 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001464 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001465 ConsumeToken();
1466 }
1467}
Douglas Gregoradcac882008-12-01 23:54:00 +00001468
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001469/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001470/// primarily follow the C++ grammar with additions for C99 and GNU,
1471/// which together subsume the C grammar. Note that the C++
1472/// type-specifier also includes the C type-qualifier (for const,
1473/// volatile, and C99 restrict). Returns true if a type-specifier was
1474/// found (and parsed), false otherwise.
1475///
1476/// type-specifier: [C++ 7.1.5]
1477/// simple-type-specifier
1478/// class-specifier
1479/// enum-specifier
1480/// elaborated-type-specifier [TODO]
1481/// cv-qualifier
1482///
1483/// cv-qualifier: [C++ 7.1.5.1]
1484/// 'const'
1485/// 'volatile'
1486/// [C99] 'restrict'
1487///
1488/// simple-type-specifier: [ C++ 7.1.5.2]
1489/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1490/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1491/// 'char'
1492/// 'wchar_t'
1493/// 'bool'
1494/// 'short'
1495/// 'int'
1496/// 'long'
1497/// 'signed'
1498/// 'unsigned'
1499/// 'float'
1500/// 'double'
1501/// 'void'
1502/// [C99] '_Bool'
1503/// [C99] '_Complex'
1504/// [C99] '_Imaginary' // Removed in TC2?
1505/// [GNU] '_Decimal32'
1506/// [GNU] '_Decimal64'
1507/// [GNU] '_Decimal128'
1508/// [GNU] typeof-specifier
1509/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1510/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001511/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001512/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001513bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001514 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001515 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001516 const ParsedTemplateInfo &TemplateInfo,
1517 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001518 SourceLocation Loc = Tok.getLocation();
1519
1520 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001521 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001522 // If we already have a type specifier, this identifier is not a type.
1523 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1524 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1525 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1526 return false;
John Thompson82287d12010-02-05 00:12:22 +00001527 // Check for need to substitute AltiVec keyword tokens.
1528 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1529 break;
1530 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001531 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001532 // Annotate typenames and C++ scope specifiers. If we get one, just
1533 // recurse to handle whatever we get.
1534 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001535 return true;
1536 if (Tok.is(tok::identifier))
1537 return false;
1538 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1539 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001540 case tok::coloncolon: // ::foo::bar
1541 if (NextToken().is(tok::kw_new) || // ::new
1542 NextToken().is(tok::kw_delete)) // ::delete
1543 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Chris Lattner166a8fc2009-01-04 23:41:41 +00001545 // Annotate typenames and C++ scope specifiers. If we get one, just
1546 // recurse to handle whatever we get.
1547 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001548 return true;
1549 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1550 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Douglas Gregor12e083c2008-11-07 15:42:26 +00001552 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001553 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00001554 if (ParsedType T = getTypeAnnotation(Tok)) {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001555 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00001556 DiagID, T);
1557 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00001558 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001559 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1560 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Douglas Gregor12e083c2008-11-07 15:42:26 +00001562 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1563 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1564 // Objective-C interface. If we don't have Objective-C or a '<', this is
1565 // just a normal reference to a typedef name.
1566 if (!Tok.is(tok::less) || !getLang().ObjC1)
1567 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001569 SourceLocation LAngleLoc, EndProtoLoc;
John McCalld226f652010-08-21 09:40:31 +00001570 llvm::SmallVector<Decl *, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001571 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1572 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1573 LAngleLoc, EndProtoLoc);
1574 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1575 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Douglas Gregor12e083c2008-11-07 15:42:26 +00001577 DS.SetRangeEnd(EndProtoLoc);
1578 return true;
1579 }
1580
1581 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001582 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001583 break;
1584 case tok::kw_long:
1585 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001586 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1587 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001588 else
John McCallfec54012009-08-03 20:12:06 +00001589 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1590 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001591 break;
1592 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001593 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001594 break;
1595 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001596 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1597 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001598 break;
1599 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001600 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1601 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001602 break;
1603 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001604 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1605 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001606 break;
1607 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001608 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001609 break;
1610 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001611 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001612 break;
1613 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001614 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001615 break;
1616 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001617 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001618 break;
1619 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001620 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001621 break;
1622 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001623 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001624 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001625 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001626 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001627 break;
1628 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001629 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001630 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001631 case tok::kw_bool:
1632 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001633 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001634 break;
1635 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001636 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1637 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001638 break;
1639 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001640 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1641 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001642 break;
1643 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001644 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1645 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001646 break;
John Thompson82287d12010-02-05 00:12:22 +00001647 case tok::kw___vector:
1648 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1649 break;
1650 case tok::kw___pixel:
1651 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1652 break;
1653
Douglas Gregor12e083c2008-11-07 15:42:26 +00001654 // class-specifier:
1655 case tok::kw_class:
1656 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001657 case tok::kw_union: {
1658 tok::TokenKind Kind = Tok.getKind();
1659 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001660 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1661 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001662 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001663 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001664
1665 // enum-specifier:
1666 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001667 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001668 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001669 return true;
1670
1671 // cv-qualifier:
1672 case tok::kw_const:
1673 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001674 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001675 break;
1676 case tok::kw_volatile:
1677 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001678 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001679 break;
1680 case tok::kw_restrict:
1681 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001682 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001683 break;
1684
1685 // GNU typeof support.
1686 case tok::kw_typeof:
1687 ParseTypeofSpecifier(DS);
1688 return true;
1689
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001690 // C++0x decltype support.
1691 case tok::kw_decltype:
1692 ParseDecltypeSpecifier(DS);
1693 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001695 // C++0x auto support.
1696 case tok::kw_auto:
1697 if (!getLang().CPlusPlus0x)
1698 return false;
1699
John McCallfec54012009-08-03 20:12:06 +00001700 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001701 break;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001702
Eli Friedman290eeb02009-06-08 23:27:34 +00001703 case tok::kw___ptr64:
1704 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001705 case tok::kw___cdecl:
1706 case tok::kw___stdcall:
1707 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001708 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001709 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001710 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001711
Dawn Perchik52fc3142010-09-03 01:29:35 +00001712 case tok::kw___pascal:
1713 DS.AddAttributes(ParseBorlandTypeAttributes());
1714 return true;
1715
Douglas Gregor12e083c2008-11-07 15:42:26 +00001716 default:
1717 // Not a type-specifier; do nothing.
1718 return false;
1719 }
1720
1721 // If the specifier combination wasn't legal, issue a diagnostic.
1722 if (isInvalid) {
1723 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001724 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001725 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001726 }
1727 DS.SetRangeEnd(Tok.getLocation());
1728 ConsumeToken(); // whatever we parsed above.
1729 return true;
1730}
Reid Spencer5f016e22007-07-11 17:01:13 +00001731
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001732/// ParseStructDeclaration - Parse a struct declaration without the terminating
1733/// semicolon.
1734///
Reid Spencer5f016e22007-07-11 17:01:13 +00001735/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001736/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001737/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001738/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001739/// struct-declarator-list:
1740/// struct-declarator
1741/// struct-declarator-list ',' struct-declarator
1742/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1743/// struct-declarator:
1744/// declarator
1745/// [GNU] declarator attributes[opt]
1746/// declarator[opt] ':' constant-expression
1747/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1748///
Chris Lattnere1359422008-04-10 06:46:29 +00001749void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001750ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001751 if (Tok.is(tok::kw___extension__)) {
1752 // __extension__ silences extension warnings in the subexpression.
1753 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001754 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001755 return ParseStructDeclaration(DS, Fields);
1756 }
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Steve Naroff28a7ca82007-08-20 22:28:22 +00001758 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001759 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001760 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001762 // If there are no declarators, this is a free-standing declaration
1763 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001764 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001765 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001766 return;
1767 }
1768
1769 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001770 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001771 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001772 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001773 FieldDeclarator DeclaratorInfo(DS);
1774
1775 // Attributes are only allowed here on successive declarators.
1776 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1777 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001778 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001779 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1780 }
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Steve Naroff28a7ca82007-08-20 22:28:22 +00001782 /// struct-declarator: declarator
1783 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001784 if (Tok.isNot(tok::colon)) {
1785 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1786 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001787 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001788 }
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Chris Lattner04d66662007-10-09 17:33:22 +00001790 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001791 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00001792 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001793 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001794 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001795 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001796 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001797 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001798
Steve Naroff28a7ca82007-08-20 22:28:22 +00001799 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001800 if (Tok.is(tok::kw___attribute)) {
1801 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001802 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001803 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1804 }
1805
John McCallbdd563e2009-11-03 02:38:08 +00001806 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00001807 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00001808 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001809
Steve Naroff28a7ca82007-08-20 22:28:22 +00001810 // If we don't have a comma, it is either the end of the list (a ';')
1811 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001812 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001813 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001814
Steve Naroff28a7ca82007-08-20 22:28:22 +00001815 // Consume the comma.
1816 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001817
John McCallbdd563e2009-11-03 02:38:08 +00001818 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001819 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001820}
1821
1822/// ParseStructUnionBody
1823/// struct-contents:
1824/// struct-declaration-list
1825/// [EXT] empty
1826/// [GNU] "struct-declaration-list" without terminatoring ';'
1827/// struct-declaration-list:
1828/// struct-declaration
1829/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001830/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001831///
Reid Spencer5f016e22007-07-11 17:01:13 +00001832void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00001833 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00001834 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
1835 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Reid Spencer5f016e22007-07-11 17:01:13 +00001837 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001839 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001840 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00001841
Reid Spencer5f016e22007-07-11 17:01:13 +00001842 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1843 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001844 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Douglas Gregor03332962010-07-29 14:29:34 +00001845 Diag(Tok, diag::ext_empty_struct_union)
1846 << (TagType == TST_union);
Reid Spencer5f016e22007-07-11 17:01:13 +00001847
John McCalld226f652010-08-21 09:40:31 +00001848 llvm::SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001849
Reid Spencer5f016e22007-07-11 17:01:13 +00001850 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001851 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001852 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Reid Spencer5f016e22007-07-11 17:01:13 +00001854 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001855 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001856 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregorf13ca062010-06-16 23:08:59 +00001857 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType)
Douglas Gregor849b2432010-03-31 17:46:05 +00001858 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001859 ConsumeToken();
1860 continue;
1861 }
Chris Lattnere1359422008-04-10 06:46:29 +00001862
1863 // Parse all the comma separated declarators.
1864 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001865
John McCallbdd563e2009-11-03 02:38:08 +00001866 if (!Tok.is(tok::at)) {
1867 struct CFieldCallback : FieldCallback {
1868 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00001869 Decl *TagDecl;
1870 llvm::SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00001871
John McCalld226f652010-08-21 09:40:31 +00001872 CFieldCallback(Parser &P, Decl *TagDecl,
1873 llvm::SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00001874 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1875
John McCalld226f652010-08-21 09:40:31 +00001876 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001877 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00001878 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00001879 FD.D.getDeclSpec().getSourceRange().getBegin(),
1880 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001881 FieldDecls.push_back(Field);
1882 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001883 }
John McCallbdd563e2009-11-03 02:38:08 +00001884 } Callback(*this, TagDecl, FieldDecls);
1885
1886 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001887 } else { // Handle @defs
1888 ConsumeToken();
1889 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1890 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001891 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001892 continue;
1893 }
1894 ConsumeToken();
1895 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1896 if (!Tok.is(tok::identifier)) {
1897 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001898 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001899 continue;
1900 }
John McCalld226f652010-08-21 09:40:31 +00001901 llvm::SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00001902 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001903 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001904 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1905 ConsumeToken();
1906 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001907 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001908
Chris Lattner04d66662007-10-09 17:33:22 +00001909 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001910 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001911 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001912 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001913 break;
1914 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001915 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1916 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001917 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001918 // If we stopped at a ';', eat it.
1919 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001920 }
1921 }
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Steve Naroff60fccee2007-10-29 21:38:07 +00001923 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001924
Ted Kremenek1e377652010-02-11 02:19:13 +00001925 llvm::OwningPtr<AttributeList> AttrList;
Reid Spencer5f016e22007-07-11 17:01:13 +00001926 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001927 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001928 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001929
Douglas Gregor23c94db2010-07-02 17:43:08 +00001930 Actions.ActOnFields(getCurScope(),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001931 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001932 LBraceLoc, RBraceLoc,
Ted Kremenek1e377652010-02-11 02:19:13 +00001933 AttrList.get());
Douglas Gregor72de6672009-01-08 20:45:30 +00001934 StructScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00001935 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001936}
1937
1938
1939/// ParseEnumSpecifier
1940/// enum-specifier: [C99 6.7.2.2]
1941/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001942///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001943/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1944/// '}' attributes[opt]
1945/// 'enum' identifier
1946/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001947///
1948/// [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 Kremenek1e377652010-02-11 02:19:13 +00001961 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001962 // If attributes exist after tag, parse them.
1963 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001964 Attr.reset(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
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001982 // Must have either 'enum name' or 'enum {...}'.
1983 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1984 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001985
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001986 // Skip the rest of this declarator, up until the comma or semicolon.
1987 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001988 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001989 }
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001991 // If an identifier is present, consume and remember it.
1992 IdentifierInfo *Name = 0;
1993 SourceLocation NameLoc;
1994 if (Tok.is(tok::identifier)) {
1995 Name = Tok.getIdentifierInfo();
1996 NameLoc = ConsumeToken();
1997 }
Mike Stump1eb44332009-09-09 15:08:12 +00001998
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001999 // There are three options here. If we have 'enum foo;', then this is a
2000 // forward declaration. If we have 'enum foo {...' then this is a
2001 // definition. Otherwise we have something like 'enum foo xyz', a reference.
2002 //
2003 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
2004 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
2005 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
2006 //
John McCallf312b1e2010-08-26 23:41:50 +00002007 Sema::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002008 if (Tok.is(tok::l_brace))
John McCallf312b1e2010-08-26 23:41:50 +00002009 TUK = Sema::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002010 else if (Tok.is(tok::semi))
John McCallf312b1e2010-08-26 23:41:50 +00002011 TUK = Sema::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002012 else
John McCallf312b1e2010-08-26 23:41:50 +00002013 TUK = Sema::TUK_Reference;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002014
2015 // enums cannot be templates, although they can be referenced from a
2016 // template.
2017 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00002018 TUK != Sema::TUK_Reference) {
Douglas Gregor8fc6d232010-05-03 17:48:54 +00002019 Diag(Tok, diag::err_enum_template);
2020
2021 // Skip the rest of this declarator, up until the comma or semicolon.
2022 SkipUntil(tok::comma, true);
2023 return;
2024 }
2025
Douglas Gregor402abb52009-05-28 23:31:59 +00002026 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00002027 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00002028 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
2029 const char *PrevSpec = 0;
2030 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00002031 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
2032 StartLoc, SS, Name, NameLoc, Attr.get(),
2033 AS,
John McCallf312b1e2010-08-26 23:41:50 +00002034 MultiTemplateParamsArg(Actions),
John McCalld226f652010-08-21 09:40:31 +00002035 Owned, IsDependent);
Douglas Gregor48c89f42010-04-24 16:38:41 +00002036 if (IsDependent) {
2037 // This enum has a dependent nested-name-specifier. Handle it as a
2038 // dependent tag.
2039 if (!Name) {
2040 DS.SetTypeSpecError();
2041 Diag(Tok, diag::err_expected_type_name_after_typename);
2042 return;
2043 }
2044
Douglas Gregor23c94db2010-07-02 17:43:08 +00002045 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00002046 TUK, SS, Name, StartLoc,
2047 NameLoc);
2048 if (Type.isInvalid()) {
2049 DS.SetTypeSpecError();
2050 return;
2051 }
2052
2053 if (DS.SetTypeSpecType(DeclSpec::TST_typename, TSTLoc, PrevSpec, DiagID,
John McCallb3d87482010-08-24 05:47:05 +00002054 Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00002055 Diag(StartLoc, DiagID) << PrevSpec;
2056
2057 return;
2058 }
Mike Stump1eb44332009-09-09 15:08:12 +00002059
John McCalld226f652010-08-21 09:40:31 +00002060 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00002061 // The action failed to produce an enumeration tag. If this is a
2062 // definition, consume the entire definition.
2063 if (Tok.is(tok::l_brace)) {
2064 ConsumeBrace();
2065 SkipUntil(tok::r_brace);
2066 }
2067
2068 DS.SetTypeSpecError();
2069 return;
2070 }
2071
Chris Lattner04d66662007-10-09 17:33:22 +00002072 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00002073 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00002074
John McCallb3d87482010-08-24 05:47:05 +00002075 // FIXME: The DeclSpec should keep the locations of both the keyword
2076 // and the name (if there is one).
Douglas Gregorb988f9c2010-01-25 16:33:23 +00002077 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
John McCalld226f652010-08-21 09:40:31 +00002078 TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00002079 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002080}
2081
2082/// ParseEnumBody - Parse a {} enclosed enumerator-list.
2083/// enumerator-list:
2084/// enumerator
2085/// enumerator-list ',' enumerator
2086/// enumerator:
2087/// enumeration-constant
2088/// enumeration-constant '=' constant-expression
2089/// enumeration-constant:
2090/// identifier
2091///
John McCalld226f652010-08-21 09:40:31 +00002092void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00002093 // Enter the scope of the enum body and start the definition.
2094 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002095 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00002096
Reid Spencer5f016e22007-07-11 17:01:13 +00002097 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00002098
Chris Lattner7946dd32007-08-27 17:24:30 +00002099 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00002100 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00002101 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00002102
John McCalld226f652010-08-21 09:40:31 +00002103 llvm::SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00002104
John McCalld226f652010-08-21 09:40:31 +00002105 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002106
Reid Spencer5f016e22007-07-11 17:01:13 +00002107 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002108 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002109 IdentifierInfo *Ident = Tok.getIdentifierInfo();
2110 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002111
Reid Spencer5f016e22007-07-11 17:01:13 +00002112 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00002113 ExprResult AssignedVal;
Chris Lattner04d66662007-10-09 17:33:22 +00002114 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002115 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002116 AssignedVal = ParseConstantExpression();
2117 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00002118 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002119 }
Mike Stump1eb44332009-09-09 15:08:12 +00002120
Reid Spencer5f016e22007-07-11 17:01:13 +00002121 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00002122 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
2123 LastEnumConstDecl,
2124 IdentLoc, Ident,
2125 EqualLoc,
2126 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002127 EnumConstantDecls.push_back(EnumConstDecl);
2128 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002129
Chris Lattner04d66662007-10-09 17:33:22 +00002130 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002131 break;
2132 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002133
2134 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002135 !(getLang().C99 || getLang().CPlusPlus0x))
2136 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2137 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002138 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002139 }
Mike Stump1eb44332009-09-09 15:08:12 +00002140
Reid Spencer5f016e22007-07-11 17:01:13 +00002141 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002142 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002143
Ted Kremenek1e377652010-02-11 02:19:13 +00002144 llvm::OwningPtr<AttributeList> Attr;
Reid Spencer5f016e22007-07-11 17:01:13 +00002145 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00002146 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00002147 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00002148
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002149 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2150 EnumConstantDecls.data(), EnumConstantDecls.size(),
Douglas Gregor23c94db2010-07-02 17:43:08 +00002151 getCurScope(), Attr.get());
Mike Stump1eb44332009-09-09 15:08:12 +00002152
Douglas Gregor72de6672009-01-08 20:45:30 +00002153 EnumScope.Exit();
Douglas Gregor23c94db2010-07-02 17:43:08 +00002154 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002155}
2156
2157/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002158/// start of a type-qualifier-list.
2159bool Parser::isTypeQualifier() const {
2160 switch (Tok.getKind()) {
2161 default: return false;
2162 // type-qualifier
2163 case tok::kw_const:
2164 case tok::kw_volatile:
2165 case tok::kw_restrict:
2166 return true;
2167 }
2168}
2169
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002170/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2171/// is definitely a type-specifier. Return false if it isn't part of a type
2172/// specifier or if we're not sure.
2173bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2174 switch (Tok.getKind()) {
2175 default: return false;
2176 // type-specifiers
2177 case tok::kw_short:
2178 case tok::kw_long:
2179 case tok::kw_signed:
2180 case tok::kw_unsigned:
2181 case tok::kw__Complex:
2182 case tok::kw__Imaginary:
2183 case tok::kw_void:
2184 case tok::kw_char:
2185 case tok::kw_wchar_t:
2186 case tok::kw_char16_t:
2187 case tok::kw_char32_t:
2188 case tok::kw_int:
2189 case tok::kw_float:
2190 case tok::kw_double:
2191 case tok::kw_bool:
2192 case tok::kw__Bool:
2193 case tok::kw__Decimal32:
2194 case tok::kw__Decimal64:
2195 case tok::kw__Decimal128:
2196 case tok::kw___vector:
2197
2198 // struct-or-union-specifier (C99) or class-specifier (C++)
2199 case tok::kw_class:
2200 case tok::kw_struct:
2201 case tok::kw_union:
2202 // enum-specifier
2203 case tok::kw_enum:
2204
2205 // typedef-name
2206 case tok::annot_typename:
2207 return true;
2208 }
2209}
2210
Steve Naroff5f8aa692008-02-11 23:15:56 +00002211/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002212/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002213bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002214 switch (Tok.getKind()) {
2215 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002216
Chris Lattner166a8fc2009-01-04 23:41:41 +00002217 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002218 if (TryAltiVecVectorToken())
2219 return true;
2220 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002221 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002222 // Annotate typenames and C++ scope specifiers. If we get one, just
2223 // recurse to handle whatever we get.
2224 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002225 return true;
2226 if (Tok.is(tok::identifier))
2227 return false;
2228 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002229
Chris Lattner166a8fc2009-01-04 23:41:41 +00002230 case tok::coloncolon: // ::foo::bar
2231 if (NextToken().is(tok::kw_new) || // ::new
2232 NextToken().is(tok::kw_delete)) // ::delete
2233 return false;
2234
Chris Lattner166a8fc2009-01-04 23:41:41 +00002235 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002236 return true;
2237 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002238
Reid Spencer5f016e22007-07-11 17:01:13 +00002239 // GNU attributes support.
2240 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002241 // GNU typeof support.
2242 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002243
Reid Spencer5f016e22007-07-11 17:01:13 +00002244 // type-specifiers
2245 case tok::kw_short:
2246 case tok::kw_long:
2247 case tok::kw_signed:
2248 case tok::kw_unsigned:
2249 case tok::kw__Complex:
2250 case tok::kw__Imaginary:
2251 case tok::kw_void:
2252 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002253 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002254 case tok::kw_char16_t:
2255 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002256 case tok::kw_int:
2257 case tok::kw_float:
2258 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002259 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002260 case tok::kw__Bool:
2261 case tok::kw__Decimal32:
2262 case tok::kw__Decimal64:
2263 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002264 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002265
Chris Lattner99dc9142008-04-13 18:59:07 +00002266 // struct-or-union-specifier (C99) or class-specifier (C++)
2267 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002268 case tok::kw_struct:
2269 case tok::kw_union:
2270 // enum-specifier
2271 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002272
Reid Spencer5f016e22007-07-11 17:01:13 +00002273 // type-qualifier
2274 case tok::kw_const:
2275 case tok::kw_volatile:
2276 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002277
2278 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002279 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002280 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002281
Chris Lattner7c186be2008-10-20 00:25:30 +00002282 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2283 case tok::less:
2284 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002285
Steve Naroff239f0732008-12-25 14:16:32 +00002286 case tok::kw___cdecl:
2287 case tok::kw___stdcall:
2288 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002289 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002290 case tok::kw___w64:
2291 case tok::kw___ptr64:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002292 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002293 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002294 }
2295}
2296
2297/// isDeclarationSpecifier() - Return true if the current token is part of a
2298/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002299bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002300 switch (Tok.getKind()) {
2301 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002302
Chris Lattner166a8fc2009-01-04 23:41:41 +00002303 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002304 // Unfortunate hack to support "Class.factoryMethod" notation.
2305 if (getLang().ObjC1 && NextToken().is(tok::period))
2306 return false;
John Thompson82287d12010-02-05 00:12:22 +00002307 if (TryAltiVecVectorToken())
2308 return true;
2309 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002310 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002311 // Annotate typenames and C++ scope specifiers. If we get one, just
2312 // recurse to handle whatever we get.
2313 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002314 return true;
2315 if (Tok.is(tok::identifier))
2316 return false;
2317 return isDeclarationSpecifier();
2318
Chris Lattner166a8fc2009-01-04 23:41:41 +00002319 case tok::coloncolon: // ::foo::bar
2320 if (NextToken().is(tok::kw_new) || // ::new
2321 NextToken().is(tok::kw_delete)) // ::delete
2322 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002323
Chris Lattner166a8fc2009-01-04 23:41:41 +00002324 // Annotate typenames and C++ scope specifiers. If we get one, just
2325 // recurse to handle whatever we get.
2326 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002327 return true;
2328 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002329
Reid Spencer5f016e22007-07-11 17:01:13 +00002330 // storage-class-specifier
2331 case tok::kw_typedef:
2332 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002333 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002334 case tok::kw_static:
2335 case tok::kw_auto:
2336 case tok::kw_register:
2337 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002338
Reid Spencer5f016e22007-07-11 17:01:13 +00002339 // type-specifiers
2340 case tok::kw_short:
2341 case tok::kw_long:
2342 case tok::kw_signed:
2343 case tok::kw_unsigned:
2344 case tok::kw__Complex:
2345 case tok::kw__Imaginary:
2346 case tok::kw_void:
2347 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002348 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002349 case tok::kw_char16_t:
2350 case tok::kw_char32_t:
2351
Reid Spencer5f016e22007-07-11 17:01:13 +00002352 case tok::kw_int:
2353 case tok::kw_float:
2354 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002355 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002356 case tok::kw__Bool:
2357 case tok::kw__Decimal32:
2358 case tok::kw__Decimal64:
2359 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002360 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002361
Chris Lattner99dc9142008-04-13 18:59:07 +00002362 // struct-or-union-specifier (C99) or class-specifier (C++)
2363 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002364 case tok::kw_struct:
2365 case tok::kw_union:
2366 // enum-specifier
2367 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002368
Reid Spencer5f016e22007-07-11 17:01:13 +00002369 // type-qualifier
2370 case tok::kw_const:
2371 case tok::kw_volatile:
2372 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002373
Reid Spencer5f016e22007-07-11 17:01:13 +00002374 // function-specifier
2375 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002376 case tok::kw_virtual:
2377 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002378
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002379 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002380 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002381
Chris Lattner1ef08762007-08-09 17:01:07 +00002382 // GNU typeof support.
2383 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002384
Chris Lattner1ef08762007-08-09 17:01:07 +00002385 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002386 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002387 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002388
Chris Lattnerf3948c42008-07-26 03:38:44 +00002389 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2390 case tok::less:
2391 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002392
Steve Naroff47f52092009-01-06 19:34:12 +00002393 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002394 case tok::kw___cdecl:
2395 case tok::kw___stdcall:
2396 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002397 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002398 case tok::kw___w64:
2399 case tok::kw___ptr64:
2400 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002401 case tok::kw___pascal:
Eli Friedman290eeb02009-06-08 23:27:34 +00002402 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002403 }
2404}
2405
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002406bool Parser::isConstructorDeclarator() {
2407 TentativeParsingAction TPA(*this);
2408
2409 // Parse the C++ scope specifier.
2410 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002411 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true)) {
John McCall9ba61662010-02-26 08:45:28 +00002412 TPA.Revert();
2413 return false;
2414 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002415
2416 // Parse the constructor name.
2417 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2418 // We already know that we have a constructor name; just consume
2419 // the token.
2420 ConsumeToken();
2421 } else {
2422 TPA.Revert();
2423 return false;
2424 }
2425
2426 // Current class name must be followed by a left parentheses.
2427 if (Tok.isNot(tok::l_paren)) {
2428 TPA.Revert();
2429 return false;
2430 }
2431 ConsumeParen();
2432
2433 // A right parentheses or ellipsis signals that we have a constructor.
2434 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2435 TPA.Revert();
2436 return true;
2437 }
2438
2439 // If we need to, enter the specified scope.
2440 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002441 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002442 DeclScopeObj.EnterDeclaratorScope();
2443
2444 // Check whether the next token(s) are part of a declaration
2445 // specifier, in which case we have the start of a parameter and,
2446 // therefore, we know that this is a constructor.
2447 bool IsConstructor = isDeclarationSpecifier();
2448 TPA.Revert();
2449 return IsConstructor;
2450}
Reid Spencer5f016e22007-07-11 17:01:13 +00002451
2452/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00002453/// type-qualifier-list: [C99 6.7.5]
2454/// type-qualifier
2455/// [vendor] attributes
2456/// [ only if VendorAttributesAllowed=true ]
2457/// type-qualifier-list type-qualifier
2458/// [vendor] type-qualifier-list attributes
2459/// [ only if VendorAttributesAllowed=true ]
2460/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2461/// [ only if CXX0XAttributesAllowed=true ]
2462/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00002463///
Dawn Perchik52fc3142010-09-03 01:29:35 +00002464void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
2465 bool VendorAttributesAllowed,
Sean Huntbbd37c62009-11-21 08:43:09 +00002466 bool CXX0XAttributesAllowed) {
2467 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2468 SourceLocation Loc = Tok.getLocation();
2469 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2470 if (CXX0XAttributesAllowed)
2471 DS.AddAttributes(Attr.AttrList);
2472 else
2473 Diag(Loc, diag::err_attributes_not_allowed);
2474 }
2475
Reid Spencer5f016e22007-07-11 17:01:13 +00002476 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002477 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002478 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002479 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002480 SourceLocation Loc = Tok.getLocation();
2481
2482 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00002483 case tok::code_completion:
2484 Actions.CodeCompleteTypeQualifiers(DS);
2485 ConsumeCodeCompletionToken();
2486 break;
2487
Reid Spencer5f016e22007-07-11 17:01:13 +00002488 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002489 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2490 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002491 break;
2492 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002493 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2494 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002495 break;
2496 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002497 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2498 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002499 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002500 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002501 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002502 case tok::kw___cdecl:
2503 case tok::kw___stdcall:
2504 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002505 case tok::kw___thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002506 if (VendorAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002507 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2508 continue;
2509 }
2510 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002511 case tok::kw___pascal:
2512 if (VendorAttributesAllowed) {
2513 DS.AddAttributes(ParseBorlandTypeAttributes());
2514 continue;
2515 }
2516 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002517 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002518 if (VendorAttributesAllowed) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002519 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002520 continue; // do *not* consume the next token!
2521 }
2522 // otherwise, FALL THROUGH!
2523 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002524 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002525 // If this is not a type-qualifier token, we're done reading type
2526 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002527 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002528 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002529 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002530
Reid Spencer5f016e22007-07-11 17:01:13 +00002531 // If the specifier combination wasn't legal, issue a diagnostic.
2532 if (isInvalid) {
2533 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002534 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002535 }
2536 ConsumeToken();
2537 }
2538}
2539
2540
2541/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2542///
2543void Parser::ParseDeclarator(Declarator &D) {
2544 /// This implements the 'declarator' production in the C grammar, then checks
2545 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002546 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002547}
2548
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002549/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2550/// is parsed by the function passed to it. Pass null, and the direct-declarator
2551/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002552/// ptr-operator production.
2553///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002554/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2555/// [C] pointer[opt] direct-declarator
2556/// [C++] direct-declarator
2557/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002558///
2559/// pointer: [C99 6.7.5]
2560/// '*' type-qualifier-list[opt]
2561/// '*' type-qualifier-list[opt] pointer
2562///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002563/// ptr-operator:
2564/// '*' cv-qualifier-seq[opt]
2565/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002566/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002567/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002568/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002569/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002570void Parser::ParseDeclaratorInternal(Declarator &D,
2571 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002572 if (Diags.hasAllExtensionsSilenced())
2573 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002574
Sebastian Redlf30208a2009-01-24 21:16:55 +00002575 // C++ member pointers start with a '::' or a nested-name.
2576 // Member pointers get special handling, since there's no place for the
2577 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002578 if (getLang().CPlusPlus &&
2579 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2580 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002581 CXXScopeSpec SS;
John McCallb3d87482010-08-24 05:47:05 +00002582 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), true); // ignore fail
John McCall9ba61662010-02-26 08:45:28 +00002583
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002584 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002585 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002586 // The scope spec really belongs to the direct-declarator.
2587 D.getCXXScopeSpec() = SS;
2588 if (DirectDeclParser)
2589 (this->*DirectDeclParser)(D);
2590 return;
2591 }
2592
2593 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002594 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002595 DeclSpec DS;
2596 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002597 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002598
2599 // Recurse to parse whatever is left.
2600 ParseDeclaratorInternal(D, DirectDeclParser);
2601
2602 // Sema will have to catch (syntactically invalid) pointers into global
2603 // scope. It has to catch pointers into namespace scope anyway.
2604 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002605 Loc, DS.TakeAttributes()),
2606 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002607 return;
2608 }
2609 }
2610
2611 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002612 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002613 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002614 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002615 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002616 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002617 if (DirectDeclParser)
2618 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002619 return;
2620 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002621
Sebastian Redl05532f22009-03-15 22:02:01 +00002622 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2623 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002624 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002625 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002626
Chris Lattner9af55002009-03-27 04:18:06 +00002627 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002628 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002629 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002630
Reid Spencer5f016e22007-07-11 17:01:13 +00002631 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002632 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002633
Reid Spencer5f016e22007-07-11 17:01:13 +00002634 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002635 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002636 if (Kind == tok::star)
2637 // Remember that we parsed a pointer type, and remember the type-quals.
2638 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002639 DS.TakeAttributes()),
2640 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002641 else
2642 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002643 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002644 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002645 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002646 } else {
2647 // Is a reference
2648 DeclSpec DS;
2649
Sebastian Redl743de1f2009-03-23 00:00:23 +00002650 // Complain about rvalue references in C++03, but then go on and build
2651 // the declarator.
2652 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2653 Diag(Loc, diag::err_rvalue_reference);
2654
Reid Spencer5f016e22007-07-11 17:01:13 +00002655 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2656 // cv-qualifiers are introduced through the use of a typedef or of a
2657 // template type argument, in which case the cv-qualifiers are ignored.
2658 //
2659 // [GNU] Retricted references are allowed.
2660 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002661 // [C++0x] Attributes on references are not allowed.
2662 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002663 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002664
2665 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2666 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2667 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002668 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002669 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2670 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002671 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002672 }
2673
2674 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002675 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002676
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002677 if (D.getNumTypeObjects() > 0) {
2678 // C++ [dcl.ref]p4: There shall be no references to references.
2679 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2680 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002681 if (const IdentifierInfo *II = D.getIdentifier())
2682 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2683 << II;
2684 else
2685 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2686 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002687
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002688 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002689 // can go ahead and build the (technically ill-formed)
2690 // declarator: reference collapsing will take care of it.
2691 }
2692 }
2693
Reid Spencer5f016e22007-07-11 17:01:13 +00002694 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002695 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002696 DS.TakeAttributes(),
2697 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002698 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002699 }
2700}
2701
2702/// ParseDirectDeclarator
2703/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002704/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002705/// '(' declarator ')'
2706/// [GNU] '(' attributes declarator ')'
2707/// [C90] direct-declarator '[' constant-expression[opt] ']'
2708/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2709/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2710/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2711/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2712/// direct-declarator '(' parameter-type-list ')'
2713/// direct-declarator '(' identifier-list[opt] ')'
2714/// [GNU] direct-declarator '(' parameter-forward-declarations
2715/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002716/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2717/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002718/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002719///
2720/// declarator-id: [C++ 8]
2721/// id-expression
2722/// '::'[opt] nested-name-specifier[opt] type-name
2723///
2724/// id-expression: [C++ 5.1]
2725/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002726/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002727///
2728/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002729/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002730/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002731/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002732/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002733/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002734///
Reid Spencer5f016e22007-07-11 17:01:13 +00002735void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002736 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002737
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002738 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2739 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002740 if (D.getCXXScopeSpec().isEmpty()) {
John McCallb3d87482010-08-24 05:47:05 +00002741 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), true);
John McCall9ba61662010-02-26 08:45:28 +00002742 }
2743
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002744 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002745 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00002746 // Change the declaration context for name lookup, until this function
2747 // is exited (and the declarator has been parsed).
2748 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002749 }
2750
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002751 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2752 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2753 // We found something that indicates the start of an unqualified-id.
2754 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002755 bool AllowConstructorName;
2756 if (D.getDeclSpec().hasTypeSpecifier())
2757 AllowConstructorName = false;
2758 else if (D.getCXXScopeSpec().isSet())
2759 AllowConstructorName =
2760 (D.getContext() == Declarator::FileContext ||
2761 (D.getContext() == Declarator::MemberContext &&
2762 D.getDeclSpec().isFriendSpecified()));
2763 else
2764 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2765
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002766 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2767 /*EnteringContext=*/true,
2768 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002769 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00002770 ParsedType(),
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002771 D.getName()) ||
2772 // Once we're past the identifier, if the scope was bad, mark the
2773 // whole declarator bad.
2774 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002775 D.SetIdentifier(0, Tok.getLocation());
2776 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002777 } else {
2778 // Parsed the unqualified-id; update range information and move along.
2779 if (D.getSourceRange().getBegin().isInvalid())
2780 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2781 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002782 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002783 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002784 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002785 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002786 assert(!getLang().CPlusPlus &&
2787 "There's a C++-specific check for tok::identifier above");
2788 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2789 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2790 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002791 goto PastIdentifier;
2792 }
2793
2794 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002795 // direct-declarator: '(' declarator ')'
2796 // direct-declarator: '(' attributes declarator ')'
2797 // Example: 'char (*X)' or 'int (*XX)(void)'
2798 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002799
2800 // If the declarator was parenthesized, we entered the declarator
2801 // scope when parsing the parenthesized declarator, then exited
2802 // the scope already. Re-enter the scope, if we need to.
2803 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002804 // If there was an error parsing parenthesized declarator, declarator
2805 // scope may have been enterred before. Don't do it again.
2806 if (!D.isInvalidType() &&
2807 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002808 // Change the declaration context for name lookup, until this function
2809 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00002810 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002811 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002812 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002813 // This could be something simple like "int" (in which case the declarator
2814 // portion is empty), if an abstract-declarator is allowed.
2815 D.SetIdentifier(0, Tok.getLocation());
2816 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002817 if (D.getContext() == Declarator::MemberContext)
2818 Diag(Tok, diag::err_expected_member_name_or_semi)
2819 << D.getDeclSpec().getSourceRange();
2820 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002821 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002822 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002823 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002824 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002825 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002826 }
Mike Stump1eb44332009-09-09 15:08:12 +00002827
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002828 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002829 assert(D.isPastIdentifier() &&
2830 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002831
Sean Huntbbd37c62009-11-21 08:43:09 +00002832 // Don't parse attributes unless we have an identifier.
Douglas Gregor3c3aaf92010-02-19 16:47:56 +00002833 if (D.getIdentifier() && getLang().CPlusPlus0x
Sean Huntbbd37c62009-11-21 08:43:09 +00002834 && isCXX0XAttributeSpecifier(true)) {
2835 SourceLocation AttrEndLoc;
2836 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2837 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2838 }
2839
Reid Spencer5f016e22007-07-11 17:01:13 +00002840 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002841 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002842 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2843 // In such a case, check if we actually have a function declarator; if it
2844 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002845 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2846 // When not in file scope, warn for ambiguous function declarators, just
2847 // in case the author intended it as a variable definition.
2848 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2849 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2850 break;
2851 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002852 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002853 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002854 ParseBracketDeclarator(D);
2855 } else {
2856 break;
2857 }
2858 }
2859}
2860
Chris Lattneref4715c2008-04-06 05:45:57 +00002861/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2862/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002863/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002864/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2865///
2866/// direct-declarator:
2867/// '(' declarator ')'
2868/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002869/// direct-declarator '(' parameter-type-list ')'
2870/// direct-declarator '(' identifier-list[opt] ')'
2871/// [GNU] direct-declarator '(' parameter-forward-declarations
2872/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002873///
2874void Parser::ParseParenDeclarator(Declarator &D) {
2875 SourceLocation StartLoc = ConsumeParen();
2876 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002877
Chris Lattner7399ee02008-10-20 02:05:46 +00002878 // Eat any attributes before we look at whether this is a grouping or function
2879 // declarator paren. If this is a grouping paren, the attribute applies to
2880 // the type being built up, for example:
2881 // int (__attribute__(()) *x)(long y)
2882 // If this ends up not being a grouping paren, the attribute applies to the
2883 // first argument, for example:
2884 // int (__attribute__(()) int x)
2885 // In either case, we need to eat any attributes to be able to determine what
2886 // sort of paren this is.
2887 //
Ted Kremenek1e377652010-02-11 02:19:13 +00002888 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner7399ee02008-10-20 02:05:46 +00002889 bool RequiresArg = false;
2890 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002891 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +00002892
Chris Lattner7399ee02008-10-20 02:05:46 +00002893 // We require that the argument list (if this is a non-grouping paren) be
2894 // present even if the attribute list was empty.
2895 RequiresArg = true;
2896 }
Steve Naroff239f0732008-12-25 14:16:32 +00002897 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002898 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002899 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
2900 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002901 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman290eeb02009-06-08 23:27:34 +00002902 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00002903 // Eat any Borland extensions.
2904 if (Tok.is(tok::kw___pascal)) {
2905 AttrList.reset(ParseBorlandTypeAttributes(AttrList.take()));
2906 }
Mike Stump1eb44332009-09-09 15:08:12 +00002907
Chris Lattneref4715c2008-04-06 05:45:57 +00002908 // If we haven't past the identifier yet (or where the identifier would be
2909 // stored, if this is an abstract declarator), then this is probably just
2910 // grouping parens. However, if this could be an abstract-declarator, then
2911 // this could also be the start of function arguments (consider 'void()').
2912 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002913
Chris Lattneref4715c2008-04-06 05:45:57 +00002914 if (!D.mayOmitIdentifier()) {
2915 // If this can't be an abstract-declarator, this *must* be a grouping
2916 // paren, because we haven't seen the identifier yet.
2917 isGrouping = true;
2918 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002919 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002920 isDeclarationSpecifier()) { // 'int(int)' is a function.
2921 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2922 // considered to be a type, not a K&R identifier-list.
2923 isGrouping = false;
2924 } else {
2925 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2926 isGrouping = true;
2927 }
Mike Stump1eb44332009-09-09 15:08:12 +00002928
Chris Lattneref4715c2008-04-06 05:45:57 +00002929 // If this is a grouping paren, handle:
2930 // direct-declarator: '(' declarator ')'
2931 // direct-declarator: '(' attributes declarator ')'
2932 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002933 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002934 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002935 if (AttrList)
Ted Kremenek1e377652010-02-11 02:19:13 +00002936 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002937
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002938 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002939 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002940 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002941
2942 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002943 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002944 return;
2945 }
Mike Stump1eb44332009-09-09 15:08:12 +00002946
Chris Lattneref4715c2008-04-06 05:45:57 +00002947 // Okay, if this wasn't a grouping paren, it must be the start of a function
2948 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002949 // identifier (and remember where it would have been), then call into
2950 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002951 D.SetIdentifier(0, Tok.getLocation());
2952
Ted Kremenek1e377652010-02-11 02:19:13 +00002953 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002954}
2955
2956/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2957/// declarator D up to a paren, which indicates that we are parsing function
2958/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002959///
Chris Lattner7399ee02008-10-20 02:05:46 +00002960/// If AttrList is non-null, then the caller parsed those arguments immediately
2961/// after the open paren - they should be considered to be the first argument of
2962/// a parameter. If RequiresArg is true, then the first argument of the
2963/// function is required to be present and required to not be an identifier
2964/// list.
2965///
Reid Spencer5f016e22007-07-11 17:01:13 +00002966/// This method also handles this portion of the grammar:
2967/// parameter-type-list: [C99 6.7.5]
2968/// parameter-list
2969/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002970/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002971///
2972/// parameter-list: [C99 6.7.5]
2973/// parameter-declaration
2974/// parameter-list ',' parameter-declaration
2975///
2976/// parameter-declaration: [C99 6.7.5]
2977/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002978/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002979/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002980/// declaration-specifiers abstract-declarator[opt]
2981/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002982/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002983/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2984///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002985/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002986/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002987///
Chris Lattner7399ee02008-10-20 02:05:46 +00002988void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2989 AttributeList *AttrList,
2990 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002991 // lparen is already consumed!
2992 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002993
Chris Lattner7399ee02008-10-20 02:05:46 +00002994 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002995 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002996 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002997 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002998 delete AttrList;
2999 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003000
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003001 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
3002 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003003
3004 // cv-qualifier-seq[opt].
3005 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003006 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003007 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003008 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003009 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003010 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003011 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003012 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003013 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003014 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003015
3016 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003017 if (Tok.is(tok::kw_throw)) {
3018 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003019 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003020 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003021 hasAnyExceptionSpec);
3022 assert(Exceptions.size() == ExceptionRanges.size() &&
3023 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003024 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003025 }
3026
Chris Lattnerf97409f2008-04-06 06:57:35 +00003027 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00003028 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003029 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00003030 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003031 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003032 /*arglist*/ 0, 0,
3033 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003034 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003035 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003036 Exceptions.data(),
3037 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003038 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003039 LParenLoc, RParenLoc, D),
3040 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00003041 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00003042 }
3043
Chris Lattner7399ee02008-10-20 02:05:46 +00003044 // Alternatively, this parameter list may be an identifier list form for a
3045 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00003046 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
3047 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00003048 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00003049 // K&R identifier lists can't have typedefs as identifiers, per
3050 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00003051 if (RequiresArg) {
3052 Diag(Tok, diag::err_argument_required_after_attribute);
3053 delete AttrList;
3054 }
Chris Lattner83a94472010-05-14 17:23:36 +00003055
Steve Naroff2d081c42009-01-28 19:16:40 +00003056 // Identifier list. Note that '(' identifier-list ')' is only allowed for
Chris Lattner83a94472010-05-14 17:23:36 +00003057 // normal declarators, not for abstract-declarators. Get the first
3058 // identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003059 Token FirstTok = Tok;
Chris Lattner83a94472010-05-14 17:23:36 +00003060 ConsumeToken(); // eat the first identifier.
Chris Lattner9a65b812010-05-14 17:44:56 +00003061
3062 // Identifier lists follow a really simple grammar: the identifiers can
3063 // be followed *only* by a ", moreidentifiers" or ")". However, K&R
3064 // identifier lists are really rare in the brave new modern world, and it
3065 // is very common for someone to typo a type in a non-k&r style list. If
3066 // we are presented with something like: "void foo(intptr x, float y)",
3067 // we don't want to start parsing the function declarator as though it is
3068 // a K&R style declarator just because intptr is an invalid type.
3069 //
3070 // To handle this, we check to see if the token after the first identifier
3071 // is a "," or ")". Only if so, do we parse it as an identifier list.
3072 if (Tok.is(tok::comma) || Tok.is(tok::r_paren))
3073 return ParseFunctionDeclaratorIdentifierList(LParenLoc,
3074 FirstTok.getIdentifierInfo(),
3075 FirstTok.getLocation(), D);
3076
3077 // If we get here, the code is invalid. Push the first identifier back
3078 // into the token stream and parse the first argument as an (invalid)
3079 // normal argument declarator.
3080 PP.EnterToken(Tok);
3081 Tok = FirstTok;
Chris Lattner7399ee02008-10-20 02:05:46 +00003082 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00003083 }
Mike Stump1eb44332009-09-09 15:08:12 +00003084
Chris Lattnerf97409f2008-04-06 06:57:35 +00003085 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00003086
Chris Lattnerf97409f2008-04-06 06:57:35 +00003087 // Build up an array of information about the parsed arguments.
3088 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00003089
3090 // Enter function-declaration scope, limiting any declarators to the
3091 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00003092 ParseScope PrototypeScope(this,
3093 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00003094
Chris Lattnerf97409f2008-04-06 06:57:35 +00003095 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003096 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00003097 while (1) {
3098 if (Tok.is(tok::ellipsis)) {
3099 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00003100 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003101 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00003102 }
Mike Stump1eb44332009-09-09 15:08:12 +00003103
Chris Lattnerf97409f2008-04-06 06:57:35 +00003104 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00003105
Chris Lattnerf97409f2008-04-06 06:57:35 +00003106 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00003107 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003108 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00003109
3110 // If the caller parsed attributes for the first argument, add them now.
3111 if (AttrList) {
3112 DS.AddAttributes(AttrList);
3113 AttrList = 0; // Only apply the attributes to the first parameter.
3114 }
Chris Lattnere64c5492009-02-27 18:38:20 +00003115 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00003116
Chris Lattnerf97409f2008-04-06 06:57:35 +00003117 // Parse the declarator. This is "PrototypeContext", because we must
3118 // accept either 'declarator' or 'abstract-declarator' here.
3119 Declarator ParmDecl(DS, Declarator::PrototypeContext);
3120 ParseDeclarator(ParmDecl);
3121
3122 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003123 if (Tok.is(tok::kw___attribute)) {
3124 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00003125 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003126 ParmDecl.AddAttributes(AttrList, Loc);
3127 }
Mike Stump1eb44332009-09-09 15:08:12 +00003128
Chris Lattnerf97409f2008-04-06 06:57:35 +00003129 // Remember this parsed parameter in ParamInfo.
3130 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003131
Douglas Gregor72b505b2008-12-16 21:30:33 +00003132 // DefArgToks is used when the parsing of default arguments needs
3133 // to be delayed.
3134 CachedTokens *DefArgToks = 0;
3135
Chris Lattnerf97409f2008-04-06 06:57:35 +00003136 // If no parameter was specified, verify that *something* was specified,
3137 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00003138 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
3139 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00003140 // Completely missing, emit error.
3141 Diag(DSStart, diag::err_missing_param);
3142 } else {
3143 // Otherwise, we have something. Add it and let semantic analysis try
3144 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00003145
Chris Lattnerf97409f2008-04-06 06:57:35 +00003146 // Inform the actions module about the parameter declarator, so it gets
3147 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00003148 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00003149
3150 // Parse the default argument, if any. We parse the default
3151 // arguments in all dialects; the semantic analysis in
3152 // ActOnParamDefaultArgument will reject the default argument in
3153 // C.
3154 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00003155 SourceLocation EqualLoc = Tok.getLocation();
3156
Chris Lattner04421082008-04-08 04:40:51 +00003157 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00003158 if (D.getContext() == Declarator::MemberContext) {
3159 // If we're inside a class definition, cache the tokens
3160 // corresponding to the default argument. We'll actually parse
3161 // them when we see the end of the class definition.
3162 // FIXME: Templates will require something similar.
3163 // FIXME: Can we use a smart pointer for Toks?
3164 DefArgToks = new CachedTokens;
3165
Mike Stump1eb44332009-09-09 15:08:12 +00003166 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00003167 /*StopAtSemi=*/true,
3168 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003169 delete DefArgToks;
3170 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00003171 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003172 } else {
3173 // Mark the end of the default argument so that we know when to
3174 // stop when we parse it later on.
3175 Token DefArgEnd;
3176 DefArgEnd.startToken();
3177 DefArgEnd.setKind(tok::cxx_defaultarg_end);
3178 DefArgEnd.setLocation(Tok.getLocation());
3179 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00003180 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00003181 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00003182 }
Chris Lattner04421082008-04-08 04:40:51 +00003183 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003184 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003185 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003186
John McCall60d7b3a2010-08-24 06:29:42 +00003187 ExprResult DefArgResult(ParseAssignmentExpression());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003188 if (DefArgResult.isInvalid()) {
3189 Actions.ActOnParamDefaultArgumentError(Param);
3190 SkipUntil(tok::comma, tok::r_paren, true, true);
3191 } else {
3192 // Inform the actions module about the default argument
3193 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00003194 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00003195 }
Chris Lattner04421082008-04-08 04:40:51 +00003196 }
3197 }
Mike Stump1eb44332009-09-09 15:08:12 +00003198
3199 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3200 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003201 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003202 }
3203
3204 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003205 if (Tok.isNot(tok::comma)) {
3206 if (Tok.is(tok::ellipsis)) {
3207 IsVariadic = true;
3208 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3209
3210 if (!getLang().CPlusPlus) {
3211 // We have ellipsis without a preceding ',', which is ill-formed
3212 // in C. Complain and provide the fix.
3213 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003214 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003215 }
3216 }
3217
3218 break;
3219 }
Mike Stump1eb44332009-09-09 15:08:12 +00003220
Chris Lattnerf97409f2008-04-06 06:57:35 +00003221 // Consume the comma.
3222 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003223 }
Mike Stump1eb44332009-09-09 15:08:12 +00003224
Chris Lattnerf97409f2008-04-06 06:57:35 +00003225 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00003226 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00003227
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003228 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003229 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3230 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003231
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003232 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003233 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003234 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003235 bool hasAnyExceptionSpec = false;
John McCallb3d87482010-08-24 05:47:05 +00003236 llvm::SmallVector<ParsedType, 2> Exceptions;
Sebastian Redlef65f062009-05-29 18:02:33 +00003237 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003238
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003239 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003240 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003241 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003242 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003243 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003244
3245 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003246 if (Tok.is(tok::kw_throw)) {
3247 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003248 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003249 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003250 hasAnyExceptionSpec);
3251 assert(Exceptions.size() == ExceptionRanges.size() &&
3252 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003253 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003254 }
3255
Reid Spencer5f016e22007-07-11 17:01:13 +00003256 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003257 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003258 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003259 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003260 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003261 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003262 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003263 Exceptions.data(),
3264 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003265 Exceptions.size(),
3266 LParenLoc, RParenLoc, D),
3267 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003268}
3269
Chris Lattner66d28652008-04-06 06:34:08 +00003270/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3271/// we found a K&R-style identifier list instead of a type argument list. The
Chris Lattner83a94472010-05-14 17:23:36 +00003272/// first identifier has already been consumed, and the current token is the
3273/// token right after it.
Chris Lattner66d28652008-04-06 06:34:08 +00003274///
3275/// identifier-list: [C99 6.7.5]
3276/// identifier
3277/// identifier-list ',' identifier
3278///
3279void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
Chris Lattner83a94472010-05-14 17:23:36 +00003280 IdentifierInfo *FirstIdent,
3281 SourceLocation FirstIdentLoc,
Chris Lattner66d28652008-04-06 06:34:08 +00003282 Declarator &D) {
3283 // Build up an array of information about the parsed arguments.
3284 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3285 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003286
Chris Lattner66d28652008-04-06 06:34:08 +00003287 // If there was no identifier specified for the declarator, either we are in
3288 // an abstract-declarator, or we are in a parameter declarator which was found
3289 // to be abstract. In abstract-declarators, identifier lists are not valid:
3290 // diagnose this.
3291 if (!D.getIdentifier())
Chris Lattner83a94472010-05-14 17:23:36 +00003292 Diag(FirstIdentLoc, diag::ext_ident_list_in_param);
Chris Lattner66d28652008-04-06 06:34:08 +00003293
Chris Lattner83a94472010-05-14 17:23:36 +00003294 // The first identifier was already read, and is known to be the first
3295 // identifier in the list. Remember this identifier in ParamInfo.
3296 ParamsSoFar.insert(FirstIdent);
John McCalld226f652010-08-21 09:40:31 +00003297 ParamInfo.push_back(DeclaratorChunk::ParamInfo(FirstIdent, FirstIdentLoc, 0));
Mike Stump1eb44332009-09-09 15:08:12 +00003298
Chris Lattner66d28652008-04-06 06:34:08 +00003299 while (Tok.is(tok::comma)) {
3300 // Eat the comma.
3301 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003302
Chris Lattner50c64772008-04-06 06:39:19 +00003303 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003304 if (Tok.isNot(tok::identifier)) {
3305 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003306 SkipUntil(tok::r_paren);
3307 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003308 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003309
Chris Lattner66d28652008-04-06 06:34:08 +00003310 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003311
3312 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003313 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003314 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003315
Chris Lattner66d28652008-04-06 06:34:08 +00003316 // Verify that the argument identifier has not already been mentioned.
3317 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003318 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003319 } else {
3320 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003321 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003322 Tok.getLocation(),
John McCalld226f652010-08-21 09:40:31 +00003323 0));
Chris Lattner50c64772008-04-06 06:39:19 +00003324 }
Mike Stump1eb44332009-09-09 15:08:12 +00003325
Chris Lattner66d28652008-04-06 06:34:08 +00003326 // Eat the identifier.
3327 ConsumeToken();
3328 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003329
3330 // If we have the closing ')', eat it and we're done.
3331 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3332
Chris Lattner50c64772008-04-06 06:39:19 +00003333 // Remember that we parsed a function type, and remember the attributes. This
3334 // function type is always a K&R style function type, which is not varargs and
3335 // has no prototype.
3336 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003337 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003338 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003339 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003340 /*exception*/false,
3341 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003342 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003343 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003344}
Chris Lattneref4715c2008-04-06 05:45:57 +00003345
Reid Spencer5f016e22007-07-11 17:01:13 +00003346/// [C90] direct-declarator '[' constant-expression[opt] ']'
3347/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3348/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3349/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3350/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3351void Parser::ParseBracketDeclarator(Declarator &D) {
3352 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003353
Chris Lattner378c7e42008-12-18 07:27:21 +00003354 // C array syntax has many features, but by-far the most common is [] and [4].
3355 // This code does a fast path to handle some of the most obvious cases.
3356 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003357 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003358 //FIXME: Use these
3359 CXX0XAttributeList Attr;
3360 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3361 Attr = ParseCXX0XAttributes();
3362 }
3363
Chris Lattner378c7e42008-12-18 07:27:21 +00003364 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00003365 ExprResult NumElements;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003366 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3367 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003368 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003369 return;
3370 } else if (Tok.getKind() == tok::numeric_constant &&
3371 GetLookAheadToken(1).is(tok::r_square)) {
3372 // [4] is very common. Parse the numeric constant expression.
John McCall60d7b3a2010-08-24 06:29:42 +00003373 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003374 ConsumeToken();
3375
Sebastian Redlab197ba2009-02-09 18:23:29 +00003376 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003377 //FIXME: Use these
3378 CXX0XAttributeList Attr;
3379 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3380 Attr = ParseCXX0XAttributes();
3381 }
Chris Lattner378c7e42008-12-18 07:27:21 +00003382
3383 // If there was an error parsing the assignment-expression, recover.
3384 if (ExprRes.isInvalid())
3385 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00003386
Chris Lattner378c7e42008-12-18 07:27:21 +00003387 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003388 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3389 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003390 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003391 return;
3392 }
Mike Stump1eb44332009-09-09 15:08:12 +00003393
Reid Spencer5f016e22007-07-11 17:01:13 +00003394 // If valid, this location is the position where we read the 'static' keyword.
3395 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003396 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003397 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003398
Reid Spencer5f016e22007-07-11 17:01:13 +00003399 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003400 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003401 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003402 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003403
Reid Spencer5f016e22007-07-11 17:01:13 +00003404 // If we haven't already read 'static', check to see if there is one after the
3405 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003406 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003407 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003408
Reid Spencer5f016e22007-07-11 17:01:13 +00003409 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3410 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00003411 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00003412
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003413 // Handle the case where we have '[*]' as the array size. However, a leading
3414 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3415 // the the token after the star is a ']'. Since stars in arrays are
3416 // infrequent, use of lookahead is not costly here.
3417 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003418 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003419
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003420 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003421 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003422 StaticLoc = SourceLocation(); // Drop the static.
3423 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003424 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003425 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003426 // Note, in C89, this production uses the constant-expr production instead
3427 // of assignment-expr. The only difference is that assignment-expr allows
3428 // things like '=' and '*='. Sema rejects these in C89 mode because they
3429 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003430
Douglas Gregore0762c92009-06-19 23:52:42 +00003431 // Parse the constant-expression or assignment-expression now (depending
3432 // on dialect).
3433 if (getLang().CPlusPlus)
3434 NumElements = ParseConstantExpression();
3435 else
3436 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003437 }
Mike Stump1eb44332009-09-09 15:08:12 +00003438
Reid Spencer5f016e22007-07-11 17:01:13 +00003439 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003440 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003441 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003442 // If the expression was invalid, skip it.
3443 SkipUntil(tok::r_square);
3444 return;
3445 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003446
3447 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3448
Sean Huntbbd37c62009-11-21 08:43:09 +00003449 //FIXME: Use these
3450 CXX0XAttributeList Attr;
3451 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3452 Attr = ParseCXX0XAttributes();
3453 }
3454
Chris Lattner378c7e42008-12-18 07:27:21 +00003455 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003456 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3457 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003458 NumElements.release(),
3459 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003460 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003461}
3462
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003463/// [GNU] typeof-specifier:
3464/// typeof ( expressions )
3465/// typeof ( type-name )
3466/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003467///
3468void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003469 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003470 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003471 SourceLocation StartLoc = ConsumeToken();
3472
John McCallcfb708c2010-01-13 20:03:27 +00003473 const bool hasParens = Tok.is(tok::l_paren);
3474
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003475 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00003476 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003477 SourceRange CastRange;
John McCall60d7b3a2010-08-24 06:29:42 +00003478 ExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
John McCall911093e2010-08-25 02:45:51 +00003479 isCastExpr,
3480 CastTy,
3481 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003482 if (hasParens)
3483 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003484
3485 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003486 // FIXME: Not accurate, the range gets one token more than it should.
3487 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003488 else
3489 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003490
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003491 if (isCastExpr) {
3492 if (!CastTy) {
3493 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003494 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003495 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003496
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003497 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003498 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003499 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3500 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003501 DiagID, CastTy))
3502 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003503 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003504 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003505
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003506 // If we get here, the operand to the typeof was an expresion.
3507 if (Operand.isInvalid()) {
3508 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003509 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003510 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003511
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003512 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003513 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003514 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3515 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00003516 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00003517 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003518}
Chris Lattner1b492422010-02-28 18:33:55 +00003519
3520
3521/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3522/// from TryAltiVecVectorToken.
3523bool Parser::TryAltiVecVectorTokenOutOfLine() {
3524 Token Next = NextToken();
3525 switch (Next.getKind()) {
3526 default: return false;
3527 case tok::kw_short:
3528 case tok::kw_long:
3529 case tok::kw_signed:
3530 case tok::kw_unsigned:
3531 case tok::kw_void:
3532 case tok::kw_char:
3533 case tok::kw_int:
3534 case tok::kw_float:
3535 case tok::kw_double:
3536 case tok::kw_bool:
3537 case tok::kw___pixel:
3538 Tok.setKind(tok::kw___vector);
3539 return true;
3540 case tok::identifier:
3541 if (Next.getIdentifierInfo() == Ident_pixel) {
3542 Tok.setKind(tok::kw___vector);
3543 return true;
3544 }
3545 return false;
3546 }
3547}
3548
3549bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3550 const char *&PrevSpec, unsigned &DiagID,
3551 bool &isInvalid) {
3552 if (Tok.getIdentifierInfo() == Ident_vector) {
3553 Token Next = NextToken();
3554 switch (Next.getKind()) {
3555 case tok::kw_short:
3556 case tok::kw_long:
3557 case tok::kw_signed:
3558 case tok::kw_unsigned:
3559 case tok::kw_void:
3560 case tok::kw_char:
3561 case tok::kw_int:
3562 case tok::kw_float:
3563 case tok::kw_double:
3564 case tok::kw_bool:
3565 case tok::kw___pixel:
3566 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3567 return true;
3568 case tok::identifier:
3569 if (Next.getIdentifierInfo() == Ident_pixel) {
3570 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3571 return true;
3572 }
3573 break;
3574 default:
3575 break;
3576 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00003577 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00003578 DS.isTypeAltiVecVector()) {
3579 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3580 return true;
3581 }
3582 return false;
3583}
3584