blob: 99752b59507f91077b31472f8493629471e2dd4e [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"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/ADT/SmallSet.h"
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.7: Declarations.
23//===----------------------------------------------------------------------===//
24
25/// ParseTypeName
26/// type-name: [C99 6.7.6]
27/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000028///
29/// Called type-id in C++.
Sebastian Redlef65f062009-05-29 18:02:33 +000030Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 // Parse the common declaration-specifiers piece.
32 DeclSpec DS;
33 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000034
Reid Spencer5f016e22007-07-11 17:01:13 +000035 // Parse the abstract-declarator, if present.
36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
37 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000038 if (Range)
39 *Range = DeclaratorInfo.getSourceRange();
40
Chris Lattnereaaebc72009-04-25 08:06:05 +000041 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000042 return true;
43
44 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000045}
46
47/// ParseAttributes - Parse a non-empty attributes list.
48///
49/// [GNU] attributes:
50/// attribute
51/// attributes attribute
52///
53/// [GNU] attribute:
54/// '__attribute__' '(' '(' attribute-list ')' ')'
55///
56/// [GNU] attribute-list:
57/// attrib
58/// attribute_list ',' attrib
59///
60/// [GNU] attrib:
61/// empty
62/// attrib-name
63/// attrib-name '(' identifier ')'
64/// attrib-name '(' identifier ',' nonempty-expr-list ')'
65/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
66///
67/// [GNU] attrib-name:
68/// identifier
69/// typespec
70/// typequal
71/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000072///
Reid Spencer5f016e22007-07-11 17:01:13 +000073/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000074/// token lookahead. Comment from gcc: "If they start with an identifier
75/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000076/// start with that identifier; otherwise they are an expression list."
77///
78/// At the moment, I am not doing 2 token lookahead. I am also unaware of
79/// any attributes that don't work (based on my limited testing). Most
80/// attributes are very simple in practice. Until we find a bug, I don't see
81/// a pressing need to implement the 2 token lookahead.
82
Sebastian Redlab197ba2009-02-09 18:23:29 +000083AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000084 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000085
Reid Spencer5f016e22007-07-11 17:01:13 +000086 AttributeList *CurrAttr = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner04d66662007-10-09 17:33:22 +000088 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000089 ConsumeToken();
90 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
91 "attribute")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
96 SkipUntil(tok::r_paren, true); // skip until ) or ;
97 return CurrAttr;
98 }
99 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000100 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
101 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000102
103 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
105 ConsumeToken();
106 continue;
107 }
108 // we have an identifier or declaration specifier (const, int, etc.)
109 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
110 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000113 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Chris Lattner04d66662007-10-09 17:33:22 +0000116 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
118 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000119
120 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 // __attribute__(( mode(byte) ))
122 ConsumeParen(); // ignore the right paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000123 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000125 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 ConsumeToken();
127 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000128 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 // now parse the non-empty comma separated list of expressions
132 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000133 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000134 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 ArgExprsOk = false;
136 SkipUntil(tok::r_paren);
137 break;
138 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000139 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 }
Chris Lattner04d66662007-10-09 17:33:22 +0000141 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 break;
143 ConsumeToken(); // Eat the comma, move to the next argument
144 }
Chris Lattner04d66662007-10-09 17:33:22 +0000145 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 ConsumeParen(); // ignore the right paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000147 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000148 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 }
150 }
151 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000152 switch (Tok.getKind()) {
153 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 // __attribute__(( nonnull() ))
156 ConsumeParen(); // ignore the right paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000157 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000159 break;
160 case tok::kw_char:
161 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000162 case tok::kw_char16_t:
163 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000164 case tok::kw_bool:
165 case tok::kw_short:
166 case tok::kw_int:
167 case tok::kw_long:
168 case tok::kw_signed:
169 case tok::kw_unsigned:
170 case tok::kw_float:
171 case tok::kw_double:
172 case tok::kw_void:
173 case tok::kw_typeof:
174 // If it's a builtin type name, eat it and expect a rparen
175 // __attribute__(( vec_type_hint(char) ))
176 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000177 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Nate Begeman6f3d8382009-06-26 06:32:41 +0000178 0, SourceLocation(), 0, 0, CurrAttr);
179 if (Tok.is(tok::r_paren))
180 ConsumeParen();
181 break;
182 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000184 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 // now parse the list of expressions
188 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000189 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000190 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 ArgExprsOk = false;
192 SkipUntil(tok::r_paren);
193 break;
194 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000195 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 }
Chris Lattner04d66662007-10-09 17:33:22 +0000197 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 break;
199 ConsumeToken(); // Eat the comma, move to the next argument
200 }
201 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000202 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000204 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
205 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 CurrAttr);
207 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000208 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 }
210 }
211 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000212 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 0, SourceLocation(), 0, 0, CurrAttr);
214 }
215 }
216 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 SkipUntil(tok::r_paren, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000218 SourceLocation Loc = Tok.getLocation();;
219 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
220 SkipUntil(tok::r_paren, false);
221 }
222 if (EndLoc)
223 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 }
225 return CurrAttr;
226}
227
Eli Friedmana23b4852009-06-08 07:21:15 +0000228/// ParseMicrosoftDeclSpec - Parse an __declspec construct
229///
230/// [MS] decl-specifier:
231/// __declspec ( extended-decl-modifier-seq )
232///
233/// [MS] extended-decl-modifier-seq:
234/// extended-decl-modifier[opt]
235/// extended-decl-modifier extended-decl-modifier-seq
236
Eli Friedman290eeb02009-06-08 23:27:34 +0000237AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000238 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000239
Steve Narofff59e17e2008-12-24 20:59:21 +0000240 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000241 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
242 "declspec")) {
243 SkipUntil(tok::r_paren, true); // skip until ) or ;
244 return CurrAttr;
245 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000246 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000247 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
248 SourceLocation AttrNameLoc = ConsumeToken();
249 if (Tok.is(tok::l_paren)) {
250 ConsumeParen();
251 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
252 // correctly.
253 OwningExprResult ArgExpr(ParseAssignmentExpression());
254 if (!ArgExpr.isInvalid()) {
255 ExprTy* ExprList = ArgExpr.take();
256 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
257 SourceLocation(), &ExprList, 1,
258 CurrAttr, true);
259 }
260 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
261 SkipUntil(tok::r_paren, false);
262 } else {
263 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, SourceLocation(),
264 0, 0, CurrAttr, true);
265 }
266 }
267 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
268 SkipUntil(tok::r_paren, false);
Eli Friedman290eeb02009-06-08 23:27:34 +0000269 return CurrAttr;
270}
271
272AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
273 // Treat these like attributes
274 // FIXME: Allow Sema to distinguish between these and real attributes!
275 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
276 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) ||
277 Tok.is(tok::kw___w64)) {
278 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
279 SourceLocation AttrNameLoc = ConsumeToken();
280 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
281 // FIXME: Support these properly!
282 continue;
283 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
284 SourceLocation(), 0, 0, CurrAttr, true);
285 }
286 return CurrAttr;
Steve Narofff59e17e2008-12-24 20:59:21 +0000287}
288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289/// ParseDeclaration - Parse a full 'declaration', which consists of
290/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000291/// 'Context' should be a Declarator::TheContext value. This returns the
292/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000293///
294/// declaration: [C99 6.7]
295/// block-declaration ->
296/// simple-declaration
297/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000298/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000299/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000300/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000301/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000302/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000303/// others... [FIXME]
304///
Chris Lattner97144fc2009-04-02 04:16:50 +0000305Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
306 SourceLocation &DeclEnd) {
Chris Lattner682bf922009-03-29 16:50:03 +0000307 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000308 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000309 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000310 case tok::kw_export:
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000311 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000312 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000313 case tok::kw_namespace:
Chris Lattner97144fc2009-04-02 04:16:50 +0000314 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000315 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000316 case tok::kw_using:
Chris Lattner97144fc2009-04-02 04:16:50 +0000317 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000318 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000319 case tok::kw_static_assert:
Chris Lattner97144fc2009-04-02 04:16:50 +0000320 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000321 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000322 default:
Chris Lattner97144fc2009-04-02 04:16:50 +0000323 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000324 }
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattner682bf922009-03-29 16:50:03 +0000326 // This routine returns a DeclGroup, if the thing we parsed only contains a
327 // single decl, convert it now.
328 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000329}
330
331/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
332/// declaration-specifiers init-declarator-list[opt] ';'
333///[C90/C++]init-declarator-list ';' [TODO]
334/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000335///
336/// If RequireSemi is false, this does not check for a ';' at the end of the
337/// declaration.
338Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000339 SourceLocation &DeclEnd) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000341 ParsingDeclSpec DS(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
345 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000346 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000348 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall54abf7d2009-11-04 02:18:39 +0000349 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000350 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 }
Mike Stump1eb44332009-09-09 15:08:12 +0000352
John McCalld8ac0572009-11-03 19:26:08 +0000353 DeclGroupPtrTy DG = ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false,
354 &DeclEnd);
355 return DG;
356}
Mike Stump1eb44332009-09-09 15:08:12 +0000357
John McCalld8ac0572009-11-03 19:26:08 +0000358/// ParseDeclGroup - Having concluded that this is either a function
359/// definition or a group of object declarations, actually parse the
360/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000361Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
362 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000363 bool AllowFunctionDefinitions,
364 SourceLocation *DeclEnd) {
365 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000366 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000367 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000368
John McCalld8ac0572009-11-03 19:26:08 +0000369 // Bail out if the first declarator didn't seem well-formed.
370 if (!D.hasName() && !D.mayOmitIdentifier()) {
371 // Skip until ; or }.
372 SkipUntil(tok::r_brace, true, true);
373 if (Tok.is(tok::semi))
374 ConsumeToken();
375 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000376 }
Mike Stump1eb44332009-09-09 15:08:12 +0000377
John McCalld8ac0572009-11-03 19:26:08 +0000378 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
379 if (isDeclarationAfterDeclarator()) {
380 // Fall though. We have to check this first, though, because
381 // __attribute__ might be the start of a function definition in
382 // (extended) K&R C.
383 } else if (isStartOfFunctionDefinition()) {
384 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
385 Diag(Tok, diag::err_function_declared_typedef);
386
387 // Recover by treating the 'typedef' as spurious.
388 DS.ClearStorageClassSpecs();
389 }
390
391 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
392 return Actions.ConvertDeclToDeclGroup(TheDecl);
393 } else {
394 Diag(Tok, diag::err_expected_fn_body);
395 SkipUntil(tok::semi);
396 return DeclGroupPtrTy();
397 }
398 }
399
400 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
401 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000402 D.complete(FirstDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000403 if (FirstDecl.get())
404 DeclsInGroup.push_back(FirstDecl);
405
406 // If we don't have a comma, it is either the end of the list (a ';') or an
407 // error, bail out.
408 while (Tok.is(tok::comma)) {
409 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000410 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000411
412 // Parse the next declarator.
413 D.clear();
414
415 // Accept attributes in an init-declarator. In the first declarator in a
416 // declaration, these would be part of the declspec. In subsequent
417 // declarators, they become part of the declarator itself, so that they
418 // don't apply to declarators after *this* one. Examples:
419 // short __attribute__((common)) var; -> declspec
420 // short var __attribute__((common)); -> declarator
421 // short x, __attribute__((common)) var; -> declarator
422 if (Tok.is(tok::kw___attribute)) {
423 SourceLocation Loc;
424 AttributeList *AttrList = ParseAttributes(&Loc);
425 D.AddAttributes(AttrList, Loc);
426 }
427
428 ParseDeclarator(D);
429
430 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000431 D.complete(ThisDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000432 if (ThisDecl.get())
433 DeclsInGroup.push_back(ThisDecl);
434 }
435
436 if (DeclEnd)
437 *DeclEnd = Tok.getLocation();
438
439 if (Context != Declarator::ForContext &&
440 ExpectAndConsume(tok::semi,
441 Context == Declarator::FileContext
442 ? diag::err_invalid_token_after_toplevel_declarator
443 : diag::err_expected_semi_declaration)) {
444 SkipUntil(tok::r_brace, true, true);
445 if (Tok.is(tok::semi))
446 ConsumeToken();
447 }
448
449 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
450 DeclsInGroup.data(),
451 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000452}
453
Douglas Gregor1426e532009-05-12 21:31:51 +0000454/// \brief Parse 'declaration' after parsing 'declaration-specifiers
455/// declarator'. This method parses the remainder of the declaration
456/// (including any attributes or initializer, among other things) and
457/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000458///
Reid Spencer5f016e22007-07-11 17:01:13 +0000459/// init-declarator: [C99 6.7]
460/// declarator
461/// declarator '=' initializer
462/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
463/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000464/// [C++] declarator initializer[opt]
465///
466/// [C++] initializer:
467/// [C++] '=' initializer-clause
468/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000469/// [C++0x] '=' 'default' [TODO]
470/// [C++0x] '=' 'delete'
471///
472/// According to the standard grammar, =default and =delete are function
473/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000474///
Douglas Gregore542c862009-06-23 23:11:28 +0000475Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
476 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000477 // If a simple-asm-expr is present, parse it.
478 if (Tok.is(tok::kw_asm)) {
479 SourceLocation Loc;
480 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
481 if (AsmLabel.isInvalid()) {
482 SkipUntil(tok::semi, true, true);
483 return DeclPtrTy();
484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Douglas Gregor1426e532009-05-12 21:31:51 +0000486 D.setAsmLabel(AsmLabel.release());
487 D.SetRangeEnd(Loc);
488 }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Douglas Gregor1426e532009-05-12 21:31:51 +0000490 // If attributes are present, parse them.
491 if (Tok.is(tok::kw___attribute)) {
492 SourceLocation Loc;
493 AttributeList *AttrList = ParseAttributes(&Loc);
494 D.AddAttributes(AttrList, Loc);
495 }
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Douglas Gregor1426e532009-05-12 21:31:51 +0000497 // Inform the current actions module that we just parsed this declarator.
Douglas Gregord5a423b2009-09-25 18:43:00 +0000498 DeclPtrTy ThisDecl;
499 switch (TemplateInfo.Kind) {
500 case ParsedTemplateInfo::NonTemplate:
501 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
502 break;
503
504 case ParsedTemplateInfo::Template:
505 case ParsedTemplateInfo::ExplicitSpecialization:
506 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregore542c862009-06-23 23:11:28 +0000507 Action::MultiTemplateParamsArg(Actions,
508 TemplateInfo.TemplateParams->data(),
509 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000510 D);
511 break;
512
513 case ParsedTemplateInfo::ExplicitInstantiation: {
514 Action::DeclResult ThisRes
515 = Actions.ActOnExplicitInstantiation(CurScope,
516 TemplateInfo.ExternLoc,
517 TemplateInfo.TemplateLoc,
518 D);
519 if (ThisRes.isInvalid()) {
520 SkipUntil(tok::semi, true, true);
521 return DeclPtrTy();
522 }
523
524 ThisDecl = ThisRes.get();
525 break;
526 }
527 }
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Douglas Gregor1426e532009-05-12 21:31:51 +0000529 // Parse declarator '=' initializer.
530 if (Tok.is(tok::equal)) {
531 ConsumeToken();
532 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
533 SourceLocation DelLoc = ConsumeToken();
534 Actions.SetDeclDeleted(ThisDecl, DelLoc);
535 } else {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000536 if (getLang().CPlusPlus)
537 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
538
Douglas Gregor1426e532009-05-12 21:31:51 +0000539 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000540
541 if (getLang().CPlusPlus)
542 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
543
Douglas Gregor1426e532009-05-12 21:31:51 +0000544 if (Init.isInvalid()) {
545 SkipUntil(tok::semi, true, true);
546 return DeclPtrTy();
547 }
Anders Carlsson9abf2ae2009-08-16 05:13:48 +0000548 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000549 }
550 } else if (Tok.is(tok::l_paren)) {
551 // Parse C++ direct initializer: '(' expression-list ')'
552 SourceLocation LParenLoc = ConsumeParen();
553 ExprVector Exprs(Actions);
554 CommaLocsTy CommaLocs;
555
556 if (ParseExpressionList(Exprs, CommaLocs)) {
557 SkipUntil(tok::r_paren);
558 } else {
559 // Match the ')'.
560 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
561
562 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
563 "Unexpected number of commas!");
564 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
565 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000566 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000567 }
568 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000569 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000570 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
571 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000572 }
573
574 return ThisDecl;
575}
576
Reid Spencer5f016e22007-07-11 17:01:13 +0000577/// ParseSpecifierQualifierList
578/// specifier-qualifier-list:
579/// type-specifier specifier-qualifier-list[opt]
580/// type-qualifier specifier-qualifier-list[opt]
581/// [GNU] attributes specifier-qualifier-list[opt]
582///
583void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
584 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
585 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000586 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 // Validate declspec for type-name.
589 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000590 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
591 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 // Issue diagnostic and remove storage class if present.
595 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
596 if (DS.getStorageClassSpecLoc().isValid())
597 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
598 else
599 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
600 DS.ClearStorageClassSpecs();
601 }
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 // Issue diagnostic and remove function specfier if present.
604 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000605 if (DS.isInlineSpecified())
606 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
607 if (DS.isVirtualSpecified())
608 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
609 if (DS.isExplicitSpecified())
610 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 DS.ClearFunctionSpecs();
612 }
613}
614
Chris Lattnerc199ab32009-04-12 20:42:31 +0000615/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
616/// specified token is valid after the identifier in a declarator which
617/// immediately follows the declspec. For example, these things are valid:
618///
619/// int x [ 4]; // direct-declarator
620/// int x ( int y); // direct-declarator
621/// int(int x ) // direct-declarator
622/// int x ; // simple-declaration
623/// int x = 17; // init-declarator-list
624/// int x , y; // init-declarator-list
625/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000626/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000627/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000628///
629/// This is not, because 'x' does not immediately follow the declspec (though
630/// ')' happens to be valid anyway).
631/// int (x)
632///
633static bool isValidAfterIdentifierInDeclarator(const Token &T) {
634 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
635 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000636 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000637}
638
Chris Lattnere40c2952009-04-14 21:34:55 +0000639
640/// ParseImplicitInt - This method is called when we have an non-typename
641/// identifier in a declspec (which normally terminates the decl spec) when
642/// the declspec has no type specifier. In this case, the declspec is either
643/// malformed or is "implicit int" (in K&R and C89).
644///
645/// This method handles diagnosing this prettily and returns false if the
646/// declspec is done being processed. If it recovers and thinks there may be
647/// other pieces of declspec after it, it returns true.
648///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000649bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000650 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000651 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000652 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Chris Lattnere40c2952009-04-14 21:34:55 +0000654 SourceLocation Loc = Tok.getLocation();
655 // If we see an identifier that is not a type name, we normally would
656 // parse it as the identifer being declared. However, when a typename
657 // is typo'd or the definition is not included, this will incorrectly
658 // parse the typename as the identifier name and fall over misparsing
659 // later parts of the diagnostic.
660 //
661 // As such, we try to do some look-ahead in cases where this would
662 // otherwise be an "implicit-int" case to see if this is invalid. For
663 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
664 // an identifier with implicit int, we'd get a parse error because the
665 // next token is obviously invalid for a type. Parse these as a case
666 // with an invalid type specifier.
667 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Chris Lattnere40c2952009-04-14 21:34:55 +0000669 // Since we know that this either implicit int (which is rare) or an
670 // error, we'd do lookahead to try to do better recovery.
671 if (isValidAfterIdentifierInDeclarator(NextToken())) {
672 // If this token is valid for implicit int, e.g. "static x = 4", then
673 // we just avoid eating the identifier, so it will be parsed as the
674 // identifier in the declarator.
675 return false;
676 }
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Chris Lattnere40c2952009-04-14 21:34:55 +0000678 // Otherwise, if we don't consume this token, we are going to emit an
679 // error anyway. Try to recover from various common problems. Check
680 // to see if this was a reference to a tag name without a tag specified.
681 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000682 //
683 // C++ doesn't need this, and isTagName doesn't take SS.
684 if (SS == 0) {
685 const char *TagName = 0;
686 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000687
Chris Lattnere40c2952009-04-14 21:34:55 +0000688 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
689 default: break;
690 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
691 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
692 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
693 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
694 }
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Chris Lattnerf4382f52009-04-14 22:17:06 +0000696 if (TagName) {
697 Diag(Loc, diag::err_use_of_tag_name_without_tag)
698 << Tok.getIdentifierInfo() << TagName
699 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Chris Lattnerf4382f52009-04-14 22:17:06 +0000701 // Parse this as a tag as if the missing tag were present.
702 if (TagKind == tok::kw_enum)
703 ParseEnumSpecifier(Loc, DS, AS);
704 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000705 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000706 return true;
707 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000708 }
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Douglas Gregora786fdb2009-10-13 23:27:22 +0000710 // This is almost certainly an invalid type name. Let the action emit a
711 // diagnostic and attempt to recover.
712 Action::TypeTy *T = 0;
713 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
714 CurScope, SS, T)) {
715 // The action emitted a diagnostic, so we don't have to.
716 if (T) {
717 // The action has suggested that the type T could be used. Set that as
718 // the type in the declaration specifiers, consume the would-be type
719 // name token, and we're done.
720 const char *PrevSpec;
721 unsigned DiagID;
722 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
723 false);
724 DS.SetRangeEnd(Tok.getLocation());
725 ConsumeToken();
726
727 // There may be other declaration specifiers after this.
728 return true;
729 }
730
731 // Fall through; the action had no suggestion for us.
732 } else {
733 // The action did not emit a diagnostic, so emit one now.
734 SourceRange R;
735 if (SS) R = SS->getRange();
736 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
737 }
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Douglas Gregora786fdb2009-10-13 23:27:22 +0000739 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000740 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000741 unsigned DiagID;
742 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000743 DS.SetRangeEnd(Tok.getLocation());
744 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Chris Lattnere40c2952009-04-14 21:34:55 +0000746 // TODO: Could inject an invalid typedef decl in an enclosing scope to
747 // avoid rippling error messages on subsequent uses of the same type,
748 // could be useful if #include was forgotten.
749 return false;
750}
751
Reid Spencer5f016e22007-07-11 17:01:13 +0000752/// ParseDeclarationSpecifiers
753/// declaration-specifiers: [C99 6.7]
754/// storage-class-specifier declaration-specifiers[opt]
755/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000756/// [C99] function-specifier declaration-specifiers[opt]
757/// [GNU] attributes declaration-specifiers[opt]
758///
759/// storage-class-specifier: [C99 6.7.1]
760/// 'typedef'
761/// 'extern'
762/// 'static'
763/// 'auto'
764/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000765/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000766/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000767/// function-specifier: [C99 6.7.4]
768/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000769/// [C++] 'virtual'
770/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000771/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000772/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000773
Reid Spencer5f016e22007-07-11 17:01:13 +0000774///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000775void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000776 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000777 AccessSpecifier AS,
778 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000779 if (Tok.is(tok::code_completion)) {
780 Actions.CodeCompleteOrdinaryName(CurScope);
781 ConsumeToken();
782 }
783
Chris Lattner81c018d2008-03-13 06:29:04 +0000784 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000786 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000788 unsigned DiagID = 0;
789
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000791
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000793 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000794 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000795 // If this is not a declaration specifier token, we're done reading decl
796 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000797 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000798 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Chris Lattner5e02c472009-01-05 00:07:25 +0000800 case tok::coloncolon: // ::foo::bar
801 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000802 if (TryAnnotateCXXScopeToken(true))
Chris Lattner5e02c472009-01-05 00:07:25 +0000803 continue;
804 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000805
806 case tok::annot_cxxscope: {
807 if (DS.hasTypeSpecifier())
808 goto DoneWithDeclSpec;
809
810 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000811 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000812 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000813 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000814 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000815 // We have a qualified template-id, e.g., N::A<int>
816 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000817 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000818 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000819 "ParseOptionalCXXScopeSpecifier not working");
820 AnnotateTemplateIdTokenAsType(&SS);
821 continue;
822 }
823
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000824 if (Next.is(tok::annot_typename)) {
825 // FIXME: is this scope-specifier getting dropped?
826 ConsumeToken(); // the scope-specifier
827 if (Tok.getAnnotationValue())
828 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
829 PrevSpec, DiagID,
830 Tok.getAnnotationValue());
831 else
832 DS.SetTypeSpecError();
833 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
834 ConsumeToken(); // The typename
835 }
836
Douglas Gregor9135c722009-03-25 15:40:00 +0000837 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000838 goto DoneWithDeclSpec;
839
840 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000841 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000842 SS.setRange(Tok.getAnnotationRange());
843
844 // If the next token is the name of the class type that the C++ scope
845 // denotes, followed by a '(', then this is a constructor declaration.
846 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000847 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000848 CurScope, &SS) &&
849 GetLookAheadToken(2).is(tok::l_paren))
850 goto DoneWithDeclSpec;
851
Douglas Gregorb696ea32009-02-04 17:00:24 +0000852 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
853 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000854
Chris Lattnerf4382f52009-04-14 22:17:06 +0000855 // If the referenced identifier is not a type, then this declspec is
856 // erroneous: We already checked about that it has no type specifier, and
857 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000858 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000859 if (TypeRep == 0) {
860 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000861 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000862 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000863 }
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000865 ConsumeToken(); // The C++ scope.
866
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000867 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000868 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000869 if (isInvalid)
870 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000871
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000872 DS.SetRangeEnd(Tok.getLocation());
873 ConsumeToken(); // The typename.
874
875 continue;
876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Chris Lattner80d0c892009-01-21 19:48:37 +0000878 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000879 if (Tok.getAnnotationValue())
880 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000881 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +0000882 else
883 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000884 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
885 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Chris Lattner80d0c892009-01-21 19:48:37 +0000887 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
888 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
889 // Objective-C interface. If we don't have Objective-C or a '<', this is
890 // just a normal reference to a typedef name.
891 if (!Tok.is(tok::less) || !getLang().ObjC1)
892 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000894 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000895 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000896 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
897 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
898 LAngleLoc, EndProtoLoc);
899 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
900 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Chris Lattner80d0c892009-01-21 19:48:37 +0000902 DS.SetRangeEnd(EndProtoLoc);
903 continue;
904 }
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Chris Lattner3bd934a2008-07-26 01:18:38 +0000906 // typedef-name
907 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000908 // In C++, check to see if this is a scope specifier like foo::bar::, if
909 // so handle it as such. This is important for ctor parsing.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000910 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner837acd02009-01-21 19:19:26 +0000911 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Chris Lattner3bd934a2008-07-26 01:18:38 +0000913 // This identifier can only be a typedef name if we haven't already seen
914 // a type-specifier. Without this check we misparse:
915 // typedef int X; struct Y { short X; }; as 'short int'.
916 if (DS.hasTypeSpecifier())
917 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Chris Lattner3bd934a2008-07-26 01:18:38 +0000919 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +0000920 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +0000921 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000922
Chris Lattnerc199ab32009-04-12 20:42:31 +0000923 // If this is not a typedef name, don't parse it as part of the declspec,
924 // it must be an implicit int or an error.
925 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000926 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000927 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000928 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000929
Douglas Gregorb48fe382008-10-31 09:07:45 +0000930 // C++: If the identifier is actually the name of the class type
931 // being defined and the next token is a '(', then this is a
932 // constructor declaration. We're done with the decl-specifiers
933 // and will treat this token as an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000934 if (getLang().CPlusPlus &&
935 (CurScope->isClassScope() ||
936 (CurScope->isTemplateParamScope() &&
Douglas Gregordec06662009-08-21 18:42:58 +0000937 CurScope->getParent()->isClassScope())) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000938 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000939 NextToken().getKind() == tok::l_paren)
940 goto DoneWithDeclSpec;
941
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000942 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000943 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000944 if (isInvalid)
945 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Chris Lattner3bd934a2008-07-26 01:18:38 +0000947 DS.SetRangeEnd(Tok.getLocation());
948 ConsumeToken(); // The identifier
949
950 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
951 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
952 // Objective-C interface. If we don't have Objective-C or a '<', this is
953 // just a normal reference to a typedef name.
954 if (!Tok.is(tok::less) || !getLang().ObjC1)
955 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000957 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000958 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000959 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
960 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
961 LAngleLoc, EndProtoLoc);
962 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
963 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Chris Lattner3bd934a2008-07-26 01:18:38 +0000965 DS.SetRangeEnd(EndProtoLoc);
966
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000967 // Need to support trailing type qualifiers (e.g. "id<p> const").
968 // If a type specifier follows, it will be diagnosed elsewhere.
969 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000970 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000971
972 // type-name
973 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +0000974 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000975 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000976 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000977 // This template-id does not refer to a type name, so we're
978 // done with the type-specifiers.
979 goto DoneWithDeclSpec;
980 }
981
982 // Turn the template-id annotation token into a type annotation
983 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000984 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000985 continue;
986 }
987
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 // GNU attributes support.
989 case tok::kw___attribute:
990 DS.AddAttributes(ParseAttributes());
991 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000992
993 // Microsoft declspec support.
994 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +0000995 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +0000996 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Steve Naroff239f0732008-12-25 14:16:32 +0000998 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000999 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001000 // FIXME: Add handling here!
1001 break;
1002
1003 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001004 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001005 case tok::kw___cdecl:
1006 case tok::kw___stdcall:
1007 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001008 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1009 continue;
1010
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 // storage-class-specifier
1012 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001013 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1014 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001015 break;
1016 case tok::kw_extern:
1017 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001018 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001019 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1020 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001022 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001023 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001024 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001025 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 case tok::kw_static:
1027 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001028 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001029 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1030 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001031 break;
1032 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001033 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001034 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1035 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001036 else
John McCallfec54012009-08-03 20:12:06 +00001037 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1038 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 break;
1040 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001041 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1042 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001044 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001045 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1046 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001047 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001049 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Reid Spencer5f016e22007-07-11 17:01:13 +00001052 // function-specifier
1053 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001054 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001056 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001057 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001058 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001059 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001060 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001061 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001062
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001063 // friend
1064 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001065 if (DSContext == DSC_class)
1066 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1067 else {
1068 PrevSpec = ""; // not actually used by the diagnostic
1069 DiagID = diag::err_friend_invalid_in_context;
1070 isInvalid = true;
1071 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001072 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Sebastian Redl2ac67232009-11-05 15:47:02 +00001074 // constexpr
1075 case tok::kw_constexpr:
1076 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1077 break;
1078
Chris Lattner80d0c892009-01-21 19:48:37 +00001079 // type-specifier
1080 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001081 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1082 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001083 break;
1084 case tok::kw_long:
1085 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001086 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1087 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001088 else
John McCallfec54012009-08-03 20:12:06 +00001089 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1090 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001091 break;
1092 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001093 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1094 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001095 break;
1096 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001097 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1098 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001099 break;
1100 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001101 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1102 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001103 break;
1104 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001105 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1106 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001107 break;
1108 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001109 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1110 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001111 break;
1112 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001113 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1114 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001115 break;
1116 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001117 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1118 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001119 break;
1120 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001121 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1122 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001123 break;
1124 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001125 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1126 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001127 break;
1128 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001129 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1130 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001131 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001132 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001133 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1134 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001135 break;
1136 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001137 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1138 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001139 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001140 case tok::kw_bool:
1141 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001142 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1143 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001144 break;
1145 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001146 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1147 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001148 break;
1149 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001150 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1151 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001152 break;
1153 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001154 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1155 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001156 break;
1157
1158 // class-specifier:
1159 case tok::kw_class:
1160 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001161 case tok::kw_union: {
1162 tok::TokenKind Kind = Tok.getKind();
1163 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001164 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001165 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001166 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001167
1168 // enum-specifier:
1169 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001170 ConsumeToken();
1171 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001172 continue;
1173
1174 // cv-qualifier:
1175 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001176 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1177 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001178 break;
1179 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001180 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1181 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001182 break;
1183 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001184 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1185 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001186 break;
1187
Douglas Gregord57959a2009-03-27 23:10:48 +00001188 // C++ typename-specifier:
1189 case tok::kw_typename:
1190 if (TryAnnotateTypeOrScopeToken())
1191 continue;
1192 break;
1193
Chris Lattner80d0c892009-01-21 19:48:37 +00001194 // GNU typeof support.
1195 case tok::kw_typeof:
1196 ParseTypeofSpecifier(DS);
1197 continue;
1198
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001199 case tok::kw_decltype:
1200 ParseDecltypeSpecifier(DS);
1201 continue;
1202
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001203 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001204 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001205 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1206 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001207 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001208 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Chris Lattnerbce61352008-07-26 00:20:22 +00001210 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001211 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001212 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001213 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1214 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1215 LAngleLoc, EndProtoLoc);
1216 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1217 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001218 DS.SetRangeEnd(EndProtoLoc);
1219
Chris Lattner1ab3b962008-11-18 07:48:38 +00001220 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001221 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001222 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001223 // Need to support trailing type qualifiers (e.g. "id<p> const").
1224 // If a type specifier follows, it will be diagnosed elsewhere.
1225 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001226 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001227 }
John McCallfec54012009-08-03 20:12:06 +00001228 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 if (isInvalid) {
1230 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001231 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001232 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001233 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001234 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001235 ConsumeToken();
1236 }
1237}
Douglas Gregoradcac882008-12-01 23:54:00 +00001238
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001239/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001240/// primarily follow the C++ grammar with additions for C99 and GNU,
1241/// which together subsume the C grammar. Note that the C++
1242/// type-specifier also includes the C type-qualifier (for const,
1243/// volatile, and C99 restrict). Returns true if a type-specifier was
1244/// found (and parsed), false otherwise.
1245///
1246/// type-specifier: [C++ 7.1.5]
1247/// simple-type-specifier
1248/// class-specifier
1249/// enum-specifier
1250/// elaborated-type-specifier [TODO]
1251/// cv-qualifier
1252///
1253/// cv-qualifier: [C++ 7.1.5.1]
1254/// 'const'
1255/// 'volatile'
1256/// [C99] 'restrict'
1257///
1258/// simple-type-specifier: [ C++ 7.1.5.2]
1259/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1260/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1261/// 'char'
1262/// 'wchar_t'
1263/// 'bool'
1264/// 'short'
1265/// 'int'
1266/// 'long'
1267/// 'signed'
1268/// 'unsigned'
1269/// 'float'
1270/// 'double'
1271/// 'void'
1272/// [C99] '_Bool'
1273/// [C99] '_Complex'
1274/// [C99] '_Imaginary' // Removed in TC2?
1275/// [GNU] '_Decimal32'
1276/// [GNU] '_Decimal64'
1277/// [GNU] '_Decimal128'
1278/// [GNU] typeof-specifier
1279/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1280/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001281/// [C++0x] 'decltype' ( expression )
John McCallfec54012009-08-03 20:12:06 +00001282bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001283 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001284 unsigned &DiagID,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001285 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001286 SourceLocation Loc = Tok.getLocation();
1287
1288 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001289 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001290 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001291 // Annotate typenames and C++ scope specifiers. If we get one, just
1292 // recurse to handle whatever we get.
1293 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001294 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1295 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001296 // Otherwise, not a type specifier.
1297 return false;
1298 case tok::coloncolon: // ::foo::bar
1299 if (NextToken().is(tok::kw_new) || // ::new
1300 NextToken().is(tok::kw_delete)) // ::delete
1301 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Chris Lattner166a8fc2009-01-04 23:41:41 +00001303 // Annotate typenames and C++ scope specifiers. If we get one, just
1304 // recurse to handle whatever we get.
1305 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001306 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1307 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001308 // Otherwise, not a type specifier.
1309 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Douglas Gregor12e083c2008-11-07 15:42:26 +00001311 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001312 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001313 if (Tok.getAnnotationValue())
1314 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001315 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001316 else
1317 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001318 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1319 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Douglas Gregor12e083c2008-11-07 15:42:26 +00001321 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1322 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1323 // Objective-C interface. If we don't have Objective-C or a '<', this is
1324 // just a normal reference to a typedef name.
1325 if (!Tok.is(tok::less) || !getLang().ObjC1)
1326 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001328 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001329 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001330 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1331 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1332 LAngleLoc, EndProtoLoc);
1333 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1334 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001335
Douglas Gregor12e083c2008-11-07 15:42:26 +00001336 DS.SetRangeEnd(EndProtoLoc);
1337 return true;
1338 }
1339
1340 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001341 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001342 break;
1343 case tok::kw_long:
1344 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001345 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1346 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001347 else
John McCallfec54012009-08-03 20:12:06 +00001348 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1349 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001350 break;
1351 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001352 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001353 break;
1354 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001355 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1356 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001357 break;
1358 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001359 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1360 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001361 break;
1362 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001363 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1364 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001365 break;
1366 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001367 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001368 break;
1369 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001370 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001371 break;
1372 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001373 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001374 break;
1375 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001376 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001377 break;
1378 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001379 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001380 break;
1381 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001382 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001383 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001384 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001385 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001386 break;
1387 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001388 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001389 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001390 case tok::kw_bool:
1391 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001392 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001393 break;
1394 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001395 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1396 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001397 break;
1398 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001399 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1400 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001401 break;
1402 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001403 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1404 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001405 break;
1406
1407 // class-specifier:
1408 case tok::kw_class:
1409 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001410 case tok::kw_union: {
1411 tok::TokenKind Kind = Tok.getKind();
1412 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001413 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001414 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001415 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001416
1417 // enum-specifier:
1418 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001419 ConsumeToken();
1420 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001421 return true;
1422
1423 // cv-qualifier:
1424 case tok::kw_const:
1425 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001426 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001427 break;
1428 case tok::kw_volatile:
1429 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001430 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001431 break;
1432 case tok::kw_restrict:
1433 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001434 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001435 break;
1436
1437 // GNU typeof support.
1438 case tok::kw_typeof:
1439 ParseTypeofSpecifier(DS);
1440 return true;
1441
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001442 // C++0x decltype support.
1443 case tok::kw_decltype:
1444 ParseDecltypeSpecifier(DS);
1445 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001447 // C++0x auto support.
1448 case tok::kw_auto:
1449 if (!getLang().CPlusPlus0x)
1450 return false;
1451
John McCallfec54012009-08-03 20:12:06 +00001452 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001453 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001454 case tok::kw___ptr64:
1455 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001456 case tok::kw___cdecl:
1457 case tok::kw___stdcall:
1458 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001459 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001460 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001461
Douglas Gregor12e083c2008-11-07 15:42:26 +00001462 default:
1463 // Not a type-specifier; do nothing.
1464 return false;
1465 }
1466
1467 // If the specifier combination wasn't legal, issue a diagnostic.
1468 if (isInvalid) {
1469 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001470 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001471 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001472 }
1473 DS.SetRangeEnd(Tok.getLocation());
1474 ConsumeToken(); // whatever we parsed above.
1475 return true;
1476}
Reid Spencer5f016e22007-07-11 17:01:13 +00001477
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001478/// ParseStructDeclaration - Parse a struct declaration without the terminating
1479/// semicolon.
1480///
Reid Spencer5f016e22007-07-11 17:01:13 +00001481/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001482/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001483/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001484/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001485/// struct-declarator-list:
1486/// struct-declarator
1487/// struct-declarator-list ',' struct-declarator
1488/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1489/// struct-declarator:
1490/// declarator
1491/// [GNU] declarator attributes[opt]
1492/// declarator[opt] ':' constant-expression
1493/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1494///
Chris Lattnere1359422008-04-10 06:46:29 +00001495void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001496ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001497 if (Tok.is(tok::kw___extension__)) {
1498 // __extension__ silences extension warnings in the subexpression.
1499 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001500 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001501 return ParseStructDeclaration(DS, Fields);
1502 }
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Steve Naroff28a7ca82007-08-20 22:28:22 +00001504 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001505 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001506 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001508 // If there are no declarators, this is a free-standing declaration
1509 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001510 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001511 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001512 return;
1513 }
1514
1515 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001516 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001517 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001518 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001519 FieldDeclarator DeclaratorInfo(DS);
1520
1521 // Attributes are only allowed here on successive declarators.
1522 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1523 SourceLocation Loc;
1524 AttributeList *AttrList = ParseAttributes(&Loc);
1525 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1526 }
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Steve Naroff28a7ca82007-08-20 22:28:22 +00001528 /// struct-declarator: declarator
1529 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001530 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001531 ParseDeclarator(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Chris Lattner04d66662007-10-09 17:33:22 +00001533 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001534 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001535 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001536 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001537 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001538 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001539 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001540 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001541
Steve Naroff28a7ca82007-08-20 22:28:22 +00001542 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001543 if (Tok.is(tok::kw___attribute)) {
1544 SourceLocation Loc;
1545 AttributeList *AttrList = ParseAttributes(&Loc);
1546 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1547 }
1548
John McCallbdd563e2009-11-03 02:38:08 +00001549 // We're done with this declarator; invoke the callback.
John McCall54abf7d2009-11-04 02:18:39 +00001550 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1551 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001552
Steve Naroff28a7ca82007-08-20 22:28:22 +00001553 // If we don't have a comma, it is either the end of the list (a ';')
1554 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001555 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001556 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001557
Steve Naroff28a7ca82007-08-20 22:28:22 +00001558 // Consume the comma.
1559 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001560
John McCallbdd563e2009-11-03 02:38:08 +00001561 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001562 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001563}
1564
1565/// ParseStructUnionBody
1566/// struct-contents:
1567/// struct-declaration-list
1568/// [EXT] empty
1569/// [GNU] "struct-declaration-list" without terminatoring ';'
1570/// struct-declaration-list:
1571/// struct-declaration
1572/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001573/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001574///
Reid Spencer5f016e22007-07-11 17:01:13 +00001575void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001576 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001577 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1578 PP.getSourceManager(),
1579 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Reid Spencer5f016e22007-07-11 17:01:13 +00001581 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001582
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001583 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001584 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1585
Reid Spencer5f016e22007-07-11 17:01:13 +00001586 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1587 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001588 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001589 Diag(Tok, diag::ext_empty_struct_union_enum)
1590 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001591
Chris Lattnerb28317a2009-03-28 19:18:32 +00001592 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001593
Reid Spencer5f016e22007-07-11 17:01:13 +00001594 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001595 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001596 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Reid Spencer5f016e22007-07-11 17:01:13 +00001598 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001599 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001600 Diag(Tok, diag::ext_extra_struct_semi)
1601 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001602 ConsumeToken();
1603 continue;
1604 }
Chris Lattnere1359422008-04-10 06:46:29 +00001605
1606 // Parse all the comma separated declarators.
1607 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001608
John McCallbdd563e2009-11-03 02:38:08 +00001609 if (!Tok.is(tok::at)) {
1610 struct CFieldCallback : FieldCallback {
1611 Parser &P;
1612 DeclPtrTy TagDecl;
1613 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1614
1615 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1616 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1617 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1618
1619 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001620 // Install the declarator into the current TagDecl.
John McCall4ba39712009-11-03 21:13:47 +00001621 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1622 FD.D.getDeclSpec().getSourceRange().getBegin(),
1623 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001624 FieldDecls.push_back(Field);
1625 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001626 }
John McCallbdd563e2009-11-03 02:38:08 +00001627 } Callback(*this, TagDecl, FieldDecls);
1628
1629 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001630 } else { // Handle @defs
1631 ConsumeToken();
1632 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1633 Diag(Tok, diag::err_unexpected_at);
1634 SkipUntil(tok::semi, true, true);
1635 continue;
1636 }
1637 ConsumeToken();
1638 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1639 if (!Tok.is(tok::identifier)) {
1640 Diag(Tok, diag::err_expected_ident);
1641 SkipUntil(tok::semi, true, true);
1642 continue;
1643 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001644 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001645 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001646 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001647 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1648 ConsumeToken();
1649 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001650 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001651
Chris Lattner04d66662007-10-09 17:33:22 +00001652 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001653 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001654 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001655 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001656 break;
1657 } else {
1658 Diag(Tok, diag::err_expected_semi_decl_list);
1659 // Skip to end of block or statement
1660 SkipUntil(tok::r_brace, true, true);
1661 }
1662 }
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Steve Naroff60fccee2007-10-29 21:38:07 +00001664 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Reid Spencer5f016e22007-07-11 17:01:13 +00001666 AttributeList *AttrList = 0;
1667 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001668 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001669 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001670
1671 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001672 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001673 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001674 AttrList);
1675 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001676 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001677}
1678
1679
1680/// ParseEnumSpecifier
1681/// enum-specifier: [C99 6.7.2.2]
1682/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001683///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001684/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1685/// '}' attributes[opt]
1686/// 'enum' identifier
1687/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001688///
1689/// [C++] elaborated-type-specifier:
1690/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1691///
Chris Lattner4c97d762009-04-12 21:49:30 +00001692void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1693 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001694 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001695 if (Tok.is(tok::code_completion)) {
1696 // Code completion for an enum name.
1697 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1698 ConsumeToken();
1699 }
1700
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001701 AttributeList *Attr = 0;
1702 // If attributes exist after tag, parse them.
1703 if (Tok.is(tok::kw___attribute))
1704 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001705
1706 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001707 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001708 if (Tok.isNot(tok::identifier)) {
1709 Diag(Tok, diag::err_expected_ident);
1710 if (Tok.isNot(tok::l_brace)) {
1711 // Has no name and is not a definition.
1712 // Skip the rest of this declarator, up until the comma or semicolon.
1713 SkipUntil(tok::comma, true);
1714 return;
1715 }
1716 }
1717 }
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001719 // Must have either 'enum name' or 'enum {...}'.
1720 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1721 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001722
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001723 // Skip the rest of this declarator, up until the comma or semicolon.
1724 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001725 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001726 }
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001728 // If an identifier is present, consume and remember it.
1729 IdentifierInfo *Name = 0;
1730 SourceLocation NameLoc;
1731 if (Tok.is(tok::identifier)) {
1732 Name = Tok.getIdentifierInfo();
1733 NameLoc = ConsumeToken();
1734 }
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001736 // There are three options here. If we have 'enum foo;', then this is a
1737 // forward declaration. If we have 'enum foo {...' then this is a
1738 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1739 //
1740 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1741 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1742 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1743 //
John McCall0f434ec2009-07-31 02:45:11 +00001744 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001745 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001746 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001747 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001748 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001749 else
John McCall0f434ec2009-07-31 02:45:11 +00001750 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001751 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001752 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00001753 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001754 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001755 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001756 Owned, IsDependent);
1757 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Chris Lattner04d66662007-10-09 17:33:22 +00001759 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001760 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Reid Spencer5f016e22007-07-11 17:01:13 +00001762 // TODO: semantic analysis on the declspec for enums.
1763 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001764 unsigned DiagID;
1765 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001766 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001767 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001768}
1769
1770/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1771/// enumerator-list:
1772/// enumerator
1773/// enumerator-list ',' enumerator
1774/// enumerator:
1775/// enumeration-constant
1776/// enumeration-constant '=' constant-expression
1777/// enumeration-constant:
1778/// identifier
1779///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001780void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001781 // Enter the scope of the enum body and start the definition.
1782 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001783 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001784
Reid Spencer5f016e22007-07-11 17:01:13 +00001785 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Chris Lattner7946dd32007-08-27 17:24:30 +00001787 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001788 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001789 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Chris Lattnerb28317a2009-03-28 19:18:32 +00001791 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001792
Chris Lattnerb28317a2009-03-28 19:18:32 +00001793 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Reid Spencer5f016e22007-07-11 17:01:13 +00001795 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001796 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001797 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1798 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Reid Spencer5f016e22007-07-11 17:01:13 +00001800 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001801 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001802 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001804 AssignedVal = ParseConstantExpression();
1805 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001806 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001807 }
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Reid Spencer5f016e22007-07-11 17:01:13 +00001809 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001810 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1811 LastEnumConstDecl,
1812 IdentLoc, Ident,
1813 EqualLoc,
1814 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 EnumConstantDecls.push_back(EnumConstDecl);
1816 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Chris Lattner04d66662007-10-09 17:33:22 +00001818 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001819 break;
1820 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001821
1822 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001823 !(getLang().C99 || getLang().CPlusPlus0x))
1824 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1825 << getLang().CPlusPlus
1826 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001827 }
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Reid Spencer5f016e22007-07-11 17:01:13 +00001829 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001830 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001831
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001832 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001834 if (Tok.is(tok::kw___attribute))
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001835 Attr = ParseAttributes();
Douglas Gregor72de6672009-01-08 20:45:30 +00001836
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001837 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1838 EnumConstantDecls.data(), EnumConstantDecls.size(),
1839 CurScope, Attr);
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Douglas Gregor72de6672009-01-08 20:45:30 +00001841 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001842 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001843}
1844
1845/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001846/// start of a type-qualifier-list.
1847bool Parser::isTypeQualifier() const {
1848 switch (Tok.getKind()) {
1849 default: return false;
1850 // type-qualifier
1851 case tok::kw_const:
1852 case tok::kw_volatile:
1853 case tok::kw_restrict:
1854 return true;
1855 }
1856}
1857
1858/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001859/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001860bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001861 switch (Tok.getKind()) {
1862 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001863
Chris Lattner166a8fc2009-01-04 23:41:41 +00001864 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001865 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001866 // Annotate typenames and C++ scope specifiers. If we get one, just
1867 // recurse to handle whatever we get.
1868 if (TryAnnotateTypeOrScopeToken())
1869 return isTypeSpecifierQualifier();
1870 // Otherwise, not a type specifier.
1871 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001872
Chris Lattner166a8fc2009-01-04 23:41:41 +00001873 case tok::coloncolon: // ::foo::bar
1874 if (NextToken().is(tok::kw_new) || // ::new
1875 NextToken().is(tok::kw_delete)) // ::delete
1876 return false;
1877
1878 // Annotate typenames and C++ scope specifiers. If we get one, just
1879 // recurse to handle whatever we get.
1880 if (TryAnnotateTypeOrScopeToken())
1881 return isTypeSpecifierQualifier();
1882 // Otherwise, not a type specifier.
1883 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001884
Reid Spencer5f016e22007-07-11 17:01:13 +00001885 // GNU attributes support.
1886 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001887 // GNU typeof support.
1888 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00001889
Reid Spencer5f016e22007-07-11 17:01:13 +00001890 // type-specifiers
1891 case tok::kw_short:
1892 case tok::kw_long:
1893 case tok::kw_signed:
1894 case tok::kw_unsigned:
1895 case tok::kw__Complex:
1896 case tok::kw__Imaginary:
1897 case tok::kw_void:
1898 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001899 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001900 case tok::kw_char16_t:
1901 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001902 case tok::kw_int:
1903 case tok::kw_float:
1904 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001905 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001906 case tok::kw__Bool:
1907 case tok::kw__Decimal32:
1908 case tok::kw__Decimal64:
1909 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Chris Lattner99dc9142008-04-13 18:59:07 +00001911 // struct-or-union-specifier (C99) or class-specifier (C++)
1912 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001913 case tok::kw_struct:
1914 case tok::kw_union:
1915 // enum-specifier
1916 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00001917
Reid Spencer5f016e22007-07-11 17:01:13 +00001918 // type-qualifier
1919 case tok::kw_const:
1920 case tok::kw_volatile:
1921 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001922
1923 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001924 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001925 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001926
Chris Lattner7c186be2008-10-20 00:25:30 +00001927 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1928 case tok::less:
1929 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Steve Naroff239f0732008-12-25 14:16:32 +00001931 case tok::kw___cdecl:
1932 case tok::kw___stdcall:
1933 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001934 case tok::kw___w64:
1935 case tok::kw___ptr64:
1936 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001937 }
1938}
1939
1940/// isDeclarationSpecifier() - Return true if the current token is part of a
1941/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001942bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001943 switch (Tok.getKind()) {
1944 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001945
Chris Lattner166a8fc2009-01-04 23:41:41 +00001946 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001947 // Unfortunate hack to support "Class.factoryMethod" notation.
1948 if (getLang().ObjC1 && NextToken().is(tok::period))
1949 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001950 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001951
Douglas Gregord57959a2009-03-27 23:10:48 +00001952 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001953 // Annotate typenames and C++ scope specifiers. If we get one, just
1954 // recurse to handle whatever we get.
1955 if (TryAnnotateTypeOrScopeToken())
1956 return isDeclarationSpecifier();
1957 // Otherwise, not a declaration specifier.
1958 return false;
1959 case tok::coloncolon: // ::foo::bar
1960 if (NextToken().is(tok::kw_new) || // ::new
1961 NextToken().is(tok::kw_delete)) // ::delete
1962 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001963
Chris Lattner166a8fc2009-01-04 23:41:41 +00001964 // Annotate typenames and C++ scope specifiers. If we get one, just
1965 // recurse to handle whatever we get.
1966 if (TryAnnotateTypeOrScopeToken())
1967 return isDeclarationSpecifier();
1968 // Otherwise, not a declaration specifier.
1969 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001970
Reid Spencer5f016e22007-07-11 17:01:13 +00001971 // storage-class-specifier
1972 case tok::kw_typedef:
1973 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001974 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001975 case tok::kw_static:
1976 case tok::kw_auto:
1977 case tok::kw_register:
1978 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00001979
Reid Spencer5f016e22007-07-11 17:01:13 +00001980 // type-specifiers
1981 case tok::kw_short:
1982 case tok::kw_long:
1983 case tok::kw_signed:
1984 case tok::kw_unsigned:
1985 case tok::kw__Complex:
1986 case tok::kw__Imaginary:
1987 case tok::kw_void:
1988 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001989 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001990 case tok::kw_char16_t:
1991 case tok::kw_char32_t:
1992
Reid Spencer5f016e22007-07-11 17:01:13 +00001993 case tok::kw_int:
1994 case tok::kw_float:
1995 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001996 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001997 case tok::kw__Bool:
1998 case tok::kw__Decimal32:
1999 case tok::kw__Decimal64:
2000 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00002001
Chris Lattner99dc9142008-04-13 18:59:07 +00002002 // struct-or-union-specifier (C99) or class-specifier (C++)
2003 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002004 case tok::kw_struct:
2005 case tok::kw_union:
2006 // enum-specifier
2007 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Reid Spencer5f016e22007-07-11 17:01:13 +00002009 // type-qualifier
2010 case tok::kw_const:
2011 case tok::kw_volatile:
2012 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002013
Reid Spencer5f016e22007-07-11 17:01:13 +00002014 // function-specifier
2015 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002016 case tok::kw_virtual:
2017 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002018
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002019 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002020 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002021
Chris Lattner1ef08762007-08-09 17:01:07 +00002022 // GNU typeof support.
2023 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002024
Chris Lattner1ef08762007-08-09 17:01:07 +00002025 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002026 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002027 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002028
Chris Lattnerf3948c42008-07-26 03:38:44 +00002029 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2030 case tok::less:
2031 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002032
Steve Naroff47f52092009-01-06 19:34:12 +00002033 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002034 case tok::kw___cdecl:
2035 case tok::kw___stdcall:
2036 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002037 case tok::kw___w64:
2038 case tok::kw___ptr64:
2039 case tok::kw___forceinline:
2040 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002041 }
2042}
2043
2044
2045/// ParseTypeQualifierListOpt
2046/// type-qualifier-list: [C99 6.7.5]
2047/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002048/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002049/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002050/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002051///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002052void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002053 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002054 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002055 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002056 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002057 SourceLocation Loc = Tok.getLocation();
2058
2059 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002060 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002061 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2062 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002063 break;
2064 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002065 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2066 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002067 break;
2068 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002069 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2070 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002071 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002072 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002073 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002074 case tok::kw___cdecl:
2075 case tok::kw___stdcall:
2076 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002077 if (AttributesAllowed) {
2078 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2079 continue;
2080 }
2081 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002082 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002083 if (AttributesAllowed) {
2084 DS.AddAttributes(ParseAttributes());
2085 continue; // do *not* consume the next token!
2086 }
2087 // otherwise, FALL THROUGH!
2088 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002089 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002090 // If this is not a type-qualifier token, we're done reading type
2091 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002092 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002093 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002094 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002095
Reid Spencer5f016e22007-07-11 17:01:13 +00002096 // If the specifier combination wasn't legal, issue a diagnostic.
2097 if (isInvalid) {
2098 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002099 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002100 }
2101 ConsumeToken();
2102 }
2103}
2104
2105
2106/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2107///
2108void Parser::ParseDeclarator(Declarator &D) {
2109 /// This implements the 'declarator' production in the C grammar, then checks
2110 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002111 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002112}
2113
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002114/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2115/// is parsed by the function passed to it. Pass null, and the direct-declarator
2116/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002117/// ptr-operator production.
2118///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002119/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2120/// [C] pointer[opt] direct-declarator
2121/// [C++] direct-declarator
2122/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002123///
2124/// pointer: [C99 6.7.5]
2125/// '*' type-qualifier-list[opt]
2126/// '*' type-qualifier-list[opt] pointer
2127///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002128/// ptr-operator:
2129/// '*' cv-qualifier-seq[opt]
2130/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002131/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002132/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002133/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002134/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002135void Parser::ParseDeclaratorInternal(Declarator &D,
2136 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002137 if (Diags.hasAllExtensionsSilenced())
2138 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002139 // C++ member pointers start with a '::' or a nested-name.
2140 // Member pointers get special handling, since there's no place for the
2141 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002142 if (getLang().CPlusPlus &&
2143 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2144 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002145 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002146 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002147 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002148 // The scope spec really belongs to the direct-declarator.
2149 D.getCXXScopeSpec() = SS;
2150 if (DirectDeclParser)
2151 (this->*DirectDeclParser)(D);
2152 return;
2153 }
2154
2155 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002156 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002157 DeclSpec DS;
2158 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002159 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002160
2161 // Recurse to parse whatever is left.
2162 ParseDeclaratorInternal(D, DirectDeclParser);
2163
2164 // Sema will have to catch (syntactically invalid) pointers into global
2165 // scope. It has to catch pointers into namespace scope anyway.
2166 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002167 Loc, DS.TakeAttributes()),
2168 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002169 return;
2170 }
2171 }
2172
2173 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002174 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002175 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002176 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002177 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002178 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002179 if (DirectDeclParser)
2180 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002181 return;
2182 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002183
Sebastian Redl05532f22009-03-15 22:02:01 +00002184 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2185 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002186 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002187 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002188
Chris Lattner9af55002009-03-27 04:18:06 +00002189 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002190 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002191 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002192
Reid Spencer5f016e22007-07-11 17:01:13 +00002193 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002194 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002195
Reid Spencer5f016e22007-07-11 17:01:13 +00002196 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002197 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002198 if (Kind == tok::star)
2199 // Remember that we parsed a pointer type, and remember the type-quals.
2200 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002201 DS.TakeAttributes()),
2202 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002203 else
2204 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002205 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002206 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002207 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002208 } else {
2209 // Is a reference
2210 DeclSpec DS;
2211
Sebastian Redl743de1f2009-03-23 00:00:23 +00002212 // Complain about rvalue references in C++03, but then go on and build
2213 // the declarator.
2214 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2215 Diag(Loc, diag::err_rvalue_reference);
2216
Reid Spencer5f016e22007-07-11 17:01:13 +00002217 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2218 // cv-qualifiers are introduced through the use of a typedef or of a
2219 // template type argument, in which case the cv-qualifiers are ignored.
2220 //
2221 // [GNU] Retricted references are allowed.
2222 // [GNU] Attributes on references are allowed.
2223 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002224 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002225
2226 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2227 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2228 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002229 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002230 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2231 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002232 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002233 }
2234
2235 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002236 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002237
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002238 if (D.getNumTypeObjects() > 0) {
2239 // C++ [dcl.ref]p4: There shall be no references to references.
2240 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2241 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002242 if (const IdentifierInfo *II = D.getIdentifier())
2243 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2244 << II;
2245 else
2246 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2247 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002248
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002249 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002250 // can go ahead and build the (technically ill-formed)
2251 // declarator: reference collapsing will take care of it.
2252 }
2253 }
2254
Reid Spencer5f016e22007-07-11 17:01:13 +00002255 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002256 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002257 DS.TakeAttributes(),
2258 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002259 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002260 }
2261}
2262
2263/// ParseDirectDeclarator
2264/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002265/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002266/// '(' declarator ')'
2267/// [GNU] '(' attributes declarator ')'
2268/// [C90] direct-declarator '[' constant-expression[opt] ']'
2269/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2270/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2271/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2272/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2273/// direct-declarator '(' parameter-type-list ')'
2274/// direct-declarator '(' identifier-list[opt] ')'
2275/// [GNU] direct-declarator '(' parameter-forward-declarations
2276/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002277/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2278/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002279/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002280///
2281/// declarator-id: [C++ 8]
2282/// id-expression
2283/// '::'[opt] nested-name-specifier[opt] type-name
2284///
2285/// id-expression: [C++ 5.1]
2286/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002287/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002288///
2289/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002290/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002291/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002292/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002293/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002294/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002295///
Reid Spencer5f016e22007-07-11 17:01:13 +00002296void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002297 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002298
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002299 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2300 // ParseDeclaratorInternal might already have parsed the scope.
2301 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2302 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2303 true);
2304 if (afterCXXScope) {
2305 // Change the declaration context for name lookup, until this function
2306 // is exited (and the declarator has been parsed).
2307 DeclScopeObj.EnterDeclaratorScope();
2308 }
2309
2310 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2311 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2312 // We found something that indicates the start of an unqualified-id.
2313 // Parse that unqualified-id.
2314 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2315 /*EnteringContext=*/true,
2316 /*AllowDestructorName=*/true,
2317 /*AllowConstructorName=*/!D.getDeclSpec().hasTypeSpecifier(),
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002318 /*ObjectType=*/0,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002319 D.getName())) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002320 D.SetIdentifier(0, Tok.getLocation());
2321 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002322 } else {
2323 // Parsed the unqualified-id; update range information and move along.
2324 if (D.getSourceRange().getBegin().isInvalid())
2325 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2326 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002327 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002328 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002329 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002330 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002331 assert(!getLang().CPlusPlus &&
2332 "There's a C++-specific check for tok::identifier above");
2333 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2334 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2335 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002336 goto PastIdentifier;
2337 }
2338
2339 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002340 // direct-declarator: '(' declarator ')'
2341 // direct-declarator: '(' attributes declarator ')'
2342 // Example: 'char (*X)' or 'int (*XX)(void)'
2343 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002344 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002345 // This could be something simple like "int" (in which case the declarator
2346 // portion is empty), if an abstract-declarator is allowed.
2347 D.SetIdentifier(0, Tok.getLocation());
2348 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002349 if (D.getContext() == Declarator::MemberContext)
2350 Diag(Tok, diag::err_expected_member_name_or_semi)
2351 << D.getDeclSpec().getSourceRange();
2352 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002353 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002354 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002355 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002356 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002357 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002358 }
Mike Stump1eb44332009-09-09 15:08:12 +00002359
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002360 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002361 assert(D.isPastIdentifier() &&
2362 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002363
Reid Spencer5f016e22007-07-11 17:01:13 +00002364 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002365 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002366 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2367 // In such a case, check if we actually have a function declarator; if it
2368 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002369 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2370 // When not in file scope, warn for ambiguous function declarators, just
2371 // in case the author intended it as a variable definition.
2372 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2373 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2374 break;
2375 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002376 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002377 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002378 ParseBracketDeclarator(D);
2379 } else {
2380 break;
2381 }
2382 }
2383}
2384
Chris Lattneref4715c2008-04-06 05:45:57 +00002385/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2386/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002387/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002388/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2389///
2390/// direct-declarator:
2391/// '(' declarator ')'
2392/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002393/// direct-declarator '(' parameter-type-list ')'
2394/// direct-declarator '(' identifier-list[opt] ')'
2395/// [GNU] direct-declarator '(' parameter-forward-declarations
2396/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002397///
2398void Parser::ParseParenDeclarator(Declarator &D) {
2399 SourceLocation StartLoc = ConsumeParen();
2400 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002401
Chris Lattner7399ee02008-10-20 02:05:46 +00002402 // Eat any attributes before we look at whether this is a grouping or function
2403 // declarator paren. If this is a grouping paren, the attribute applies to
2404 // the type being built up, for example:
2405 // int (__attribute__(()) *x)(long y)
2406 // If this ends up not being a grouping paren, the attribute applies to the
2407 // first argument, for example:
2408 // int (__attribute__(()) int x)
2409 // In either case, we need to eat any attributes to be able to determine what
2410 // sort of paren this is.
2411 //
2412 AttributeList *AttrList = 0;
2413 bool RequiresArg = false;
2414 if (Tok.is(tok::kw___attribute)) {
2415 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00002416
Chris Lattner7399ee02008-10-20 02:05:46 +00002417 // We require that the argument list (if this is a non-grouping paren) be
2418 // present even if the attribute list was empty.
2419 RequiresArg = true;
2420 }
Steve Naroff239f0732008-12-25 14:16:32 +00002421 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002422 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2423 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2424 Tok.is(tok::kw___ptr64)) {
2425 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2426 }
Mike Stump1eb44332009-09-09 15:08:12 +00002427
Chris Lattneref4715c2008-04-06 05:45:57 +00002428 // If we haven't past the identifier yet (or where the identifier would be
2429 // stored, if this is an abstract declarator), then this is probably just
2430 // grouping parens. However, if this could be an abstract-declarator, then
2431 // this could also be the start of function arguments (consider 'void()').
2432 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002433
Chris Lattneref4715c2008-04-06 05:45:57 +00002434 if (!D.mayOmitIdentifier()) {
2435 // If this can't be an abstract-declarator, this *must* be a grouping
2436 // paren, because we haven't seen the identifier yet.
2437 isGrouping = true;
2438 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002439 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002440 isDeclarationSpecifier()) { // 'int(int)' is a function.
2441 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2442 // considered to be a type, not a K&R identifier-list.
2443 isGrouping = false;
2444 } else {
2445 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2446 isGrouping = true;
2447 }
Mike Stump1eb44332009-09-09 15:08:12 +00002448
Chris Lattneref4715c2008-04-06 05:45:57 +00002449 // If this is a grouping paren, handle:
2450 // direct-declarator: '(' declarator ')'
2451 // direct-declarator: '(' attributes declarator ')'
2452 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002453 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002454 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002455 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002456 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002457
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002458 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002459 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002460 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002461
2462 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002463 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002464 return;
2465 }
Mike Stump1eb44332009-09-09 15:08:12 +00002466
Chris Lattneref4715c2008-04-06 05:45:57 +00002467 // Okay, if this wasn't a grouping paren, it must be the start of a function
2468 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002469 // identifier (and remember where it would have been), then call into
2470 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002471 D.SetIdentifier(0, Tok.getLocation());
2472
Chris Lattner7399ee02008-10-20 02:05:46 +00002473 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002474}
2475
2476/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2477/// declarator D up to a paren, which indicates that we are parsing function
2478/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002479///
Chris Lattner7399ee02008-10-20 02:05:46 +00002480/// If AttrList is non-null, then the caller parsed those arguments immediately
2481/// after the open paren - they should be considered to be the first argument of
2482/// a parameter. If RequiresArg is true, then the first argument of the
2483/// function is required to be present and required to not be an identifier
2484/// list.
2485///
Reid Spencer5f016e22007-07-11 17:01:13 +00002486/// This method also handles this portion of the grammar:
2487/// parameter-type-list: [C99 6.7.5]
2488/// parameter-list
2489/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002490/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002491///
2492/// parameter-list: [C99 6.7.5]
2493/// parameter-declaration
2494/// parameter-list ',' parameter-declaration
2495///
2496/// parameter-declaration: [C99 6.7.5]
2497/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002498/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002499/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002500/// declaration-specifiers abstract-declarator[opt]
2501/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002502/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002503/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2504///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002505/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002506/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002507///
Chris Lattner7399ee02008-10-20 02:05:46 +00002508void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2509 AttributeList *AttrList,
2510 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002511 // lparen is already consumed!
2512 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002513
Chris Lattner7399ee02008-10-20 02:05:46 +00002514 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002515 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002516 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002517 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002518 delete AttrList;
2519 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002520
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002521 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2522 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002523
2524 // cv-qualifier-seq[opt].
2525 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002526 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002527 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002528 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002529 llvm::SmallVector<TypeTy*, 2> Exceptions;
2530 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002531 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002532 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002533 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002534 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002535
2536 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002537 if (Tok.is(tok::kw_throw)) {
2538 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002539 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002540 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002541 hasAnyExceptionSpec);
2542 assert(Exceptions.size() == ExceptionRanges.size() &&
2543 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002544 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002545 }
2546
Chris Lattnerf97409f2008-04-06 06:57:35 +00002547 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002548 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002549 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002550 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002551 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002552 /*arglist*/ 0, 0,
2553 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002554 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002555 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002556 Exceptions.data(),
2557 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002558 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002559 LParenLoc, RParenLoc, D),
2560 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002561 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002562 }
2563
Chris Lattner7399ee02008-10-20 02:05:46 +00002564 // Alternatively, this parameter list may be an identifier list form for a
2565 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002566 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002567 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002568 // K&R identifier lists can't have typedefs as identifiers, per
2569 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002570 if (RequiresArg) {
2571 Diag(Tok, diag::err_argument_required_after_attribute);
2572 delete AttrList;
2573 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002574 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2575 // normal declarators, not for abstract-declarators.
2576 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002577 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002578 }
Mike Stump1eb44332009-09-09 15:08:12 +00002579
Chris Lattnerf97409f2008-04-06 06:57:35 +00002580 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002581
Chris Lattnerf97409f2008-04-06 06:57:35 +00002582 // Build up an array of information about the parsed arguments.
2583 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002584
2585 // Enter function-declaration scope, limiting any declarators to the
2586 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002587 ParseScope PrototypeScope(this,
2588 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002589
Chris Lattnerf97409f2008-04-06 06:57:35 +00002590 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002591 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002592 while (1) {
2593 if (Tok.is(tok::ellipsis)) {
2594 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002595 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002596 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002597 }
Mike Stump1eb44332009-09-09 15:08:12 +00002598
Chris Lattnerf97409f2008-04-06 06:57:35 +00002599 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002600
Chris Lattnerf97409f2008-04-06 06:57:35 +00002601 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00002602 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002603 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002604
2605 // If the caller parsed attributes for the first argument, add them now.
2606 if (AttrList) {
2607 DS.AddAttributes(AttrList);
2608 AttrList = 0; // Only apply the attributes to the first parameter.
2609 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002610 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002611
Chris Lattnerf97409f2008-04-06 06:57:35 +00002612 // Parse the declarator. This is "PrototypeContext", because we must
2613 // accept either 'declarator' or 'abstract-declarator' here.
2614 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2615 ParseDeclarator(ParmDecl);
2616
2617 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002618 if (Tok.is(tok::kw___attribute)) {
2619 SourceLocation Loc;
2620 AttributeList *AttrList = ParseAttributes(&Loc);
2621 ParmDecl.AddAttributes(AttrList, Loc);
2622 }
Mike Stump1eb44332009-09-09 15:08:12 +00002623
Chris Lattnerf97409f2008-04-06 06:57:35 +00002624 // Remember this parsed parameter in ParamInfo.
2625 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002626
Douglas Gregor72b505b2008-12-16 21:30:33 +00002627 // DefArgToks is used when the parsing of default arguments needs
2628 // to be delayed.
2629 CachedTokens *DefArgToks = 0;
2630
Chris Lattnerf97409f2008-04-06 06:57:35 +00002631 // If no parameter was specified, verify that *something* was specified,
2632 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002633 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2634 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002635 // Completely missing, emit error.
2636 Diag(DSStart, diag::err_missing_param);
2637 } else {
2638 // Otherwise, we have something. Add it and let semantic analysis try
2639 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00002640
Chris Lattnerf97409f2008-04-06 06:57:35 +00002641 // Inform the actions module about the parameter declarator, so it gets
2642 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002643 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002644
2645 // Parse the default argument, if any. We parse the default
2646 // arguments in all dialects; the semantic analysis in
2647 // ActOnParamDefaultArgument will reject the default argument in
2648 // C.
2649 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002650 SourceLocation EqualLoc = Tok.getLocation();
2651
Chris Lattner04421082008-04-08 04:40:51 +00002652 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002653 if (D.getContext() == Declarator::MemberContext) {
2654 // If we're inside a class definition, cache the tokens
2655 // corresponding to the default argument. We'll actually parse
2656 // them when we see the end of the class definition.
2657 // FIXME: Templates will require something similar.
2658 // FIXME: Can we use a smart pointer for Toks?
2659 DefArgToks = new CachedTokens;
2660
Mike Stump1eb44332009-09-09 15:08:12 +00002661 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002662 tok::semi, false)) {
2663 delete DefArgToks;
2664 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002665 Actions.ActOnParamDefaultArgumentError(Param);
2666 } else
Mike Stump1eb44332009-09-09 15:08:12 +00002667 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00002668 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002669 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002670 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002671 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002672
Douglas Gregor72b505b2008-12-16 21:30:33 +00002673 OwningExprResult DefArgResult(ParseAssignmentExpression());
2674 if (DefArgResult.isInvalid()) {
2675 Actions.ActOnParamDefaultArgumentError(Param);
2676 SkipUntil(tok::comma, tok::r_paren, true, true);
2677 } else {
2678 // Inform the actions module about the default argument
2679 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002680 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002681 }
Chris Lattner04421082008-04-08 04:40:51 +00002682 }
2683 }
Mike Stump1eb44332009-09-09 15:08:12 +00002684
2685 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2686 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002687 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002688 }
2689
2690 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00002691 if (Tok.isNot(tok::comma)) {
2692 if (Tok.is(tok::ellipsis)) {
2693 IsVariadic = true;
2694 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2695
2696 if (!getLang().CPlusPlus) {
2697 // We have ellipsis without a preceding ',', which is ill-formed
2698 // in C. Complain and provide the fix.
2699 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2700 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2701 }
2702 }
2703
2704 break;
2705 }
Mike Stump1eb44332009-09-09 15:08:12 +00002706
Chris Lattnerf97409f2008-04-06 06:57:35 +00002707 // Consume the comma.
2708 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002709 }
Mike Stump1eb44332009-09-09 15:08:12 +00002710
Chris Lattnerf97409f2008-04-06 06:57:35 +00002711 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002712 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00002713
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002714 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002715 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2716 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002717
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002718 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002719 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002720 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002721 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002722 llvm::SmallVector<TypeTy*, 2> Exceptions;
2723 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002724 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002725 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002726 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002727 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002728 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002729
2730 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002731 if (Tok.is(tok::kw_throw)) {
2732 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002733 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002734 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002735 hasAnyExceptionSpec);
2736 assert(Exceptions.size() == ExceptionRanges.size() &&
2737 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002738 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002739 }
2740
Reid Spencer5f016e22007-07-11 17:01:13 +00002741 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002742 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002743 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002744 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002745 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002746 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002747 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002748 Exceptions.data(),
2749 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002750 Exceptions.size(),
2751 LParenLoc, RParenLoc, D),
2752 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002753}
2754
Chris Lattner66d28652008-04-06 06:34:08 +00002755/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2756/// we found a K&R-style identifier list instead of a type argument list. The
2757/// current token is known to be the first identifier in the list.
2758///
2759/// identifier-list: [C99 6.7.5]
2760/// identifier
2761/// identifier-list ',' identifier
2762///
2763void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2764 Declarator &D) {
2765 // Build up an array of information about the parsed arguments.
2766 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2767 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00002768
Chris Lattner66d28652008-04-06 06:34:08 +00002769 // If there was no identifier specified for the declarator, either we are in
2770 // an abstract-declarator, or we are in a parameter declarator which was found
2771 // to be abstract. In abstract-declarators, identifier lists are not valid:
2772 // diagnose this.
2773 if (!D.getIdentifier())
2774 Diag(Tok, diag::ext_ident_list_in_param);
2775
2776 // Tok is known to be the first identifier in the list. Remember this
2777 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002778 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002779 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002780 Tok.getLocation(),
2781 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00002782
Chris Lattner50c64772008-04-06 06:39:19 +00002783 ConsumeToken(); // eat the first identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00002784
Chris Lattner66d28652008-04-06 06:34:08 +00002785 while (Tok.is(tok::comma)) {
2786 // Eat the comma.
2787 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002788
Chris Lattner50c64772008-04-06 06:39:19 +00002789 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002790 if (Tok.isNot(tok::identifier)) {
2791 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002792 SkipUntil(tok::r_paren);
2793 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002794 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002795
Chris Lattner66d28652008-04-06 06:34:08 +00002796 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002797
2798 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002799 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002800 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00002801
Chris Lattner66d28652008-04-06 06:34:08 +00002802 // Verify that the argument identifier has not already been mentioned.
2803 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002804 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002805 } else {
2806 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002807 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002808 Tok.getLocation(),
2809 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002810 }
Mike Stump1eb44332009-09-09 15:08:12 +00002811
Chris Lattner66d28652008-04-06 06:34:08 +00002812 // Eat the identifier.
2813 ConsumeToken();
2814 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002815
2816 // If we have the closing ')', eat it and we're done.
2817 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2818
Chris Lattner50c64772008-04-06 06:39:19 +00002819 // Remember that we parsed a function type, and remember the attributes. This
2820 // function type is always a K&R style function type, which is not varargs and
2821 // has no prototype.
2822 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002823 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002824 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002825 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00002826 /*exception*/false,
2827 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002828 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002829 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002830}
Chris Lattneref4715c2008-04-06 05:45:57 +00002831
Reid Spencer5f016e22007-07-11 17:01:13 +00002832/// [C90] direct-declarator '[' constant-expression[opt] ']'
2833/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2834/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2835/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2836/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2837void Parser::ParseBracketDeclarator(Declarator &D) {
2838 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00002839
Chris Lattner378c7e42008-12-18 07:27:21 +00002840 // C array syntax has many features, but by-far the most common is [] and [4].
2841 // This code does a fast path to handle some of the most obvious cases.
2842 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002843 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002844 // Remember that we parsed the empty array type.
2845 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002846 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2847 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002848 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002849 return;
2850 } else if (Tok.getKind() == tok::numeric_constant &&
2851 GetLookAheadToken(1).is(tok::r_square)) {
2852 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002853 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002854 ConsumeToken();
2855
Sebastian Redlab197ba2009-02-09 18:23:29 +00002856 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002857
2858 // If there was an error parsing the assignment-expression, recover.
2859 if (ExprRes.isInvalid())
2860 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00002861
Chris Lattner378c7e42008-12-18 07:27:21 +00002862 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002863 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2864 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002865 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002866 return;
2867 }
Mike Stump1eb44332009-09-09 15:08:12 +00002868
Reid Spencer5f016e22007-07-11 17:01:13 +00002869 // If valid, this location is the position where we read the 'static' keyword.
2870 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002871 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002872 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002873
Reid Spencer5f016e22007-07-11 17:01:13 +00002874 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002875 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002876 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002877 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00002878
Reid Spencer5f016e22007-07-11 17:01:13 +00002879 // If we haven't already read 'static', check to see if there is one after the
2880 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002881 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002882 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002883
Reid Spencer5f016e22007-07-11 17:01:13 +00002884 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2885 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002886 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00002887
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002888 // Handle the case where we have '[*]' as the array size. However, a leading
2889 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2890 // the the token after the star is a ']'. Since stars in arrays are
2891 // infrequent, use of lookahead is not costly here.
2892 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002893 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002894
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002895 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002896 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002897 StaticLoc = SourceLocation(); // Drop the static.
2898 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002899 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002900 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002901 // Note, in C89, this production uses the constant-expr production instead
2902 // of assignment-expr. The only difference is that assignment-expr allows
2903 // things like '=' and '*='. Sema rejects these in C89 mode because they
2904 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00002905
Douglas Gregore0762c92009-06-19 23:52:42 +00002906 // Parse the constant-expression or assignment-expression now (depending
2907 // on dialect).
2908 if (getLang().CPlusPlus)
2909 NumElements = ParseConstantExpression();
2910 else
2911 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00002912 }
Mike Stump1eb44332009-09-09 15:08:12 +00002913
Reid Spencer5f016e22007-07-11 17:01:13 +00002914 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002915 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002916 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002917 // If the expression was invalid, skip it.
2918 SkipUntil(tok::r_square);
2919 return;
2920 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002921
2922 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2923
Chris Lattner378c7e42008-12-18 07:27:21 +00002924 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002925 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2926 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002927 NumElements.release(),
2928 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002929 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002930}
2931
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002932/// [GNU] typeof-specifier:
2933/// typeof ( expressions )
2934/// typeof ( type-name )
2935/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002936///
2937void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002938 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002939 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002940 SourceLocation StartLoc = ConsumeToken();
2941
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002942 bool isCastExpr;
2943 TypeTy *CastTy;
2944 SourceRange CastRange;
2945 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2946 isCastExpr,
2947 CastTy,
2948 CastRange);
2949
2950 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002951 // FIXME: Not accurate, the range gets one token more than it should.
2952 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002953 else
2954 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00002955
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002956 if (isCastExpr) {
2957 if (!CastTy) {
2958 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002959 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002960 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002961
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002962 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002963 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002964 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2965 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002966 DiagID, CastTy))
2967 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002968 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002969 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002970
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002971 // If we get here, the operand to the typeof was an expresion.
2972 if (Operand.isInvalid()) {
2973 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002974 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002975 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002976
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002977 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002978 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002979 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2980 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002981 DiagID, Operand.release()))
2982 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002983}