blob: e15a4cd6880513ab71207afd095c6d31d8b7ebf1 [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,
Chris Lattner97144fc2009-04-02 04:16:50 +0000339 SourceLocation &DeclEnd,
Chris Lattnercd147752009-03-29 17:27:48 +0000340 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // Parse the common declaration-specifiers piece.
342 DeclSpec DS;
343 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
346 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000347 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000349 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
350 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 }
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
354 ParseDeclarator(DeclaratorInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chris Lattner23c4b182009-03-29 17:18:04 +0000356 DeclGroupPtrTy DG =
357 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnercd147752009-03-29 17:27:48 +0000358
Chris Lattner97144fc2009-04-02 04:16:50 +0000359 DeclEnd = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattnercd147752009-03-29 17:27:48 +0000361 // If the client wants to check what comes after the declaration, just return
362 // immediately without checking anything!
363 if (!RequireSemi) return DG;
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Chris Lattner23c4b182009-03-29 17:18:04 +0000365 if (Tok.is(tok::semi)) {
366 ConsumeToken();
Chris Lattner23c4b182009-03-29 17:18:04 +0000367 return DG;
368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
John McCall5c15fe12009-07-31 02:20:35 +0000370 Diag(Tok, diag::err_expected_semi_declaration);
Chris Lattner23c4b182009-03-29 17:18:04 +0000371 // Skip to end of block or statement
372 SkipUntil(tok::r_brace, true, true);
373 if (Tok.is(tok::semi))
374 ConsumeToken();
375 return DG;
Reid Spencer5f016e22007-07-11 17:01:13 +0000376}
377
Douglas Gregor1426e532009-05-12 21:31:51 +0000378/// \brief Parse 'declaration' after parsing 'declaration-specifiers
379/// declarator'. This method parses the remainder of the declaration
380/// (including any attributes or initializer, among other things) and
381/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000382///
Reid Spencer5f016e22007-07-11 17:01:13 +0000383/// init-declarator: [C99 6.7]
384/// declarator
385/// declarator '=' initializer
386/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
387/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000388/// [C++] declarator initializer[opt]
389///
390/// [C++] initializer:
391/// [C++] '=' initializer-clause
392/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000393/// [C++0x] '=' 'default' [TODO]
394/// [C++0x] '=' 'delete'
395///
396/// According to the standard grammar, =default and =delete are function
397/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000398///
Douglas Gregore542c862009-06-23 23:11:28 +0000399Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
400 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000401 // If a simple-asm-expr is present, parse it.
402 if (Tok.is(tok::kw_asm)) {
403 SourceLocation Loc;
404 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
405 if (AsmLabel.isInvalid()) {
406 SkipUntil(tok::semi, true, true);
407 return DeclPtrTy();
408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Douglas Gregor1426e532009-05-12 21:31:51 +0000410 D.setAsmLabel(AsmLabel.release());
411 D.SetRangeEnd(Loc);
412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Douglas Gregor1426e532009-05-12 21:31:51 +0000414 // If attributes are present, parse them.
415 if (Tok.is(tok::kw___attribute)) {
416 SourceLocation Loc;
417 AttributeList *AttrList = ParseAttributes(&Loc);
418 D.AddAttributes(AttrList, Loc);
419 }
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Douglas Gregor1426e532009-05-12 21:31:51 +0000421 // Inform the current actions module that we just parsed this declarator.
Douglas Gregord5a423b2009-09-25 18:43:00 +0000422 DeclPtrTy ThisDecl;
423 switch (TemplateInfo.Kind) {
424 case ParsedTemplateInfo::NonTemplate:
425 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
426 break;
427
428 case ParsedTemplateInfo::Template:
429 case ParsedTemplateInfo::ExplicitSpecialization:
430 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregore542c862009-06-23 23:11:28 +0000431 Action::MultiTemplateParamsArg(Actions,
432 TemplateInfo.TemplateParams->data(),
433 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000434 D);
435 break;
436
437 case ParsedTemplateInfo::ExplicitInstantiation: {
438 Action::DeclResult ThisRes
439 = Actions.ActOnExplicitInstantiation(CurScope,
440 TemplateInfo.ExternLoc,
441 TemplateInfo.TemplateLoc,
442 D);
443 if (ThisRes.isInvalid()) {
444 SkipUntil(tok::semi, true, true);
445 return DeclPtrTy();
446 }
447
448 ThisDecl = ThisRes.get();
449 break;
450 }
451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Douglas Gregor1426e532009-05-12 21:31:51 +0000453 // Parse declarator '=' initializer.
454 if (Tok.is(tok::equal)) {
455 ConsumeToken();
456 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
457 SourceLocation DelLoc = ConsumeToken();
458 Actions.SetDeclDeleted(ThisDecl, DelLoc);
459 } else {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000460 if (getLang().CPlusPlus)
461 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
462
Douglas Gregor1426e532009-05-12 21:31:51 +0000463 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000464
465 if (getLang().CPlusPlus)
466 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
467
Douglas Gregor1426e532009-05-12 21:31:51 +0000468 if (Init.isInvalid()) {
469 SkipUntil(tok::semi, true, true);
470 return DeclPtrTy();
471 }
Anders Carlsson9abf2ae2009-08-16 05:13:48 +0000472 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000473 }
474 } else if (Tok.is(tok::l_paren)) {
475 // Parse C++ direct initializer: '(' expression-list ')'
476 SourceLocation LParenLoc = ConsumeParen();
477 ExprVector Exprs(Actions);
478 CommaLocsTy CommaLocs;
479
480 if (ParseExpressionList(Exprs, CommaLocs)) {
481 SkipUntil(tok::r_paren);
482 } else {
483 // Match the ')'.
484 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
485
486 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
487 "Unexpected number of commas!");
488 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
489 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000490 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000491 }
492 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000493 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000494 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
495 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000496 }
497
498 return ThisDecl;
499}
500
501/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
502/// parsing 'declaration-specifiers declarator'. This method is split out this
503/// way to handle the ambiguity between top-level function-definitions and
504/// declarations.
505///
506/// init-declarator-list: [C99 6.7]
507/// init-declarator
508/// init-declarator-list ',' init-declarator
509///
510/// According to the standard grammar, =default and =delete are function
511/// definitions, but that definitely doesn't fit with the parser here.
512///
Chris Lattner682bf922009-03-29 16:50:03 +0000513Parser::DeclGroupPtrTy Parser::
Reid Spencer5f016e22007-07-11 17:01:13 +0000514ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner682bf922009-03-29 16:50:03 +0000515 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
516 // that we parse together here.
517 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 // At this point, we know that it is not a function definition. Parse the
520 // rest of the init-declarator-list.
521 while (1) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000522 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
523 if (ThisDecl.get())
524 DeclsInGroup.push_back(ThisDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 // If we don't have a comma, it is either the end of the list (a ';') or an
527 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000528 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 // Consume the comma.
532 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 // Parse the next declarator.
535 D.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chris Lattneraab740a2008-10-20 04:57:38 +0000537 // Accept attributes in an init-declarator. In the first declarator in a
538 // declaration, these would be part of the declspec. In subsequent
539 // declarators, they become part of the declarator itself, so that they
540 // don't apply to declarators after *this* one. Examples:
541 // short __attribute__((common)) var; -> declspec
542 // short var __attribute__((common)); -> declarator
543 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlab197ba2009-02-09 18:23:29 +0000544 if (Tok.is(tok::kw___attribute)) {
545 SourceLocation Loc;
546 AttributeList *AttrList = ParseAttributes(&Loc);
547 D.AddAttributes(AttrList, Loc);
548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 ParseDeclarator(D);
551 }
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Eli Friedmanc1dc6532009-05-29 01:49:24 +0000553 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
554 DeclsInGroup.data(),
Chris Lattner23c4b182009-03-29 17:18:04 +0000555 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000556}
557
558/// ParseSpecifierQualifierList
559/// specifier-qualifier-list:
560/// type-specifier specifier-qualifier-list[opt]
561/// type-qualifier specifier-qualifier-list[opt]
562/// [GNU] attributes specifier-qualifier-list[opt]
563///
564void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
565 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
566 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 // Validate declspec for type-name.
570 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000571 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
572 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 // Issue diagnostic and remove storage class if present.
576 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
577 if (DS.getStorageClassSpecLoc().isValid())
578 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
579 else
580 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
581 DS.ClearStorageClassSpecs();
582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 // Issue diagnostic and remove function specfier if present.
585 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000586 if (DS.isInlineSpecified())
587 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
588 if (DS.isVirtualSpecified())
589 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
590 if (DS.isExplicitSpecified())
591 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 DS.ClearFunctionSpecs();
593 }
594}
595
Chris Lattnerc199ab32009-04-12 20:42:31 +0000596/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
597/// specified token is valid after the identifier in a declarator which
598/// immediately follows the declspec. For example, these things are valid:
599///
600/// int x [ 4]; // direct-declarator
601/// int x ( int y); // direct-declarator
602/// int(int x ) // direct-declarator
603/// int x ; // simple-declaration
604/// int x = 17; // init-declarator-list
605/// int x , y; // init-declarator-list
606/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000607/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000608/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000609///
610/// This is not, because 'x' does not immediately follow the declspec (though
611/// ')' happens to be valid anyway).
612/// int (x)
613///
614static bool isValidAfterIdentifierInDeclarator(const Token &T) {
615 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
616 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000617 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000618}
619
Chris Lattnere40c2952009-04-14 21:34:55 +0000620
621/// ParseImplicitInt - This method is called when we have an non-typename
622/// identifier in a declspec (which normally terminates the decl spec) when
623/// the declspec has no type specifier. In this case, the declspec is either
624/// malformed or is "implicit int" (in K&R and C89).
625///
626/// This method handles diagnosing this prettily and returns false if the
627/// declspec is done being processed. If it recovers and thinks there may be
628/// other pieces of declspec after it, it returns true.
629///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000630bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000631 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000632 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000633 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Chris Lattnere40c2952009-04-14 21:34:55 +0000635 SourceLocation Loc = Tok.getLocation();
636 // If we see an identifier that is not a type name, we normally would
637 // parse it as the identifer being declared. However, when a typename
638 // is typo'd or the definition is not included, this will incorrectly
639 // parse the typename as the identifier name and fall over misparsing
640 // later parts of the diagnostic.
641 //
642 // As such, we try to do some look-ahead in cases where this would
643 // otherwise be an "implicit-int" case to see if this is invalid. For
644 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
645 // an identifier with implicit int, we'd get a parse error because the
646 // next token is obviously invalid for a type. Parse these as a case
647 // with an invalid type specifier.
648 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Chris Lattnere40c2952009-04-14 21:34:55 +0000650 // Since we know that this either implicit int (which is rare) or an
651 // error, we'd do lookahead to try to do better recovery.
652 if (isValidAfterIdentifierInDeclarator(NextToken())) {
653 // If this token is valid for implicit int, e.g. "static x = 4", then
654 // we just avoid eating the identifier, so it will be parsed as the
655 // identifier in the declarator.
656 return false;
657 }
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Chris Lattnere40c2952009-04-14 21:34:55 +0000659 // Otherwise, if we don't consume this token, we are going to emit an
660 // error anyway. Try to recover from various common problems. Check
661 // to see if this was a reference to a tag name without a tag specified.
662 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000663 //
664 // C++ doesn't need this, and isTagName doesn't take SS.
665 if (SS == 0) {
666 const char *TagName = 0;
667 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Chris Lattnere40c2952009-04-14 21:34:55 +0000669 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
670 default: break;
671 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
672 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
673 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
674 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
675 }
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Chris Lattnerf4382f52009-04-14 22:17:06 +0000677 if (TagName) {
678 Diag(Loc, diag::err_use_of_tag_name_without_tag)
679 << Tok.getIdentifierInfo() << TagName
680 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Chris Lattnerf4382f52009-04-14 22:17:06 +0000682 // Parse this as a tag as if the missing tag were present.
683 if (TagKind == tok::kw_enum)
684 ParseEnumSpecifier(Loc, DS, AS);
685 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000686 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000687 return true;
688 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Chris Lattnere40c2952009-04-14 21:34:55 +0000691 // Since this is almost certainly an invalid type name, emit a
692 // diagnostic that says it, eat the token, and mark the declspec as
693 // invalid.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000694 SourceRange R;
695 if (SS) R = SS->getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Chris Lattnerf4382f52009-04-14 22:17:06 +0000697 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattnere40c2952009-04-14 21:34:55 +0000698 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000699 unsigned DiagID;
700 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000701 DS.SetRangeEnd(Tok.getLocation());
702 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Chris Lattnere40c2952009-04-14 21:34:55 +0000704 // TODO: Could inject an invalid typedef decl in an enclosing scope to
705 // avoid rippling error messages on subsequent uses of the same type,
706 // could be useful if #include was forgotten.
707 return false;
708}
709
Reid Spencer5f016e22007-07-11 17:01:13 +0000710/// ParseDeclarationSpecifiers
711/// declaration-specifiers: [C99 6.7]
712/// storage-class-specifier declaration-specifiers[opt]
713/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000714/// [C99] function-specifier declaration-specifiers[opt]
715/// [GNU] attributes declaration-specifiers[opt]
716///
717/// storage-class-specifier: [C99 6.7.1]
718/// 'typedef'
719/// 'extern'
720/// 'static'
721/// 'auto'
722/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000723/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000724/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000725/// function-specifier: [C99 6.7.4]
726/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000727/// [C++] 'virtual'
728/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000729/// 'friend': [C++ dcl.friend]
730
Reid Spencer5f016e22007-07-11 17:01:13 +0000731///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000732void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000733 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000734 AccessSpecifier AS,
735 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000736 if (Tok.is(tok::code_completion)) {
737 Actions.CodeCompleteOrdinaryName(CurScope);
738 ConsumeToken();
739 }
740
Chris Lattner81c018d2008-03-13 06:29:04 +0000741 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000743 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000745 unsigned DiagID = 0;
746
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000748
Reid Spencer5f016e22007-07-11 17:01:13 +0000749 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000750 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000751 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 // If this is not a declaration specifier token, we're done reading decl
753 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000754 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Chris Lattner5e02c472009-01-05 00:07:25 +0000757 case tok::coloncolon: // ::foo::bar
758 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000759 if (TryAnnotateCXXScopeToken(true))
Chris Lattner5e02c472009-01-05 00:07:25 +0000760 continue;
761 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000762
763 case tok::annot_cxxscope: {
764 if (DS.hasTypeSpecifier())
765 goto DoneWithDeclSpec;
766
767 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000768 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000769 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000770 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000771 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000772 // We have a qualified template-id, e.g., N::A<int>
773 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000774 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000775 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000776 "ParseOptionalCXXScopeSpecifier not working");
777 AnnotateTemplateIdTokenAsType(&SS);
778 continue;
779 }
780
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000781 if (Next.is(tok::annot_typename)) {
782 // FIXME: is this scope-specifier getting dropped?
783 ConsumeToken(); // the scope-specifier
784 if (Tok.getAnnotationValue())
785 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
786 PrevSpec, DiagID,
787 Tok.getAnnotationValue());
788 else
789 DS.SetTypeSpecError();
790 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
791 ConsumeToken(); // The typename
792 }
793
Douglas Gregor9135c722009-03-25 15:40:00 +0000794 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000795 goto DoneWithDeclSpec;
796
797 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000798 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000799 SS.setRange(Tok.getAnnotationRange());
800
801 // If the next token is the name of the class type that the C++ scope
802 // denotes, followed by a '(', then this is a constructor declaration.
803 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000804 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000805 CurScope, &SS) &&
806 GetLookAheadToken(2).is(tok::l_paren))
807 goto DoneWithDeclSpec;
808
Douglas Gregorb696ea32009-02-04 17:00:24 +0000809 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
810 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000811
Chris Lattnerf4382f52009-04-14 22:17:06 +0000812 // If the referenced identifier is not a type, then this declspec is
813 // erroneous: We already checked about that it has no type specifier, and
814 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000815 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000816 if (TypeRep == 0) {
817 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000818 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000819 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000822 ConsumeToken(); // The C++ scope.
823
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000824 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000825 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000826 if (isInvalid)
827 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000829 DS.SetRangeEnd(Tok.getLocation());
830 ConsumeToken(); // The typename.
831
832 continue;
833 }
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Chris Lattner80d0c892009-01-21 19:48:37 +0000835 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000836 if (Tok.getAnnotationValue())
837 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000838 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +0000839 else
840 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000841 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
842 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Chris Lattner80d0c892009-01-21 19:48:37 +0000844 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
845 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
846 // Objective-C interface. If we don't have Objective-C or a '<', this is
847 // just a normal reference to a typedef name.
848 if (!Tok.is(tok::less) || !getLang().ObjC1)
849 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000851 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000852 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000853 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
854 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
855 LAngleLoc, EndProtoLoc);
856 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
857 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Chris Lattner80d0c892009-01-21 19:48:37 +0000859 DS.SetRangeEnd(EndProtoLoc);
860 continue;
861 }
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Chris Lattner3bd934a2008-07-26 01:18:38 +0000863 // typedef-name
864 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000865 // In C++, check to see if this is a scope specifier like foo::bar::, if
866 // so handle it as such. This is important for ctor parsing.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000867 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner837acd02009-01-21 19:19:26 +0000868 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Chris Lattner3bd934a2008-07-26 01:18:38 +0000870 // This identifier can only be a typedef name if we haven't already seen
871 // a type-specifier. Without this check we misparse:
872 // typedef int X; struct Y { short X; }; as 'short int'.
873 if (DS.hasTypeSpecifier())
874 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Chris Lattner3bd934a2008-07-26 01:18:38 +0000876 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +0000877 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +0000878 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000879
Chris Lattnerc199ab32009-04-12 20:42:31 +0000880 // If this is not a typedef name, don't parse it as part of the declspec,
881 // it must be an implicit int or an error.
882 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000883 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000884 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000885 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000886
Douglas Gregorb48fe382008-10-31 09:07:45 +0000887 // C++: If the identifier is actually the name of the class type
888 // being defined and the next token is a '(', then this is a
889 // constructor declaration. We're done with the decl-specifiers
890 // and will treat this token as an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000891 if (getLang().CPlusPlus &&
892 (CurScope->isClassScope() ||
893 (CurScope->isTemplateParamScope() &&
Douglas Gregordec06662009-08-21 18:42:58 +0000894 CurScope->getParent()->isClassScope())) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000895 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000896 NextToken().getKind() == tok::l_paren)
897 goto DoneWithDeclSpec;
898
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000899 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000900 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000901 if (isInvalid)
902 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Chris Lattner3bd934a2008-07-26 01:18:38 +0000904 DS.SetRangeEnd(Tok.getLocation());
905 ConsumeToken(); // The identifier
906
907 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
908 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
909 // Objective-C interface. If we don't have Objective-C or a '<', this is
910 // just a normal reference to a typedef name.
911 if (!Tok.is(tok::less) || !getLang().ObjC1)
912 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000914 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000915 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000916 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
917 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
918 LAngleLoc, EndProtoLoc);
919 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
920 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Chris Lattner3bd934a2008-07-26 01:18:38 +0000922 DS.SetRangeEnd(EndProtoLoc);
923
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000924 // Need to support trailing type qualifiers (e.g. "id<p> const").
925 // If a type specifier follows, it will be diagnosed elsewhere.
926 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000927 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000928
929 // type-name
930 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +0000931 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000932 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000933 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000934 // This template-id does not refer to a type name, so we're
935 // done with the type-specifiers.
936 goto DoneWithDeclSpec;
937 }
938
939 // Turn the template-id annotation token into a type annotation
940 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000941 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000942 continue;
943 }
944
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 // GNU attributes support.
946 case tok::kw___attribute:
947 DS.AddAttributes(ParseAttributes());
948 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000949
950 // Microsoft declspec support.
951 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +0000952 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +0000953 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Steve Naroff239f0732008-12-25 14:16:32 +0000955 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000956 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +0000957 // FIXME: Add handling here!
958 break;
959
960 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000961 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000962 case tok::kw___cdecl:
963 case tok::kw___stdcall:
964 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000965 DS.AddAttributes(ParseMicrosoftTypeAttributes());
966 continue;
967
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 // storage-class-specifier
969 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +0000970 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
971 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 break;
973 case tok::kw_extern:
974 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000975 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +0000976 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
977 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000979 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000980 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +0000981 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000982 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000983 case tok::kw_static:
984 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000985 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +0000986 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
987 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 break;
989 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +0000990 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +0000991 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
992 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +0000993 else
John McCallfec54012009-08-03 20:12:06 +0000994 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
995 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 break;
997 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +0000998 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
999 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001001 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001002 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1003 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001004 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001006 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001007 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 // function-specifier
1010 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001011 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001013 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001014 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001015 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001016 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001017 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001018 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001019
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001020 // friend
1021 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001022 if (DSContext == DSC_class)
1023 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1024 else {
1025 PrevSpec = ""; // not actually used by the diagnostic
1026 DiagID = diag::err_friend_invalid_in_context;
1027 isInvalid = true;
1028 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001029 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Chris Lattner80d0c892009-01-21 19:48:37 +00001031 // type-specifier
1032 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001033 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1034 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001035 break;
1036 case tok::kw_long:
1037 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001038 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1039 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001040 else
John McCallfec54012009-08-03 20:12:06 +00001041 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1042 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001043 break;
1044 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001045 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1046 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001047 break;
1048 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001049 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1050 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001051 break;
1052 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001053 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1054 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001055 break;
1056 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001057 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1058 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001059 break;
1060 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001061 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1062 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001063 break;
1064 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001065 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1066 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001067 break;
1068 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001069 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1070 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001071 break;
1072 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001073 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1074 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001075 break;
1076 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001077 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1078 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001079 break;
1080 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001081 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1082 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001083 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001084 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001085 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1086 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001087 break;
1088 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001089 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1090 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001091 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001092 case tok::kw_bool:
1093 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001094 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1095 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001096 break;
1097 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001098 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1099 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001100 break;
1101 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001102 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1103 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001104 break;
1105 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001106 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1107 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001108 break;
1109
1110 // class-specifier:
1111 case tok::kw_class:
1112 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001113 case tok::kw_union: {
1114 tok::TokenKind Kind = Tok.getKind();
1115 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001116 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001117 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001118 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001119
1120 // enum-specifier:
1121 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001122 ConsumeToken();
1123 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001124 continue;
1125
1126 // cv-qualifier:
1127 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001128 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1129 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001130 break;
1131 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001132 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1133 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001134 break;
1135 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001136 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1137 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001138 break;
1139
Douglas Gregord57959a2009-03-27 23:10:48 +00001140 // C++ typename-specifier:
1141 case tok::kw_typename:
1142 if (TryAnnotateTypeOrScopeToken())
1143 continue;
1144 break;
1145
Chris Lattner80d0c892009-01-21 19:48:37 +00001146 // GNU typeof support.
1147 case tok::kw_typeof:
1148 ParseTypeofSpecifier(DS);
1149 continue;
1150
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001151 case tok::kw_decltype:
1152 ParseDecltypeSpecifier(DS);
1153 continue;
1154
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001155 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001156 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001157 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1158 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001159 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001160 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Chris Lattnerbce61352008-07-26 00:20:22 +00001162 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001163 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001164 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001165 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1166 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1167 LAngleLoc, EndProtoLoc);
1168 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1169 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001170 DS.SetRangeEnd(EndProtoLoc);
1171
Chris Lattner1ab3b962008-11-18 07:48:38 +00001172 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001173 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001174 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001175 // Need to support trailing type qualifiers (e.g. "id<p> const").
1176 // If a type specifier follows, it will be diagnosed elsewhere.
1177 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001178 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001179 }
John McCallfec54012009-08-03 20:12:06 +00001180 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 if (isInvalid) {
1182 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001183 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001184 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001186 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 ConsumeToken();
1188 }
1189}
Douglas Gregoradcac882008-12-01 23:54:00 +00001190
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001191/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001192/// primarily follow the C++ grammar with additions for C99 and GNU,
1193/// which together subsume the C grammar. Note that the C++
1194/// type-specifier also includes the C type-qualifier (for const,
1195/// volatile, and C99 restrict). Returns true if a type-specifier was
1196/// found (and parsed), false otherwise.
1197///
1198/// type-specifier: [C++ 7.1.5]
1199/// simple-type-specifier
1200/// class-specifier
1201/// enum-specifier
1202/// elaborated-type-specifier [TODO]
1203/// cv-qualifier
1204///
1205/// cv-qualifier: [C++ 7.1.5.1]
1206/// 'const'
1207/// 'volatile'
1208/// [C99] 'restrict'
1209///
1210/// simple-type-specifier: [ C++ 7.1.5.2]
1211/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1212/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1213/// 'char'
1214/// 'wchar_t'
1215/// 'bool'
1216/// 'short'
1217/// 'int'
1218/// 'long'
1219/// 'signed'
1220/// 'unsigned'
1221/// 'float'
1222/// 'double'
1223/// 'void'
1224/// [C99] '_Bool'
1225/// [C99] '_Complex'
1226/// [C99] '_Imaginary' // Removed in TC2?
1227/// [GNU] '_Decimal32'
1228/// [GNU] '_Decimal64'
1229/// [GNU] '_Decimal128'
1230/// [GNU] typeof-specifier
1231/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1232/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001233/// [C++0x] 'decltype' ( expression )
John McCallfec54012009-08-03 20:12:06 +00001234bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001235 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001236 unsigned &DiagID,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001237 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001238 SourceLocation Loc = Tok.getLocation();
1239
1240 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001241 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001242 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001243 // Annotate typenames and C++ scope specifiers. If we get one, just
1244 // recurse to handle whatever we get.
1245 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001246 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1247 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001248 // Otherwise, not a type specifier.
1249 return false;
1250 case tok::coloncolon: // ::foo::bar
1251 if (NextToken().is(tok::kw_new) || // ::new
1252 NextToken().is(tok::kw_delete)) // ::delete
1253 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Chris Lattner166a8fc2009-01-04 23:41:41 +00001255 // Annotate typenames and C++ scope specifiers. If we get one, just
1256 // recurse to handle whatever we get.
1257 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001258 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1259 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001260 // Otherwise, not a type specifier.
1261 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001262
Douglas Gregor12e083c2008-11-07 15:42:26 +00001263 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001264 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001265 if (Tok.getAnnotationValue())
1266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001267 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001268 else
1269 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001270 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1271 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Douglas Gregor12e083c2008-11-07 15:42:26 +00001273 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1274 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1275 // Objective-C interface. If we don't have Objective-C or a '<', this is
1276 // just a normal reference to a typedef name.
1277 if (!Tok.is(tok::less) || !getLang().ObjC1)
1278 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001279
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001280 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001281 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001282 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1283 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1284 LAngleLoc, EndProtoLoc);
1285 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1286 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Douglas Gregor12e083c2008-11-07 15:42:26 +00001288 DS.SetRangeEnd(EndProtoLoc);
1289 return true;
1290 }
1291
1292 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001293 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001294 break;
1295 case tok::kw_long:
1296 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001297 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1298 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001299 else
John McCallfec54012009-08-03 20:12:06 +00001300 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1301 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001302 break;
1303 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001304 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001305 break;
1306 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001307 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1308 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001309 break;
1310 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001311 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1312 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001313 break;
1314 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001315 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1316 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001317 break;
1318 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001319 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001320 break;
1321 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001322 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001323 break;
1324 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001325 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001326 break;
1327 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001328 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001329 break;
1330 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001331 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001332 break;
1333 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001334 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001335 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001336 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001337 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001338 break;
1339 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001340 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001341 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001342 case tok::kw_bool:
1343 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001344 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001345 break;
1346 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001347 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1348 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001349 break;
1350 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001351 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1352 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001353 break;
1354 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001355 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1356 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001357 break;
1358
1359 // class-specifier:
1360 case tok::kw_class:
1361 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001362 case tok::kw_union: {
1363 tok::TokenKind Kind = Tok.getKind();
1364 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001365 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001366 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001367 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001368
1369 // enum-specifier:
1370 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001371 ConsumeToken();
1372 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001373 return true;
1374
1375 // cv-qualifier:
1376 case tok::kw_const:
1377 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001378 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001379 break;
1380 case tok::kw_volatile:
1381 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001382 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001383 break;
1384 case tok::kw_restrict:
1385 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001386 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001387 break;
1388
1389 // GNU typeof support.
1390 case tok::kw_typeof:
1391 ParseTypeofSpecifier(DS);
1392 return true;
1393
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001394 // C++0x decltype support.
1395 case tok::kw_decltype:
1396 ParseDecltypeSpecifier(DS);
1397 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001399 // C++0x auto support.
1400 case tok::kw_auto:
1401 if (!getLang().CPlusPlus0x)
1402 return false;
1403
John McCallfec54012009-08-03 20:12:06 +00001404 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001405 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001406 case tok::kw___ptr64:
1407 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001408 case tok::kw___cdecl:
1409 case tok::kw___stdcall:
1410 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001411 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001412 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001413
Douglas Gregor12e083c2008-11-07 15:42:26 +00001414 default:
1415 // Not a type-specifier; do nothing.
1416 return false;
1417 }
1418
1419 // If the specifier combination wasn't legal, issue a diagnostic.
1420 if (isInvalid) {
1421 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001422 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001423 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001424 }
1425 DS.SetRangeEnd(Tok.getLocation());
1426 ConsumeToken(); // whatever we parsed above.
1427 return true;
1428}
Reid Spencer5f016e22007-07-11 17:01:13 +00001429
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001430/// ParseStructDeclaration - Parse a struct declaration without the terminating
1431/// semicolon.
1432///
Reid Spencer5f016e22007-07-11 17:01:13 +00001433/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001434/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001435/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001436/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001437/// struct-declarator-list:
1438/// struct-declarator
1439/// struct-declarator-list ',' struct-declarator
1440/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1441/// struct-declarator:
1442/// declarator
1443/// [GNU] declarator attributes[opt]
1444/// declarator[opt] ':' constant-expression
1445/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1446///
Chris Lattnere1359422008-04-10 06:46:29 +00001447void Parser::
1448ParseStructDeclaration(DeclSpec &DS,
1449 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001450 if (Tok.is(tok::kw___extension__)) {
1451 // __extension__ silences extension warnings in the subexpression.
1452 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001453 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001454 return ParseStructDeclaration(DS, Fields);
1455 }
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Steve Naroff28a7ca82007-08-20 22:28:22 +00001457 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001458 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001459 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001461 // If there are no declarators, this is a free-standing declaration
1462 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001463 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001464 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001465 return;
1466 }
1467
1468 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001469 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001470 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001471 FieldDeclarator &DeclaratorInfo = Fields.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Steve Naroff28a7ca82007-08-20 22:28:22 +00001473 /// struct-declarator: declarator
1474 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001475 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001476 ParseDeclarator(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Chris Lattner04d66662007-10-09 17:33:22 +00001478 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001479 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001480 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001481 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001482 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001483 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001484 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001485 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001486
Steve Naroff28a7ca82007-08-20 22:28:22 +00001487 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001488 if (Tok.is(tok::kw___attribute)) {
1489 SourceLocation Loc;
1490 AttributeList *AttrList = ParseAttributes(&Loc);
1491 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1492 }
1493
Steve Naroff28a7ca82007-08-20 22:28:22 +00001494 // If we don't have a comma, it is either the end of the list (a ';')
1495 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001496 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001497 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001498
Steve Naroff28a7ca82007-08-20 22:28:22 +00001499 // Consume the comma.
1500 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001501
Steve Naroff28a7ca82007-08-20 22:28:22 +00001502 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001503 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001504
Steve Naroff28a7ca82007-08-20 22:28:22 +00001505 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001506 if (Tok.is(tok::kw___attribute)) {
1507 SourceLocation Loc;
1508 AttributeList *AttrList = ParseAttributes(&Loc);
1509 Fields.back().D.AddAttributes(AttrList, Loc);
1510 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001511 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001512}
1513
1514/// ParseStructUnionBody
1515/// struct-contents:
1516/// struct-declaration-list
1517/// [EXT] empty
1518/// [GNU] "struct-declaration-list" without terminatoring ';'
1519/// struct-declaration-list:
1520/// struct-declaration
1521/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001522/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001523///
Reid Spencer5f016e22007-07-11 17:01:13 +00001524void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001525 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001526 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1527 PP.getSourceManager(),
1528 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Reid Spencer5f016e22007-07-11 17:01:13 +00001530 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001532 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001533 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1534
Reid Spencer5f016e22007-07-11 17:01:13 +00001535 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1536 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001537 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001538 Diag(Tok, diag::ext_empty_struct_union_enum)
1539 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001540
Chris Lattnerb28317a2009-03-28 19:18:32 +00001541 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001542 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1543
Reid Spencer5f016e22007-07-11 17:01:13 +00001544 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001545 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001546 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Reid Spencer5f016e22007-07-11 17:01:13 +00001548 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001549 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001550 Diag(Tok, diag::ext_extra_struct_semi)
1551 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001552 ConsumeToken();
1553 continue;
1554 }
Chris Lattnere1359422008-04-10 06:46:29 +00001555
1556 // Parse all the comma separated declarators.
1557 DeclSpec DS;
1558 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001559 if (!Tok.is(tok::at)) {
1560 ParseStructDeclaration(DS, FieldDeclarators);
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001562 // Convert them all to fields.
1563 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1564 FieldDeclarator &FD = FieldDeclarators[i];
Douglas Gregor91a28862009-08-26 14:27:30 +00001565 DeclPtrTy Field;
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001566 // Install the declarator into the current TagDecl.
Douglas Gregor91a28862009-08-26 14:27:30 +00001567 if (FD.D.getExtension()) {
1568 // Silences extension warnings
1569 ExtensionRAIIObject O(Diags);
1570 Field = Actions.ActOnField(CurScope, TagDecl,
1571 DS.getSourceRange().getBegin(),
1572 FD.D, FD.BitfieldSize);
1573 } else {
1574 Field = Actions.ActOnField(CurScope, TagDecl,
1575 DS.getSourceRange().getBegin(),
1576 FD.D, FD.BitfieldSize);
1577 }
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001578 FieldDecls.push_back(Field);
1579 }
1580 } else { // Handle @defs
1581 ConsumeToken();
1582 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1583 Diag(Tok, diag::err_unexpected_at);
1584 SkipUntil(tok::semi, true, true);
1585 continue;
1586 }
1587 ConsumeToken();
1588 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1589 if (!Tok.is(tok::identifier)) {
1590 Diag(Tok, diag::err_expected_ident);
1591 SkipUntil(tok::semi, true, true);
1592 continue;
1593 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001594 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001595 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001596 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001597 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1598 ConsumeToken();
1599 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001600 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001601
Chris Lattner04d66662007-10-09 17:33:22 +00001602 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001603 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001604 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001605 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001606 break;
1607 } else {
1608 Diag(Tok, diag::err_expected_semi_decl_list);
1609 // Skip to end of block or statement
1610 SkipUntil(tok::r_brace, true, true);
1611 }
1612 }
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Steve Naroff60fccee2007-10-29 21:38:07 +00001614 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 AttributeList *AttrList = 0;
1617 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001618 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001619 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001620
1621 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001622 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001623 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001624 AttrList);
1625 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001626 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001627}
1628
1629
1630/// ParseEnumSpecifier
1631/// enum-specifier: [C99 6.7.2.2]
1632/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001633///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001634/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1635/// '}' attributes[opt]
1636/// 'enum' identifier
1637/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001638///
1639/// [C++] elaborated-type-specifier:
1640/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1641///
Chris Lattner4c97d762009-04-12 21:49:30 +00001642void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1643 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001644 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001645 if (Tok.is(tok::code_completion)) {
1646 // Code completion for an enum name.
1647 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1648 ConsumeToken();
1649 }
1650
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001651 AttributeList *Attr = 0;
1652 // If attributes exist after tag, parse them.
1653 if (Tok.is(tok::kw___attribute))
1654 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001655
1656 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001657 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001658 if (Tok.isNot(tok::identifier)) {
1659 Diag(Tok, diag::err_expected_ident);
1660 if (Tok.isNot(tok::l_brace)) {
1661 // Has no name and is not a definition.
1662 // Skip the rest of this declarator, up until the comma or semicolon.
1663 SkipUntil(tok::comma, true);
1664 return;
1665 }
1666 }
1667 }
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001669 // Must have either 'enum name' or 'enum {...}'.
1670 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1671 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001673 // Skip the rest of this declarator, up until the comma or semicolon.
1674 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001676 }
Mike Stump1eb44332009-09-09 15:08:12 +00001677
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001678 // If an identifier is present, consume and remember it.
1679 IdentifierInfo *Name = 0;
1680 SourceLocation NameLoc;
1681 if (Tok.is(tok::identifier)) {
1682 Name = Tok.getIdentifierInfo();
1683 NameLoc = ConsumeToken();
1684 }
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001686 // There are three options here. If we have 'enum foo;', then this is a
1687 // forward declaration. If we have 'enum foo {...' then this is a
1688 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1689 //
1690 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1691 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1692 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1693 //
John McCall0f434ec2009-07-31 02:45:11 +00001694 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001695 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001696 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001697 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001698 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001699 else
John McCall0f434ec2009-07-31 02:45:11 +00001700 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001701 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001702 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00001703 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001704 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001705 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001706 Owned, IsDependent);
1707 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Chris Lattner04d66662007-10-09 17:33:22 +00001709 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001710 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Reid Spencer5f016e22007-07-11 17:01:13 +00001712 // TODO: semantic analysis on the declspec for enums.
1713 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001714 unsigned DiagID;
1715 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001716 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001717 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001718}
1719
1720/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1721/// enumerator-list:
1722/// enumerator
1723/// enumerator-list ',' enumerator
1724/// enumerator:
1725/// enumeration-constant
1726/// enumeration-constant '=' constant-expression
1727/// enumeration-constant:
1728/// identifier
1729///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001730void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001731 // Enter the scope of the enum body and start the definition.
1732 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001733 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001734
Reid Spencer5f016e22007-07-11 17:01:13 +00001735 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Chris Lattner7946dd32007-08-27 17:24:30 +00001737 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001738 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001739 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Chris Lattnerb28317a2009-03-28 19:18:32 +00001741 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001742
Chris Lattnerb28317a2009-03-28 19:18:32 +00001743 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Reid Spencer5f016e22007-07-11 17:01:13 +00001745 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001746 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001747 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1748 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001749
Reid Spencer5f016e22007-07-11 17:01:13 +00001750 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001751 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001752 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001753 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001754 AssignedVal = ParseConstantExpression();
1755 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001756 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001757 }
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Reid Spencer5f016e22007-07-11 17:01:13 +00001759 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001760 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1761 LastEnumConstDecl,
1762 IdentLoc, Ident,
1763 EqualLoc,
1764 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 EnumConstantDecls.push_back(EnumConstDecl);
1766 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001767
Chris Lattner04d66662007-10-09 17:33:22 +00001768 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001769 break;
1770 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001771
1772 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001773 !(getLang().C99 || getLang().CPlusPlus0x))
1774 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1775 << getLang().CPlusPlus
1776 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 }
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Reid Spencer5f016e22007-07-11 17:01:13 +00001779 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001780 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001781
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001782 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001783 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001784 if (Tok.is(tok::kw___attribute))
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001785 Attr = ParseAttributes();
Douglas Gregor72de6672009-01-08 20:45:30 +00001786
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001787 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1788 EnumConstantDecls.data(), EnumConstantDecls.size(),
1789 CurScope, Attr);
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Douglas Gregor72de6672009-01-08 20:45:30 +00001791 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001792 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001793}
1794
1795/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001796/// start of a type-qualifier-list.
1797bool Parser::isTypeQualifier() const {
1798 switch (Tok.getKind()) {
1799 default: return false;
1800 // type-qualifier
1801 case tok::kw_const:
1802 case tok::kw_volatile:
1803 case tok::kw_restrict:
1804 return true;
1805 }
1806}
1807
1808/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001809/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001810bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001811 switch (Tok.getKind()) {
1812 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Chris Lattner166a8fc2009-01-04 23:41:41 +00001814 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001815 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001816 // Annotate typenames and C++ scope specifiers. If we get one, just
1817 // recurse to handle whatever we get.
1818 if (TryAnnotateTypeOrScopeToken())
1819 return isTypeSpecifierQualifier();
1820 // Otherwise, not a type specifier.
1821 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001822
Chris Lattner166a8fc2009-01-04 23:41:41 +00001823 case tok::coloncolon: // ::foo::bar
1824 if (NextToken().is(tok::kw_new) || // ::new
1825 NextToken().is(tok::kw_delete)) // ::delete
1826 return false;
1827
1828 // Annotate typenames and C++ scope specifiers. If we get one, just
1829 // recurse to handle whatever we get.
1830 if (TryAnnotateTypeOrScopeToken())
1831 return isTypeSpecifierQualifier();
1832 // Otherwise, not a type specifier.
1833 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Reid Spencer5f016e22007-07-11 17:01:13 +00001835 // GNU attributes support.
1836 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001837 // GNU typeof support.
1838 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Reid Spencer5f016e22007-07-11 17:01:13 +00001840 // type-specifiers
1841 case tok::kw_short:
1842 case tok::kw_long:
1843 case tok::kw_signed:
1844 case tok::kw_unsigned:
1845 case tok::kw__Complex:
1846 case tok::kw__Imaginary:
1847 case tok::kw_void:
1848 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001849 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001850 case tok::kw_char16_t:
1851 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001852 case tok::kw_int:
1853 case tok::kw_float:
1854 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001855 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001856 case tok::kw__Bool:
1857 case tok::kw__Decimal32:
1858 case tok::kw__Decimal64:
1859 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Chris Lattner99dc9142008-04-13 18:59:07 +00001861 // struct-or-union-specifier (C99) or class-specifier (C++)
1862 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001863 case tok::kw_struct:
1864 case tok::kw_union:
1865 // enum-specifier
1866 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Reid Spencer5f016e22007-07-11 17:01:13 +00001868 // type-qualifier
1869 case tok::kw_const:
1870 case tok::kw_volatile:
1871 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001872
1873 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001874 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001875 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Chris Lattner7c186be2008-10-20 00:25:30 +00001877 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1878 case tok::less:
1879 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00001880
Steve Naroff239f0732008-12-25 14:16:32 +00001881 case tok::kw___cdecl:
1882 case tok::kw___stdcall:
1883 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001884 case tok::kw___w64:
1885 case tok::kw___ptr64:
1886 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001887 }
1888}
1889
1890/// isDeclarationSpecifier() - Return true if the current token is part of a
1891/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001892bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001893 switch (Tok.getKind()) {
1894 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Chris Lattner166a8fc2009-01-04 23:41:41 +00001896 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001897 // Unfortunate hack to support "Class.factoryMethod" notation.
1898 if (getLang().ObjC1 && NextToken().is(tok::period))
1899 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001900 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001901
Douglas Gregord57959a2009-03-27 23:10:48 +00001902 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001903 // Annotate typenames and C++ scope specifiers. If we get one, just
1904 // recurse to handle whatever we get.
1905 if (TryAnnotateTypeOrScopeToken())
1906 return isDeclarationSpecifier();
1907 // Otherwise, not a declaration specifier.
1908 return false;
1909 case tok::coloncolon: // ::foo::bar
1910 if (NextToken().is(tok::kw_new) || // ::new
1911 NextToken().is(tok::kw_delete)) // ::delete
1912 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001913
Chris Lattner166a8fc2009-01-04 23:41:41 +00001914 // Annotate typenames and C++ scope specifiers. If we get one, just
1915 // recurse to handle whatever we get.
1916 if (TryAnnotateTypeOrScopeToken())
1917 return isDeclarationSpecifier();
1918 // Otherwise, not a declaration specifier.
1919 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Reid Spencer5f016e22007-07-11 17:01:13 +00001921 // storage-class-specifier
1922 case tok::kw_typedef:
1923 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001924 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001925 case tok::kw_static:
1926 case tok::kw_auto:
1927 case tok::kw_register:
1928 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Reid Spencer5f016e22007-07-11 17:01:13 +00001930 // type-specifiers
1931 case tok::kw_short:
1932 case tok::kw_long:
1933 case tok::kw_signed:
1934 case tok::kw_unsigned:
1935 case tok::kw__Complex:
1936 case tok::kw__Imaginary:
1937 case tok::kw_void:
1938 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001939 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001940 case tok::kw_char16_t:
1941 case tok::kw_char32_t:
1942
Reid Spencer5f016e22007-07-11 17:01:13 +00001943 case tok::kw_int:
1944 case tok::kw_float:
1945 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001946 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001947 case tok::kw__Bool:
1948 case tok::kw__Decimal32:
1949 case tok::kw__Decimal64:
1950 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Chris Lattner99dc9142008-04-13 18:59:07 +00001952 // struct-or-union-specifier (C99) or class-specifier (C++)
1953 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001954 case tok::kw_struct:
1955 case tok::kw_union:
1956 // enum-specifier
1957 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00001958
Reid Spencer5f016e22007-07-11 17:01:13 +00001959 // type-qualifier
1960 case tok::kw_const:
1961 case tok::kw_volatile:
1962 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001963
Reid Spencer5f016e22007-07-11 17:01:13 +00001964 // function-specifier
1965 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001966 case tok::kw_virtual:
1967 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001968
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001969 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001970 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001971
Chris Lattner1ef08762007-08-09 17:01:07 +00001972 // GNU typeof support.
1973 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Chris Lattner1ef08762007-08-09 17:01:07 +00001975 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001976 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001977 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Chris Lattnerf3948c42008-07-26 03:38:44 +00001979 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1980 case tok::less:
1981 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00001982
Steve Naroff47f52092009-01-06 19:34:12 +00001983 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001984 case tok::kw___cdecl:
1985 case tok::kw___stdcall:
1986 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001987 case tok::kw___w64:
1988 case tok::kw___ptr64:
1989 case tok::kw___forceinline:
1990 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001991 }
1992}
1993
1994
1995/// ParseTypeQualifierListOpt
1996/// type-qualifier-list: [C99 6.7.5]
1997/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001998/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001999/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002000/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002001///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002002void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002003 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002004 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002005 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002006 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002007 SourceLocation Loc = Tok.getLocation();
2008
2009 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002010 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002011 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2012 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002013 break;
2014 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002015 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2016 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002017 break;
2018 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002019 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2020 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002021 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002022 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002023 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002024 case tok::kw___cdecl:
2025 case tok::kw___stdcall:
2026 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002027 if (AttributesAllowed) {
2028 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2029 continue;
2030 }
2031 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002032 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002033 if (AttributesAllowed) {
2034 DS.AddAttributes(ParseAttributes());
2035 continue; // do *not* consume the next token!
2036 }
2037 // otherwise, FALL THROUGH!
2038 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002039 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002040 // If this is not a type-qualifier token, we're done reading type
2041 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002042 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002043 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002044 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002045
Reid Spencer5f016e22007-07-11 17:01:13 +00002046 // If the specifier combination wasn't legal, issue a diagnostic.
2047 if (isInvalid) {
2048 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002049 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002050 }
2051 ConsumeToken();
2052 }
2053}
2054
2055
2056/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2057///
2058void Parser::ParseDeclarator(Declarator &D) {
2059 /// This implements the 'declarator' production in the C grammar, then checks
2060 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002061 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002062}
2063
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002064/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2065/// is parsed by the function passed to it. Pass null, and the direct-declarator
2066/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002067/// ptr-operator production.
2068///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002069/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2070/// [C] pointer[opt] direct-declarator
2071/// [C++] direct-declarator
2072/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002073///
2074/// pointer: [C99 6.7.5]
2075/// '*' type-qualifier-list[opt]
2076/// '*' type-qualifier-list[opt] pointer
2077///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002078/// ptr-operator:
2079/// '*' cv-qualifier-seq[opt]
2080/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002081/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002082/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002083/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002084/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002085void Parser::ParseDeclaratorInternal(Declarator &D,
2086 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002087
Douglas Gregor91a28862009-08-26 14:27:30 +00002088 if (Diags.hasAllExtensionsSilenced())
2089 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002090 // C++ member pointers start with a '::' or a nested-name.
2091 // Member pointers get special handling, since there's no place for the
2092 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002093 if (getLang().CPlusPlus &&
2094 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2095 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002096 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002097 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002098 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002099 // The scope spec really belongs to the direct-declarator.
2100 D.getCXXScopeSpec() = SS;
2101 if (DirectDeclParser)
2102 (this->*DirectDeclParser)(D);
2103 return;
2104 }
2105
2106 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002107 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002108 DeclSpec DS;
2109 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002110 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002111
2112 // Recurse to parse whatever is left.
2113 ParseDeclaratorInternal(D, DirectDeclParser);
2114
2115 // Sema will have to catch (syntactically invalid) pointers into global
2116 // scope. It has to catch pointers into namespace scope anyway.
2117 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002118 Loc, DS.TakeAttributes()),
2119 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002120 return;
2121 }
2122 }
2123
2124 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002125 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002126 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002127 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002128 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002129 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002130 if (DirectDeclParser)
2131 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002132 return;
2133 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002134
Sebastian Redl05532f22009-03-15 22:02:01 +00002135 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2136 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002137 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002138 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002139
Chris Lattner9af55002009-03-27 04:18:06 +00002140 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002141 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002142 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002143
Reid Spencer5f016e22007-07-11 17:01:13 +00002144 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002145 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002146
Reid Spencer5f016e22007-07-11 17:01:13 +00002147 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002148 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002149 if (Kind == tok::star)
2150 // Remember that we parsed a pointer type, and remember the type-quals.
2151 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002152 DS.TakeAttributes()),
2153 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002154 else
2155 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002156 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002157 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002158 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002159 } else {
2160 // Is a reference
2161 DeclSpec DS;
2162
Sebastian Redl743de1f2009-03-23 00:00:23 +00002163 // Complain about rvalue references in C++03, but then go on and build
2164 // the declarator.
2165 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2166 Diag(Loc, diag::err_rvalue_reference);
2167
Reid Spencer5f016e22007-07-11 17:01:13 +00002168 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2169 // cv-qualifiers are introduced through the use of a typedef or of a
2170 // template type argument, in which case the cv-qualifiers are ignored.
2171 //
2172 // [GNU] Retricted references are allowed.
2173 // [GNU] Attributes on references are allowed.
2174 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002175 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002176
2177 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2178 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2179 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002180 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002181 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2182 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002183 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002184 }
2185
2186 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002187 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002188
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002189 if (D.getNumTypeObjects() > 0) {
2190 // C++ [dcl.ref]p4: There shall be no references to references.
2191 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2192 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002193 if (const IdentifierInfo *II = D.getIdentifier())
2194 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2195 << II;
2196 else
2197 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2198 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002199
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002200 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002201 // can go ahead and build the (technically ill-formed)
2202 // declarator: reference collapsing will take care of it.
2203 }
2204 }
2205
Reid Spencer5f016e22007-07-11 17:01:13 +00002206 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002207 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002208 DS.TakeAttributes(),
2209 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002210 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002211 }
2212}
2213
2214/// ParseDirectDeclarator
2215/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002216/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002217/// '(' declarator ')'
2218/// [GNU] '(' attributes declarator ')'
2219/// [C90] direct-declarator '[' constant-expression[opt] ']'
2220/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2221/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2222/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2223/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2224/// direct-declarator '(' parameter-type-list ')'
2225/// direct-declarator '(' identifier-list[opt] ')'
2226/// [GNU] direct-declarator '(' parameter-forward-declarations
2227/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002228/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2229/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002230/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002231///
2232/// declarator-id: [C++ 8]
2233/// id-expression
2234/// '::'[opt] nested-name-specifier[opt] type-name
2235///
2236/// id-expression: [C++ 5.1]
2237/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002238/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002239///
2240/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002241/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002242/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002243/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002244/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002245/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002246///
Reid Spencer5f016e22007-07-11 17:01:13 +00002247void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002248 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002249
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002250 if (getLang().CPlusPlus) {
2251 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002252 // ParseDeclaratorInternal might already have parsed the scope.
2253 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
Mike Stump1eb44332009-09-09 15:08:12 +00002254 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002255 true);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002256 if (afterCXXScope) {
2257 // Change the declaration context for name lookup, until this function
2258 // is exited (and the declarator has been parsed).
2259 DeclScopeObj.EnterDeclaratorScope();
2260 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002261
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002262 if (Tok.is(tok::identifier)) {
2263 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlsson4649cac2009-04-30 22:41:11 +00002264
2265 // If this identifier is the name of the current class, it's a
Mike Stump1eb44332009-09-09 15:08:12 +00002266 // constructor name.
Anders Carlsson4649cac2009-04-30 22:41:11 +00002267 if (!D.getDeclSpec().hasTypeSpecifier() &&
2268 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
Douglas Gregor675431d2009-07-06 16:40:48 +00002269 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Anders Carlsson4649cac2009-04-30 22:41:11 +00002270 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor675431d2009-07-06 16:40:48 +00002271 Tok.getLocation(), CurScope, SS),
Anders Carlsson4649cac2009-04-30 22:41:11 +00002272 Tok.getLocation());
2273 // This is a normal identifier.
2274 } else
2275 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002276 ConsumeToken();
2277 goto PastIdentifier;
Douglas Gregor39a8de12009-02-25 19:37:18 +00002278 } else if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002279 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00002280 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2281
Douglas Gregordb422df2009-09-25 21:45:23 +00002282 D.setTemplateId(TemplateId);
Douglas Gregor39a8de12009-02-25 19:37:18 +00002283 ConsumeToken();
2284 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00002285 } else if (Tok.is(tok::kw_operator)) {
2286 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002287 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002288
Douglas Gregor70316a02008-12-26 15:00:45 +00002289 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00002290 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2291 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00002292 } else {
2293 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00002294 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2295 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2296 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00002297 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002298 }
Douglas Gregor70316a02008-12-26 15:00:45 +00002299 }
2300 goto PastIdentifier;
2301 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002302 // This should be a C++ destructor.
2303 SourceLocation TildeLoc = ConsumeToken();
Douglas Gregor42c39f32009-08-26 18:27:52 +00002304 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002305 // FIXME: Inaccurate.
2306 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7f43d672009-02-25 23:52:28 +00002307 SourceLocation EndLoc;
Douglas Gregor675431d2009-07-06 16:40:48 +00002308 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Fariborz Jahaniand33c8682009-07-20 17:43:15 +00002309 TypeResult Type = ParseClassName(EndLoc, SS, true);
Douglas Gregor31a19b62009-04-01 21:51:26 +00002310 if (Type.isInvalid())
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002311 D.SetIdentifier(0, TildeLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00002312 else
2313 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002314 } else {
Fariborz Jahaniand33c8682009-07-20 17:43:15 +00002315 Diag(Tok, diag::err_destructor_class_name);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002316 D.SetIdentifier(0, TildeLoc);
2317 }
2318 goto PastIdentifier;
2319 }
2320
2321 // If we reached this point, token is not identifier and not '~'.
2322
2323 if (afterCXXScope) {
2324 Diag(Tok, diag::err_expected_unqualified_id);
2325 D.SetIdentifier(0, Tok.getLocation());
2326 D.setInvalidType(true);
2327 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002328 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002329 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002330 }
2331
2332 // If we reached this point, we are either in C/ObjC or the token didn't
2333 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002334 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2335 assert(!getLang().CPlusPlus &&
2336 "There's a C++-specific check for tok::identifier above");
2337 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2338 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2339 ConsumeToken();
2340 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002341 // direct-declarator: '(' declarator ')'
2342 // direct-declarator: '(' attributes declarator ')'
2343 // Example: 'char (*X)' or 'int (*XX)(void)'
2344 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002345 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002346 // This could be something simple like "int" (in which case the declarator
2347 // portion is empty), if an abstract-declarator is allowed.
2348 D.SetIdentifier(0, Tok.getLocation());
2349 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002350 if (D.getContext() == Declarator::MemberContext)
2351 Diag(Tok, diag::err_expected_member_name_or_semi)
2352 << D.getDeclSpec().getSourceRange();
2353 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002354 Diag(Tok, diag::err_expected_unqualified_id);
2355 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002356 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002357 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002358 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002359 }
Mike Stump1eb44332009-09-09 15:08:12 +00002360
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002361 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002362 assert(D.isPastIdentifier() &&
2363 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002364
Reid Spencer5f016e22007-07-11 17:01:13 +00002365 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002366 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002367 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2368 // In such a case, check if we actually have a function declarator; if it
2369 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002370 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2371 // When not in file scope, warn for ambiguous function declarators, just
2372 // in case the author intended it as a variable definition.
2373 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2374 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2375 break;
2376 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002377 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002378 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002379 ParseBracketDeclarator(D);
2380 } else {
2381 break;
2382 }
2383 }
2384}
2385
Chris Lattneref4715c2008-04-06 05:45:57 +00002386/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2387/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002388/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002389/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2390///
2391/// direct-declarator:
2392/// '(' declarator ')'
2393/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002394/// direct-declarator '(' parameter-type-list ')'
2395/// direct-declarator '(' identifier-list[opt] ')'
2396/// [GNU] direct-declarator '(' parameter-forward-declarations
2397/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002398///
2399void Parser::ParseParenDeclarator(Declarator &D) {
2400 SourceLocation StartLoc = ConsumeParen();
2401 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002402
Chris Lattner7399ee02008-10-20 02:05:46 +00002403 // Eat any attributes before we look at whether this is a grouping or function
2404 // declarator paren. If this is a grouping paren, the attribute applies to
2405 // the type being built up, for example:
2406 // int (__attribute__(()) *x)(long y)
2407 // If this ends up not being a grouping paren, the attribute applies to the
2408 // first argument, for example:
2409 // int (__attribute__(()) int x)
2410 // In either case, we need to eat any attributes to be able to determine what
2411 // sort of paren this is.
2412 //
2413 AttributeList *AttrList = 0;
2414 bool RequiresArg = false;
2415 if (Tok.is(tok::kw___attribute)) {
2416 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00002417
Chris Lattner7399ee02008-10-20 02:05:46 +00002418 // We require that the argument list (if this is a non-grouping paren) be
2419 // present even if the attribute list was empty.
2420 RequiresArg = true;
2421 }
Steve Naroff239f0732008-12-25 14:16:32 +00002422 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002423 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2424 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2425 Tok.is(tok::kw___ptr64)) {
2426 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2427 }
Mike Stump1eb44332009-09-09 15:08:12 +00002428
Chris Lattneref4715c2008-04-06 05:45:57 +00002429 // If we haven't past the identifier yet (or where the identifier would be
2430 // stored, if this is an abstract declarator), then this is probably just
2431 // grouping parens. However, if this could be an abstract-declarator, then
2432 // this could also be the start of function arguments (consider 'void()').
2433 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002434
Chris Lattneref4715c2008-04-06 05:45:57 +00002435 if (!D.mayOmitIdentifier()) {
2436 // If this can't be an abstract-declarator, this *must* be a grouping
2437 // paren, because we haven't seen the identifier yet.
2438 isGrouping = true;
2439 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002440 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002441 isDeclarationSpecifier()) { // 'int(int)' is a function.
2442 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2443 // considered to be a type, not a K&R identifier-list.
2444 isGrouping = false;
2445 } else {
2446 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2447 isGrouping = true;
2448 }
Mike Stump1eb44332009-09-09 15:08:12 +00002449
Chris Lattneref4715c2008-04-06 05:45:57 +00002450 // If this is a grouping paren, handle:
2451 // direct-declarator: '(' declarator ')'
2452 // direct-declarator: '(' attributes declarator ')'
2453 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002454 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002455 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002456 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002457 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002458
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002459 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002460 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002461 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002462
2463 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002464 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002465 return;
2466 }
Mike Stump1eb44332009-09-09 15:08:12 +00002467
Chris Lattneref4715c2008-04-06 05:45:57 +00002468 // Okay, if this wasn't a grouping paren, it must be the start of a function
2469 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002470 // identifier (and remember where it would have been), then call into
2471 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002472 D.SetIdentifier(0, Tok.getLocation());
2473
Chris Lattner7399ee02008-10-20 02:05:46 +00002474 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002475}
2476
2477/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2478/// declarator D up to a paren, which indicates that we are parsing function
2479/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002480///
Chris Lattner7399ee02008-10-20 02:05:46 +00002481/// If AttrList is non-null, then the caller parsed those arguments immediately
2482/// after the open paren - they should be considered to be the first argument of
2483/// a parameter. If RequiresArg is true, then the first argument of the
2484/// function is required to be present and required to not be an identifier
2485/// list.
2486///
Reid Spencer5f016e22007-07-11 17:01:13 +00002487/// This method also handles this portion of the grammar:
2488/// parameter-type-list: [C99 6.7.5]
2489/// parameter-list
2490/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002491/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002492///
2493/// parameter-list: [C99 6.7.5]
2494/// parameter-declaration
2495/// parameter-list ',' parameter-declaration
2496///
2497/// parameter-declaration: [C99 6.7.5]
2498/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002499/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002500/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002501/// declaration-specifiers abstract-declarator[opt]
2502/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002503/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002504/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2505///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002506/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002507/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002508///
Chris Lattner7399ee02008-10-20 02:05:46 +00002509void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2510 AttributeList *AttrList,
2511 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002512 // lparen is already consumed!
2513 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002514
Chris Lattner7399ee02008-10-20 02:05:46 +00002515 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002516 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002517 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002518 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002519 delete AttrList;
2520 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002521
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002522 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2523 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002524
2525 // cv-qualifier-seq[opt].
2526 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002527 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002528 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002529 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002530 llvm::SmallVector<TypeTy*, 2> Exceptions;
2531 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002532 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002533 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002534 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002535 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002536
2537 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002538 if (Tok.is(tok::kw_throw)) {
2539 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002540 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002541 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002542 hasAnyExceptionSpec);
2543 assert(Exceptions.size() == ExceptionRanges.size() &&
2544 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002545 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002546 }
2547
Chris Lattnerf97409f2008-04-06 06:57:35 +00002548 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002549 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002550 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002551 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002552 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002553 /*arglist*/ 0, 0,
2554 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002555 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002556 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002557 Exceptions.data(),
2558 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002559 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002560 LParenLoc, RParenLoc, D),
2561 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002562 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002563 }
2564
Chris Lattner7399ee02008-10-20 02:05:46 +00002565 // Alternatively, this parameter list may be an identifier list form for a
2566 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002567 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002568 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002569 // K&R identifier lists can't have typedefs as identifiers, per
2570 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002571 if (RequiresArg) {
2572 Diag(Tok, diag::err_argument_required_after_attribute);
2573 delete AttrList;
2574 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002575 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2576 // normal declarators, not for abstract-declarators.
2577 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002578 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002579 }
Mike Stump1eb44332009-09-09 15:08:12 +00002580
Chris Lattnerf97409f2008-04-06 06:57:35 +00002581 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002582
Chris Lattnerf97409f2008-04-06 06:57:35 +00002583 // Build up an array of information about the parsed arguments.
2584 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002585
2586 // Enter function-declaration scope, limiting any declarators to the
2587 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002588 ParseScope PrototypeScope(this,
2589 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002590
Chris Lattnerf97409f2008-04-06 06:57:35 +00002591 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002592 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002593 while (1) {
2594 if (Tok.is(tok::ellipsis)) {
2595 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002596 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002597 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002598 }
Mike Stump1eb44332009-09-09 15:08:12 +00002599
Chris Lattnerf97409f2008-04-06 06:57:35 +00002600 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002601
Chris Lattnerf97409f2008-04-06 06:57:35 +00002602 // Parse the declaration-specifiers.
2603 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}