blob: 2eccbd04dad964cc69c2ba11fe94e09e08ff45e3 [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.
Mike Stump1eb44332009-09-09 15:08:12 +0000422 DeclPtrTy ThisDecl = TemplateInfo.TemplateParams?
Douglas Gregore542c862009-06-23 23:11:28 +0000423 Actions.ActOnTemplateDeclarator(CurScope,
424 Action::MultiTemplateParamsArg(Actions,
425 TemplateInfo.TemplateParams->data(),
426 TemplateInfo.TemplateParams->size()),
427 D)
428 : Actions.ActOnDeclarator(CurScope, D);
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Douglas Gregor1426e532009-05-12 21:31:51 +0000430 // Parse declarator '=' initializer.
431 if (Tok.is(tok::equal)) {
432 ConsumeToken();
433 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
434 SourceLocation DelLoc = ConsumeToken();
435 Actions.SetDeclDeleted(ThisDecl, DelLoc);
436 } else {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000437 if (getLang().CPlusPlus)
438 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
439
Douglas Gregor1426e532009-05-12 21:31:51 +0000440 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000441
442 if (getLang().CPlusPlus)
443 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
444
Douglas Gregor1426e532009-05-12 21:31:51 +0000445 if (Init.isInvalid()) {
446 SkipUntil(tok::semi, true, true);
447 return DeclPtrTy();
448 }
Anders Carlsson9abf2ae2009-08-16 05:13:48 +0000449 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000450 }
451 } else if (Tok.is(tok::l_paren)) {
452 // Parse C++ direct initializer: '(' expression-list ')'
453 SourceLocation LParenLoc = ConsumeParen();
454 ExprVector Exprs(Actions);
455 CommaLocsTy CommaLocs;
456
457 if (ParseExpressionList(Exprs, CommaLocs)) {
458 SkipUntil(tok::r_paren);
459 } else {
460 // Match the ')'.
461 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
462
463 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
464 "Unexpected number of commas!");
465 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
466 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000467 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000468 }
469 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000470 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000471 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
472 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000473 }
474
475 return ThisDecl;
476}
477
478/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
479/// parsing 'declaration-specifiers declarator'. This method is split out this
480/// way to handle the ambiguity between top-level function-definitions and
481/// declarations.
482///
483/// init-declarator-list: [C99 6.7]
484/// init-declarator
485/// init-declarator-list ',' init-declarator
486///
487/// According to the standard grammar, =default and =delete are function
488/// definitions, but that definitely doesn't fit with the parser here.
489///
Chris Lattner682bf922009-03-29 16:50:03 +0000490Parser::DeclGroupPtrTy Parser::
Reid Spencer5f016e22007-07-11 17:01:13 +0000491ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner682bf922009-03-29 16:50:03 +0000492 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
493 // that we parse together here.
494 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 // At this point, we know that it is not a function definition. Parse the
497 // rest of the init-declarator-list.
498 while (1) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000499 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
500 if (ThisDecl.get())
501 DeclsInGroup.push_back(ThisDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Reid Spencer5f016e22007-07-11 17:01:13 +0000503 // If we don't have a comma, it is either the end of the list (a ';') or an
504 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000505 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Reid Spencer5f016e22007-07-11 17:01:13 +0000508 // Consume the comma.
509 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000510
Reid Spencer5f016e22007-07-11 17:01:13 +0000511 // Parse the next declarator.
512 D.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattneraab740a2008-10-20 04:57:38 +0000514 // Accept attributes in an init-declarator. In the first declarator in a
515 // declaration, these would be part of the declspec. In subsequent
516 // declarators, they become part of the declarator itself, so that they
517 // don't apply to declarators after *this* one. Examples:
518 // short __attribute__((common)) var; -> declspec
519 // short var __attribute__((common)); -> declarator
520 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlab197ba2009-02-09 18:23:29 +0000521 if (Tok.is(tok::kw___attribute)) {
522 SourceLocation Loc;
523 AttributeList *AttrList = ParseAttributes(&Loc);
524 D.AddAttributes(AttrList, Loc);
525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 ParseDeclarator(D);
528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Eli Friedmanc1dc6532009-05-29 01:49:24 +0000530 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
531 DeclsInGroup.data(),
Chris Lattner23c4b182009-03-29 17:18:04 +0000532 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000533}
534
535/// ParseSpecifierQualifierList
536/// specifier-qualifier-list:
537/// type-specifier specifier-qualifier-list[opt]
538/// type-qualifier specifier-qualifier-list[opt]
539/// [GNU] attributes specifier-qualifier-list[opt]
540///
541void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
542 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
543 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Reid Spencer5f016e22007-07-11 17:01:13 +0000546 // Validate declspec for type-name.
547 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000548 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
549 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Reid Spencer5f016e22007-07-11 17:01:13 +0000552 // Issue diagnostic and remove storage class if present.
553 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
554 if (DS.getStorageClassSpecLoc().isValid())
555 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
556 else
557 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
558 DS.ClearStorageClassSpecs();
559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 // Issue diagnostic and remove function specfier if present.
562 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000563 if (DS.isInlineSpecified())
564 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
565 if (DS.isVirtualSpecified())
566 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
567 if (DS.isExplicitSpecified())
568 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 DS.ClearFunctionSpecs();
570 }
571}
572
Chris Lattnerc199ab32009-04-12 20:42:31 +0000573/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
574/// specified token is valid after the identifier in a declarator which
575/// immediately follows the declspec. For example, these things are valid:
576///
577/// int x [ 4]; // direct-declarator
578/// int x ( int y); // direct-declarator
579/// int(int x ) // direct-declarator
580/// int x ; // simple-declaration
581/// int x = 17; // init-declarator-list
582/// int x , y; // init-declarator-list
583/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000584/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000585/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000586///
587/// This is not, because 'x' does not immediately follow the declspec (though
588/// ')' happens to be valid anyway).
589/// int (x)
590///
591static bool isValidAfterIdentifierInDeclarator(const Token &T) {
592 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
593 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000594 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000595}
596
Chris Lattnere40c2952009-04-14 21:34:55 +0000597
598/// ParseImplicitInt - This method is called when we have an non-typename
599/// identifier in a declspec (which normally terminates the decl spec) when
600/// the declspec has no type specifier. In this case, the declspec is either
601/// malformed or is "implicit int" (in K&R and C89).
602///
603/// This method handles diagnosing this prettily and returns false if the
604/// declspec is done being processed. If it recovers and thinks there may be
605/// other pieces of declspec after it, it returns true.
606///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000607bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000608 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000609 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000610 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Chris Lattnere40c2952009-04-14 21:34:55 +0000612 SourceLocation Loc = Tok.getLocation();
613 // If we see an identifier that is not a type name, we normally would
614 // parse it as the identifer being declared. However, when a typename
615 // is typo'd or the definition is not included, this will incorrectly
616 // parse the typename as the identifier name and fall over misparsing
617 // later parts of the diagnostic.
618 //
619 // As such, we try to do some look-ahead in cases where this would
620 // otherwise be an "implicit-int" case to see if this is invalid. For
621 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
622 // an identifier with implicit int, we'd get a parse error because the
623 // next token is obviously invalid for a type. Parse these as a case
624 // with an invalid type specifier.
625 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Chris Lattnere40c2952009-04-14 21:34:55 +0000627 // Since we know that this either implicit int (which is rare) or an
628 // error, we'd do lookahead to try to do better recovery.
629 if (isValidAfterIdentifierInDeclarator(NextToken())) {
630 // If this token is valid for implicit int, e.g. "static x = 4", then
631 // we just avoid eating the identifier, so it will be parsed as the
632 // identifier in the declarator.
633 return false;
634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Chris Lattnere40c2952009-04-14 21:34:55 +0000636 // Otherwise, if we don't consume this token, we are going to emit an
637 // error anyway. Try to recover from various common problems. Check
638 // to see if this was a reference to a tag name without a tag specified.
639 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000640 //
641 // C++ doesn't need this, and isTagName doesn't take SS.
642 if (SS == 0) {
643 const char *TagName = 0;
644 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Chris Lattnere40c2952009-04-14 21:34:55 +0000646 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
647 default: break;
648 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
649 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
650 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
651 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
652 }
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Chris Lattnerf4382f52009-04-14 22:17:06 +0000654 if (TagName) {
655 Diag(Loc, diag::err_use_of_tag_name_without_tag)
656 << Tok.getIdentifierInfo() << TagName
657 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Chris Lattnerf4382f52009-04-14 22:17:06 +0000659 // Parse this as a tag as if the missing tag were present.
660 if (TagKind == tok::kw_enum)
661 ParseEnumSpecifier(Loc, DS, AS);
662 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000663 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000664 return true;
665 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000666 }
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Chris Lattnere40c2952009-04-14 21:34:55 +0000668 // Since this is almost certainly an invalid type name, emit a
669 // diagnostic that says it, eat the token, and mark the declspec as
670 // invalid.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000671 SourceRange R;
672 if (SS) R = SS->getRange();
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Chris Lattnerf4382f52009-04-14 22:17:06 +0000674 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattnere40c2952009-04-14 21:34:55 +0000675 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000676 unsigned DiagID;
677 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000678 DS.SetRangeEnd(Tok.getLocation());
679 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Chris Lattnere40c2952009-04-14 21:34:55 +0000681 // TODO: Could inject an invalid typedef decl in an enclosing scope to
682 // avoid rippling error messages on subsequent uses of the same type,
683 // could be useful if #include was forgotten.
684 return false;
685}
686
Reid Spencer5f016e22007-07-11 17:01:13 +0000687/// ParseDeclarationSpecifiers
688/// declaration-specifiers: [C99 6.7]
689/// storage-class-specifier declaration-specifiers[opt]
690/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000691/// [C99] function-specifier declaration-specifiers[opt]
692/// [GNU] attributes declaration-specifiers[opt]
693///
694/// storage-class-specifier: [C99 6.7.1]
695/// 'typedef'
696/// 'extern'
697/// 'static'
698/// 'auto'
699/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000700/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000701/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000702/// function-specifier: [C99 6.7.4]
703/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000704/// [C++] 'virtual'
705/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000706/// 'friend': [C++ dcl.friend]
707
Reid Spencer5f016e22007-07-11 17:01:13 +0000708///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000709void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000710 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000711 AccessSpecifier AS,
712 DeclSpecContext DSContext) {
Chris Lattner81c018d2008-03-13 06:29:04 +0000713 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000714 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000715 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000717 unsigned DiagID = 0;
718
Reid Spencer5f016e22007-07-11 17:01:13 +0000719 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000720
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000722 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000723 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 // If this is not a declaration specifier token, we're done reading decl
725 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000726 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Chris Lattner5e02c472009-01-05 00:07:25 +0000729 case tok::coloncolon: // ::foo::bar
730 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000731 if (TryAnnotateCXXScopeToken(true))
Chris Lattner5e02c472009-01-05 00:07:25 +0000732 continue;
733 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000734
735 case tok::annot_cxxscope: {
736 if (DS.hasTypeSpecifier())
737 goto DoneWithDeclSpec;
738
739 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000740 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000741 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000742 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000743 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000744 // We have a qualified template-id, e.g., N::A<int>
745 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000746 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000747 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000748 "ParseOptionalCXXScopeSpecifier not working");
749 AnnotateTemplateIdTokenAsType(&SS);
750 continue;
751 }
752
753 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000754 goto DoneWithDeclSpec;
755
756 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000757 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000758 SS.setRange(Tok.getAnnotationRange());
759
760 // If the next token is the name of the class type that the C++ scope
761 // denotes, followed by a '(', then this is a constructor declaration.
762 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000763 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000764 CurScope, &SS) &&
765 GetLookAheadToken(2).is(tok::l_paren))
766 goto DoneWithDeclSpec;
767
Douglas Gregorb696ea32009-02-04 17:00:24 +0000768 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
769 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000770
Chris Lattnerf4382f52009-04-14 22:17:06 +0000771 // If the referenced identifier is not a type, then this declspec is
772 // erroneous: We already checked about that it has no type specifier, and
773 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000774 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000775 if (TypeRep == 0) {
776 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000777 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000778 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000779 }
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000781 ConsumeToken(); // The C++ scope.
782
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000783 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000784 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000785 if (isInvalid)
786 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000788 DS.SetRangeEnd(Tok.getLocation());
789 ConsumeToken(); // The typename.
790
791 continue;
792 }
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Chris Lattner80d0c892009-01-21 19:48:37 +0000794 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000795 if (Tok.getAnnotationValue())
796 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000797 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +0000798 else
799 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000800 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
801 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Chris Lattner80d0c892009-01-21 19:48:37 +0000803 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
804 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
805 // Objective-C interface. If we don't have Objective-C or a '<', this is
806 // just a normal reference to a typedef name.
807 if (!Tok.is(tok::less) || !getLang().ObjC1)
808 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Chris Lattner80d0c892009-01-21 19:48:37 +0000810 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000811 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner80d0c892009-01-21 19:48:37 +0000812 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +0000813 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Chris Lattner80d0c892009-01-21 19:48:37 +0000815 DS.SetRangeEnd(EndProtoLoc);
816 continue;
817 }
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Chris Lattner3bd934a2008-07-26 01:18:38 +0000819 // typedef-name
820 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000821 // In C++, check to see if this is a scope specifier like foo::bar::, if
822 // so handle it as such. This is important for ctor parsing.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000823 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner837acd02009-01-21 19:19:26 +0000824 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Chris Lattner3bd934a2008-07-26 01:18:38 +0000826 // This identifier can only be a typedef name if we haven't already seen
827 // a type-specifier. Without this check we misparse:
828 // typedef int X; struct Y { short X; }; as 'short int'.
829 if (DS.hasTypeSpecifier())
830 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Chris Lattner3bd934a2008-07-26 01:18:38 +0000832 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +0000833 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +0000834 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000835
Chris Lattnerc199ab32009-04-12 20:42:31 +0000836 // If this is not a typedef name, don't parse it as part of the declspec,
837 // it must be an implicit int or an error.
838 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000839 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000840 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000841 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000842
Douglas Gregorb48fe382008-10-31 09:07:45 +0000843 // C++: If the identifier is actually the name of the class type
844 // being defined and the next token is a '(', then this is a
845 // constructor declaration. We're done with the decl-specifiers
846 // and will treat this token as an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000847 if (getLang().CPlusPlus &&
848 (CurScope->isClassScope() ||
849 (CurScope->isTemplateParamScope() &&
Douglas Gregordec06662009-08-21 18:42:58 +0000850 CurScope->getParent()->isClassScope())) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000851 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000852 NextToken().getKind() == tok::l_paren)
853 goto DoneWithDeclSpec;
854
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000855 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000856 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000857 if (isInvalid)
858 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Chris Lattner3bd934a2008-07-26 01:18:38 +0000860 DS.SetRangeEnd(Tok.getLocation());
861 ConsumeToken(); // The identifier
862
863 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
864 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
865 // Objective-C interface. If we don't have Objective-C or a '<', this is
866 // just a normal reference to a typedef name.
867 if (!Tok.is(tok::less) || !getLang().ObjC1)
868 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Chris Lattner3bd934a2008-07-26 01:18:38 +0000870 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000871 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +0000872 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +0000873 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Chris Lattner3bd934a2008-07-26 01:18:38 +0000875 DS.SetRangeEnd(EndProtoLoc);
876
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000877 // Need to support trailing type qualifiers (e.g. "id<p> const").
878 // If a type specifier follows, it will be diagnosed elsewhere.
879 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000880 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000881
882 // type-name
883 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +0000884 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000885 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000886 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000887 // This template-id does not refer to a type name, so we're
888 // done with the type-specifiers.
889 goto DoneWithDeclSpec;
890 }
891
892 // Turn the template-id annotation token into a type annotation
893 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000894 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000895 continue;
896 }
897
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 // GNU attributes support.
899 case tok::kw___attribute:
900 DS.AddAttributes(ParseAttributes());
901 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000902
903 // Microsoft declspec support.
904 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +0000905 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +0000906 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Steve Naroff239f0732008-12-25 14:16:32 +0000908 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000909 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +0000910 // FIXME: Add handling here!
911 break;
912
913 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000914 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000915 case tok::kw___cdecl:
916 case tok::kw___stdcall:
917 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000918 DS.AddAttributes(ParseMicrosoftTypeAttributes());
919 continue;
920
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 // storage-class-specifier
922 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +0000923 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
924 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 break;
926 case tok::kw_extern:
927 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000928 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +0000929 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
930 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +0000932 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +0000933 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +0000934 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +0000935 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 case tok::kw_static:
937 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000938 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +0000939 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
940 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 break;
942 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +0000943 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +0000944 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
945 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +0000946 else
John McCallfec54012009-08-03 20:12:06 +0000947 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
948 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 break;
950 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +0000951 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
952 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000953 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +0000954 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +0000955 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
956 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +0000957 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +0000959 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000960 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 // function-specifier
963 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +0000964 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000966 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +0000967 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000968 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +0000969 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +0000970 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +0000971 break;
Chris Lattner80d0c892009-01-21 19:48:37 +0000972
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000973 // friend
974 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +0000975 if (DSContext == DSC_class)
976 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
977 else {
978 PrevSpec = ""; // not actually used by the diagnostic
979 DiagID = diag::err_friend_invalid_in_context;
980 isInvalid = true;
981 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000982 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000983
Chris Lattner80d0c892009-01-21 19:48:37 +0000984 // type-specifier
985 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +0000986 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
987 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +0000988 break;
989 case tok::kw_long:
990 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +0000991 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
992 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +0000993 else
John McCallfec54012009-08-03 20:12:06 +0000994 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
995 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +0000996 break;
997 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +0000998 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
999 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001000 break;
1001 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001002 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1003 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001004 break;
1005 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001006 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1007 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001008 break;
1009 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001010 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1011 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001012 break;
1013 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001014 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1015 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001016 break;
1017 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001018 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1019 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001020 break;
1021 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001022 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1023 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001024 break;
1025 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001026 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1027 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001028 break;
1029 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001030 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1031 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001032 break;
1033 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001034 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1035 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001036 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001037 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001038 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1039 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001040 break;
1041 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001042 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1043 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001044 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001045 case tok::kw_bool:
1046 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001047 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1048 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001049 break;
1050 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001051 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1052 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001053 break;
1054 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001055 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1056 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001057 break;
1058 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001059 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1060 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001061 break;
1062
1063 // class-specifier:
1064 case tok::kw_class:
1065 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001066 case tok::kw_union: {
1067 tok::TokenKind Kind = Tok.getKind();
1068 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001069 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001070 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001071 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001072
1073 // enum-specifier:
1074 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001075 ConsumeToken();
1076 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001077 continue;
1078
1079 // cv-qualifier:
1080 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001081 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1082 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001083 break;
1084 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001085 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1086 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001087 break;
1088 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001089 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1090 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001091 break;
1092
Douglas Gregord57959a2009-03-27 23:10:48 +00001093 // C++ typename-specifier:
1094 case tok::kw_typename:
1095 if (TryAnnotateTypeOrScopeToken())
1096 continue;
1097 break;
1098
Chris Lattner80d0c892009-01-21 19:48:37 +00001099 // GNU typeof support.
1100 case tok::kw_typeof:
1101 ParseTypeofSpecifier(DS);
1102 continue;
1103
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001104 case tok::kw_decltype:
1105 ParseDecltypeSpecifier(DS);
1106 continue;
1107
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001108 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001109 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001110 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1111 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001112 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001113 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Chris Lattnerbce61352008-07-26 00:20:22 +00001115 {
1116 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001117 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere13b9592008-07-26 04:03:38 +00001118 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +00001119 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattner3bd934a2008-07-26 01:18:38 +00001120 DS.SetRangeEnd(EndProtoLoc);
1121
Chris Lattner1ab3b962008-11-18 07:48:38 +00001122 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001123 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001124 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001125 // Need to support trailing type qualifiers (e.g. "id<p> const").
1126 // If a type specifier follows, it will be diagnosed elsewhere.
1127 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001128 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 }
John McCallfec54012009-08-03 20:12:06 +00001130 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 if (isInvalid) {
1132 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001133 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001134 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001135 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001136 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 ConsumeToken();
1138 }
1139}
Douglas Gregoradcac882008-12-01 23:54:00 +00001140
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001141/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001142/// primarily follow the C++ grammar with additions for C99 and GNU,
1143/// which together subsume the C grammar. Note that the C++
1144/// type-specifier also includes the C type-qualifier (for const,
1145/// volatile, and C99 restrict). Returns true if a type-specifier was
1146/// found (and parsed), false otherwise.
1147///
1148/// type-specifier: [C++ 7.1.5]
1149/// simple-type-specifier
1150/// class-specifier
1151/// enum-specifier
1152/// elaborated-type-specifier [TODO]
1153/// cv-qualifier
1154///
1155/// cv-qualifier: [C++ 7.1.5.1]
1156/// 'const'
1157/// 'volatile'
1158/// [C99] 'restrict'
1159///
1160/// simple-type-specifier: [ C++ 7.1.5.2]
1161/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1162/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1163/// 'char'
1164/// 'wchar_t'
1165/// 'bool'
1166/// 'short'
1167/// 'int'
1168/// 'long'
1169/// 'signed'
1170/// 'unsigned'
1171/// 'float'
1172/// 'double'
1173/// 'void'
1174/// [C99] '_Bool'
1175/// [C99] '_Complex'
1176/// [C99] '_Imaginary' // Removed in TC2?
1177/// [GNU] '_Decimal32'
1178/// [GNU] '_Decimal64'
1179/// [GNU] '_Decimal128'
1180/// [GNU] typeof-specifier
1181/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1182/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001183/// [C++0x] 'decltype' ( expression )
John McCallfec54012009-08-03 20:12:06 +00001184bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001185 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001186 unsigned &DiagID,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001187 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001188 SourceLocation Loc = Tok.getLocation();
1189
1190 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001191 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001192 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001193 // Annotate typenames and C++ scope specifiers. If we get one, just
1194 // recurse to handle whatever we get.
1195 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001196 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1197 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001198 // Otherwise, not a type specifier.
1199 return false;
1200 case tok::coloncolon: // ::foo::bar
1201 if (NextToken().is(tok::kw_new) || // ::new
1202 NextToken().is(tok::kw_delete)) // ::delete
1203 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001204
Chris Lattner166a8fc2009-01-04 23:41:41 +00001205 // Annotate typenames and C++ scope specifiers. If we get one, just
1206 // recurse to handle whatever we get.
1207 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001208 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1209 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001210 // Otherwise, not a type specifier.
1211 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Douglas Gregor12e083c2008-11-07 15:42:26 +00001213 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001214 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001215 if (Tok.getAnnotationValue())
1216 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001217 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001218 else
1219 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001220 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1221 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001222
Douglas Gregor12e083c2008-11-07 15:42:26 +00001223 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1224 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1225 // Objective-C interface. If we don't have Objective-C or a '<', this is
1226 // just a normal reference to a typedef name.
1227 if (!Tok.is(tok::less) || !getLang().ObjC1)
1228 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001229
Douglas Gregor12e083c2008-11-07 15:42:26 +00001230 SourceLocation EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001231 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001232 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek1bc5bbf2009-06-30 22:19:00 +00001233 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Douglas Gregor12e083c2008-11-07 15:42:26 +00001235 DS.SetRangeEnd(EndProtoLoc);
1236 return true;
1237 }
1238
1239 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001240 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001241 break;
1242 case tok::kw_long:
1243 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001244 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1245 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001246 else
John McCallfec54012009-08-03 20:12:06 +00001247 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1248 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001249 break;
1250 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001251 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001252 break;
1253 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001254 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1255 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001256 break;
1257 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001258 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1259 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001260 break;
1261 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001262 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1263 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001264 break;
1265 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001267 break;
1268 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001269 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001270 break;
1271 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001272 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001273 break;
1274 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001276 break;
1277 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001278 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001279 break;
1280 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001281 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001282 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001283 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001285 break;
1286 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001287 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001288 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001289 case tok::kw_bool:
1290 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001291 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001292 break;
1293 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001294 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1295 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001296 break;
1297 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001298 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1299 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001300 break;
1301 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001302 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1303 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001304 break;
1305
1306 // class-specifier:
1307 case tok::kw_class:
1308 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001309 case tok::kw_union: {
1310 tok::TokenKind Kind = Tok.getKind();
1311 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001312 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001313 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001314 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001315
1316 // enum-specifier:
1317 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001318 ConsumeToken();
1319 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001320 return true;
1321
1322 // cv-qualifier:
1323 case tok::kw_const:
1324 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001325 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001326 break;
1327 case tok::kw_volatile:
1328 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001329 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001330 break;
1331 case tok::kw_restrict:
1332 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001333 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001334 break;
1335
1336 // GNU typeof support.
1337 case tok::kw_typeof:
1338 ParseTypeofSpecifier(DS);
1339 return true;
1340
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001341 // C++0x decltype support.
1342 case tok::kw_decltype:
1343 ParseDecltypeSpecifier(DS);
1344 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001346 // C++0x auto support.
1347 case tok::kw_auto:
1348 if (!getLang().CPlusPlus0x)
1349 return false;
1350
John McCallfec54012009-08-03 20:12:06 +00001351 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001352 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001353 case tok::kw___ptr64:
1354 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001355 case tok::kw___cdecl:
1356 case tok::kw___stdcall:
1357 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001358 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001359 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001360
Douglas Gregor12e083c2008-11-07 15:42:26 +00001361 default:
1362 // Not a type-specifier; do nothing.
1363 return false;
1364 }
1365
1366 // If the specifier combination wasn't legal, issue a diagnostic.
1367 if (isInvalid) {
1368 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001369 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001370 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001371 }
1372 DS.SetRangeEnd(Tok.getLocation());
1373 ConsumeToken(); // whatever we parsed above.
1374 return true;
1375}
Reid Spencer5f016e22007-07-11 17:01:13 +00001376
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001377/// ParseStructDeclaration - Parse a struct declaration without the terminating
1378/// semicolon.
1379///
Reid Spencer5f016e22007-07-11 17:01:13 +00001380/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001381/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001382/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001383/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001384/// struct-declarator-list:
1385/// struct-declarator
1386/// struct-declarator-list ',' struct-declarator
1387/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1388/// struct-declarator:
1389/// declarator
1390/// [GNU] declarator attributes[opt]
1391/// declarator[opt] ':' constant-expression
1392/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1393///
Chris Lattnere1359422008-04-10 06:46:29 +00001394void Parser::
1395ParseStructDeclaration(DeclSpec &DS,
1396 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001397 if (Tok.is(tok::kw___extension__)) {
1398 // __extension__ silences extension warnings in the subexpression.
1399 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001400 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001401 return ParseStructDeclaration(DS, Fields);
1402 }
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Steve Naroff28a7ca82007-08-20 22:28:22 +00001404 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001405 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001406 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001408 // If there are no declarators, this is a free-standing declaration
1409 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001410 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001411 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001412 return;
1413 }
1414
1415 // Read struct-declarators until we find the semicolon.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001416 Fields.push_back(FieldDeclarator(DS));
Steve Naroff28a7ca82007-08-20 22:28:22 +00001417 while (1) {
Chris Lattnere1359422008-04-10 06:46:29 +00001418 FieldDeclarator &DeclaratorInfo = Fields.back();
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Steve Naroff28a7ca82007-08-20 22:28:22 +00001420 /// struct-declarator: declarator
1421 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001422 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001423 ParseDeclarator(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Chris Lattner04d66662007-10-09 17:33:22 +00001425 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001426 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001427 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001428 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001429 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001430 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001431 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001432 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001433
Steve Naroff28a7ca82007-08-20 22:28:22 +00001434 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001435 if (Tok.is(tok::kw___attribute)) {
1436 SourceLocation Loc;
1437 AttributeList *AttrList = ParseAttributes(&Loc);
1438 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1439 }
1440
Steve Naroff28a7ca82007-08-20 22:28:22 +00001441 // If we don't have a comma, it is either the end of the list (a ';')
1442 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001443 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001444 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001445
Steve Naroff28a7ca82007-08-20 22:28:22 +00001446 // Consume the comma.
1447 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001448
Steve Naroff28a7ca82007-08-20 22:28:22 +00001449 // Parse the next declarator.
Chris Lattnerebe457c2008-04-10 16:37:40 +00001450 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlab197ba2009-02-09 18:23:29 +00001451
Steve Naroff28a7ca82007-08-20 22:28:22 +00001452 // Attributes are only allowed on the second declarator.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001453 if (Tok.is(tok::kw___attribute)) {
1454 SourceLocation Loc;
1455 AttributeList *AttrList = ParseAttributes(&Loc);
1456 Fields.back().D.AddAttributes(AttrList, Loc);
1457 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001458 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001459}
1460
1461/// ParseStructUnionBody
1462/// struct-contents:
1463/// struct-declaration-list
1464/// [EXT] empty
1465/// [GNU] "struct-declaration-list" without terminatoring ';'
1466/// struct-declaration-list:
1467/// struct-declaration
1468/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001469/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001470///
Reid Spencer5f016e22007-07-11 17:01:13 +00001471void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001472 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001473 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1474 PP.getSourceManager(),
1475 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001476
Reid Spencer5f016e22007-07-11 17:01:13 +00001477 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001479 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001480 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1481
Reid Spencer5f016e22007-07-11 17:01:13 +00001482 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1483 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001484 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001485 Diag(Tok, diag::ext_empty_struct_union_enum)
1486 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001487
Chris Lattnerb28317a2009-03-28 19:18:32 +00001488 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001489 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1490
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001492 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001493 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Reid Spencer5f016e22007-07-11 17:01:13 +00001495 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001496 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001497 Diag(Tok, diag::ext_extra_struct_semi)
1498 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001499 ConsumeToken();
1500 continue;
1501 }
Chris Lattnere1359422008-04-10 06:46:29 +00001502
1503 // Parse all the comma separated declarators.
1504 DeclSpec DS;
1505 FieldDeclarators.clear();
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001506 if (!Tok.is(tok::at)) {
1507 ParseStructDeclaration(DS, FieldDeclarators);
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001509 // Convert them all to fields.
1510 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1511 FieldDeclarator &FD = FieldDeclarators[i];
Douglas Gregor91a28862009-08-26 14:27:30 +00001512 DeclPtrTy Field;
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001513 // Install the declarator into the current TagDecl.
Douglas Gregor91a28862009-08-26 14:27:30 +00001514 if (FD.D.getExtension()) {
1515 // Silences extension warnings
1516 ExtensionRAIIObject O(Diags);
1517 Field = Actions.ActOnField(CurScope, TagDecl,
1518 DS.getSourceRange().getBegin(),
1519 FD.D, FD.BitfieldSize);
1520 } else {
1521 Field = Actions.ActOnField(CurScope, TagDecl,
1522 DS.getSourceRange().getBegin(),
1523 FD.D, FD.BitfieldSize);
1524 }
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001525 FieldDecls.push_back(Field);
1526 }
1527 } else { // Handle @defs
1528 ConsumeToken();
1529 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1530 Diag(Tok, diag::err_unexpected_at);
1531 SkipUntil(tok::semi, true, true);
1532 continue;
1533 }
1534 ConsumeToken();
1535 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1536 if (!Tok.is(tok::identifier)) {
1537 Diag(Tok, diag::err_expected_ident);
1538 SkipUntil(tok::semi, true, true);
1539 continue;
1540 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001541 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001542 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001543 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001544 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1545 ConsumeToken();
1546 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001547 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001548
Chris Lattner04d66662007-10-09 17:33:22 +00001549 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001550 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001551 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001552 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001553 break;
1554 } else {
1555 Diag(Tok, diag::err_expected_semi_decl_list);
1556 // Skip to end of block or statement
1557 SkipUntil(tok::r_brace, true, true);
1558 }
1559 }
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Steve Naroff60fccee2007-10-29 21:38:07 +00001561 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Reid Spencer5f016e22007-07-11 17:01:13 +00001563 AttributeList *AttrList = 0;
1564 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001565 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001566 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001567
1568 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001569 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001570 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001571 AttrList);
1572 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001573 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001574}
1575
1576
1577/// ParseEnumSpecifier
1578/// enum-specifier: [C99 6.7.2.2]
1579/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001580///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001581/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1582/// '}' attributes[opt]
1583/// 'enum' identifier
1584/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001585///
1586/// [C++] elaborated-type-specifier:
1587/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1588///
Chris Lattner4c97d762009-04-12 21:49:30 +00001589void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1590 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001591 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001592 if (Tok.is(tok::code_completion)) {
1593 // Code completion for an enum name.
1594 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1595 ConsumeToken();
1596 }
1597
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001598 AttributeList *Attr = 0;
1599 // If attributes exist after tag, parse them.
1600 if (Tok.is(tok::kw___attribute))
1601 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001602
1603 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001604 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001605 if (Tok.isNot(tok::identifier)) {
1606 Diag(Tok, diag::err_expected_ident);
1607 if (Tok.isNot(tok::l_brace)) {
1608 // Has no name and is not a definition.
1609 // Skip the rest of this declarator, up until the comma or semicolon.
1610 SkipUntil(tok::comma, true);
1611 return;
1612 }
1613 }
1614 }
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001616 // Must have either 'enum name' or 'enum {...}'.
1617 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1618 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001620 // Skip the rest of this declarator, up until the comma or semicolon.
1621 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001623 }
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001625 // If an identifier is present, consume and remember it.
1626 IdentifierInfo *Name = 0;
1627 SourceLocation NameLoc;
1628 if (Tok.is(tok::identifier)) {
1629 Name = Tok.getIdentifierInfo();
1630 NameLoc = ConsumeToken();
1631 }
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001633 // There are three options here. If we have 'enum foo;', then this is a
1634 // forward declaration. If we have 'enum foo {...' then this is a
1635 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1636 //
1637 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1638 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1639 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1640 //
John McCall0f434ec2009-07-31 02:45:11 +00001641 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001642 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001643 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001644 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001645 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001646 else
John McCall0f434ec2009-07-31 02:45:11 +00001647 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001648 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001649 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00001650 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001651 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001652 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001653 Owned, IsDependent);
1654 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Chris Lattner04d66662007-10-09 17:33:22 +00001656 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Reid Spencer5f016e22007-07-11 17:01:13 +00001659 // TODO: semantic analysis on the declspec for enums.
1660 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001661 unsigned DiagID;
1662 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001663 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001664 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001665}
1666
1667/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1668/// enumerator-list:
1669/// enumerator
1670/// enumerator-list ',' enumerator
1671/// enumerator:
1672/// enumeration-constant
1673/// enumeration-constant '=' constant-expression
1674/// enumeration-constant:
1675/// identifier
1676///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001677void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001678 // Enter the scope of the enum body and start the definition.
1679 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001680 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001681
Reid Spencer5f016e22007-07-11 17:01:13 +00001682 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Chris Lattner7946dd32007-08-27 17:24:30 +00001684 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001685 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001686 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Chris Lattnerb28317a2009-03-28 19:18:32 +00001688 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001689
Chris Lattnerb28317a2009-03-28 19:18:32 +00001690 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Reid Spencer5f016e22007-07-11 17:01:13 +00001692 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001693 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001694 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1695 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Reid Spencer5f016e22007-07-11 17:01:13 +00001697 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001698 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001699 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001700 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001701 AssignedVal = ParseConstantExpression();
1702 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001703 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 }
Mike Stump1eb44332009-09-09 15:08:12 +00001705
Reid Spencer5f016e22007-07-11 17:01:13 +00001706 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001707 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1708 LastEnumConstDecl,
1709 IdentLoc, Ident,
1710 EqualLoc,
1711 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001712 EnumConstantDecls.push_back(EnumConstDecl);
1713 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001714
Chris Lattner04d66662007-10-09 17:33:22 +00001715 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001716 break;
1717 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001718
1719 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001720 !(getLang().C99 || getLang().CPlusPlus0x))
1721 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1722 << getLang().CPlusPlus
1723 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001724 }
Mike Stump1eb44332009-09-09 15:08:12 +00001725
Reid Spencer5f016e22007-07-11 17:01:13 +00001726 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001727 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001728
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001729 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001730 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001731 if (Tok.is(tok::kw___attribute))
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001732 Attr = ParseAttributes();
Douglas Gregor72de6672009-01-08 20:45:30 +00001733
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001734 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1735 EnumConstantDecls.data(), EnumConstantDecls.size(),
1736 CurScope, Attr);
Mike Stump1eb44332009-09-09 15:08:12 +00001737
Douglas Gregor72de6672009-01-08 20:45:30 +00001738 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001739 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001740}
1741
1742/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001743/// start of a type-qualifier-list.
1744bool Parser::isTypeQualifier() const {
1745 switch (Tok.getKind()) {
1746 default: return false;
1747 // type-qualifier
1748 case tok::kw_const:
1749 case tok::kw_volatile:
1750 case tok::kw_restrict:
1751 return true;
1752 }
1753}
1754
1755/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001756/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001757bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 switch (Tok.getKind()) {
1759 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Chris Lattner166a8fc2009-01-04 23:41:41 +00001761 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001762 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001763 // Annotate typenames and C++ scope specifiers. If we get one, just
1764 // recurse to handle whatever we get.
1765 if (TryAnnotateTypeOrScopeToken())
1766 return isTypeSpecifierQualifier();
1767 // Otherwise, not a type specifier.
1768 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001769
Chris Lattner166a8fc2009-01-04 23:41:41 +00001770 case tok::coloncolon: // ::foo::bar
1771 if (NextToken().is(tok::kw_new) || // ::new
1772 NextToken().is(tok::kw_delete)) // ::delete
1773 return false;
1774
1775 // Annotate typenames and C++ scope specifiers. If we get one, just
1776 // recurse to handle whatever we get.
1777 if (TryAnnotateTypeOrScopeToken())
1778 return isTypeSpecifierQualifier();
1779 // Otherwise, not a type specifier.
1780 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Reid Spencer5f016e22007-07-11 17:01:13 +00001782 // GNU attributes support.
1783 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001784 // GNU typeof support.
1785 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Reid Spencer5f016e22007-07-11 17:01:13 +00001787 // type-specifiers
1788 case tok::kw_short:
1789 case tok::kw_long:
1790 case tok::kw_signed:
1791 case tok::kw_unsigned:
1792 case tok::kw__Complex:
1793 case tok::kw__Imaginary:
1794 case tok::kw_void:
1795 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001796 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001797 case tok::kw_char16_t:
1798 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 case tok::kw_int:
1800 case tok::kw_float:
1801 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001802 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 case tok::kw__Bool:
1804 case tok::kw__Decimal32:
1805 case tok::kw__Decimal64:
1806 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Chris Lattner99dc9142008-04-13 18:59:07 +00001808 // struct-or-union-specifier (C99) or class-specifier (C++)
1809 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001810 case tok::kw_struct:
1811 case tok::kw_union:
1812 // enum-specifier
1813 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 // type-qualifier
1816 case tok::kw_const:
1817 case tok::kw_volatile:
1818 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001819
1820 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001821 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001822 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001823
Chris Lattner7c186be2008-10-20 00:25:30 +00001824 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1825 case tok::less:
1826 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Steve Naroff239f0732008-12-25 14:16:32 +00001828 case tok::kw___cdecl:
1829 case tok::kw___stdcall:
1830 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001831 case tok::kw___w64:
1832 case tok::kw___ptr64:
1833 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001834 }
1835}
1836
1837/// isDeclarationSpecifier() - Return true if the current token is part of a
1838/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001839bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001840 switch (Tok.getKind()) {
1841 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Chris Lattner166a8fc2009-01-04 23:41:41 +00001843 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001844 // Unfortunate hack to support "Class.factoryMethod" notation.
1845 if (getLang().ObjC1 && NextToken().is(tok::period))
1846 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001847 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001848
Douglas Gregord57959a2009-03-27 23:10:48 +00001849 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001850 // Annotate typenames and C++ scope specifiers. If we get one, just
1851 // recurse to handle whatever we get.
1852 if (TryAnnotateTypeOrScopeToken())
1853 return isDeclarationSpecifier();
1854 // Otherwise, not a declaration specifier.
1855 return false;
1856 case tok::coloncolon: // ::foo::bar
1857 if (NextToken().is(tok::kw_new) || // ::new
1858 NextToken().is(tok::kw_delete)) // ::delete
1859 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Chris Lattner166a8fc2009-01-04 23:41:41 +00001861 // Annotate typenames and C++ scope specifiers. If we get one, just
1862 // recurse to handle whatever we get.
1863 if (TryAnnotateTypeOrScopeToken())
1864 return isDeclarationSpecifier();
1865 // Otherwise, not a declaration specifier.
1866 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Reid Spencer5f016e22007-07-11 17:01:13 +00001868 // storage-class-specifier
1869 case tok::kw_typedef:
1870 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001871 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001872 case tok::kw_static:
1873 case tok::kw_auto:
1874 case tok::kw_register:
1875 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Reid Spencer5f016e22007-07-11 17:01:13 +00001877 // type-specifiers
1878 case tok::kw_short:
1879 case tok::kw_long:
1880 case tok::kw_signed:
1881 case tok::kw_unsigned:
1882 case tok::kw__Complex:
1883 case tok::kw__Imaginary:
1884 case tok::kw_void:
1885 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001886 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001887 case tok::kw_char16_t:
1888 case tok::kw_char32_t:
1889
Reid Spencer5f016e22007-07-11 17:01:13 +00001890 case tok::kw_int:
1891 case tok::kw_float:
1892 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001893 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001894 case tok::kw__Bool:
1895 case tok::kw__Decimal32:
1896 case tok::kw__Decimal64:
1897 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00001898
Chris Lattner99dc9142008-04-13 18:59:07 +00001899 // struct-or-union-specifier (C99) or class-specifier (C++)
1900 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001901 case tok::kw_struct:
1902 case tok::kw_union:
1903 // enum-specifier
1904 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00001905
Reid Spencer5f016e22007-07-11 17:01:13 +00001906 // type-qualifier
1907 case tok::kw_const:
1908 case tok::kw_volatile:
1909 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001910
Reid Spencer5f016e22007-07-11 17:01:13 +00001911 // function-specifier
1912 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00001913 case tok::kw_virtual:
1914 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001915
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001916 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001917 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001918
Chris Lattner1ef08762007-08-09 17:01:07 +00001919 // GNU typeof support.
1920 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00001921
Chris Lattner1ef08762007-08-09 17:01:07 +00001922 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00001923 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00001924 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001925
Chris Lattnerf3948c42008-07-26 03:38:44 +00001926 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1927 case tok::less:
1928 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Steve Naroff47f52092009-01-06 19:34:12 +00001930 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00001931 case tok::kw___cdecl:
1932 case tok::kw___stdcall:
1933 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001934 case tok::kw___w64:
1935 case tok::kw___ptr64:
1936 case tok::kw___forceinline:
1937 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001938 }
1939}
1940
1941
1942/// ParseTypeQualifierListOpt
1943/// type-qualifier-list: [C99 6.7.5]
1944/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001945/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001946/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001947/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00001948///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001949void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001950 while (1) {
John McCallfec54012009-08-03 20:12:06 +00001951 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001953 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001954 SourceLocation Loc = Tok.getLocation();
1955
1956 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001957 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001958 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
1959 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001960 break;
1961 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001962 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1963 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001964 break;
1965 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001966 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1967 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00001968 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001969 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001970 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00001971 case tok::kw___cdecl:
1972 case tok::kw___stdcall:
1973 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001974 if (AttributesAllowed) {
1975 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1976 continue;
1977 }
1978 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00001979 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001980 if (AttributesAllowed) {
1981 DS.AddAttributes(ParseAttributes());
1982 continue; // do *not* consume the next token!
1983 }
1984 // otherwise, FALL THROUGH!
1985 default:
Steve Naroff239f0732008-12-25 14:16:32 +00001986 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001987 // If this is not a type-qualifier token, we're done reading type
1988 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001989 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00001990 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001991 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00001992
Reid Spencer5f016e22007-07-11 17:01:13 +00001993 // If the specifier combination wasn't legal, issue a diagnostic.
1994 if (isInvalid) {
1995 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001996 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001997 }
1998 ConsumeToken();
1999 }
2000}
2001
2002
2003/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2004///
2005void Parser::ParseDeclarator(Declarator &D) {
2006 /// This implements the 'declarator' production in the C grammar, then checks
2007 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002008 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002009}
2010
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002011/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2012/// is parsed by the function passed to it. Pass null, and the direct-declarator
2013/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002014/// ptr-operator production.
2015///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002016/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2017/// [C] pointer[opt] direct-declarator
2018/// [C++] direct-declarator
2019/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002020///
2021/// pointer: [C99 6.7.5]
2022/// '*' type-qualifier-list[opt]
2023/// '*' type-qualifier-list[opt] pointer
2024///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002025/// ptr-operator:
2026/// '*' cv-qualifier-seq[opt]
2027/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002028/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002029/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002030/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002031/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002032void Parser::ParseDeclaratorInternal(Declarator &D,
2033 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002034
Douglas Gregor91a28862009-08-26 14:27:30 +00002035 if (Diags.hasAllExtensionsSilenced())
2036 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002037 // C++ member pointers start with a '::' or a nested-name.
2038 // Member pointers get special handling, since there's no place for the
2039 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002040 if (getLang().CPlusPlus &&
2041 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2042 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002043 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002044 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002045 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002046 // The scope spec really belongs to the direct-declarator.
2047 D.getCXXScopeSpec() = SS;
2048 if (DirectDeclParser)
2049 (this->*DirectDeclParser)(D);
2050 return;
2051 }
2052
2053 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002054 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002055 DeclSpec DS;
2056 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002057 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002058
2059 // Recurse to parse whatever is left.
2060 ParseDeclaratorInternal(D, DirectDeclParser);
2061
2062 // Sema will have to catch (syntactically invalid) pointers into global
2063 // scope. It has to catch pointers into namespace scope anyway.
2064 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002065 Loc, DS.TakeAttributes()),
2066 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002067 return;
2068 }
2069 }
2070
2071 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002072 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002073 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002074 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002075 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002076 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002077 if (DirectDeclParser)
2078 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002079 return;
2080 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002081
Sebastian Redl05532f22009-03-15 22:02:01 +00002082 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2083 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002084 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002085 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002086
Chris Lattner9af55002009-03-27 04:18:06 +00002087 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002088 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002089 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002090
Reid Spencer5f016e22007-07-11 17:01:13 +00002091 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002092 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002093
Reid Spencer5f016e22007-07-11 17:01:13 +00002094 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002095 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002096 if (Kind == tok::star)
2097 // Remember that we parsed a pointer type, and remember the type-quals.
2098 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002099 DS.TakeAttributes()),
2100 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002101 else
2102 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002103 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002104 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002105 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002106 } else {
2107 // Is a reference
2108 DeclSpec DS;
2109
Sebastian Redl743de1f2009-03-23 00:00:23 +00002110 // Complain about rvalue references in C++03, but then go on and build
2111 // the declarator.
2112 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2113 Diag(Loc, diag::err_rvalue_reference);
2114
Reid Spencer5f016e22007-07-11 17:01:13 +00002115 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2116 // cv-qualifiers are introduced through the use of a typedef or of a
2117 // template type argument, in which case the cv-qualifiers are ignored.
2118 //
2119 // [GNU] Retricted references are allowed.
2120 // [GNU] Attributes on references are allowed.
2121 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002122 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002123
2124 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2125 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2126 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002127 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002128 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2129 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002130 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002131 }
2132
2133 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002134 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002135
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002136 if (D.getNumTypeObjects() > 0) {
2137 // C++ [dcl.ref]p4: There shall be no references to references.
2138 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2139 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002140 if (const IdentifierInfo *II = D.getIdentifier())
2141 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2142 << II;
2143 else
2144 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2145 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002146
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002147 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002148 // can go ahead and build the (technically ill-formed)
2149 // declarator: reference collapsing will take care of it.
2150 }
2151 }
2152
Reid Spencer5f016e22007-07-11 17:01:13 +00002153 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002154 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002155 DS.TakeAttributes(),
2156 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002157 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002158 }
2159}
2160
2161/// ParseDirectDeclarator
2162/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002163/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002164/// '(' declarator ')'
2165/// [GNU] '(' attributes declarator ')'
2166/// [C90] direct-declarator '[' constant-expression[opt] ']'
2167/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2168/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2169/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2170/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2171/// direct-declarator '(' parameter-type-list ')'
2172/// direct-declarator '(' identifier-list[opt] ')'
2173/// [GNU] direct-declarator '(' parameter-forward-declarations
2174/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002175/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2176/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002177/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002178///
2179/// declarator-id: [C++ 8]
2180/// id-expression
2181/// '::'[opt] nested-name-specifier[opt] type-name
2182///
2183/// id-expression: [C++ 5.1]
2184/// unqualified-id
2185/// qualified-id [TODO]
2186///
2187/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002188/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002189/// operator-function-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002190/// conversion-function-id [TODO]
Mike Stump1eb44332009-09-09 15:08:12 +00002191/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002192/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002193///
Reid Spencer5f016e22007-07-11 17:01:13 +00002194void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002195 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002196
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002197 if (getLang().CPlusPlus) {
2198 if (D.mayHaveIdentifier()) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002199 // ParseDeclaratorInternal might already have parsed the scope.
2200 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
Mike Stump1eb44332009-09-09 15:08:12 +00002201 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002202 true);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002203 if (afterCXXScope) {
2204 // Change the declaration context for name lookup, until this function
2205 // is exited (and the declarator has been parsed).
2206 DeclScopeObj.EnterDeclaratorScope();
2207 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002208
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002209 if (Tok.is(tok::identifier)) {
2210 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlsson4649cac2009-04-30 22:41:11 +00002211
2212 // If this identifier is the name of the current class, it's a
Mike Stump1eb44332009-09-09 15:08:12 +00002213 // constructor name.
Anders Carlsson4649cac2009-04-30 22:41:11 +00002214 if (!D.getDeclSpec().hasTypeSpecifier() &&
2215 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
Douglas Gregor675431d2009-07-06 16:40:48 +00002216 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Anders Carlsson4649cac2009-04-30 22:41:11 +00002217 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor675431d2009-07-06 16:40:48 +00002218 Tok.getLocation(), CurScope, SS),
Anders Carlsson4649cac2009-04-30 22:41:11 +00002219 Tok.getLocation());
2220 // This is a normal identifier.
2221 } else
2222 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002223 ConsumeToken();
2224 goto PastIdentifier;
Douglas Gregor39a8de12009-02-25 19:37:18 +00002225 } else if (Tok.is(tok::annot_template_id)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002226 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00002227 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2228
2229 // FIXME: Could this template-id name a constructor?
2230
2231 // FIXME: This is an egregious hack, where we silently ignore
2232 // the specialization (which should be a function template
2233 // specialization name) and use the name instead. This hack
2234 // will go away when we have support for function
2235 // specializations.
2236 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2237 TemplateId->Destroy();
2238 ConsumeToken();
2239 goto PastIdentifier;
Douglas Gregor70316a02008-12-26 15:00:45 +00002240 } else if (Tok.is(tok::kw_operator)) {
2241 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002242 SourceLocation EndLoc;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002243
Douglas Gregor70316a02008-12-26 15:00:45 +00002244 // First try the name of an overloaded operator
Sebastian Redlab197ba2009-02-09 18:23:29 +00002245 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2246 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor70316a02008-12-26 15:00:45 +00002247 } else {
2248 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlab197ba2009-02-09 18:23:29 +00002249 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2250 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2251 else {
Douglas Gregor70316a02008-12-26 15:00:45 +00002252 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlab197ba2009-02-09 18:23:29 +00002253 }
Douglas Gregor70316a02008-12-26 15:00:45 +00002254 }
2255 goto PastIdentifier;
2256 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002257 // This should be a C++ destructor.
2258 SourceLocation TildeLoc = ConsumeToken();
Douglas Gregor42c39f32009-08-26 18:27:52 +00002259 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002260 // FIXME: Inaccurate.
2261 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregor7f43d672009-02-25 23:52:28 +00002262 SourceLocation EndLoc;
Douglas Gregor675431d2009-07-06 16:40:48 +00002263 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Fariborz Jahaniand33c8682009-07-20 17:43:15 +00002264 TypeResult Type = ParseClassName(EndLoc, SS, true);
Douglas Gregor31a19b62009-04-01 21:51:26 +00002265 if (Type.isInvalid())
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002266 D.SetIdentifier(0, TildeLoc);
Douglas Gregor31a19b62009-04-01 21:51:26 +00002267 else
2268 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002269 } else {
Fariborz Jahaniand33c8682009-07-20 17:43:15 +00002270 Diag(Tok, diag::err_destructor_class_name);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002271 D.SetIdentifier(0, TildeLoc);
2272 }
2273 goto PastIdentifier;
2274 }
2275
2276 // If we reached this point, token is not identifier and not '~'.
2277
2278 if (afterCXXScope) {
2279 Diag(Tok, diag::err_expected_unqualified_id);
2280 D.SetIdentifier(0, Tok.getLocation());
2281 D.setInvalidType(true);
2282 goto PastIdentifier;
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002283 }
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002284 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002285 }
2286
2287 // If we reached this point, we are either in C/ObjC or the token didn't
2288 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002289 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2290 assert(!getLang().CPlusPlus &&
2291 "There's a C++-specific check for tok::identifier above");
2292 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2293 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2294 ConsumeToken();
2295 } else if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002296 // direct-declarator: '(' declarator ')'
2297 // direct-declarator: '(' attributes declarator ')'
2298 // Example: 'char (*X)' or 'int (*XX)(void)'
2299 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002300 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002301 // This could be something simple like "int" (in which case the declarator
2302 // portion is empty), if an abstract-declarator is allowed.
2303 D.SetIdentifier(0, Tok.getLocation());
2304 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002305 if (D.getContext() == Declarator::MemberContext)
2306 Diag(Tok, diag::err_expected_member_name_or_semi)
2307 << D.getDeclSpec().getSourceRange();
2308 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002309 Diag(Tok, diag::err_expected_unqualified_id);
2310 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002311 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002312 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002313 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002314 }
Mike Stump1eb44332009-09-09 15:08:12 +00002315
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002316 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002317 assert(D.isPastIdentifier() &&
2318 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002319
Reid Spencer5f016e22007-07-11 17:01:13 +00002320 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002321 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002322 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2323 // In such a case, check if we actually have a function declarator; if it
2324 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002325 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2326 // When not in file scope, warn for ambiguous function declarators, just
2327 // in case the author intended it as a variable definition.
2328 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2329 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2330 break;
2331 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002332 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002333 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002334 ParseBracketDeclarator(D);
2335 } else {
2336 break;
2337 }
2338 }
2339}
2340
Chris Lattneref4715c2008-04-06 05:45:57 +00002341/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2342/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002343/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002344/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2345///
2346/// direct-declarator:
2347/// '(' declarator ')'
2348/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002349/// direct-declarator '(' parameter-type-list ')'
2350/// direct-declarator '(' identifier-list[opt] ')'
2351/// [GNU] direct-declarator '(' parameter-forward-declarations
2352/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002353///
2354void Parser::ParseParenDeclarator(Declarator &D) {
2355 SourceLocation StartLoc = ConsumeParen();
2356 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002357
Chris Lattner7399ee02008-10-20 02:05:46 +00002358 // Eat any attributes before we look at whether this is a grouping or function
2359 // declarator paren. If this is a grouping paren, the attribute applies to
2360 // the type being built up, for example:
2361 // int (__attribute__(()) *x)(long y)
2362 // If this ends up not being a grouping paren, the attribute applies to the
2363 // first argument, for example:
2364 // int (__attribute__(()) int x)
2365 // In either case, we need to eat any attributes to be able to determine what
2366 // sort of paren this is.
2367 //
2368 AttributeList *AttrList = 0;
2369 bool RequiresArg = false;
2370 if (Tok.is(tok::kw___attribute)) {
2371 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00002372
Chris Lattner7399ee02008-10-20 02:05:46 +00002373 // We require that the argument list (if this is a non-grouping paren) be
2374 // present even if the attribute list was empty.
2375 RequiresArg = true;
2376 }
Steve Naroff239f0732008-12-25 14:16:32 +00002377 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002378 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2379 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2380 Tok.is(tok::kw___ptr64)) {
2381 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2382 }
Mike Stump1eb44332009-09-09 15:08:12 +00002383
Chris Lattneref4715c2008-04-06 05:45:57 +00002384 // If we haven't past the identifier yet (or where the identifier would be
2385 // stored, if this is an abstract declarator), then this is probably just
2386 // grouping parens. However, if this could be an abstract-declarator, then
2387 // this could also be the start of function arguments (consider 'void()').
2388 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002389
Chris Lattneref4715c2008-04-06 05:45:57 +00002390 if (!D.mayOmitIdentifier()) {
2391 // If this can't be an abstract-declarator, this *must* be a grouping
2392 // paren, because we haven't seen the identifier yet.
2393 isGrouping = true;
2394 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002395 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002396 isDeclarationSpecifier()) { // 'int(int)' is a function.
2397 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2398 // considered to be a type, not a K&R identifier-list.
2399 isGrouping = false;
2400 } else {
2401 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2402 isGrouping = true;
2403 }
Mike Stump1eb44332009-09-09 15:08:12 +00002404
Chris Lattneref4715c2008-04-06 05:45:57 +00002405 // If this is a grouping paren, handle:
2406 // direct-declarator: '(' declarator ')'
2407 // direct-declarator: '(' attributes declarator ')'
2408 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002409 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002410 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002411 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002412 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002413
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002414 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002415 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002416 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002417
2418 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002419 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002420 return;
2421 }
Mike Stump1eb44332009-09-09 15:08:12 +00002422
Chris Lattneref4715c2008-04-06 05:45:57 +00002423 // Okay, if this wasn't a grouping paren, it must be the start of a function
2424 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002425 // identifier (and remember where it would have been), then call into
2426 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002427 D.SetIdentifier(0, Tok.getLocation());
2428
Chris Lattner7399ee02008-10-20 02:05:46 +00002429 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002430}
2431
2432/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2433/// declarator D up to a paren, which indicates that we are parsing function
2434/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002435///
Chris Lattner7399ee02008-10-20 02:05:46 +00002436/// If AttrList is non-null, then the caller parsed those arguments immediately
2437/// after the open paren - they should be considered to be the first argument of
2438/// a parameter. If RequiresArg is true, then the first argument of the
2439/// function is required to be present and required to not be an identifier
2440/// list.
2441///
Reid Spencer5f016e22007-07-11 17:01:13 +00002442/// This method also handles this portion of the grammar:
2443/// parameter-type-list: [C99 6.7.5]
2444/// parameter-list
2445/// parameter-list ',' '...'
2446///
2447/// parameter-list: [C99 6.7.5]
2448/// parameter-declaration
2449/// parameter-list ',' parameter-declaration
2450///
2451/// parameter-declaration: [C99 6.7.5]
2452/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002453/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002454/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002455/// declaration-specifiers abstract-declarator[opt]
2456/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002457/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002458/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2459///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002460/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002461/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002462///
Chris Lattner7399ee02008-10-20 02:05:46 +00002463void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2464 AttributeList *AttrList,
2465 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002466 // lparen is already consumed!
2467 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002468
Chris Lattner7399ee02008-10-20 02:05:46 +00002469 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002470 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002471 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002472 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002473 delete AttrList;
2474 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002475
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002476 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2477 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002478
2479 // cv-qualifier-seq[opt].
2480 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002481 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002482 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002483 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002484 llvm::SmallVector<TypeTy*, 2> Exceptions;
2485 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002486 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002487 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002488 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002489 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002490
2491 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002492 if (Tok.is(tok::kw_throw)) {
2493 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002494 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002495 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002496 hasAnyExceptionSpec);
2497 assert(Exceptions.size() == ExceptionRanges.size() &&
2498 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002499 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002500 }
2501
Chris Lattnerf97409f2008-04-06 06:57:35 +00002502 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002503 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002504 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002505 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002506 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002507 /*arglist*/ 0, 0,
2508 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002509 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002510 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002511 Exceptions.data(),
2512 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002513 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002514 LParenLoc, RParenLoc, D),
2515 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002516 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002517 }
2518
Chris Lattner7399ee02008-10-20 02:05:46 +00002519 // Alternatively, this parameter list may be an identifier list form for a
2520 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002521 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002522 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002523 // K&R identifier lists can't have typedefs as identifiers, per
2524 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002525 if (RequiresArg) {
2526 Diag(Tok, diag::err_argument_required_after_attribute);
2527 delete AttrList;
2528 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002529 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2530 // normal declarators, not for abstract-declarators.
2531 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002532 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002533 }
Mike Stump1eb44332009-09-09 15:08:12 +00002534
Chris Lattnerf97409f2008-04-06 06:57:35 +00002535 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002536
Chris Lattnerf97409f2008-04-06 06:57:35 +00002537 // Build up an array of information about the parsed arguments.
2538 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002539
2540 // Enter function-declaration scope, limiting any declarators to the
2541 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002542 ParseScope PrototypeScope(this,
2543 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002544
Chris Lattnerf97409f2008-04-06 06:57:35 +00002545 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002546 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002547 while (1) {
2548 if (Tok.is(tok::ellipsis)) {
2549 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002550 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002551 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002552 }
Mike Stump1eb44332009-09-09 15:08:12 +00002553
Chris Lattnerf97409f2008-04-06 06:57:35 +00002554 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002555
Chris Lattnerf97409f2008-04-06 06:57:35 +00002556 // Parse the declaration-specifiers.
2557 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002558
2559 // If the caller parsed attributes for the first argument, add them now.
2560 if (AttrList) {
2561 DS.AddAttributes(AttrList);
2562 AttrList = 0; // Only apply the attributes to the first parameter.
2563 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002564 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002565
Chris Lattnerf97409f2008-04-06 06:57:35 +00002566 // Parse the declarator. This is "PrototypeContext", because we must
2567 // accept either 'declarator' or 'abstract-declarator' here.
2568 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2569 ParseDeclarator(ParmDecl);
2570
2571 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002572 if (Tok.is(tok::kw___attribute)) {
2573 SourceLocation Loc;
2574 AttributeList *AttrList = ParseAttributes(&Loc);
2575 ParmDecl.AddAttributes(AttrList, Loc);
2576 }
Mike Stump1eb44332009-09-09 15:08:12 +00002577
Chris Lattnerf97409f2008-04-06 06:57:35 +00002578 // Remember this parsed parameter in ParamInfo.
2579 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002580
Douglas Gregor72b505b2008-12-16 21:30:33 +00002581 // DefArgToks is used when the parsing of default arguments needs
2582 // to be delayed.
2583 CachedTokens *DefArgToks = 0;
2584
Chris Lattnerf97409f2008-04-06 06:57:35 +00002585 // If no parameter was specified, verify that *something* was specified,
2586 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002587 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2588 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002589 // Completely missing, emit error.
2590 Diag(DSStart, diag::err_missing_param);
2591 } else {
2592 // Otherwise, we have something. Add it and let semantic analysis try
2593 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00002594
Chris Lattnerf97409f2008-04-06 06:57:35 +00002595 // Inform the actions module about the parameter declarator, so it gets
2596 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002597 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002598
2599 // Parse the default argument, if any. We parse the default
2600 // arguments in all dialects; the semantic analysis in
2601 // ActOnParamDefaultArgument will reject the default argument in
2602 // C.
2603 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002604 SourceLocation EqualLoc = Tok.getLocation();
2605
Chris Lattner04421082008-04-08 04:40:51 +00002606 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002607 if (D.getContext() == Declarator::MemberContext) {
2608 // If we're inside a class definition, cache the tokens
2609 // corresponding to the default argument. We'll actually parse
2610 // them when we see the end of the class definition.
2611 // FIXME: Templates will require something similar.
2612 // FIXME: Can we use a smart pointer for Toks?
2613 DefArgToks = new CachedTokens;
2614
Mike Stump1eb44332009-09-09 15:08:12 +00002615 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002616 tok::semi, false)) {
2617 delete DefArgToks;
2618 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002619 Actions.ActOnParamDefaultArgumentError(Param);
2620 } else
Mike Stump1eb44332009-09-09 15:08:12 +00002621 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00002622 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002623 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002624 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002625 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002626
Douglas Gregor72b505b2008-12-16 21:30:33 +00002627 OwningExprResult DefArgResult(ParseAssignmentExpression());
2628 if (DefArgResult.isInvalid()) {
2629 Actions.ActOnParamDefaultArgumentError(Param);
2630 SkipUntil(tok::comma, tok::r_paren, true, true);
2631 } else {
2632 // Inform the actions module about the default argument
2633 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002634 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002635 }
Chris Lattner04421082008-04-08 04:40:51 +00002636 }
2637 }
Mike Stump1eb44332009-09-09 15:08:12 +00002638
2639 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2640 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002641 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002642 }
2643
2644 // If the next token is a comma, consume it and keep reading arguments.
2645 if (Tok.isNot(tok::comma)) break;
Mike Stump1eb44332009-09-09 15:08:12 +00002646
Chris Lattnerf97409f2008-04-06 06:57:35 +00002647 // Consume the comma.
2648 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002649 }
Mike Stump1eb44332009-09-09 15:08:12 +00002650
Chris Lattnerf97409f2008-04-06 06:57:35 +00002651 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002652 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00002653
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002654 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002655 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2656 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002657
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002658 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002659 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002660 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002661 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002662 llvm::SmallVector<TypeTy*, 2> Exceptions;
2663 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002664 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002665 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002666 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002667 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002668 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002669
2670 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002671 if (Tok.is(tok::kw_throw)) {
2672 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002673 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002674 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002675 hasAnyExceptionSpec);
2676 assert(Exceptions.size() == ExceptionRanges.size() &&
2677 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002678 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002679 }
2680
Reid Spencer5f016e22007-07-11 17:01:13 +00002681 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002682 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002683 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002684 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002685 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002686 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002687 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002688 Exceptions.data(),
2689 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002690 Exceptions.size(),
2691 LParenLoc, RParenLoc, D),
2692 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002693}
2694
Chris Lattner66d28652008-04-06 06:34:08 +00002695/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2696/// we found a K&R-style identifier list instead of a type argument list. The
2697/// current token is known to be the first identifier in the list.
2698///
2699/// identifier-list: [C99 6.7.5]
2700/// identifier
2701/// identifier-list ',' identifier
2702///
2703void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2704 Declarator &D) {
2705 // Build up an array of information about the parsed arguments.
2706 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2707 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00002708
Chris Lattner66d28652008-04-06 06:34:08 +00002709 // If there was no identifier specified for the declarator, either we are in
2710 // an abstract-declarator, or we are in a parameter declarator which was found
2711 // to be abstract. In abstract-declarators, identifier lists are not valid:
2712 // diagnose this.
2713 if (!D.getIdentifier())
2714 Diag(Tok, diag::ext_ident_list_in_param);
2715
2716 // Tok is known to be the first identifier in the list. Remember this
2717 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002718 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002719 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002720 Tok.getLocation(),
2721 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00002722
Chris Lattner50c64772008-04-06 06:39:19 +00002723 ConsumeToken(); // eat the first identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00002724
Chris Lattner66d28652008-04-06 06:34:08 +00002725 while (Tok.is(tok::comma)) {
2726 // Eat the comma.
2727 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002728
Chris Lattner50c64772008-04-06 06:39:19 +00002729 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002730 if (Tok.isNot(tok::identifier)) {
2731 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002732 SkipUntil(tok::r_paren);
2733 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002734 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002735
Chris Lattner66d28652008-04-06 06:34:08 +00002736 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002737
2738 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002739 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002740 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00002741
Chris Lattner66d28652008-04-06 06:34:08 +00002742 // Verify that the argument identifier has not already been mentioned.
2743 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002744 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002745 } else {
2746 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002747 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002748 Tok.getLocation(),
2749 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002750 }
Mike Stump1eb44332009-09-09 15:08:12 +00002751
Chris Lattner66d28652008-04-06 06:34:08 +00002752 // Eat the identifier.
2753 ConsumeToken();
2754 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002755
2756 // If we have the closing ')', eat it and we're done.
2757 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2758
Chris Lattner50c64772008-04-06 06:39:19 +00002759 // Remember that we parsed a function type, and remember the attributes. This
2760 // function type is always a K&R style function type, which is not varargs and
2761 // has no prototype.
2762 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002763 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002764 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002765 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00002766 /*exception*/false,
2767 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002768 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002769 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002770}
Chris Lattneref4715c2008-04-06 05:45:57 +00002771
Reid Spencer5f016e22007-07-11 17:01:13 +00002772/// [C90] direct-declarator '[' constant-expression[opt] ']'
2773/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2774/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2775/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2776/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2777void Parser::ParseBracketDeclarator(Declarator &D) {
2778 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00002779
Chris Lattner378c7e42008-12-18 07:27:21 +00002780 // C array syntax has many features, but by-far the most common is [] and [4].
2781 // This code does a fast path to handle some of the most obvious cases.
2782 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002783 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002784 // Remember that we parsed the empty array type.
2785 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002786 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2787 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002788 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002789 return;
2790 } else if (Tok.getKind() == tok::numeric_constant &&
2791 GetLookAheadToken(1).is(tok::r_square)) {
2792 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002793 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002794 ConsumeToken();
2795
Sebastian Redlab197ba2009-02-09 18:23:29 +00002796 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002797
2798 // If there was an error parsing the assignment-expression, recover.
2799 if (ExprRes.isInvalid())
2800 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00002801
Chris Lattner378c7e42008-12-18 07:27:21 +00002802 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002803 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2804 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002805 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002806 return;
2807 }
Mike Stump1eb44332009-09-09 15:08:12 +00002808
Reid Spencer5f016e22007-07-11 17:01:13 +00002809 // If valid, this location is the position where we read the 'static' keyword.
2810 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002811 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002812 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002813
Reid Spencer5f016e22007-07-11 17:01:13 +00002814 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002815 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002816 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002817 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00002818
Reid Spencer5f016e22007-07-11 17:01:13 +00002819 // If we haven't already read 'static', check to see if there is one after the
2820 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002821 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002822 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002823
Reid Spencer5f016e22007-07-11 17:01:13 +00002824 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2825 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002826 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00002827
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002828 // Handle the case where we have '[*]' as the array size. However, a leading
2829 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2830 // the the token after the star is a ']'. Since stars in arrays are
2831 // infrequent, use of lookahead is not costly here.
2832 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002833 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002834
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002835 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002836 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002837 StaticLoc = SourceLocation(); // Drop the static.
2838 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002839 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002840 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002841 // Note, in C89, this production uses the constant-expr production instead
2842 // of assignment-expr. The only difference is that assignment-expr allows
2843 // things like '=' and '*='. Sema rejects these in C89 mode because they
2844 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00002845
Douglas Gregore0762c92009-06-19 23:52:42 +00002846 // Parse the constant-expression or assignment-expression now (depending
2847 // on dialect).
2848 if (getLang().CPlusPlus)
2849 NumElements = ParseConstantExpression();
2850 else
2851 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00002852 }
Mike Stump1eb44332009-09-09 15:08:12 +00002853
Reid Spencer5f016e22007-07-11 17:01:13 +00002854 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002855 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002856 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002857 // If the expression was invalid, skip it.
2858 SkipUntil(tok::r_square);
2859 return;
2860 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002861
2862 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2863
Chris Lattner378c7e42008-12-18 07:27:21 +00002864 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002865 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2866 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002867 NumElements.release(),
2868 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002869 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002870}
2871
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002872/// [GNU] typeof-specifier:
2873/// typeof ( expressions )
2874/// typeof ( type-name )
2875/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002876///
2877void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002878 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002879 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002880 SourceLocation StartLoc = ConsumeToken();
2881
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002882 bool isCastExpr;
2883 TypeTy *CastTy;
2884 SourceRange CastRange;
2885 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2886 isCastExpr,
2887 CastTy,
2888 CastRange);
2889
2890 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002891 // FIXME: Not accurate, the range gets one token more than it should.
2892 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002893 else
2894 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00002895
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002896 if (isCastExpr) {
2897 if (!CastTy) {
2898 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002899 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002900 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002901
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002902 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002903 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002904 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2905 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002906 DiagID, CastTy))
2907 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002908 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002909 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002910
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002911 // If we get here, the operand to the typeof was an expresion.
2912 if (Operand.isInvalid()) {
2913 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002914 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002915 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002916
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002917 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002918 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002919 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2920 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002921 DiagID, Operand.release()))
2922 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002923}