blob: 9525eb364c346d0d8d46fd1b5a715e85e567cbc7 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner1a76a3c2007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000018#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000019using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.7: Declarations.
23//===----------------------------------------------------------------------===//
24
Chris Lattnerf5fbd792006-08-10 23:56:11 +000025/// ParseTypeName
26/// type-name: [C99 6.7.6]
27/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000028///
29/// Called type-id in C++.
Sebastian Redld6434562009-05-29 18:02:33 +000030Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000031 // Parse the common declaration-specifiers piece.
32 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000033 ParseSpecifierQualifierList(DS);
Sebastian Redld6434562009-05-29 18:02:33 +000034
Chris Lattnerf5fbd792006-08-10 23:56:11 +000035 // Parse the abstract-declarator, if present.
36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
37 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000038 if (Range)
39 *Range = DeclaratorInfo.getSourceRange();
40
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000041 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000042 return true;
43
44 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000045}
46
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000047/// 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
Steve Naroff0f2fe172007-06-01 17:11:19 +000062/// attrib-name
63/// attrib-name '(' identifier ')'
64/// attrib-name '(' identifier ',' nonempty-expr-list ')'
65/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000066///
Steve Naroff0f2fe172007-06-01 17:11:19 +000067/// [GNU] attrib-name:
68/// identifier
69/// typespec
70/// typequal
71/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000072///
Steve Naroff0f2fe172007-06-01 17:11:19 +000073/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-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
Steve Naroff0f2fe172007-06-01 17:11:19 +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.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000082
Sebastian Redlf6591ca2009-02-09 18:23:29 +000083AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000084 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000085
Steve Naroffb8371e12007-06-09 03:39:29 +000086 AttributeList *CurrAttr = 0;
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner76c72282007-10-09 17:33:22 +000088 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +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 Lattner76c72282007-10-09 17:33:22 +0000100 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
101 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000102
103 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +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 Stump11289f42009-09-09 15:08:12 +0000111
Steve Naroff0f2fe172007-06-01 17:11:19 +0000112 // check if we have a "paramterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000113 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000114 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000115
Chris Lattner76c72282007-10-09 17:33:22 +0000116 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000117 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
118 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000119
120 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000121 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000122 ConsumeParen(); // ignore the right paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000123 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000124 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000125 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000126 ConsumeToken();
127 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000128 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000129 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000130
Steve Naroff0f2fe172007-06-01 17:11:19 +0000131 // now parse the non-empty comma separated list of expressions
132 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000133 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000134 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000135 ArgExprsOk = false;
136 SkipUntil(tok::r_paren);
137 break;
138 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000139 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000140 }
Chris Lattner76c72282007-10-09 17:33:22 +0000141 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000142 break;
143 ConsumeToken(); // Eat the comma, move to the next argument
144 }
Chris Lattner76c72282007-10-09 17:33:22 +0000145 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000146 ConsumeParen(); // ignore the right paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000147 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl511ed552008-11-25 22:21:31 +0000148 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000149 }
150 }
151 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000152 switch (Tok.getKind()) {
153 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000154 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000155 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000156 ConsumeParen(); // ignore the right paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000157 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000158 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begemanf2758702009-06-26 06:32:41 +0000159 break;
160 case tok::kw_char:
161 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000162 case tok::kw_char16_t:
163 case tok::kw_char32_t:
Nate Begemanf2758702009-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 Stump11289f42009-09-09 15:08:12 +0000177 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Nate Begemanf2758702009-06-26 06:32:41 +0000178 0, SourceLocation(), 0, 0, CurrAttr);
179 if (Tok.is(tok::r_paren))
180 ConsumeParen();
181 break;
182 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000183 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000184 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000185 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000186
Steve Naroff0f2fe172007-06-01 17:11:19 +0000187 // now parse the list of expressions
188 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000189 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000190 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000191 ArgExprsOk = false;
192 SkipUntil(tok::r_paren);
193 break;
194 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000195 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000196 }
Chris Lattner76c72282007-10-09 17:33:22 +0000197 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000198 break;
199 ConsumeToken(); // Eat the comma, move to the next argument
200 }
201 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000202 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000203 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl511ed552008-11-25 22:21:31 +0000204 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
205 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Steve Naroffb8371e12007-06-09 03:39:29 +0000206 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000207 }
Nate Begemanf2758702009-06-26 06:32:41 +0000208 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000209 }
210 }
211 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000212 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000213 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000214 }
215 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000216 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000217 SkipUntil(tok::r_paren, false);
Sebastian Redlf6591ca2009-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;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000224 }
225 return CurrAttr;
226}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000227
Eli Friedman06de2b52009-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 Friedman53339e02009-06-08 23:27:34 +0000237AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000238 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000239
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000240 ConsumeToken();
Eli Friedman06de2b52009-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 Friedman53339e02009-06-08 23:27:34 +0000246 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-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 Friedman53339e02009-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 Naroff3a9b7e02008-12-24 20:59:21 +0000287}
288
Chris Lattner53361ac2006-08-10 05:19:57 +0000289/// ParseDeclaration - Parse a full 'declaration', which consists of
290/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000291/// 'Context' should be a Declarator::TheContext value. This returns the
292/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000293///
294/// declaration: [C99 6.7]
295/// block-declaration ->
296/// simple-declaration
297/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000298/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000299/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000300/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000301/// [C++] using-declaration
Sebastian Redlf769df52009-03-24 22:27:57 +0000302/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000303/// others... [FIXME]
304///
Chris Lattner49836b42009-04-02 04:16:50 +0000305Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
306 SourceLocation &DeclEnd) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000307 DeclPtrTy SingleDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000308 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000309 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000310 case tok::kw_export:
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000311 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000312 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000313 case tok::kw_namespace:
Chris Lattner49836b42009-04-02 04:16:50 +0000314 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000315 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000316 case tok::kw_using:
Chris Lattner49836b42009-04-02 04:16:50 +0000317 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000318 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000319 case tok::kw_static_assert:
Chris Lattner49836b42009-04-02 04:16:50 +0000320 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000321 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000322 default:
Chris Lattner49836b42009-04-02 04:16:50 +0000323 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattnera5235172007-08-25 06:57:03 +0000324 }
Mike Stump11289f42009-09-09 15:08:12 +0000325
Chris Lattner5bbb3c82009-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 Lattnera5235172007-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 Lattner32dc41c2009-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 Lattner49836b42009-04-02 04:16:50 +0000339 SourceLocation &DeclEnd,
Chris Lattner32dc41c2009-03-29 17:27:48 +0000340 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000341 // Parse the common declaration-specifiers piece.
342 DeclSpec DS;
343 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000344
Chris Lattner0e894622006-08-13 19:58:17 +0000345 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
346 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000347 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000348 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000349 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
350 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000351 }
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattner53361ac2006-08-10 05:19:57 +0000353 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
354 ParseDeclarator(DeclaratorInfo);
Mike Stump11289f42009-09-09 15:08:12 +0000355
Chris Lattnerefb0f112009-03-29 17:18:04 +0000356 DeclGroupPtrTy DG =
357 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000358
Chris Lattner49836b42009-04-02 04:16:50 +0000359 DeclEnd = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattner32dc41c2009-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 Stump11289f42009-09-09 15:08:12 +0000364
Chris Lattnerefb0f112009-03-29 17:18:04 +0000365 if (Tok.is(tok::semi)) {
366 ConsumeToken();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000367 return DG;
368 }
Mike Stump11289f42009-09-09 15:08:12 +0000369
John McCallef50e992009-07-31 02:20:35 +0000370 Diag(Tok, diag::err_expected_semi_declaration);
Chris Lattnerefb0f112009-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;
Chris Lattner53361ac2006-08-10 05:19:57 +0000376}
377
Douglas Gregor23996282009-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.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000382///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000383/// init-declarator: [C99 6.7]
384/// declarator
385/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000386/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
387/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000388/// [C++] declarator initializer[opt]
389///
390/// [C++] initializer:
391/// [C++] '=' initializer-clause
392/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-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.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000398///
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000399Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
400 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-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 Stump11289f42009-09-09 15:08:12 +0000409
Douglas Gregor23996282009-05-12 21:31:51 +0000410 D.setAsmLabel(AsmLabel.release());
411 D.SetRangeEnd(Loc);
412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
Douglas Gregor23996282009-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 Stump11289f42009-09-09 15:08:12 +0000420
Douglas Gregor23996282009-05-12 21:31:51 +0000421 // Inform the current actions module that we just parsed this declarator.
Douglas Gregor450f00842009-09-25 18:43:00 +0000422 DeclPtrTy ThisDecl;
423 switch (TemplateInfo.Kind) {
424 case ParsedTemplateInfo::NonTemplate:
425 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
426 break;
427
428 case ParsedTemplateInfo::Template:
429 case ParsedTemplateInfo::ExplicitSpecialization:
430 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000431 Action::MultiTemplateParamsArg(Actions,
432 TemplateInfo.TemplateParams->data(),
433 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000434 D);
435 break;
436
437 case ParsedTemplateInfo::ExplicitInstantiation: {
438 Action::DeclResult ThisRes
439 = Actions.ActOnExplicitInstantiation(CurScope,
440 TemplateInfo.ExternLoc,
441 TemplateInfo.TemplateLoc,
442 D);
443 if (ThisRes.isInvalid()) {
444 SkipUntil(tok::semi, true, true);
445 return DeclPtrTy();
446 }
447
448 ThisDecl = ThisRes.get();
449 break;
450 }
451 }
Mike Stump11289f42009-09-09 15:08:12 +0000452
Douglas Gregor23996282009-05-12 21:31:51 +0000453 // Parse declarator '=' initializer.
454 if (Tok.is(tok::equal)) {
455 ConsumeToken();
456 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
457 SourceLocation DelLoc = ConsumeToken();
458 Actions.SetDeclDeleted(ThisDecl, DelLoc);
459 } else {
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000460 if (getLang().CPlusPlus)
461 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
462
Douglas Gregor23996282009-05-12 21:31:51 +0000463 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000464
465 if (getLang().CPlusPlus)
466 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
467
Douglas Gregor23996282009-05-12 21:31:51 +0000468 if (Init.isInvalid()) {
469 SkipUntil(tok::semi, true, true);
470 return DeclPtrTy();
471 }
Anders Carlsson250aada2009-08-16 05:13:48 +0000472 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor23996282009-05-12 21:31:51 +0000473 }
474 } else if (Tok.is(tok::l_paren)) {
475 // Parse C++ direct initializer: '(' expression-list ')'
476 SourceLocation LParenLoc = ConsumeParen();
477 ExprVector Exprs(Actions);
478 CommaLocsTy CommaLocs;
479
480 if (ParseExpressionList(Exprs, CommaLocs)) {
481 SkipUntil(tok::r_paren);
482 } else {
483 // Match the ')'.
484 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
485
486 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
487 "Unexpected number of commas!");
488 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
489 move_arg(Exprs),
Jay Foad7d0479f2009-05-21 09:52:38 +0000490 CommaLocs.data(), RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000491 }
492 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000493 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000494 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
495 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000496 }
497
498 return ThisDecl;
499}
500
501/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
502/// parsing 'declaration-specifiers declarator'. This method is split out this
503/// way to handle the ambiguity between top-level function-definitions and
504/// declarations.
505///
506/// init-declarator-list: [C99 6.7]
507/// init-declarator
508/// init-declarator-list ',' init-declarator
509///
510/// According to the standard grammar, =default and =delete are function
511/// definitions, but that definitely doesn't fit with the parser here.
512///
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000513Parser::DeclGroupPtrTy Parser::
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000514ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000515 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
516 // that we parse together here.
517 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Mike Stump11289f42009-09-09 15:08:12 +0000518
Chris Lattner53361ac2006-08-10 05:19:57 +0000519 // At this point, we know that it is not a function definition. Parse the
520 // rest of the init-declarator-list.
521 while (1) {
Douglas Gregor23996282009-05-12 21:31:51 +0000522 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
523 if (ThisDecl.get())
524 DeclsInGroup.push_back(ThisDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000525
Chris Lattner53361ac2006-08-10 05:19:57 +0000526 // If we don't have a comma, it is either the end of the list (a ';') or an
527 // error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000528 if (Tok.isNot(tok::comma))
Chris Lattner53361ac2006-08-10 05:19:57 +0000529 break;
Mike Stump11289f42009-09-09 15:08:12 +0000530
Chris Lattner53361ac2006-08-10 05:19:57 +0000531 // Consume the comma.
532 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000533
Chris Lattner53361ac2006-08-10 05:19:57 +0000534 // Parse the next declarator.
535 D.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000536
Chris Lattner29e6f2b2008-10-20 04:57:38 +0000537 // Accept attributes in an init-declarator. In the first declarator in a
538 // declaration, these would be part of the declspec. In subsequent
539 // declarators, they become part of the declarator itself, so that they
540 // don't apply to declarators after *this* one. Examples:
541 // short __attribute__((common)) var; -> declspec
542 // short var __attribute__((common)); -> declarator
543 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000544 if (Tok.is(tok::kw___attribute)) {
545 SourceLocation Loc;
546 AttributeList *AttrList = ParseAttributes(&Loc);
547 D.AddAttributes(AttrList, Loc);
548 }
Mike Stump11289f42009-09-09 15:08:12 +0000549
Chris Lattner53361ac2006-08-10 05:19:57 +0000550 ParseDeclarator(D);
551 }
Mike Stump11289f42009-09-09 15:08:12 +0000552
Eli Friedman55b9ecb2009-05-29 01:49:24 +0000553 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
554 DeclsInGroup.data(),
Chris Lattnerefb0f112009-03-29 17:18:04 +0000555 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000556}
557
Chris Lattner1890ac82006-08-13 01:16:23 +0000558/// ParseSpecifierQualifierList
559/// specifier-qualifier-list:
560/// type-specifier specifier-qualifier-list[opt]
561/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000562/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000563///
564void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
565 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
566 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000567 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000568
Chris Lattner1890ac82006-08-13 01:16:23 +0000569 // Validate declspec for type-name.
570 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000571 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
572 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000573 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000574
Chris Lattner1b22eed2006-11-28 05:12:07 +0000575 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000576 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000577 if (DS.getStorageClassSpecLoc().isValid())
578 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
579 else
580 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000581 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000582 }
Mike Stump11289f42009-09-09 15:08:12 +0000583
Chris Lattner1b22eed2006-11-28 05:12:07 +0000584 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000585 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000586 if (DS.isInlineSpecified())
587 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
588 if (DS.isVirtualSpecified())
589 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
590 if (DS.isExplicitSpecified())
591 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000592 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000593 }
594}
Chris Lattner53361ac2006-08-10 05:19:57 +0000595
Chris Lattner6cc055a2009-04-12 20:42:31 +0000596/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
597/// specified token is valid after the identifier in a declarator which
598/// immediately follows the declspec. For example, these things are valid:
599///
600/// int x [ 4]; // direct-declarator
601/// int x ( int y); // direct-declarator
602/// int(int x ) // direct-declarator
603/// int x ; // simple-declaration
604/// int x = 17; // init-declarator-list
605/// int x , y; // init-declarator-list
606/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000607/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000608/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000609///
610/// This is not, because 'x' does not immediately follow the declspec (though
611/// ')' happens to be valid anyway).
612/// int (x)
613///
614static bool isValidAfterIdentifierInDeclarator(const Token &T) {
615 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
616 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000617 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000618}
619
Chris Lattner20a0c612009-04-14 21:34:55 +0000620
621/// ParseImplicitInt - This method is called when we have an non-typename
622/// identifier in a declspec (which normally terminates the decl spec) when
623/// the declspec has no type specifier. In this case, the declspec is either
624/// malformed or is "implicit int" (in K&R and C89).
625///
626/// This method handles diagnosing this prettily and returns false if the
627/// declspec is done being processed. If it recovers and thinks there may be
628/// other pieces of declspec after it, it returns true.
629///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000630bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000631 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000632 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000633 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000634
Chris Lattner20a0c612009-04-14 21:34:55 +0000635 SourceLocation Loc = Tok.getLocation();
636 // If we see an identifier that is not a type name, we normally would
637 // parse it as the identifer being declared. However, when a typename
638 // is typo'd or the definition is not included, this will incorrectly
639 // parse the typename as the identifier name and fall over misparsing
640 // later parts of the diagnostic.
641 //
642 // As such, we try to do some look-ahead in cases where this would
643 // otherwise be an "implicit-int" case to see if this is invalid. For
644 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
645 // an identifier with implicit int, we'd get a parse error because the
646 // next token is obviously invalid for a type. Parse these as a case
647 // with an invalid type specifier.
648 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000649
Chris Lattner20a0c612009-04-14 21:34:55 +0000650 // Since we know that this either implicit int (which is rare) or an
651 // error, we'd do lookahead to try to do better recovery.
652 if (isValidAfterIdentifierInDeclarator(NextToken())) {
653 // If this token is valid for implicit int, e.g. "static x = 4", then
654 // we just avoid eating the identifier, so it will be parsed as the
655 // identifier in the declarator.
656 return false;
657 }
Mike Stump11289f42009-09-09 15:08:12 +0000658
Chris Lattner20a0c612009-04-14 21:34:55 +0000659 // Otherwise, if we don't consume this token, we are going to emit an
660 // error anyway. Try to recover from various common problems. Check
661 // to see if this was a reference to a tag name without a tag specified.
662 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000663 //
664 // C++ doesn't need this, and isTagName doesn't take SS.
665 if (SS == 0) {
666 const char *TagName = 0;
667 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000668
Chris Lattner20a0c612009-04-14 21:34:55 +0000669 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
670 default: break;
671 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
672 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
673 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
674 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
675 }
Mike Stump11289f42009-09-09 15:08:12 +0000676
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000677 if (TagName) {
678 Diag(Loc, diag::err_use_of_tag_name_without_tag)
679 << Tok.getIdentifierInfo() << TagName
680 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000681
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000682 // Parse this as a tag as if the missing tag were present.
683 if (TagKind == tok::kw_enum)
684 ParseEnumSpecifier(Loc, DS, AS);
685 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000686 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000687 return true;
688 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000689 }
Mike Stump11289f42009-09-09 15:08:12 +0000690
Chris Lattner20a0c612009-04-14 21:34:55 +0000691 // Since this is almost certainly an invalid type name, emit a
692 // diagnostic that says it, eat the token, and mark the declspec as
693 // invalid.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000694 SourceRange R;
695 if (SS) R = SS->getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000696
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000697 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattner20a0c612009-04-14 21:34:55 +0000698 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000699 unsigned DiagID;
700 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000701 DS.SetRangeEnd(Tok.getLocation());
702 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000703
Chris Lattner20a0c612009-04-14 21:34:55 +0000704 // TODO: Could inject an invalid typedef decl in an enclosing scope to
705 // avoid rippling error messages on subsequent uses of the same type,
706 // could be useful if #include was forgotten.
707 return false;
708}
709
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000710/// ParseDeclarationSpecifiers
711/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000712/// storage-class-specifier declaration-specifiers[opt]
713/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000714/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000715/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000716///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000717/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000718/// 'typedef'
719/// 'extern'
720/// 'static'
721/// 'auto'
722/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000723/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000724/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000725/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000726/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000727/// [C++] 'virtual'
728/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000729/// 'friend': [C++ dcl.friend]
730
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000731///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000732void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000733 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000734 AccessSpecifier AS,
735 DeclSpecContext DSContext) {
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000736 if (Tok.is(tok::code_completion)) {
737 Actions.CodeCompleteOrdinaryName(CurScope);
738 ConsumeToken();
739 }
740
Chris Lattner2e232092008-03-13 06:29:04 +0000741 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000742 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000743 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000744 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000745 unsigned DiagID = 0;
746
Chris Lattner4d8f8732006-11-28 05:05:08 +0000747 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000748
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000749 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000750 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000751 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000752 // If this is not a declaration specifier token, we're done reading decl
753 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000754 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000755 return;
Mike Stump11289f42009-09-09 15:08:12 +0000756
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000757 case tok::coloncolon: // ::foo::bar
758 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregore861bac2009-08-25 22:51:20 +0000759 if (TryAnnotateCXXScopeToken(true))
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000760 continue;
761 goto DoneWithDeclSpec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000762
763 case tok::annot_cxxscope: {
764 if (DS.hasTypeSpecifier())
765 goto DoneWithDeclSpec;
766
767 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000768 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000769 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000770 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000771 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000772 // We have a qualified template-id, e.g., N::A<int>
773 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000774 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Mike Stump11289f42009-09-09 15:08:12 +0000775 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000776 "ParseOptionalCXXScopeSpecifier not working");
777 AnnotateTemplateIdTokenAsType(&SS);
778 continue;
779 }
780
781 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000782 goto DoneWithDeclSpec;
783
784 CXXScopeSpec SS;
Douglas Gregorc23500e2009-03-26 23:56:24 +0000785 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000786 SS.setRange(Tok.getAnnotationRange());
787
788 // If the next token is the name of the class type that the C++ scope
789 // denotes, followed by a '(', then this is a constructor declaration.
790 // We're done with the decl-specifiers.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000791 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000792 CurScope, &SS) &&
793 GetLookAheadToken(2).is(tok::l_paren))
794 goto DoneWithDeclSpec;
795
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000796 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
797 Next.getLocation(), CurScope, &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000798
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000799 // If the referenced identifier is not a type, then this declspec is
800 // erroneous: We already checked about that it has no type specifier, and
801 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +0000802 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000803 if (TypeRep == 0) {
804 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000805 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000806 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000807 }
Mike Stump11289f42009-09-09 15:08:12 +0000808
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000809 ConsumeToken(); // The C++ scope.
810
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000811 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000812 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000813 if (isInvalid)
814 break;
Mike Stump11289f42009-09-09 15:08:12 +0000815
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000816 DS.SetRangeEnd(Tok.getLocation());
817 ConsumeToken(); // The typename.
818
819 continue;
820 }
Mike Stump11289f42009-09-09 15:08:12 +0000821
Chris Lattnere387d9e2009-01-21 19:48:37 +0000822 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000823 if (Tok.getAnnotationValue())
824 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000825 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000826 else
827 DS.SetTypeSpecError();
Chris Lattnere387d9e2009-01-21 19:48:37 +0000828 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
829 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +0000830
Chris Lattnere387d9e2009-01-21 19:48:37 +0000831 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
832 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
833 // Objective-C interface. If we don't have Objective-C or a '<', this is
834 // just a normal reference to a typedef name.
835 if (!Tok.is(tok::less) || !getLang().ObjC1)
836 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000837
Chris Lattnere387d9e2009-01-21 19:48:37 +0000838 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000839 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere387d9e2009-01-21 19:48:37 +0000840 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek58d81902009-06-30 22:19:00 +0000841 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Mike Stump11289f42009-09-09 15:08:12 +0000842
Chris Lattnere387d9e2009-01-21 19:48:37 +0000843 DS.SetRangeEnd(EndProtoLoc);
844 continue;
845 }
Mike Stump11289f42009-09-09 15:08:12 +0000846
Chris Lattner16fac4f2008-07-26 01:18:38 +0000847 // typedef-name
848 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000849 // In C++, check to see if this is a scope specifier like foo::bar::, if
850 // so handle it as such. This is important for ctor parsing.
Douglas Gregore861bac2009-08-25 22:51:20 +0000851 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner78ecd4f2009-01-21 19:19:26 +0000852 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000853
Chris Lattner16fac4f2008-07-26 01:18:38 +0000854 // This identifier can only be a typedef name if we haven't already seen
855 // a type-specifier. Without this check we misparse:
856 // typedef int X; struct Y { short X; }; as 'short int'.
857 if (DS.hasTypeSpecifier())
858 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +0000859
Chris Lattner16fac4f2008-07-26 01:18:38 +0000860 // It has to be available as a typedef too!
Mike Stump11289f42009-09-09 15:08:12 +0000861 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000862 Tok.getLocation(), CurScope);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000863
Chris Lattner6cc055a2009-04-12 20:42:31 +0000864 // If this is not a typedef name, don't parse it as part of the declspec,
865 // it must be an implicit int or an error.
866 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000867 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000868 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +0000869 }
Douglas Gregor8bf42052009-02-09 18:46:07 +0000870
Douglas Gregor61956c42008-10-31 09:07:45 +0000871 // C++: If the identifier is actually the name of the class type
872 // being defined and the next token is a '(', then this is a
873 // constructor declaration. We're done with the decl-specifiers
874 // and will treat this token as an identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000875 if (getLang().CPlusPlus &&
876 (CurScope->isClassScope() ||
877 (CurScope->isTemplateParamScope() &&
Douglas Gregor5ed5ae42009-08-21 18:42:58 +0000878 CurScope->getParent()->isClassScope())) &&
Mike Stump11289f42009-09-09 15:08:12 +0000879 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor61956c42008-10-31 09:07:45 +0000880 NextToken().getKind() == tok::l_paren)
881 goto DoneWithDeclSpec;
882
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000883 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000884 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +0000885 if (isInvalid)
886 break;
Mike Stump11289f42009-09-09 15:08:12 +0000887
Chris Lattner16fac4f2008-07-26 01:18:38 +0000888 DS.SetRangeEnd(Tok.getLocation());
889 ConsumeToken(); // The identifier
890
891 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
892 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
893 // Objective-C interface. If we don't have Objective-C or a '<', this is
894 // just a normal reference to a typedef name.
895 if (!Tok.is(tok::less) || !getLang().ObjC1)
896 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000897
Chris Lattner16fac4f2008-07-26 01:18:38 +0000898 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000899 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000900 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek58d81902009-06-30 22:19:00 +0000901 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Mike Stump11289f42009-09-09 15:08:12 +0000902
Chris Lattner16fac4f2008-07-26 01:18:38 +0000903 DS.SetRangeEnd(EndProtoLoc);
904
Steve Naroffcd5e7822008-09-22 10:28:57 +0000905 // Need to support trailing type qualifiers (e.g. "id<p> const").
906 // If a type specifier follows, it will be diagnosed elsewhere.
907 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000908 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000909
910 // type-name
911 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +0000912 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000913 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000914 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000915 // This template-id does not refer to a type name, so we're
916 // done with the type-specifiers.
917 goto DoneWithDeclSpec;
918 }
919
920 // Turn the template-id annotation token into a type annotation
921 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000922 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +0000923 continue;
924 }
925
Chris Lattnere37e2332006-08-15 04:50:22 +0000926 // GNU attributes support.
927 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000928 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000929 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000930
931 // Microsoft declspec support.
932 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +0000933 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000934 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000935
Steve Naroff44ac7772008-12-25 14:16:32 +0000936 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +0000937 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +0000938 // FIXME: Add handling here!
939 break;
940
941 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +0000942 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +0000943 case tok::kw___cdecl:
944 case tok::kw___stdcall:
945 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +0000946 DS.AddAttributes(ParseMicrosoftTypeAttributes());
947 continue;
948
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000949 // storage-class-specifier
950 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +0000951 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
952 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000953 break;
954 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000955 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +0000956 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +0000957 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
958 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000959 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000960 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000961 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +0000962 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000963 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000964 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000965 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +0000966 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +0000967 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
968 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000969 break;
970 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +0000971 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +0000972 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
973 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +0000974 else
John McCall49bfce42009-08-03 20:12:06 +0000975 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
976 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000977 break;
978 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +0000979 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
980 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000981 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000982 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +0000983 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
984 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000985 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000986 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +0000987 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000988 break;
Mike Stump11289f42009-09-09 15:08:12 +0000989
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000990 // function-specifier
991 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +0000992 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000993 break;
Douglas Gregor61956c42008-10-31 09:07:45 +0000994 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +0000995 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +0000996 break;
Douglas Gregor61956c42008-10-31 09:07:45 +0000997 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +0000998 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +0000999 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001000
Anders Carlssoncd8db412009-05-06 04:46:28 +00001001 // friend
1002 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001003 if (DSContext == DSC_class)
1004 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1005 else {
1006 PrevSpec = ""; // not actually used by the diagnostic
1007 DiagID = diag::err_friend_invalid_in_context;
1008 isInvalid = true;
1009 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001010 break;
Mike Stump11289f42009-09-09 15:08:12 +00001011
Chris Lattnere387d9e2009-01-21 19:48:37 +00001012 // type-specifier
1013 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001014 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1015 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001016 break;
1017 case tok::kw_long:
1018 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001019 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1020 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001021 else
John McCall49bfce42009-08-03 20:12:06 +00001022 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1023 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001024 break;
1025 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001026 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1027 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001028 break;
1029 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001030 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1031 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001032 break;
1033 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001034 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1035 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001036 break;
1037 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001038 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1039 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001040 break;
1041 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001042 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1043 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001044 break;
1045 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001046 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1047 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001048 break;
1049 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001050 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1051 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001052 break;
1053 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001054 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1055 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001056 break;
1057 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001058 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1059 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001060 break;
1061 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001062 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1063 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001064 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001065 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001066 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1067 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001068 break;
1069 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001070 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1071 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001072 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001073 case tok::kw_bool:
1074 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001075 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1076 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001077 break;
1078 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001079 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1080 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001081 break;
1082 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001083 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1084 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001085 break;
1086 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001087 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1088 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001089 break;
1090
1091 // class-specifier:
1092 case tok::kw_class:
1093 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001094 case tok::kw_union: {
1095 tok::TokenKind Kind = Tok.getKind();
1096 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001097 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001098 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001099 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001100
1101 // enum-specifier:
1102 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001103 ConsumeToken();
1104 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001105 continue;
1106
1107 // cv-qualifier:
1108 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001109 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1110 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001111 break;
1112 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001113 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1114 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001115 break;
1116 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001117 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1118 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001119 break;
1120
Douglas Gregor333489b2009-03-27 23:10:48 +00001121 // C++ typename-specifier:
1122 case tok::kw_typename:
1123 if (TryAnnotateTypeOrScopeToken())
1124 continue;
1125 break;
1126
Chris Lattnere387d9e2009-01-21 19:48:37 +00001127 // GNU typeof support.
1128 case tok::kw_typeof:
1129 ParseTypeofSpecifier(DS);
1130 continue;
1131
Anders Carlsson74948d02009-06-24 17:47:40 +00001132 case tok::kw_decltype:
1133 ParseDecltypeSpecifier(DS);
1134 continue;
1135
Steve Naroffcfdf6162008-06-05 00:02:44 +00001136 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001137 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001138 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1139 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001140 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001141 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001142
Chris Lattner0974b232008-07-26 00:20:22 +00001143 {
1144 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001145 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +00001146 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek58d81902009-06-30 22:19:00 +00001147 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +00001148 DS.SetRangeEnd(EndProtoLoc);
1149
Chris Lattner6d29c102008-11-18 07:48:38 +00001150 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner3a4e4312009-04-03 18:38:42 +00001151 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001152 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001153 // Need to support trailing type qualifiers (e.g. "id<p> const").
1154 // If a type specifier follows, it will be diagnosed elsewhere.
1155 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001156 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001157 }
John McCall49bfce42009-08-03 20:12:06 +00001158 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001159 if (isInvalid) {
1160 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001161 assert(DiagID);
Chris Lattner6d29c102008-11-18 07:48:38 +00001162 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001163 }
Chris Lattner2e232092008-03-13 06:29:04 +00001164 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001165 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001166 }
1167}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001168
Chris Lattnera448d752009-01-06 06:59:53 +00001169/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001170/// primarily follow the C++ grammar with additions for C99 and GNU,
1171/// which together subsume the C grammar. Note that the C++
1172/// type-specifier also includes the C type-qualifier (for const,
1173/// volatile, and C99 restrict). Returns true if a type-specifier was
1174/// found (and parsed), false otherwise.
1175///
1176/// type-specifier: [C++ 7.1.5]
1177/// simple-type-specifier
1178/// class-specifier
1179/// enum-specifier
1180/// elaborated-type-specifier [TODO]
1181/// cv-qualifier
1182///
1183/// cv-qualifier: [C++ 7.1.5.1]
1184/// 'const'
1185/// 'volatile'
1186/// [C99] 'restrict'
1187///
1188/// simple-type-specifier: [ C++ 7.1.5.2]
1189/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1190/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1191/// 'char'
1192/// 'wchar_t'
1193/// 'bool'
1194/// 'short'
1195/// 'int'
1196/// 'long'
1197/// 'signed'
1198/// 'unsigned'
1199/// 'float'
1200/// 'double'
1201/// 'void'
1202/// [C99] '_Bool'
1203/// [C99] '_Complex'
1204/// [C99] '_Imaginary' // Removed in TC2?
1205/// [GNU] '_Decimal32'
1206/// [GNU] '_Decimal64'
1207/// [GNU] '_Decimal128'
1208/// [GNU] typeof-specifier
1209/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1210/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001211/// [C++0x] 'decltype' ( expression )
John McCall49bfce42009-08-03 20:12:06 +00001212bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001213 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001214 unsigned &DiagID,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001215 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001216 SourceLocation Loc = Tok.getLocation();
1217
1218 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001219 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001220 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001221 // Annotate typenames and C++ scope specifiers. If we get one, just
1222 // recurse to handle whatever we get.
1223 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001224 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1225 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001226 // Otherwise, not a type specifier.
1227 return false;
1228 case tok::coloncolon: // ::foo::bar
1229 if (NextToken().is(tok::kw_new) || // ::new
1230 NextToken().is(tok::kw_delete)) // ::delete
1231 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001232
Chris Lattner020bab92009-01-04 23:41:41 +00001233 // Annotate typenames and C++ scope specifiers. If we get one, just
1234 // recurse to handle whatever we get.
1235 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001236 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1237 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001238 // Otherwise, not a type specifier.
1239 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001240
Douglas Gregor450c75a2008-11-07 15:42:26 +00001241 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001242 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001243 if (Tok.getAnnotationValue())
1244 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001245 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001246 else
1247 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001248 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1249 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001250
Douglas Gregor450c75a2008-11-07 15:42:26 +00001251 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1252 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1253 // Objective-C interface. If we don't have Objective-C or a '<', this is
1254 // just a normal reference to a typedef name.
1255 if (!Tok.is(tok::less) || !getLang().ObjC1)
1256 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001257
Douglas Gregor450c75a2008-11-07 15:42:26 +00001258 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001259 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001260 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek58d81902009-06-30 22:19:00 +00001261 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Mike Stump11289f42009-09-09 15:08:12 +00001262
Douglas Gregor450c75a2008-11-07 15:42:26 +00001263 DS.SetRangeEnd(EndProtoLoc);
1264 return true;
1265 }
1266
1267 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001268 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001269 break;
1270 case tok::kw_long:
1271 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001272 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1273 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001274 else
John McCall49bfce42009-08-03 20:12:06 +00001275 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1276 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001277 break;
1278 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001279 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001280 break;
1281 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001282 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1283 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001284 break;
1285 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001286 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1287 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001288 break;
1289 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001290 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1291 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001292 break;
1293 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001294 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001295 break;
1296 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001297 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001298 break;
1299 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001300 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001301 break;
1302 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001303 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001304 break;
1305 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001306 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001307 break;
1308 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001309 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001310 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001311 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001312 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001313 break;
1314 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001315 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001316 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001317 case tok::kw_bool:
1318 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001319 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001320 break;
1321 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001322 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1323 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001324 break;
1325 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1327 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001328 break;
1329 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001330 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1331 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001332 break;
1333
1334 // class-specifier:
1335 case tok::kw_class:
1336 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001337 case tok::kw_union: {
1338 tok::TokenKind Kind = Tok.getKind();
1339 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001340 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001341 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001342 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001343
1344 // enum-specifier:
1345 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001346 ConsumeToken();
1347 ParseEnumSpecifier(Loc, DS);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001348 return true;
1349
1350 // cv-qualifier:
1351 case tok::kw_const:
1352 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001353 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001354 break;
1355 case tok::kw_volatile:
1356 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001357 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001358 break;
1359 case tok::kw_restrict:
1360 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001361 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001362 break;
1363
1364 // GNU typeof support.
1365 case tok::kw_typeof:
1366 ParseTypeofSpecifier(DS);
1367 return true;
1368
Anders Carlsson74948d02009-06-24 17:47:40 +00001369 // C++0x decltype support.
1370 case tok::kw_decltype:
1371 ParseDecltypeSpecifier(DS);
1372 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001373
Anders Carlssonbae27372009-06-26 23:44:14 +00001374 // C++0x auto support.
1375 case tok::kw_auto:
1376 if (!getLang().CPlusPlus0x)
1377 return false;
1378
John McCall49bfce42009-08-03 20:12:06 +00001379 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001380 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001381 case tok::kw___ptr64:
1382 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001383 case tok::kw___cdecl:
1384 case tok::kw___stdcall:
1385 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001386 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001387 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001388
Douglas Gregor450c75a2008-11-07 15:42:26 +00001389 default:
1390 // Not a type-specifier; do nothing.
1391 return false;
1392 }
1393
1394 // If the specifier combination wasn't legal, issue a diagnostic.
1395 if (isInvalid) {
1396 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001397 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001398 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001399 }
1400 DS.SetRangeEnd(Tok.getLocation());
1401 ConsumeToken(); // whatever we parsed above.
1402 return true;
1403}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001404
Chris Lattner70ae4912007-10-29 04:42:53 +00001405/// ParseStructDeclaration - Parse a struct declaration without the terminating
1406/// semicolon.
1407///
Chris Lattner90a26b02007-01-23 04:38:16 +00001408/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001409/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001410/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001411/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001412/// struct-declarator-list:
1413/// struct-declarator
1414/// struct-declarator-list ',' struct-declarator
1415/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1416/// struct-declarator:
1417/// declarator
1418/// [GNU] declarator attributes[opt]
1419/// declarator[opt] ':' constant-expression
1420/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1421///
Chris Lattnera12405b2008-04-10 06:46:29 +00001422void Parser::
1423ParseStructDeclaration(DeclSpec &DS,
1424 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001425 if (Tok.is(tok::kw___extension__)) {
1426 // __extension__ silences extension warnings in the subexpression.
1427 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001428 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001429 return ParseStructDeclaration(DS, Fields);
1430 }
Mike Stump11289f42009-09-09 15:08:12 +00001431
Steve Naroff97170802007-08-20 22:28:22 +00001432 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001433 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001434 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001435
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001436 // If there are no declarators, this is a free-standing declaration
1437 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001438 if (Tok.is(tok::semi)) {
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001439 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001440 return;
1441 }
1442
1443 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +00001444 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +00001445 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +00001446 FieldDeclarator &DeclaratorInfo = Fields.back();
Mike Stump11289f42009-09-09 15:08:12 +00001447
Steve Naroff97170802007-08-20 22:28:22 +00001448 /// struct-declarator: declarator
1449 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +00001450 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +00001451 ParseDeclarator(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00001452
Chris Lattner76c72282007-10-09 17:33:22 +00001453 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001454 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001455 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001456 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001457 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001458 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001459 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001460 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001461
Steve Naroff97170802007-08-20 22:28:22 +00001462 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001463 if (Tok.is(tok::kw___attribute)) {
1464 SourceLocation Loc;
1465 AttributeList *AttrList = ParseAttributes(&Loc);
1466 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1467 }
1468
Steve Naroff97170802007-08-20 22:28:22 +00001469 // If we don't have a comma, it is either the end of the list (a ';')
1470 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001471 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001472 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001473
Steve Naroff97170802007-08-20 22:28:22 +00001474 // Consume the comma.
1475 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001476
Steve Naroff97170802007-08-20 22:28:22 +00001477 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +00001478 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001479
Steve Naroff97170802007-08-20 22:28:22 +00001480 // Attributes are only allowed on the second declarator.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001481 if (Tok.is(tok::kw___attribute)) {
1482 SourceLocation Loc;
1483 AttributeList *AttrList = ParseAttributes(&Loc);
1484 Fields.back().D.AddAttributes(AttrList, Loc);
1485 }
Steve Naroff97170802007-08-20 22:28:22 +00001486 }
Steve Naroff97170802007-08-20 22:28:22 +00001487}
1488
1489/// ParseStructUnionBody
1490/// struct-contents:
1491/// struct-declaration-list
1492/// [EXT] empty
1493/// [GNU] "struct-declaration-list" without terminatoring ';'
1494/// struct-declaration-list:
1495/// struct-declaration
1496/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001497/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001498///
Chris Lattner1300fb92007-01-23 23:42:53 +00001499void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001500 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001501 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1502 PP.getSourceManager(),
1503 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001504
Chris Lattner90a26b02007-01-23 04:38:16 +00001505 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001506
Douglas Gregor658b9552009-01-09 22:42:13 +00001507 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001508 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1509
Chris Lattner7b9ace62007-01-23 20:11:08 +00001510 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1511 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001512 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001513 Diag(Tok, diag::ext_empty_struct_union_enum)
1514 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001515
Chris Lattner83f095c2009-03-28 19:18:32 +00001516 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001517 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1518
Chris Lattner7b9ace62007-01-23 20:11:08 +00001519 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001520 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001521 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001522
Chris Lattner736ed5d2007-06-09 05:59:07 +00001523 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001524 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001525 Diag(Tok, diag::ext_extra_struct_semi)
1526 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner36e46a22007-06-09 05:49:55 +00001527 ConsumeToken();
1528 continue;
1529 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001530
1531 // Parse all the comma separated declarators.
1532 DeclSpec DS;
1533 FieldDeclarators.clear();
Chris Lattner535b8302008-06-21 19:39:06 +00001534 if (!Tok.is(tok::at)) {
1535 ParseStructDeclaration(DS, FieldDeclarators);
Mike Stump11289f42009-09-09 15:08:12 +00001536
Chris Lattner535b8302008-06-21 19:39:06 +00001537 // Convert them all to fields.
1538 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1539 FieldDeclarator &FD = FieldDeclarators[i];
Douglas Gregor66a985d2009-08-26 14:27:30 +00001540 DeclPtrTy Field;
Chris Lattner535b8302008-06-21 19:39:06 +00001541 // Install the declarator into the current TagDecl.
Douglas Gregor66a985d2009-08-26 14:27:30 +00001542 if (FD.D.getExtension()) {
1543 // Silences extension warnings
1544 ExtensionRAIIObject O(Diags);
1545 Field = Actions.ActOnField(CurScope, TagDecl,
1546 DS.getSourceRange().getBegin(),
1547 FD.D, FD.BitfieldSize);
1548 } else {
1549 Field = Actions.ActOnField(CurScope, TagDecl,
1550 DS.getSourceRange().getBegin(),
1551 FD.D, FD.BitfieldSize);
1552 }
Chris Lattner535b8302008-06-21 19:39:06 +00001553 FieldDecls.push_back(Field);
1554 }
1555 } else { // Handle @defs
1556 ConsumeToken();
1557 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1558 Diag(Tok, diag::err_unexpected_at);
1559 SkipUntil(tok::semi, true, true);
1560 continue;
1561 }
1562 ConsumeToken();
1563 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1564 if (!Tok.is(tok::identifier)) {
1565 Diag(Tok, diag::err_expected_ident);
1566 SkipUntil(tok::semi, true, true);
1567 continue;
1568 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001569 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump11289f42009-09-09 15:08:12 +00001570 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001571 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001572 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1573 ConsumeToken();
1574 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001575 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001576
Chris Lattner76c72282007-10-09 17:33:22 +00001577 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001578 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001579 } else if (Tok.is(tok::r_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001580 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001581 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001582 } else {
1583 Diag(Tok, diag::err_expected_semi_decl_list);
1584 // Skip to end of block or statement
1585 SkipUntil(tok::r_brace, true, true);
1586 }
1587 }
Mike Stump11289f42009-09-09 15:08:12 +00001588
Steve Naroff33a1e802007-10-29 21:38:07 +00001589 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001590
Steve Naroffb8371e12007-06-09 03:39:29 +00001591 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +00001592 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001593 if (Tok.is(tok::kw___attribute))
Daniel Dunbare4ac7a42008-10-03 16:42:10 +00001594 AttrList = ParseAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +00001595
1596 Actions.ActOnFields(CurScope,
Jay Foad7d0479f2009-05-21 09:52:38 +00001597 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001598 LBraceLoc, RBraceLoc,
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001599 AttrList);
1600 StructScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001601 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001602}
1603
1604
Chris Lattner3b561a32006-08-13 00:12:11 +00001605/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001606/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001607/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001608///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001609/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1610/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001611/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001612/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001613///
1614/// [C++] elaborated-type-specifier:
1615/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1616///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001617void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1618 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001619 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001620 if (Tok.is(tok::code_completion)) {
1621 // Code completion for an enum name.
1622 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1623 ConsumeToken();
1624 }
1625
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001626 AttributeList *Attr = 0;
1627 // If attributes exist after tag, parse them.
1628 if (Tok.is(tok::kw___attribute))
1629 Attr = ParseAttributes();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001630
1631 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001632 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001633 if (Tok.isNot(tok::identifier)) {
1634 Diag(Tok, diag::err_expected_ident);
1635 if (Tok.isNot(tok::l_brace)) {
1636 // Has no name and is not a definition.
1637 // Skip the rest of this declarator, up until the comma or semicolon.
1638 SkipUntil(tok::comma, true);
1639 return;
1640 }
1641 }
1642 }
Mike Stump11289f42009-09-09 15:08:12 +00001643
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001644 // Must have either 'enum name' or 'enum {...}'.
1645 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1646 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001647
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001648 // Skip the rest of this declarator, up until the comma or semicolon.
1649 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001650 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001651 }
Mike Stump11289f42009-09-09 15:08:12 +00001652
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001653 // If an identifier is present, consume and remember it.
1654 IdentifierInfo *Name = 0;
1655 SourceLocation NameLoc;
1656 if (Tok.is(tok::identifier)) {
1657 Name = Tok.getIdentifierInfo();
1658 NameLoc = ConsumeToken();
1659 }
Mike Stump11289f42009-09-09 15:08:12 +00001660
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001661 // There are three options here. If we have 'enum foo;', then this is a
1662 // forward declaration. If we have 'enum foo {...' then this is a
1663 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1664 //
1665 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1666 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1667 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1668 //
John McCall9bb74a52009-07-31 02:45:11 +00001669 Action::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001670 if (Tok.is(tok::l_brace))
John McCall9bb74a52009-07-31 02:45:11 +00001671 TUK = Action::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001672 else if (Tok.is(tok::semi))
John McCall9bb74a52009-07-31 02:45:11 +00001673 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001674 else
John McCall9bb74a52009-07-31 02:45:11 +00001675 TUK = Action::TUK_Reference;
Douglas Gregord6ab8742009-05-28 23:31:59 +00001676 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001677 bool IsDependent = false;
John McCall9bb74a52009-07-31 02:45:11 +00001678 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001679 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregor27bdf00f2009-07-23 16:36:45 +00001680 Action::MultiTemplateParamsArg(Actions),
John McCall7f41d982009-09-11 04:59:25 +00001681 Owned, IsDependent);
1682 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump11289f42009-09-09 15:08:12 +00001683
Chris Lattner76c72282007-10-09 17:33:22 +00001684 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001685 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001686
Chris Lattner3b561a32006-08-13 00:12:11 +00001687 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +00001688 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001689 unsigned DiagID;
1690 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001691 TagDecl.getAs<void>(), Owned))
John McCall49bfce42009-08-03 20:12:06 +00001692 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00001693}
1694
Chris Lattnerc1915e22007-01-25 07:29:02 +00001695/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1696/// enumerator-list:
1697/// enumerator
1698/// enumerator-list ',' enumerator
1699/// enumerator:
1700/// enumeration-constant
1701/// enumeration-constant '=' constant-expression
1702/// enumeration-constant:
1703/// identifier
1704///
Chris Lattner83f095c2009-03-28 19:18:32 +00001705void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001706 // Enter the scope of the enum body and start the definition.
1707 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001708 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00001709
Chris Lattnerc1915e22007-01-25 07:29:02 +00001710 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001711
Chris Lattner37256fb2007-08-27 17:24:30 +00001712 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00001713 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001714 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump11289f42009-09-09 15:08:12 +00001715
Chris Lattner83f095c2009-03-28 19:18:32 +00001716 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001717
Chris Lattner83f095c2009-03-28 19:18:32 +00001718 DeclPtrTy LastEnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001719
Chris Lattnerc1915e22007-01-25 07:29:02 +00001720 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001721 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001722 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1723 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001724
Chris Lattnerc1915e22007-01-25 07:29:02 +00001725 SourceLocation EqualLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001726 OwningExprResult AssignedVal(Actions);
Chris Lattner76c72282007-10-09 17:33:22 +00001727 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001728 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001729 AssignedVal = ParseConstantExpression();
1730 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00001731 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001732 }
Mike Stump11289f42009-09-09 15:08:12 +00001733
Chris Lattnerc1915e22007-01-25 07:29:02 +00001734 // Install the enumerator constant into EnumDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00001735 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1736 LastEnumConstDecl,
1737 IdentLoc, Ident,
1738 EqualLoc,
1739 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00001740 EnumConstantDecls.push_back(EnumConstDecl);
1741 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001742
Chris Lattner76c72282007-10-09 17:33:22 +00001743 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001744 break;
1745 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001746
1747 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00001748 !(getLang().C99 || getLang().CPlusPlus0x))
1749 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1750 << getLang().CPlusPlus
1751 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattnerc1915e22007-01-25 07:29:02 +00001752 }
Mike Stump11289f42009-09-09 15:08:12 +00001753
Chris Lattnerc1915e22007-01-25 07:29:02 +00001754 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00001755 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001756
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001757 AttributeList *Attr = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001758 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001759 if (Tok.is(tok::kw___attribute))
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001760 Attr = ParseAttributes();
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001761
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001762 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1763 EnumConstantDecls.data(), EnumConstantDecls.size(),
1764 CurScope, Attr);
Mike Stump11289f42009-09-09 15:08:12 +00001765
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001766 EnumScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001767 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001768}
Chris Lattner3b561a32006-08-13 00:12:11 +00001769
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001770/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001771/// start of a type-qualifier-list.
1772bool Parser::isTypeQualifier() const {
1773 switch (Tok.getKind()) {
1774 default: return false;
1775 // type-qualifier
1776 case tok::kw_const:
1777 case tok::kw_volatile:
1778 case tok::kw_restrict:
1779 return true;
1780 }
1781}
1782
1783/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001784/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001785bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001786 switch (Tok.getKind()) {
1787 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001788
Chris Lattner020bab92009-01-04 23:41:41 +00001789 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001790 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001791 // Annotate typenames and C++ scope specifiers. If we get one, just
1792 // recurse to handle whatever we get.
1793 if (TryAnnotateTypeOrScopeToken())
1794 return isTypeSpecifierQualifier();
1795 // Otherwise, not a type specifier.
1796 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001797
Chris Lattner020bab92009-01-04 23:41:41 +00001798 case tok::coloncolon: // ::foo::bar
1799 if (NextToken().is(tok::kw_new) || // ::new
1800 NextToken().is(tok::kw_delete)) // ::delete
1801 return false;
1802
1803 // Annotate typenames and C++ scope specifiers. If we get one, just
1804 // recurse to handle whatever we get.
1805 if (TryAnnotateTypeOrScopeToken())
1806 return isTypeSpecifierQualifier();
1807 // Otherwise, not a type specifier.
1808 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001809
Chris Lattnere37e2332006-08-15 04:50:22 +00001810 // GNU attributes support.
1811 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00001812 // GNU typeof support.
1813 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00001814
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001815 // type-specifiers
1816 case tok::kw_short:
1817 case tok::kw_long:
1818 case tok::kw_signed:
1819 case tok::kw_unsigned:
1820 case tok::kw__Complex:
1821 case tok::kw__Imaginary:
1822 case tok::kw_void:
1823 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001824 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001825 case tok::kw_char16_t:
1826 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001827 case tok::kw_int:
1828 case tok::kw_float:
1829 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001830 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001831 case tok::kw__Bool:
1832 case tok::kw__Decimal32:
1833 case tok::kw__Decimal64:
1834 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00001835
Chris Lattner861a2262008-04-13 18:59:07 +00001836 // struct-or-union-specifier (C99) or class-specifier (C++)
1837 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001838 case tok::kw_struct:
1839 case tok::kw_union:
1840 // enum-specifier
1841 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00001842
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001843 // type-qualifier
1844 case tok::kw_const:
1845 case tok::kw_volatile:
1846 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001847
1848 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001849 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001850 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001851
Chris Lattner409bf7d2008-10-20 00:25:30 +00001852 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1853 case tok::less:
1854 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00001855
Steve Naroff44ac7772008-12-25 14:16:32 +00001856 case tok::kw___cdecl:
1857 case tok::kw___stdcall:
1858 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001859 case tok::kw___w64:
1860 case tok::kw___ptr64:
1861 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001862 }
1863}
1864
Chris Lattneracd58a32006-08-06 17:24:14 +00001865/// isDeclarationSpecifier() - Return true if the current token is part of a
1866/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001867bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00001868 switch (Tok.getKind()) {
1869 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001870
Chris Lattner020bab92009-01-04 23:41:41 +00001871 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00001872 // Unfortunate hack to support "Class.factoryMethod" notation.
1873 if (getLang().ObjC1 && NextToken().is(tok::period))
1874 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001875 // Fall through
Steve Naroff9527bbf2009-03-09 21:12:44 +00001876
Douglas Gregor333489b2009-03-27 23:10:48 +00001877 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001878 // Annotate typenames and C++ scope specifiers. If we get one, just
1879 // recurse to handle whatever we get.
1880 if (TryAnnotateTypeOrScopeToken())
1881 return isDeclarationSpecifier();
1882 // Otherwise, not a declaration specifier.
1883 return false;
1884 case tok::coloncolon: // ::foo::bar
1885 if (NextToken().is(tok::kw_new) || // ::new
1886 NextToken().is(tok::kw_delete)) // ::delete
1887 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001888
Chris Lattner020bab92009-01-04 23:41:41 +00001889 // Annotate typenames and C++ scope specifiers. If we get one, just
1890 // recurse to handle whatever we get.
1891 if (TryAnnotateTypeOrScopeToken())
1892 return isDeclarationSpecifier();
1893 // Otherwise, not a declaration specifier.
1894 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001895
Chris Lattneracd58a32006-08-06 17:24:14 +00001896 // storage-class-specifier
1897 case tok::kw_typedef:
1898 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00001899 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00001900 case tok::kw_static:
1901 case tok::kw_auto:
1902 case tok::kw_register:
1903 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00001904
Chris Lattneracd58a32006-08-06 17:24:14 +00001905 // type-specifiers
1906 case tok::kw_short:
1907 case tok::kw_long:
1908 case tok::kw_signed:
1909 case tok::kw_unsigned:
1910 case tok::kw__Complex:
1911 case tok::kw__Imaginary:
1912 case tok::kw_void:
1913 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001914 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001915 case tok::kw_char16_t:
1916 case tok::kw_char32_t:
1917
Chris Lattneracd58a32006-08-06 17:24:14 +00001918 case tok::kw_int:
1919 case tok::kw_float:
1920 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001921 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00001922 case tok::kw__Bool:
1923 case tok::kw__Decimal32:
1924 case tok::kw__Decimal64:
1925 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00001926
Chris Lattner861a2262008-04-13 18:59:07 +00001927 // struct-or-union-specifier (C99) or class-specifier (C++)
1928 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00001929 case tok::kw_struct:
1930 case tok::kw_union:
1931 // enum-specifier
1932 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00001933
Chris Lattneracd58a32006-08-06 17:24:14 +00001934 // type-qualifier
1935 case tok::kw_const:
1936 case tok::kw_volatile:
1937 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00001938
Chris Lattneracd58a32006-08-06 17:24:14 +00001939 // function-specifier
1940 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00001941 case tok::kw_virtual:
1942 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001943
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001944 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001945 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001946
Chris Lattner599e47e2007-08-09 17:01:07 +00001947 // GNU typeof support.
1948 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00001949
Chris Lattner599e47e2007-08-09 17:01:07 +00001950 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001951 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00001952 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001953
Chris Lattner8b2ec162008-07-26 03:38:44 +00001954 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1955 case tok::less:
1956 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00001957
Steve Narofff192fab2009-01-06 19:34:12 +00001958 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00001959 case tok::kw___cdecl:
1960 case tok::kw___stdcall:
1961 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001962 case tok::kw___w64:
1963 case tok::kw___ptr64:
1964 case tok::kw___forceinline:
1965 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001966 }
1967}
1968
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001969
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001970/// ParseTypeQualifierListOpt
1971/// type-qualifier-list: [C99 6.7.5]
1972/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00001973/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001974/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00001975/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001976///
Chris Lattnercf0bab22008-12-18 07:02:59 +00001977void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001978 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001979 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001980 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001981 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001982 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001983
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001984 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001985 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001986 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
1987 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001988 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001989 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001990 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1991 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001992 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001993 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001994 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1995 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001996 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001997 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001998 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001999 case tok::kw___cdecl:
2000 case tok::kw___stdcall:
2001 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00002002 if (AttributesAllowed) {
2003 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2004 continue;
2005 }
2006 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002007 case tok::kw___attribute:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002008 if (AttributesAllowed) {
2009 DS.AddAttributes(ParseAttributes());
2010 continue; // do *not* consume the next token!
2011 }
2012 // otherwise, FALL THROUGH!
2013 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002014 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002015 // If this is not a type-qualifier token, we're done reading type
2016 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002017 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002018 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002019 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002020
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002021 // If the specifier combination wasn't legal, issue a diagnostic.
2022 if (isInvalid) {
2023 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002024 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002025 }
2026 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002027 }
2028}
2029
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002030
2031/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2032///
2033void Parser::ParseDeclarator(Declarator &D) {
2034 /// This implements the 'declarator' production in the C grammar, then checks
2035 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002036 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002037}
2038
Sebastian Redlbd150f42008-11-21 19:14:01 +00002039/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2040/// is parsed by the function passed to it. Pass null, and the direct-declarator
2041/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002042/// ptr-operator production.
2043///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002044/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2045/// [C] pointer[opt] direct-declarator
2046/// [C++] direct-declarator
2047/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002048///
2049/// pointer: [C99 6.7.5]
2050/// '*' type-qualifier-list[opt]
2051/// '*' type-qualifier-list[opt] pointer
2052///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002053/// ptr-operator:
2054/// '*' cv-qualifier-seq[opt]
2055/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002056/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002057/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002058/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002059/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002060void Parser::ParseDeclaratorInternal(Declarator &D,
2061 DirectDeclParseFunction DirectDeclParser) {
Bill Wendling3708c182007-05-27 10:15:43 +00002062
Douglas Gregor66a985d2009-08-26 14:27:30 +00002063 if (Diags.hasAllExtensionsSilenced())
2064 D.setExtension();
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002065 // C++ member pointers start with a '::' or a nested-name.
2066 // Member pointers get special handling, since there's no place for the
2067 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002068 if (getLang().CPlusPlus &&
2069 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2070 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002071 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00002072 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump11289f42009-09-09 15:08:12 +00002073 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002074 // The scope spec really belongs to the direct-declarator.
2075 D.getCXXScopeSpec() = SS;
2076 if (DirectDeclParser)
2077 (this->*DirectDeclParser)(D);
2078 return;
2079 }
2080
2081 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002082 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002083 DeclSpec DS;
2084 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002085 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002086
2087 // Recurse to parse whatever is left.
2088 ParseDeclaratorInternal(D, DirectDeclParser);
2089
2090 // Sema will have to catch (syntactically invalid) pointers into global
2091 // scope. It has to catch pointers into namespace scope anyway.
2092 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002093 Loc, DS.TakeAttributes()),
2094 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002095 return;
2096 }
2097 }
2098
2099 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002100 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002101 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002102 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002103 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002104 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002105 if (DirectDeclParser)
2106 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002107 return;
2108 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002109
Sebastian Redled0f3b02009-03-15 22:02:01 +00002110 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2111 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002112 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002113 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002114
Chris Lattner9eac9312009-03-27 04:18:06 +00002115 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002116 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002117 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002118
Bill Wendling3708c182007-05-27 10:15:43 +00002119 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002120 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002121
Bill Wendling3708c182007-05-27 10:15:43 +00002122 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002123 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002124 if (Kind == tok::star)
2125 // Remember that we parsed a pointer type, and remember the type-quals.
2126 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002127 DS.TakeAttributes()),
2128 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002129 else
2130 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002131 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002132 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002133 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002134 } else {
2135 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002136 DeclSpec DS;
2137
Sebastian Redl3b27be62009-03-23 00:00:23 +00002138 // Complain about rvalue references in C++03, but then go on and build
2139 // the declarator.
2140 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2141 Diag(Loc, diag::err_rvalue_reference);
2142
Bill Wendling93efb222007-06-02 23:28:54 +00002143 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2144 // cv-qualifiers are introduced through the use of a typedef or of a
2145 // template type argument, in which case the cv-qualifiers are ignored.
2146 //
2147 // [GNU] Retricted references are allowed.
2148 // [GNU] Attributes on references are allowed.
2149 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002150 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002151
2152 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2153 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2154 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002155 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002156 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2157 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002158 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002159 }
Bill Wendling3708c182007-05-27 10:15:43 +00002160
2161 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002162 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002163
Douglas Gregor66583c52008-11-03 15:51:28 +00002164 if (D.getNumTypeObjects() > 0) {
2165 // C++ [dcl.ref]p4: There shall be no references to references.
2166 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2167 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002168 if (const IdentifierInfo *II = D.getIdentifier())
2169 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2170 << II;
2171 else
2172 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2173 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002174
Sebastian Redlbd150f42008-11-21 19:14:01 +00002175 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002176 // can go ahead and build the (technically ill-formed)
2177 // declarator: reference collapsing will take care of it.
2178 }
2179 }
2180
Bill Wendling3708c182007-05-27 10:15:43 +00002181 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002182 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002183 DS.TakeAttributes(),
2184 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002185 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002186 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002187}
2188
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002189/// ParseDirectDeclarator
2190/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002191/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002192/// '(' declarator ')'
2193/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002194/// [C90] direct-declarator '[' constant-expression[opt] ']'
2195/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2196/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2197/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2198/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002199/// direct-declarator '(' parameter-type-list ')'
2200/// direct-declarator '(' identifier-list[opt] ')'
2201/// [GNU] direct-declarator '(' parameter-forward-declarations
2202/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002203/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2204/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002205/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002206///
2207/// declarator-id: [C++ 8]
2208/// id-expression
2209/// '::'[opt] nested-name-specifier[opt] type-name
2210///
2211/// id-expression: [C++ 5.1]
2212/// unqualified-id
2213/// qualified-id [TODO]
2214///
2215/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002216/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002217/// operator-function-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002218/// conversion-function-id [TODO]
Mike Stump11289f42009-09-09 15:08:12 +00002219/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002220/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002221///
Chris Lattneracd58a32006-08-06 17:24:14 +00002222void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002223 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002224
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002225 if (getLang().CPlusPlus) {
2226 if (D.mayHaveIdentifier()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002227 // ParseDeclaratorInternal might already have parsed the scope.
2228 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
Mike Stump11289f42009-09-09 15:08:12 +00002229 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
Douglas Gregorb7bfe792009-09-02 22:59:36 +00002230 true);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002231 if (afterCXXScope) {
2232 // Change the declaration context for name lookup, until this function
2233 // is exited (and the declarator has been parsed).
2234 DeclScopeObj.EnterDeclaratorScope();
2235 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002236
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002237 if (Tok.is(tok::identifier)) {
2238 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssona0886932009-04-30 22:41:11 +00002239
2240 // If this identifier is the name of the current class, it's a
Mike Stump11289f42009-09-09 15:08:12 +00002241 // constructor name.
Anders Carlssona0886932009-04-30 22:41:11 +00002242 if (!D.getDeclSpec().hasTypeSpecifier() &&
2243 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
Douglas Gregordce892e2009-07-06 16:40:48 +00002244 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Anders Carlssona0886932009-04-30 22:41:11 +00002245 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregordce892e2009-07-06 16:40:48 +00002246 Tok.getLocation(), CurScope, SS),
Anders Carlssona0886932009-04-30 22:41:11 +00002247 Tok.getLocation());
2248 // This is a normal identifier.
2249 } else
2250 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002251 ConsumeToken();
2252 goto PastIdentifier;
Douglas Gregor7f741122009-02-25 19:37:18 +00002253 } else if (Tok.is(tok::annot_template_id)) {
Mike Stump11289f42009-09-09 15:08:12 +00002254 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00002255 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2256
2257 // FIXME: Could this template-id name a constructor?
2258
2259 // FIXME: This is an egregious hack, where we silently ignore
2260 // the specialization (which should be a function template
2261 // specialization name) and use the name instead. This hack
2262 // will go away when we have support for function
2263 // specializations.
2264 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2265 TemplateId->Destroy();
2266 ConsumeToken();
2267 goto PastIdentifier;
Douglas Gregor1dc98262008-12-26 15:00:45 +00002268 } else if (Tok.is(tok::kw_operator)) {
2269 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002270 SourceLocation EndLoc;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002271
Douglas Gregor1dc98262008-12-26 15:00:45 +00002272 // First try the name of an overloaded operator
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002273 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2274 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor1dc98262008-12-26 15:00:45 +00002275 } else {
2276 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002277 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2278 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2279 else {
Douglas Gregor1dc98262008-12-26 15:00:45 +00002280 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002281 }
Douglas Gregor1dc98262008-12-26 15:00:45 +00002282 }
2283 goto PastIdentifier;
2284 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002285 // This should be a C++ destructor.
2286 SourceLocation TildeLoc = ConsumeToken();
Douglas Gregor5e0962f2009-08-26 18:27:52 +00002287 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002288 // FIXME: Inaccurate.
2289 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregord54dfb82009-02-25 23:52:28 +00002290 SourceLocation EndLoc;
Douglas Gregordce892e2009-07-06 16:40:48 +00002291 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +00002292 TypeResult Type = ParseClassName(EndLoc, SS, true);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002293 if (Type.isInvalid())
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002294 D.SetIdentifier(0, TildeLoc);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002295 else
2296 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002297 } else {
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +00002298 Diag(Tok, diag::err_destructor_class_name);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002299 D.SetIdentifier(0, TildeLoc);
2300 }
2301 goto PastIdentifier;
2302 }
2303
2304 // If we reached this point, token is not identifier and not '~'.
2305
2306 if (afterCXXScope) {
2307 Diag(Tok, diag::err_expected_unqualified_id);
2308 D.SetIdentifier(0, Tok.getLocation());
2309 D.setInvalidType(true);
2310 goto PastIdentifier;
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002311 }
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002312 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002313 }
2314
2315 // If we reached this point, we are either in C/ObjC or the token didn't
2316 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002317 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2318 assert(!getLang().CPlusPlus &&
2319 "There's a C++-specific check for tok::identifier above");
2320 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2321 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2322 ConsumeToken();
2323 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002324 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002325 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002326 // Example: 'char (*X)' or 'int (*XX)(void)'
2327 ParseParenDeclarator(D);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002328 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002329 // This could be something simple like "int" (in which case the declarator
2330 // portion is empty), if an abstract-declarator is allowed.
2331 D.SetIdentifier(0, Tok.getLocation());
2332 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002333 if (D.getContext() == Declarator::MemberContext)
2334 Diag(Tok, diag::err_expected_member_name_or_semi)
2335 << D.getDeclSpec().getSourceRange();
2336 else if (getLang().CPlusPlus)
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002337 Diag(Tok, diag::err_expected_unqualified_id);
2338 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002339 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002340 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002341 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002342 }
Mike Stump11289f42009-09-09 15:08:12 +00002343
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002344 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002345 assert(D.isPastIdentifier() &&
2346 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002347
Chris Lattneracd58a32006-08-06 17:24:14 +00002348 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002349 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002350 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2351 // In such a case, check if we actually have a function declarator; if it
2352 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002353 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2354 // When not in file scope, warn for ambiguous function declarators, just
2355 // in case the author intended it as a variable definition.
2356 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2357 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2358 break;
2359 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002360 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002361 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002362 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002363 } else {
2364 break;
2365 }
2366 }
2367}
2368
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002369/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2370/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002371/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002372/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2373///
2374/// direct-declarator:
2375/// '(' declarator ')'
2376/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002377/// direct-declarator '(' parameter-type-list ')'
2378/// direct-declarator '(' identifier-list[opt] ')'
2379/// [GNU] direct-declarator '(' parameter-forward-declarations
2380/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002381///
2382void Parser::ParseParenDeclarator(Declarator &D) {
2383 SourceLocation StartLoc = ConsumeParen();
2384 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002385
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002386 // Eat any attributes before we look at whether this is a grouping or function
2387 // declarator paren. If this is a grouping paren, the attribute applies to
2388 // the type being built up, for example:
2389 // int (__attribute__(()) *x)(long y)
2390 // If this ends up not being a grouping paren, the attribute applies to the
2391 // first argument, for example:
2392 // int (__attribute__(()) int x)
2393 // In either case, we need to eat any attributes to be able to determine what
2394 // sort of paren this is.
2395 //
2396 AttributeList *AttrList = 0;
2397 bool RequiresArg = false;
2398 if (Tok.is(tok::kw___attribute)) {
2399 AttrList = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +00002400
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002401 // We require that the argument list (if this is a non-grouping paren) be
2402 // present even if the attribute list was empty.
2403 RequiresArg = true;
2404 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002405 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002406 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2407 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2408 Tok.is(tok::kw___ptr64)) {
2409 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2410 }
Mike Stump11289f42009-09-09 15:08:12 +00002411
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002412 // If we haven't past the identifier yet (or where the identifier would be
2413 // stored, if this is an abstract declarator), then this is probably just
2414 // grouping parens. However, if this could be an abstract-declarator, then
2415 // this could also be the start of function arguments (consider 'void()').
2416 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002417
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002418 if (!D.mayOmitIdentifier()) {
2419 // If this can't be an abstract-declarator, this *must* be a grouping
2420 // paren, because we haven't seen the identifier yet.
2421 isGrouping = true;
2422 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002423 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002424 isDeclarationSpecifier()) { // 'int(int)' is a function.
2425 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2426 // considered to be a type, not a K&R identifier-list.
2427 isGrouping = false;
2428 } else {
2429 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2430 isGrouping = true;
2431 }
Mike Stump11289f42009-09-09 15:08:12 +00002432
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002433 // If this is a grouping paren, handle:
2434 // direct-declarator: '(' declarator ')'
2435 // direct-declarator: '(' attributes declarator ')'
2436 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002437 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002438 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002439 if (AttrList)
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002440 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002441
Sebastian Redlbd150f42008-11-21 19:14:01 +00002442 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002443 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002444 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002445
2446 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002447 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002448 return;
2449 }
Mike Stump11289f42009-09-09 15:08:12 +00002450
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002451 // Okay, if this wasn't a grouping paren, it must be the start of a function
2452 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002453 // identifier (and remember where it would have been), then call into
2454 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002455 D.SetIdentifier(0, Tok.getLocation());
2456
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002457 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002458}
2459
2460/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2461/// declarator D up to a paren, which indicates that we are parsing function
2462/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002463///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002464/// If AttrList is non-null, then the caller parsed those arguments immediately
2465/// after the open paren - they should be considered to be the first argument of
2466/// a parameter. If RequiresArg is true, then the first argument of the
2467/// function is required to be present and required to not be an identifier
2468/// list.
2469///
Chris Lattneracd58a32006-08-06 17:24:14 +00002470/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002471/// parameter-type-list: [C99 6.7.5]
2472/// parameter-list
2473/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002474/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002475///
2476/// parameter-list: [C99 6.7.5]
2477/// parameter-declaration
2478/// parameter-list ',' parameter-declaration
2479///
2480/// parameter-declaration: [C99 6.7.5]
2481/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002482/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002483/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002484/// declaration-specifiers abstract-declarator[opt]
2485/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002486/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002487/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002488///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002489/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002490/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002491///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002492void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2493 AttributeList *AttrList,
2494 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002495 // lparen is already consumed!
2496 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002497
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002498 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002499 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002500 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002501 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002502 delete AttrList;
2503 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002504
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002505 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2506 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002507
2508 // cv-qualifier-seq[opt].
2509 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002510 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002511 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002512 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002513 llvm::SmallVector<TypeTy*, 2> Exceptions;
2514 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002515 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002516 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002517 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002518 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002519
2520 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002521 if (Tok.is(tok::kw_throw)) {
2522 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002523 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002524 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002525 hasAnyExceptionSpec);
2526 assert(Exceptions.size() == ExceptionRanges.size() &&
2527 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002528 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002529 }
2530
Chris Lattner371ed4e2008-04-06 06:57:35 +00002531 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002532 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002533 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002534 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002535 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002536 /*arglist*/ 0, 0,
2537 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002538 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002539 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002540 Exceptions.data(),
2541 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002542 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002543 LParenLoc, RParenLoc, D),
2544 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002545 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002546 }
2547
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002548 // Alternatively, this parameter list may be an identifier list form for a
2549 // K&R-style function: void foo(a,b,c)
Steve Naroffb0486722009-01-28 19:16:40 +00002550 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff3b6a4bd2009-01-30 14:23:32 +00002551 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002552 // K&R identifier lists can't have typedefs as identifiers, per
2553 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002554 if (RequiresArg) {
2555 Diag(Tok, diag::err_argument_required_after_attribute);
2556 delete AttrList;
2557 }
Steve Naroffb0486722009-01-28 19:16:40 +00002558 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2559 // normal declarators, not for abstract-declarators.
2560 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002561 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002562 }
Mike Stump11289f42009-09-09 15:08:12 +00002563
Chris Lattner371ed4e2008-04-06 06:57:35 +00002564 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00002565
Chris Lattner371ed4e2008-04-06 06:57:35 +00002566 // Build up an array of information about the parsed arguments.
2567 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002568
2569 // Enter function-declaration scope, limiting any declarators to the
2570 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00002571 ParseScope PrototypeScope(this,
2572 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00002573
Chris Lattner371ed4e2008-04-06 06:57:35 +00002574 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002575 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00002576 while (1) {
2577 if (Tok.is(tok::ellipsis)) {
2578 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002579 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002580 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00002581 }
Mike Stump11289f42009-09-09 15:08:12 +00002582
Chris Lattner371ed4e2008-04-06 06:57:35 +00002583 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002584
Chris Lattner371ed4e2008-04-06 06:57:35 +00002585 // Parse the declaration-specifiers.
2586 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002587
2588 // If the caller parsed attributes for the first argument, add them now.
2589 if (AttrList) {
2590 DS.AddAttributes(AttrList);
2591 AttrList = 0; // Only apply the attributes to the first parameter.
2592 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002593 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002594
Chris Lattner371ed4e2008-04-06 06:57:35 +00002595 // Parse the declarator. This is "PrototypeContext", because we must
2596 // accept either 'declarator' or 'abstract-declarator' here.
2597 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2598 ParseDeclarator(ParmDecl);
2599
2600 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002601 if (Tok.is(tok::kw___attribute)) {
2602 SourceLocation Loc;
2603 AttributeList *AttrList = ParseAttributes(&Loc);
2604 ParmDecl.AddAttributes(AttrList, Loc);
2605 }
Mike Stump11289f42009-09-09 15:08:12 +00002606
Chris Lattner371ed4e2008-04-06 06:57:35 +00002607 // Remember this parsed parameter in ParamInfo.
2608 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00002609
Douglas Gregor4d87df52008-12-16 21:30:33 +00002610 // DefArgToks is used when the parsing of default arguments needs
2611 // to be delayed.
2612 CachedTokens *DefArgToks = 0;
2613
Chris Lattner371ed4e2008-04-06 06:57:35 +00002614 // If no parameter was specified, verify that *something* was specified,
2615 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002616 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2617 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00002618 // Completely missing, emit error.
2619 Diag(DSStart, diag::err_missing_param);
2620 } else {
2621 // Otherwise, we have something. Add it and let semantic analysis try
2622 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00002623
Chris Lattner371ed4e2008-04-06 06:57:35 +00002624 // Inform the actions module about the parameter declarator, so it gets
2625 // added to the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002626 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002627
2628 // Parse the default argument, if any. We parse the default
2629 // arguments in all dialects; the semantic analysis in
2630 // ActOnParamDefaultArgument will reject the default argument in
2631 // C.
2632 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00002633 SourceLocation EqualLoc = Tok.getLocation();
2634
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002635 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00002636 if (D.getContext() == Declarator::MemberContext) {
2637 // If we're inside a class definition, cache the tokens
2638 // corresponding to the default argument. We'll actually parse
2639 // them when we see the end of the class definition.
2640 // FIXME: Templates will require something similar.
2641 // FIXME: Can we use a smart pointer for Toks?
2642 DefArgToks = new CachedTokens;
2643
Mike Stump11289f42009-09-09 15:08:12 +00002644 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002645 tok::semi, false)) {
2646 delete DefArgToks;
2647 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00002648 Actions.ActOnParamDefaultArgumentError(Param);
2649 } else
Mike Stump11289f42009-09-09 15:08:12 +00002650 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00002651 (*DefArgToks)[1].getLocation());
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002652 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00002653 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00002654 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002655
Douglas Gregor4d87df52008-12-16 21:30:33 +00002656 OwningExprResult DefArgResult(ParseAssignmentExpression());
2657 if (DefArgResult.isInvalid()) {
2658 Actions.ActOnParamDefaultArgumentError(Param);
2659 SkipUntil(tok::comma, tok::r_paren, true, true);
2660 } else {
2661 // Inform the actions module about the default argument
2662 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002663 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00002664 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002665 }
2666 }
Mike Stump11289f42009-09-09 15:08:12 +00002667
2668 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2669 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002670 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00002671 }
2672
2673 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002674 if (Tok.isNot(tok::comma)) {
2675 if (Tok.is(tok::ellipsis)) {
2676 IsVariadic = true;
2677 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2678
2679 if (!getLang().CPlusPlus) {
2680 // We have ellipsis without a preceding ',', which is ill-formed
2681 // in C. Complain and provide the fix.
2682 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2683 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2684 }
2685 }
2686
2687 break;
2688 }
Mike Stump11289f42009-09-09 15:08:12 +00002689
Chris Lattner371ed4e2008-04-06 06:57:35 +00002690 // Consume the comma.
2691 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00002692 }
Mike Stump11289f42009-09-09 15:08:12 +00002693
Chris Lattner371ed4e2008-04-06 06:57:35 +00002694 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002695 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00002696
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002697 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002698 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2699 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002700
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002701 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002702 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002703 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002704 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002705 llvm::SmallVector<TypeTy*, 2> Exceptions;
2706 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002707 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002708 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00002709 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002710 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002711 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002712
2713 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002714 if (Tok.is(tok::kw_throw)) {
2715 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002716 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002717 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002718 hasAnyExceptionSpec);
2719 assert(Exceptions.size() == ExceptionRanges.size() &&
2720 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002721 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002722 }
2723
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002724 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002725 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002726 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002727 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002728 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002729 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002730 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002731 Exceptions.data(),
2732 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002733 Exceptions.size(),
2734 LParenLoc, RParenLoc, D),
2735 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002736}
Chris Lattneracd58a32006-08-06 17:24:14 +00002737
Chris Lattner6c940e62008-04-06 06:34:08 +00002738/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2739/// we found a K&R-style identifier list instead of a type argument list. The
2740/// current token is known to be the first identifier in the list.
2741///
2742/// identifier-list: [C99 6.7.5]
2743/// identifier
2744/// identifier-list ',' identifier
2745///
2746void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2747 Declarator &D) {
2748 // Build up an array of information about the parsed arguments.
2749 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2750 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00002751
Chris Lattner6c940e62008-04-06 06:34:08 +00002752 // If there was no identifier specified for the declarator, either we are in
2753 // an abstract-declarator, or we are in a parameter declarator which was found
2754 // to be abstract. In abstract-declarators, identifier lists are not valid:
2755 // diagnose this.
2756 if (!D.getIdentifier())
2757 Diag(Tok, diag::ext_ident_list_in_param);
2758
2759 // Tok is known to be the first identifier in the list. Remember this
2760 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00002761 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00002762 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner83f095c2009-03-28 19:18:32 +00002763 Tok.getLocation(),
2764 DeclPtrTy()));
Mike Stump11289f42009-09-09 15:08:12 +00002765
Chris Lattner9186f552008-04-06 06:39:19 +00002766 ConsumeToken(); // eat the first identifier.
Mike Stump11289f42009-09-09 15:08:12 +00002767
Chris Lattner6c940e62008-04-06 06:34:08 +00002768 while (Tok.is(tok::comma)) {
2769 // Eat the comma.
2770 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002771
Chris Lattner9186f552008-04-06 06:39:19 +00002772 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00002773 if (Tok.isNot(tok::identifier)) {
2774 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00002775 SkipUntil(tok::r_paren);
2776 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00002777 }
Chris Lattner67b450c2008-04-06 06:47:48 +00002778
Chris Lattner6c940e62008-04-06 06:34:08 +00002779 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00002780
2781 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00002782 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerebad6a22008-11-19 07:37:42 +00002783 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00002784
Chris Lattner6c940e62008-04-06 06:34:08 +00002785 // Verify that the argument identifier has not already been mentioned.
2786 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002787 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00002788 } else {
2789 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00002790 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00002791 Tok.getLocation(),
2792 DeclPtrTy()));
Chris Lattner9186f552008-04-06 06:39:19 +00002793 }
Mike Stump11289f42009-09-09 15:08:12 +00002794
Chris Lattner6c940e62008-04-06 06:34:08 +00002795 // Eat the identifier.
2796 ConsumeToken();
2797 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002798
2799 // If we have the closing ')', eat it and we're done.
2800 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2801
Chris Lattner9186f552008-04-06 06:39:19 +00002802 // Remember that we parsed a function type, and remember the attributes. This
2803 // function type is always a K&R style function type, which is not varargs and
2804 // has no prototype.
2805 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002806 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00002807 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002808 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002809 /*exception*/false,
2810 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002811 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002812 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00002813}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002814
Chris Lattnere8074e62006-08-06 18:30:15 +00002815/// [C90] direct-declarator '[' constant-expression[opt] ']'
2816/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2817/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2818/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2819/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2820void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00002821 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00002822
Chris Lattner84a11622008-12-18 07:27:21 +00002823 // C array syntax has many features, but by-far the most common is [] and [4].
2824 // This code does a fast path to handle some of the most obvious cases.
2825 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002826 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002827 // Remember that we parsed the empty array type.
2828 OwningExprResult NumElements(Actions);
Douglas Gregor04318252009-07-06 15:59:29 +00002829 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2830 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002831 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002832 return;
2833 } else if (Tok.getKind() == tok::numeric_constant &&
2834 GetLookAheadToken(1).is(tok::r_square)) {
2835 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00002836 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00002837 ConsumeToken();
2838
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002839 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002840
2841 // If there was an error parsing the assignment-expression, recover.
2842 if (ExprRes.isInvalid())
2843 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00002844
Chris Lattner84a11622008-12-18 07:27:21 +00002845 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00002846 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2847 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002848 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002849 return;
2850 }
Mike Stump11289f42009-09-09 15:08:12 +00002851
Chris Lattnere8074e62006-08-06 18:30:15 +00002852 // If valid, this location is the position where we read the 'static' keyword.
2853 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00002854 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002855 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002856
Chris Lattnere8074e62006-08-06 18:30:15 +00002857 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002858 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00002859 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00002860 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00002861
Chris Lattnere8074e62006-08-06 18:30:15 +00002862 // If we haven't already read 'static', check to see if there is one after the
2863 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002864 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002865 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002866
Chris Lattnere8074e62006-08-06 18:30:15 +00002867 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00002868 bool isStar = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +00002869 OwningExprResult NumElements(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00002870
Chris Lattner521ff2b2008-04-06 05:26:30 +00002871 // Handle the case where we have '[*]' as the array size. However, a leading
2872 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2873 // the the token after the star is a ']'. Since stars in arrays are
2874 // infrequent, use of lookahead is not costly here.
2875 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00002876 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00002877
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002878 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00002879 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002880 StaticLoc = SourceLocation(); // Drop the static.
2881 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00002882 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00002883 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00002884 // Note, in C89, this production uses the constant-expr production instead
2885 // of assignment-expr. The only difference is that assignment-expr allows
2886 // things like '=' and '*='. Sema rejects these in C89 mode because they
2887 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00002888
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00002889 // Parse the constant-expression or assignment-expression now (depending
2890 // on dialect).
2891 if (getLang().CPlusPlus)
2892 NumElements = ParseConstantExpression();
2893 else
2894 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00002895 }
Mike Stump11289f42009-09-09 15:08:12 +00002896
Chris Lattner62591722006-08-12 18:40:58 +00002897 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002898 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00002899 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00002900 // If the expression was invalid, skip it.
2901 SkipUntil(tok::r_square);
2902 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00002903 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002904
2905 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2906
Chris Lattner84a11622008-12-18 07:27:21 +00002907 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00002908 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2909 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00002910 NumElements.release(),
2911 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002912 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00002913}
2914
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002915/// [GNU] typeof-specifier:
2916/// typeof ( expressions )
2917/// typeof ( type-name )
2918/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00002919///
2920void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00002921 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002922 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00002923 SourceLocation StartLoc = ConsumeToken();
2924
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002925 bool isCastExpr;
2926 TypeTy *CastTy;
2927 SourceRange CastRange;
2928 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2929 isCastExpr,
2930 CastTy,
2931 CastRange);
2932
2933 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002934 // FIXME: Not accurate, the range gets one token more than it should.
2935 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002936 else
2937 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00002938
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002939 if (isCastExpr) {
2940 if (!CastTy) {
2941 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002942 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00002943 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002944
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002945 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002946 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002947 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2948 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002949 DiagID, CastTy))
2950 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002951 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002952 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002953
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002954 // If we get here, the operand to the typeof was an expresion.
2955 if (Operand.isInvalid()) {
2956 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00002957 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00002958 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002959
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002960 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002961 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002962 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2963 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002964 DiagID, Operand.release()))
2965 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00002966}