blob: d885558e1f383ed1b142d7886a6cf4b67ef0ace7 [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
Douglas Gregorc5790df2009-09-28 07:26:33 +0000781 if (Next.is(tok::annot_typename)) {
782 // FIXME: is this scope-specifier getting dropped?
783 ConsumeToken(); // the scope-specifier
784 if (Tok.getAnnotationValue())
785 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
786 PrevSpec, DiagID,
787 Tok.getAnnotationValue());
788 else
789 DS.SetTypeSpecError();
790 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
791 ConsumeToken(); // The typename
792 }
793
Douglas Gregor167fa622009-03-25 15:40:00 +0000794 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000795 goto DoneWithDeclSpec;
796
797 CXXScopeSpec SS;
Douglas Gregorc23500e2009-03-26 23:56:24 +0000798 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000799 SS.setRange(Tok.getAnnotationRange());
800
801 // If the next token is the name of the class type that the C++ scope
802 // denotes, followed by a '(', then this is a constructor declaration.
803 // We're done with the decl-specifiers.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000804 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000805 CurScope, &SS) &&
806 GetLookAheadToken(2).is(tok::l_paren))
807 goto DoneWithDeclSpec;
808
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000809 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
810 Next.getLocation(), CurScope, &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000811
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000812 // If the referenced identifier is not a type, then this declspec is
813 // erroneous: We already checked about that it has no type specifier, and
814 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +0000815 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000816 if (TypeRep == 0) {
817 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000818 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000819 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000820 }
Mike Stump11289f42009-09-09 15:08:12 +0000821
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000822 ConsumeToken(); // The C++ scope.
823
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000824 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000825 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000826 if (isInvalid)
827 break;
Mike Stump11289f42009-09-09 15:08:12 +0000828
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000829 DS.SetRangeEnd(Tok.getLocation());
830 ConsumeToken(); // The typename.
831
832 continue;
833 }
Mike Stump11289f42009-09-09 15:08:12 +0000834
Chris Lattnere387d9e2009-01-21 19:48:37 +0000835 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000836 if (Tok.getAnnotationValue())
837 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000838 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000839 else
840 DS.SetTypeSpecError();
Chris Lattnere387d9e2009-01-21 19:48:37 +0000841 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
842 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +0000843
Chris Lattnere387d9e2009-01-21 19:48:37 +0000844 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
845 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
846 // Objective-C interface. If we don't have Objective-C or a '<', this is
847 // just a normal reference to a typedef name.
848 if (!Tok.is(tok::less) || !getLang().ObjC1)
849 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000850
Chris Lattnere387d9e2009-01-21 19:48:37 +0000851 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000852 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere387d9e2009-01-21 19:48:37 +0000853 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek58d81902009-06-30 22:19:00 +0000854 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Mike Stump11289f42009-09-09 15:08:12 +0000855
Chris Lattnere387d9e2009-01-21 19:48:37 +0000856 DS.SetRangeEnd(EndProtoLoc);
857 continue;
858 }
Mike Stump11289f42009-09-09 15:08:12 +0000859
Chris Lattner16fac4f2008-07-26 01:18:38 +0000860 // typedef-name
861 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000862 // In C++, check to see if this is a scope specifier like foo::bar::, if
863 // so handle it as such. This is important for ctor parsing.
Douglas Gregore861bac2009-08-25 22:51:20 +0000864 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner78ecd4f2009-01-21 19:19:26 +0000865 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000866
Chris Lattner16fac4f2008-07-26 01:18:38 +0000867 // This identifier can only be a typedef name if we haven't already seen
868 // a type-specifier. Without this check we misparse:
869 // typedef int X; struct Y { short X; }; as 'short int'.
870 if (DS.hasTypeSpecifier())
871 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +0000872
Chris Lattner16fac4f2008-07-26 01:18:38 +0000873 // It has to be available as a typedef too!
Mike Stump11289f42009-09-09 15:08:12 +0000874 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000875 Tok.getLocation(), CurScope);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000876
Chris Lattner6cc055a2009-04-12 20:42:31 +0000877 // If this is not a typedef name, don't parse it as part of the declspec,
878 // it must be an implicit int or an error.
879 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000880 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000881 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +0000882 }
Douglas Gregor8bf42052009-02-09 18:46:07 +0000883
Douglas Gregor61956c42008-10-31 09:07:45 +0000884 // C++: If the identifier is actually the name of the class type
885 // being defined and the next token is a '(', then this is a
886 // constructor declaration. We're done with the decl-specifiers
887 // and will treat this token as an identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000888 if (getLang().CPlusPlus &&
889 (CurScope->isClassScope() ||
890 (CurScope->isTemplateParamScope() &&
Douglas Gregor5ed5ae42009-08-21 18:42:58 +0000891 CurScope->getParent()->isClassScope())) &&
Mike Stump11289f42009-09-09 15:08:12 +0000892 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor61956c42008-10-31 09:07:45 +0000893 NextToken().getKind() == tok::l_paren)
894 goto DoneWithDeclSpec;
895
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000896 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000897 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +0000898 if (isInvalid)
899 break;
Mike Stump11289f42009-09-09 15:08:12 +0000900
Chris Lattner16fac4f2008-07-26 01:18:38 +0000901 DS.SetRangeEnd(Tok.getLocation());
902 ConsumeToken(); // The identifier
903
904 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
905 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
906 // Objective-C interface. If we don't have Objective-C or a '<', this is
907 // just a normal reference to a typedef name.
908 if (!Tok.is(tok::less) || !getLang().ObjC1)
909 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000910
Chris Lattner16fac4f2008-07-26 01:18:38 +0000911 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000912 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000913 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek58d81902009-06-30 22:19:00 +0000914 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Mike Stump11289f42009-09-09 15:08:12 +0000915
Chris Lattner16fac4f2008-07-26 01:18:38 +0000916 DS.SetRangeEnd(EndProtoLoc);
917
Steve Naroffcd5e7822008-09-22 10:28:57 +0000918 // Need to support trailing type qualifiers (e.g. "id<p> const").
919 // If a type specifier follows, it will be diagnosed elsewhere.
920 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000921 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000922
923 // type-name
924 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +0000925 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000926 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000927 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000928 // This template-id does not refer to a type name, so we're
929 // done with the type-specifiers.
930 goto DoneWithDeclSpec;
931 }
932
933 // Turn the template-id annotation token into a type annotation
934 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000935 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +0000936 continue;
937 }
938
Chris Lattnere37e2332006-08-15 04:50:22 +0000939 // GNU attributes support.
940 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000941 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000942 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000943
944 // Microsoft declspec support.
945 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +0000946 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000947 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000948
Steve Naroff44ac7772008-12-25 14:16:32 +0000949 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +0000950 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +0000951 // FIXME: Add handling here!
952 break;
953
954 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +0000955 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +0000956 case tok::kw___cdecl:
957 case tok::kw___stdcall:
958 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +0000959 DS.AddAttributes(ParseMicrosoftTypeAttributes());
960 continue;
961
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000962 // storage-class-specifier
963 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +0000964 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
965 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000966 break;
967 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000968 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +0000969 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +0000970 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
971 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000972 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000973 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000974 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +0000975 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000976 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000977 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000978 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +0000979 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +0000980 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
981 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000982 break;
983 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +0000984 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +0000985 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
986 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +0000987 else
John McCall49bfce42009-08-03 20:12:06 +0000988 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
989 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000990 break;
991 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +0000992 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
993 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000994 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000995 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +0000996 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
997 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000998 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000999 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001000 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001001 break;
Mike Stump11289f42009-09-09 15:08:12 +00001002
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001003 // function-specifier
1004 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001005 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001006 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001007 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001008 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001009 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001010 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001011 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001012 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001013
Anders Carlssoncd8db412009-05-06 04:46:28 +00001014 // friend
1015 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001016 if (DSContext == DSC_class)
1017 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1018 else {
1019 PrevSpec = ""; // not actually used by the diagnostic
1020 DiagID = diag::err_friend_invalid_in_context;
1021 isInvalid = true;
1022 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001023 break;
Mike Stump11289f42009-09-09 15:08:12 +00001024
Chris Lattnere387d9e2009-01-21 19:48:37 +00001025 // type-specifier
1026 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001027 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1028 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001029 break;
1030 case tok::kw_long:
1031 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001032 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1033 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001034 else
John McCall49bfce42009-08-03 20:12:06 +00001035 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1036 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001037 break;
1038 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001039 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1040 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001041 break;
1042 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001043 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1044 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001045 break;
1046 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001047 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1048 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001049 break;
1050 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001051 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1052 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001053 break;
1054 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001055 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1056 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001057 break;
1058 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001059 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1060 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001061 break;
1062 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001063 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1064 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001065 break;
1066 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001067 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1068 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001069 break;
1070 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001071 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1072 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001073 break;
1074 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001075 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1076 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001077 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001078 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001079 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1080 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001081 break;
1082 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001083 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1084 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001085 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001086 case tok::kw_bool:
1087 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001088 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1089 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001090 break;
1091 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001092 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1093 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001094 break;
1095 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001096 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1097 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001098 break;
1099 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001100 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1101 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001102 break;
1103
1104 // class-specifier:
1105 case tok::kw_class:
1106 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001107 case tok::kw_union: {
1108 tok::TokenKind Kind = Tok.getKind();
1109 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001110 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001111 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001112 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001113
1114 // enum-specifier:
1115 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001116 ConsumeToken();
1117 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001118 continue;
1119
1120 // cv-qualifier:
1121 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001122 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1123 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001124 break;
1125 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001126 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1127 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001128 break;
1129 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001130 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1131 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001132 break;
1133
Douglas Gregor333489b2009-03-27 23:10:48 +00001134 // C++ typename-specifier:
1135 case tok::kw_typename:
1136 if (TryAnnotateTypeOrScopeToken())
1137 continue;
1138 break;
1139
Chris Lattnere387d9e2009-01-21 19:48:37 +00001140 // GNU typeof support.
1141 case tok::kw_typeof:
1142 ParseTypeofSpecifier(DS);
1143 continue;
1144
Anders Carlsson74948d02009-06-24 17:47:40 +00001145 case tok::kw_decltype:
1146 ParseDecltypeSpecifier(DS);
1147 continue;
1148
Steve Naroffcfdf6162008-06-05 00:02:44 +00001149 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001150 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001151 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1152 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001153 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001154 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001155
Chris Lattner0974b232008-07-26 00:20:22 +00001156 {
1157 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001158 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +00001159 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek58d81902009-06-30 22:19:00 +00001160 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +00001161 DS.SetRangeEnd(EndProtoLoc);
1162
Chris Lattner6d29c102008-11-18 07:48:38 +00001163 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner3a4e4312009-04-03 18:38:42 +00001164 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001165 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001166 // Need to support trailing type qualifiers (e.g. "id<p> const").
1167 // If a type specifier follows, it will be diagnosed elsewhere.
1168 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001169 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001170 }
John McCall49bfce42009-08-03 20:12:06 +00001171 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001172 if (isInvalid) {
1173 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001174 assert(DiagID);
Chris Lattner6d29c102008-11-18 07:48:38 +00001175 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001176 }
Chris Lattner2e232092008-03-13 06:29:04 +00001177 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001178 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001179 }
1180}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001181
Chris Lattnera448d752009-01-06 06:59:53 +00001182/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001183/// primarily follow the C++ grammar with additions for C99 and GNU,
1184/// which together subsume the C grammar. Note that the C++
1185/// type-specifier also includes the C type-qualifier (for const,
1186/// volatile, and C99 restrict). Returns true if a type-specifier was
1187/// found (and parsed), false otherwise.
1188///
1189/// type-specifier: [C++ 7.1.5]
1190/// simple-type-specifier
1191/// class-specifier
1192/// enum-specifier
1193/// elaborated-type-specifier [TODO]
1194/// cv-qualifier
1195///
1196/// cv-qualifier: [C++ 7.1.5.1]
1197/// 'const'
1198/// 'volatile'
1199/// [C99] 'restrict'
1200///
1201/// simple-type-specifier: [ C++ 7.1.5.2]
1202/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1203/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1204/// 'char'
1205/// 'wchar_t'
1206/// 'bool'
1207/// 'short'
1208/// 'int'
1209/// 'long'
1210/// 'signed'
1211/// 'unsigned'
1212/// 'float'
1213/// 'double'
1214/// 'void'
1215/// [C99] '_Bool'
1216/// [C99] '_Complex'
1217/// [C99] '_Imaginary' // Removed in TC2?
1218/// [GNU] '_Decimal32'
1219/// [GNU] '_Decimal64'
1220/// [GNU] '_Decimal128'
1221/// [GNU] typeof-specifier
1222/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1223/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001224/// [C++0x] 'decltype' ( expression )
John McCall49bfce42009-08-03 20:12:06 +00001225bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001226 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001227 unsigned &DiagID,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001228 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001229 SourceLocation Loc = Tok.getLocation();
1230
1231 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001232 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001233 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001234 // Annotate typenames and C++ scope specifiers. If we get one, just
1235 // recurse to handle whatever we get.
1236 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001237 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1238 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001239 // Otherwise, not a type specifier.
1240 return false;
1241 case tok::coloncolon: // ::foo::bar
1242 if (NextToken().is(tok::kw_new) || // ::new
1243 NextToken().is(tok::kw_delete)) // ::delete
1244 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001245
Chris Lattner020bab92009-01-04 23:41:41 +00001246 // Annotate typenames and C++ scope specifiers. If we get one, just
1247 // recurse to handle whatever we get.
1248 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001249 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1250 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001251 // Otherwise, not a type specifier.
1252 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001253
Douglas Gregor450c75a2008-11-07 15:42:26 +00001254 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001255 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001256 if (Tok.getAnnotationValue())
1257 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001258 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001259 else
1260 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001261 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1262 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001263
Douglas Gregor450c75a2008-11-07 15:42:26 +00001264 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1265 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1266 // Objective-C interface. If we don't have Objective-C or a '<', this is
1267 // just a normal reference to a typedef name.
1268 if (!Tok.is(tok::less) || !getLang().ObjC1)
1269 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001270
Douglas Gregor450c75a2008-11-07 15:42:26 +00001271 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001272 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001273 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek58d81902009-06-30 22:19:00 +00001274 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Mike Stump11289f42009-09-09 15:08:12 +00001275
Douglas Gregor450c75a2008-11-07 15:42:26 +00001276 DS.SetRangeEnd(EndProtoLoc);
1277 return true;
1278 }
1279
1280 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001281 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001282 break;
1283 case tok::kw_long:
1284 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001285 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1286 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001287 else
John McCall49bfce42009-08-03 20:12:06 +00001288 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1289 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001290 break;
1291 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001292 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001293 break;
1294 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001295 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1296 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001297 break;
1298 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001299 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1300 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001301 break;
1302 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001303 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1304 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001305 break;
1306 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001307 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001308 break;
1309 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001310 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001311 break;
1312 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001313 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001314 break;
1315 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001316 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001317 break;
1318 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001319 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001320 break;
1321 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001322 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001323 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001324 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001325 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001326 break;
1327 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001328 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001329 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001330 case tok::kw_bool:
1331 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001333 break;
1334 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001335 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1336 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001337 break;
1338 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001339 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1340 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001341 break;
1342 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001343 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1344 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001345 break;
1346
1347 // class-specifier:
1348 case tok::kw_class:
1349 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001350 case tok::kw_union: {
1351 tok::TokenKind Kind = Tok.getKind();
1352 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001353 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001354 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001355 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001356
1357 // enum-specifier:
1358 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001359 ConsumeToken();
1360 ParseEnumSpecifier(Loc, DS);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001361 return true;
1362
1363 // cv-qualifier:
1364 case tok::kw_const:
1365 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001366 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001367 break;
1368 case tok::kw_volatile:
1369 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001370 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001371 break;
1372 case tok::kw_restrict:
1373 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001374 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001375 break;
1376
1377 // GNU typeof support.
1378 case tok::kw_typeof:
1379 ParseTypeofSpecifier(DS);
1380 return true;
1381
Anders Carlsson74948d02009-06-24 17:47:40 +00001382 // C++0x decltype support.
1383 case tok::kw_decltype:
1384 ParseDecltypeSpecifier(DS);
1385 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001386
Anders Carlssonbae27372009-06-26 23:44:14 +00001387 // C++0x auto support.
1388 case tok::kw_auto:
1389 if (!getLang().CPlusPlus0x)
1390 return false;
1391
John McCall49bfce42009-08-03 20:12:06 +00001392 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001393 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001394 case tok::kw___ptr64:
1395 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001396 case tok::kw___cdecl:
1397 case tok::kw___stdcall:
1398 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001399 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001400 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001401
Douglas Gregor450c75a2008-11-07 15:42:26 +00001402 default:
1403 // Not a type-specifier; do nothing.
1404 return false;
1405 }
1406
1407 // If the specifier combination wasn't legal, issue a diagnostic.
1408 if (isInvalid) {
1409 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001410 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001411 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001412 }
1413 DS.SetRangeEnd(Tok.getLocation());
1414 ConsumeToken(); // whatever we parsed above.
1415 return true;
1416}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001417
Chris Lattner70ae4912007-10-29 04:42:53 +00001418/// ParseStructDeclaration - Parse a struct declaration without the terminating
1419/// semicolon.
1420///
Chris Lattner90a26b02007-01-23 04:38:16 +00001421/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001422/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001423/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001424/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001425/// struct-declarator-list:
1426/// struct-declarator
1427/// struct-declarator-list ',' struct-declarator
1428/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1429/// struct-declarator:
1430/// declarator
1431/// [GNU] declarator attributes[opt]
1432/// declarator[opt] ':' constant-expression
1433/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1434///
Chris Lattnera12405b2008-04-10 06:46:29 +00001435void Parser::
1436ParseStructDeclaration(DeclSpec &DS,
1437 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001438 if (Tok.is(tok::kw___extension__)) {
1439 // __extension__ silences extension warnings in the subexpression.
1440 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001441 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001442 return ParseStructDeclaration(DS, Fields);
1443 }
Mike Stump11289f42009-09-09 15:08:12 +00001444
Steve Naroff97170802007-08-20 22:28:22 +00001445 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001446 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001447 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001448
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001449 // If there are no declarators, this is a free-standing declaration
1450 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001451 if (Tok.is(tok::semi)) {
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001452 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001453 return;
1454 }
1455
1456 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +00001457 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +00001458 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +00001459 FieldDeclarator &DeclaratorInfo = Fields.back();
Mike Stump11289f42009-09-09 15:08:12 +00001460
Steve Naroff97170802007-08-20 22:28:22 +00001461 /// struct-declarator: declarator
1462 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +00001463 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +00001464 ParseDeclarator(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00001465
Chris Lattner76c72282007-10-09 17:33:22 +00001466 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001467 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001468 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001469 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001470 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001471 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001472 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001473 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001474
Steve Naroff97170802007-08-20 22:28:22 +00001475 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001476 if (Tok.is(tok::kw___attribute)) {
1477 SourceLocation Loc;
1478 AttributeList *AttrList = ParseAttributes(&Loc);
1479 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1480 }
1481
Steve Naroff97170802007-08-20 22:28:22 +00001482 // If we don't have a comma, it is either the end of the list (a ';')
1483 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001484 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001485 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001486
Steve Naroff97170802007-08-20 22:28:22 +00001487 // Consume the comma.
1488 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001489
Steve Naroff97170802007-08-20 22:28:22 +00001490 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +00001491 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001492
Steve Naroff97170802007-08-20 22:28:22 +00001493 // Attributes are only allowed on the second declarator.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001494 if (Tok.is(tok::kw___attribute)) {
1495 SourceLocation Loc;
1496 AttributeList *AttrList = ParseAttributes(&Loc);
1497 Fields.back().D.AddAttributes(AttrList, Loc);
1498 }
Steve Naroff97170802007-08-20 22:28:22 +00001499 }
Steve Naroff97170802007-08-20 22:28:22 +00001500}
1501
1502/// ParseStructUnionBody
1503/// struct-contents:
1504/// struct-declaration-list
1505/// [EXT] empty
1506/// [GNU] "struct-declaration-list" without terminatoring ';'
1507/// struct-declaration-list:
1508/// struct-declaration
1509/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001510/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001511///
Chris Lattner1300fb92007-01-23 23:42:53 +00001512void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001513 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001514 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1515 PP.getSourceManager(),
1516 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001517
Chris Lattner90a26b02007-01-23 04:38:16 +00001518 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001519
Douglas Gregor658b9552009-01-09 22:42:13 +00001520 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001521 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1522
Chris Lattner7b9ace62007-01-23 20:11:08 +00001523 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1524 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001525 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001526 Diag(Tok, diag::ext_empty_struct_union_enum)
1527 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001528
Chris Lattner83f095c2009-03-28 19:18:32 +00001529 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001530 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1531
Chris Lattner7b9ace62007-01-23 20:11:08 +00001532 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001533 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001534 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001535
Chris Lattner736ed5d2007-06-09 05:59:07 +00001536 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001537 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001538 Diag(Tok, diag::ext_extra_struct_semi)
1539 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner36e46a22007-06-09 05:49:55 +00001540 ConsumeToken();
1541 continue;
1542 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001543
1544 // Parse all the comma separated declarators.
1545 DeclSpec DS;
1546 FieldDeclarators.clear();
Chris Lattner535b8302008-06-21 19:39:06 +00001547 if (!Tok.is(tok::at)) {
1548 ParseStructDeclaration(DS, FieldDeclarators);
Mike Stump11289f42009-09-09 15:08:12 +00001549
Chris Lattner535b8302008-06-21 19:39:06 +00001550 // Convert them all to fields.
1551 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1552 FieldDeclarator &FD = FieldDeclarators[i];
Douglas Gregor66a985d2009-08-26 14:27:30 +00001553 DeclPtrTy Field;
Chris Lattner535b8302008-06-21 19:39:06 +00001554 // Install the declarator into the current TagDecl.
Douglas Gregor66a985d2009-08-26 14:27:30 +00001555 if (FD.D.getExtension()) {
1556 // Silences extension warnings
1557 ExtensionRAIIObject O(Diags);
1558 Field = Actions.ActOnField(CurScope, TagDecl,
1559 DS.getSourceRange().getBegin(),
1560 FD.D, FD.BitfieldSize);
1561 } else {
1562 Field = Actions.ActOnField(CurScope, TagDecl,
1563 DS.getSourceRange().getBegin(),
1564 FD.D, FD.BitfieldSize);
1565 }
Chris Lattner535b8302008-06-21 19:39:06 +00001566 FieldDecls.push_back(Field);
1567 }
1568 } else { // Handle @defs
1569 ConsumeToken();
1570 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1571 Diag(Tok, diag::err_unexpected_at);
1572 SkipUntil(tok::semi, true, true);
1573 continue;
1574 }
1575 ConsumeToken();
1576 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1577 if (!Tok.is(tok::identifier)) {
1578 Diag(Tok, diag::err_expected_ident);
1579 SkipUntil(tok::semi, true, true);
1580 continue;
1581 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001582 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump11289f42009-09-09 15:08:12 +00001583 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001584 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001585 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1586 ConsumeToken();
1587 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001588 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001589
Chris Lattner76c72282007-10-09 17:33:22 +00001590 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001591 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001592 } else if (Tok.is(tok::r_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001593 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001594 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001595 } else {
1596 Diag(Tok, diag::err_expected_semi_decl_list);
1597 // Skip to end of block or statement
1598 SkipUntil(tok::r_brace, true, true);
1599 }
1600 }
Mike Stump11289f42009-09-09 15:08:12 +00001601
Steve Naroff33a1e802007-10-29 21:38:07 +00001602 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001603
Steve Naroffb8371e12007-06-09 03:39:29 +00001604 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +00001605 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001606 if (Tok.is(tok::kw___attribute))
Daniel Dunbare4ac7a42008-10-03 16:42:10 +00001607 AttrList = ParseAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +00001608
1609 Actions.ActOnFields(CurScope,
Jay Foad7d0479f2009-05-21 09:52:38 +00001610 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001611 LBraceLoc, RBraceLoc,
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001612 AttrList);
1613 StructScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001614 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001615}
1616
1617
Chris Lattner3b561a32006-08-13 00:12:11 +00001618/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001619/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001620/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001621///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001622/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1623/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001624/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001625/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001626///
1627/// [C++] elaborated-type-specifier:
1628/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1629///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001630void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1631 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001632 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001633 if (Tok.is(tok::code_completion)) {
1634 // Code completion for an enum name.
1635 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1636 ConsumeToken();
1637 }
1638
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001639 AttributeList *Attr = 0;
1640 // If attributes exist after tag, parse them.
1641 if (Tok.is(tok::kw___attribute))
1642 Attr = ParseAttributes();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001643
1644 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001645 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001646 if (Tok.isNot(tok::identifier)) {
1647 Diag(Tok, diag::err_expected_ident);
1648 if (Tok.isNot(tok::l_brace)) {
1649 // Has no name and is not a definition.
1650 // Skip the rest of this declarator, up until the comma or semicolon.
1651 SkipUntil(tok::comma, true);
1652 return;
1653 }
1654 }
1655 }
Mike Stump11289f42009-09-09 15:08:12 +00001656
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001657 // Must have either 'enum name' or 'enum {...}'.
1658 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1659 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001660
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001661 // Skip the rest of this declarator, up until the comma or semicolon.
1662 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001663 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001664 }
Mike Stump11289f42009-09-09 15:08:12 +00001665
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001666 // If an identifier is present, consume and remember it.
1667 IdentifierInfo *Name = 0;
1668 SourceLocation NameLoc;
1669 if (Tok.is(tok::identifier)) {
1670 Name = Tok.getIdentifierInfo();
1671 NameLoc = ConsumeToken();
1672 }
Mike Stump11289f42009-09-09 15:08:12 +00001673
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001674 // There are three options here. If we have 'enum foo;', then this is a
1675 // forward declaration. If we have 'enum foo {...' then this is a
1676 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1677 //
1678 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1679 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1680 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1681 //
John McCall9bb74a52009-07-31 02:45:11 +00001682 Action::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001683 if (Tok.is(tok::l_brace))
John McCall9bb74a52009-07-31 02:45:11 +00001684 TUK = Action::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001685 else if (Tok.is(tok::semi))
John McCall9bb74a52009-07-31 02:45:11 +00001686 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001687 else
John McCall9bb74a52009-07-31 02:45:11 +00001688 TUK = Action::TUK_Reference;
Douglas Gregord6ab8742009-05-28 23:31:59 +00001689 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001690 bool IsDependent = false;
John McCall9bb74a52009-07-31 02:45:11 +00001691 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001692 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregor27bdf00f2009-07-23 16:36:45 +00001693 Action::MultiTemplateParamsArg(Actions),
John McCall7f41d982009-09-11 04:59:25 +00001694 Owned, IsDependent);
1695 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump11289f42009-09-09 15:08:12 +00001696
Chris Lattner76c72282007-10-09 17:33:22 +00001697 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001698 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001699
Chris Lattner3b561a32006-08-13 00:12:11 +00001700 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +00001701 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001702 unsigned DiagID;
1703 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001704 TagDecl.getAs<void>(), Owned))
John McCall49bfce42009-08-03 20:12:06 +00001705 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00001706}
1707
Chris Lattnerc1915e22007-01-25 07:29:02 +00001708/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1709/// enumerator-list:
1710/// enumerator
1711/// enumerator-list ',' enumerator
1712/// enumerator:
1713/// enumeration-constant
1714/// enumeration-constant '=' constant-expression
1715/// enumeration-constant:
1716/// identifier
1717///
Chris Lattner83f095c2009-03-28 19:18:32 +00001718void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001719 // Enter the scope of the enum body and start the definition.
1720 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001721 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00001722
Chris Lattnerc1915e22007-01-25 07:29:02 +00001723 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001724
Chris Lattner37256fb2007-08-27 17:24:30 +00001725 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00001726 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001727 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump11289f42009-09-09 15:08:12 +00001728
Chris Lattner83f095c2009-03-28 19:18:32 +00001729 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001730
Chris Lattner83f095c2009-03-28 19:18:32 +00001731 DeclPtrTy LastEnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001732
Chris Lattnerc1915e22007-01-25 07:29:02 +00001733 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001734 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001735 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1736 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001737
Chris Lattnerc1915e22007-01-25 07:29:02 +00001738 SourceLocation EqualLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001739 OwningExprResult AssignedVal(Actions);
Chris Lattner76c72282007-10-09 17:33:22 +00001740 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001741 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001742 AssignedVal = ParseConstantExpression();
1743 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00001744 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001745 }
Mike Stump11289f42009-09-09 15:08:12 +00001746
Chris Lattnerc1915e22007-01-25 07:29:02 +00001747 // Install the enumerator constant into EnumDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00001748 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1749 LastEnumConstDecl,
1750 IdentLoc, Ident,
1751 EqualLoc,
1752 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00001753 EnumConstantDecls.push_back(EnumConstDecl);
1754 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001755
Chris Lattner76c72282007-10-09 17:33:22 +00001756 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001757 break;
1758 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001759
1760 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00001761 !(getLang().C99 || getLang().CPlusPlus0x))
1762 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1763 << getLang().CPlusPlus
1764 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattnerc1915e22007-01-25 07:29:02 +00001765 }
Mike Stump11289f42009-09-09 15:08:12 +00001766
Chris Lattnerc1915e22007-01-25 07:29:02 +00001767 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00001768 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001769
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001770 AttributeList *Attr = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001771 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001772 if (Tok.is(tok::kw___attribute))
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001773 Attr = ParseAttributes();
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001774
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001775 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1776 EnumConstantDecls.data(), EnumConstantDecls.size(),
1777 CurScope, Attr);
Mike Stump11289f42009-09-09 15:08:12 +00001778
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001779 EnumScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001780 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001781}
Chris Lattner3b561a32006-08-13 00:12:11 +00001782
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001783/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001784/// start of a type-qualifier-list.
1785bool Parser::isTypeQualifier() const {
1786 switch (Tok.getKind()) {
1787 default: return false;
1788 // type-qualifier
1789 case tok::kw_const:
1790 case tok::kw_volatile:
1791 case tok::kw_restrict:
1792 return true;
1793 }
1794}
1795
1796/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001797/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001798bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001799 switch (Tok.getKind()) {
1800 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001801
Chris Lattner020bab92009-01-04 23:41:41 +00001802 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001803 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001804 // Annotate typenames and C++ scope specifiers. If we get one, just
1805 // recurse to handle whatever we get.
1806 if (TryAnnotateTypeOrScopeToken())
1807 return isTypeSpecifierQualifier();
1808 // Otherwise, not a type specifier.
1809 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001810
Chris Lattner020bab92009-01-04 23:41:41 +00001811 case tok::coloncolon: // ::foo::bar
1812 if (NextToken().is(tok::kw_new) || // ::new
1813 NextToken().is(tok::kw_delete)) // ::delete
1814 return false;
1815
1816 // Annotate typenames and C++ scope specifiers. If we get one, just
1817 // recurse to handle whatever we get.
1818 if (TryAnnotateTypeOrScopeToken())
1819 return isTypeSpecifierQualifier();
1820 // Otherwise, not a type specifier.
1821 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001822
Chris Lattnere37e2332006-08-15 04:50:22 +00001823 // GNU attributes support.
1824 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00001825 // GNU typeof support.
1826 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00001827
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001828 // type-specifiers
1829 case tok::kw_short:
1830 case tok::kw_long:
1831 case tok::kw_signed:
1832 case tok::kw_unsigned:
1833 case tok::kw__Complex:
1834 case tok::kw__Imaginary:
1835 case tok::kw_void:
1836 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001837 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001838 case tok::kw_char16_t:
1839 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001840 case tok::kw_int:
1841 case tok::kw_float:
1842 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001843 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001844 case tok::kw__Bool:
1845 case tok::kw__Decimal32:
1846 case tok::kw__Decimal64:
1847 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00001848
Chris Lattner861a2262008-04-13 18:59:07 +00001849 // struct-or-union-specifier (C99) or class-specifier (C++)
1850 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001851 case tok::kw_struct:
1852 case tok::kw_union:
1853 // enum-specifier
1854 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00001855
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001856 // type-qualifier
1857 case tok::kw_const:
1858 case tok::kw_volatile:
1859 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001860
1861 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001862 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001863 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001864
Chris Lattner409bf7d2008-10-20 00:25:30 +00001865 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1866 case tok::less:
1867 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00001868
Steve Naroff44ac7772008-12-25 14:16:32 +00001869 case tok::kw___cdecl:
1870 case tok::kw___stdcall:
1871 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001872 case tok::kw___w64:
1873 case tok::kw___ptr64:
1874 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001875 }
1876}
1877
Chris Lattneracd58a32006-08-06 17:24:14 +00001878/// isDeclarationSpecifier() - Return true if the current token is part of a
1879/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001880bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00001881 switch (Tok.getKind()) {
1882 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001883
Chris Lattner020bab92009-01-04 23:41:41 +00001884 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00001885 // Unfortunate hack to support "Class.factoryMethod" notation.
1886 if (getLang().ObjC1 && NextToken().is(tok::period))
1887 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001888 // Fall through
Steve Naroff9527bbf2009-03-09 21:12:44 +00001889
Douglas Gregor333489b2009-03-27 23:10:48 +00001890 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001891 // Annotate typenames and C++ scope specifiers. If we get one, just
1892 // recurse to handle whatever we get.
1893 if (TryAnnotateTypeOrScopeToken())
1894 return isDeclarationSpecifier();
1895 // Otherwise, not a declaration specifier.
1896 return false;
1897 case tok::coloncolon: // ::foo::bar
1898 if (NextToken().is(tok::kw_new) || // ::new
1899 NextToken().is(tok::kw_delete)) // ::delete
1900 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001901
Chris Lattner020bab92009-01-04 23:41:41 +00001902 // Annotate typenames and C++ scope specifiers. If we get one, just
1903 // recurse to handle whatever we get.
1904 if (TryAnnotateTypeOrScopeToken())
1905 return isDeclarationSpecifier();
1906 // Otherwise, not a declaration specifier.
1907 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001908
Chris Lattneracd58a32006-08-06 17:24:14 +00001909 // storage-class-specifier
1910 case tok::kw_typedef:
1911 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00001912 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00001913 case tok::kw_static:
1914 case tok::kw_auto:
1915 case tok::kw_register:
1916 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00001917
Chris Lattneracd58a32006-08-06 17:24:14 +00001918 // type-specifiers
1919 case tok::kw_short:
1920 case tok::kw_long:
1921 case tok::kw_signed:
1922 case tok::kw_unsigned:
1923 case tok::kw__Complex:
1924 case tok::kw__Imaginary:
1925 case tok::kw_void:
1926 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001927 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001928 case tok::kw_char16_t:
1929 case tok::kw_char32_t:
1930
Chris Lattneracd58a32006-08-06 17:24:14 +00001931 case tok::kw_int:
1932 case tok::kw_float:
1933 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001934 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00001935 case tok::kw__Bool:
1936 case tok::kw__Decimal32:
1937 case tok::kw__Decimal64:
1938 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00001939
Chris Lattner861a2262008-04-13 18:59:07 +00001940 // struct-or-union-specifier (C99) or class-specifier (C++)
1941 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00001942 case tok::kw_struct:
1943 case tok::kw_union:
1944 // enum-specifier
1945 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00001946
Chris Lattneracd58a32006-08-06 17:24:14 +00001947 // type-qualifier
1948 case tok::kw_const:
1949 case tok::kw_volatile:
1950 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00001951
Chris Lattneracd58a32006-08-06 17:24:14 +00001952 // function-specifier
1953 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00001954 case tok::kw_virtual:
1955 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001956
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001957 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001958 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001959
Chris Lattner599e47e2007-08-09 17:01:07 +00001960 // GNU typeof support.
1961 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00001962
Chris Lattner599e47e2007-08-09 17:01:07 +00001963 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001964 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00001965 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001966
Chris Lattner8b2ec162008-07-26 03:38:44 +00001967 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1968 case tok::less:
1969 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00001970
Steve Narofff192fab2009-01-06 19:34:12 +00001971 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00001972 case tok::kw___cdecl:
1973 case tok::kw___stdcall:
1974 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001975 case tok::kw___w64:
1976 case tok::kw___ptr64:
1977 case tok::kw___forceinline:
1978 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001979 }
1980}
1981
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001982
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001983/// ParseTypeQualifierListOpt
1984/// type-qualifier-list: [C99 6.7.5]
1985/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00001986/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001987/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00001988/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001989///
Chris Lattnercf0bab22008-12-18 07:02:59 +00001990void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001991 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001992 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001993 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001994 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001995 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001996
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001997 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001998 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001999 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2000 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002001 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002002 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002003 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2004 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002005 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002006 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002007 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2008 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002009 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002010 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002011 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002012 case tok::kw___cdecl:
2013 case tok::kw___stdcall:
2014 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00002015 if (AttributesAllowed) {
2016 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2017 continue;
2018 }
2019 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002020 case tok::kw___attribute:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002021 if (AttributesAllowed) {
2022 DS.AddAttributes(ParseAttributes());
2023 continue; // do *not* consume the next token!
2024 }
2025 // otherwise, FALL THROUGH!
2026 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002027 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002028 // If this is not a type-qualifier token, we're done reading type
2029 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002030 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002031 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002032 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002033
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002034 // If the specifier combination wasn't legal, issue a diagnostic.
2035 if (isInvalid) {
2036 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002037 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002038 }
2039 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002040 }
2041}
2042
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002043
2044/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2045///
2046void Parser::ParseDeclarator(Declarator &D) {
2047 /// This implements the 'declarator' production in the C grammar, then checks
2048 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002049 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002050}
2051
Sebastian Redlbd150f42008-11-21 19:14:01 +00002052/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2053/// is parsed by the function passed to it. Pass null, and the direct-declarator
2054/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002055/// ptr-operator production.
2056///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002057/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2058/// [C] pointer[opt] direct-declarator
2059/// [C++] direct-declarator
2060/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002061///
2062/// pointer: [C99 6.7.5]
2063/// '*' type-qualifier-list[opt]
2064/// '*' type-qualifier-list[opt] pointer
2065///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002066/// ptr-operator:
2067/// '*' cv-qualifier-seq[opt]
2068/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002069/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002070/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002071/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002072/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002073void Parser::ParseDeclaratorInternal(Declarator &D,
2074 DirectDeclParseFunction DirectDeclParser) {
Bill Wendling3708c182007-05-27 10:15:43 +00002075
Douglas Gregor66a985d2009-08-26 14:27:30 +00002076 if (Diags.hasAllExtensionsSilenced())
2077 D.setExtension();
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002078 // C++ member pointers start with a '::' or a nested-name.
2079 // Member pointers get special handling, since there's no place for the
2080 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002081 if (getLang().CPlusPlus &&
2082 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2083 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002084 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00002085 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump11289f42009-09-09 15:08:12 +00002086 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002087 // The scope spec really belongs to the direct-declarator.
2088 D.getCXXScopeSpec() = SS;
2089 if (DirectDeclParser)
2090 (this->*DirectDeclParser)(D);
2091 return;
2092 }
2093
2094 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002095 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002096 DeclSpec DS;
2097 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002098 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002099
2100 // Recurse to parse whatever is left.
2101 ParseDeclaratorInternal(D, DirectDeclParser);
2102
2103 // Sema will have to catch (syntactically invalid) pointers into global
2104 // scope. It has to catch pointers into namespace scope anyway.
2105 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002106 Loc, DS.TakeAttributes()),
2107 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002108 return;
2109 }
2110 }
2111
2112 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002113 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002114 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002115 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002116 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002117 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002118 if (DirectDeclParser)
2119 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002120 return;
2121 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002122
Sebastian Redled0f3b02009-03-15 22:02:01 +00002123 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2124 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002125 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002126 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002127
Chris Lattner9eac9312009-03-27 04:18:06 +00002128 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002129 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002130 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002131
Bill Wendling3708c182007-05-27 10:15:43 +00002132 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002133 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002134
Bill Wendling3708c182007-05-27 10:15:43 +00002135 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002136 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002137 if (Kind == tok::star)
2138 // Remember that we parsed a pointer type, and remember the type-quals.
2139 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002140 DS.TakeAttributes()),
2141 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002142 else
2143 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002144 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002145 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002146 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002147 } else {
2148 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002149 DeclSpec DS;
2150
Sebastian Redl3b27be62009-03-23 00:00:23 +00002151 // Complain about rvalue references in C++03, but then go on and build
2152 // the declarator.
2153 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2154 Diag(Loc, diag::err_rvalue_reference);
2155
Bill Wendling93efb222007-06-02 23:28:54 +00002156 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2157 // cv-qualifiers are introduced through the use of a typedef or of a
2158 // template type argument, in which case the cv-qualifiers are ignored.
2159 //
2160 // [GNU] Retricted references are allowed.
2161 // [GNU] Attributes on references are allowed.
2162 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002163 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002164
2165 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2166 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2167 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002168 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002169 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2170 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002171 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002172 }
Bill Wendling3708c182007-05-27 10:15:43 +00002173
2174 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002175 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002176
Douglas Gregor66583c52008-11-03 15:51:28 +00002177 if (D.getNumTypeObjects() > 0) {
2178 // C++ [dcl.ref]p4: There shall be no references to references.
2179 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2180 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002181 if (const IdentifierInfo *II = D.getIdentifier())
2182 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2183 << II;
2184 else
2185 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2186 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002187
Sebastian Redlbd150f42008-11-21 19:14:01 +00002188 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002189 // can go ahead and build the (technically ill-formed)
2190 // declarator: reference collapsing will take care of it.
2191 }
2192 }
2193
Bill Wendling3708c182007-05-27 10:15:43 +00002194 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002195 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002196 DS.TakeAttributes(),
2197 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002198 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002199 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002200}
2201
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002202/// ParseDirectDeclarator
2203/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002204/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002205/// '(' declarator ')'
2206/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002207/// [C90] direct-declarator '[' constant-expression[opt] ']'
2208/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2209/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2210/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2211/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002212/// direct-declarator '(' parameter-type-list ')'
2213/// direct-declarator '(' identifier-list[opt] ')'
2214/// [GNU] direct-declarator '(' parameter-forward-declarations
2215/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002216/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2217/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002218/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002219///
2220/// declarator-id: [C++ 8]
2221/// id-expression
2222/// '::'[opt] nested-name-specifier[opt] type-name
2223///
2224/// id-expression: [C++ 5.1]
2225/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002226/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002227///
2228/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002229/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002230/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002231/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002232/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002233/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002234///
Chris Lattneracd58a32006-08-06 17:24:14 +00002235void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002236 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002237
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002238 if (getLang().CPlusPlus) {
2239 if (D.mayHaveIdentifier()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002240 // ParseDeclaratorInternal might already have parsed the scope.
2241 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
Mike Stump11289f42009-09-09 15:08:12 +00002242 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
Douglas Gregorb7bfe792009-09-02 22:59:36 +00002243 true);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002244 if (afterCXXScope) {
2245 // Change the declaration context for name lookup, until this function
2246 // is exited (and the declarator has been parsed).
2247 DeclScopeObj.EnterDeclaratorScope();
2248 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002249
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002250 if (Tok.is(tok::identifier)) {
2251 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssona0886932009-04-30 22:41:11 +00002252
2253 // If this identifier is the name of the current class, it's a
Mike Stump11289f42009-09-09 15:08:12 +00002254 // constructor name.
Anders Carlssona0886932009-04-30 22:41:11 +00002255 if (!D.getDeclSpec().hasTypeSpecifier() &&
2256 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
Douglas Gregordce892e2009-07-06 16:40:48 +00002257 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Anders Carlssona0886932009-04-30 22:41:11 +00002258 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregordce892e2009-07-06 16:40:48 +00002259 Tok.getLocation(), CurScope, SS),
Anders Carlssona0886932009-04-30 22:41:11 +00002260 Tok.getLocation());
2261 // This is a normal identifier.
2262 } else
2263 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002264 ConsumeToken();
2265 goto PastIdentifier;
Douglas Gregor7f741122009-02-25 19:37:18 +00002266 } else if (Tok.is(tok::annot_template_id)) {
Mike Stump11289f42009-09-09 15:08:12 +00002267 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00002268 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2269
Douglas Gregord90fd522009-09-25 21:45:23 +00002270 D.setTemplateId(TemplateId);
Douglas Gregor7f741122009-02-25 19:37:18 +00002271 ConsumeToken();
2272 goto PastIdentifier;
Douglas Gregor1dc98262008-12-26 15:00:45 +00002273 } else if (Tok.is(tok::kw_operator)) {
2274 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002275 SourceLocation EndLoc;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002276
Douglas Gregor1dc98262008-12-26 15:00:45 +00002277 // First try the name of an overloaded operator
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002278 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2279 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor1dc98262008-12-26 15:00:45 +00002280 } else {
2281 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002282 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2283 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2284 else {
Douglas Gregor1dc98262008-12-26 15:00:45 +00002285 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002286 }
Douglas Gregor1dc98262008-12-26 15:00:45 +00002287 }
2288 goto PastIdentifier;
2289 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002290 // This should be a C++ destructor.
2291 SourceLocation TildeLoc = ConsumeToken();
Douglas Gregor5e0962f2009-08-26 18:27:52 +00002292 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002293 // FIXME: Inaccurate.
2294 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregord54dfb82009-02-25 23:52:28 +00002295 SourceLocation EndLoc;
Douglas Gregordce892e2009-07-06 16:40:48 +00002296 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +00002297 TypeResult Type = ParseClassName(EndLoc, SS, true);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002298 if (Type.isInvalid())
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002299 D.SetIdentifier(0, TildeLoc);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002300 else
2301 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002302 } else {
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +00002303 Diag(Tok, diag::err_destructor_class_name);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002304 D.SetIdentifier(0, TildeLoc);
2305 }
2306 goto PastIdentifier;
2307 }
2308
2309 // If we reached this point, token is not identifier and not '~'.
2310
2311 if (afterCXXScope) {
2312 Diag(Tok, diag::err_expected_unqualified_id);
2313 D.SetIdentifier(0, Tok.getLocation());
2314 D.setInvalidType(true);
2315 goto PastIdentifier;
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002316 }
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002317 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002318 }
2319
2320 // If we reached this point, we are either in C/ObjC or the token didn't
2321 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002322 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2323 assert(!getLang().CPlusPlus &&
2324 "There's a C++-specific check for tok::identifier above");
2325 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2326 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2327 ConsumeToken();
2328 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002329 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002330 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002331 // Example: 'char (*X)' or 'int (*XX)(void)'
2332 ParseParenDeclarator(D);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002333 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002334 // This could be something simple like "int" (in which case the declarator
2335 // portion is empty), if an abstract-declarator is allowed.
2336 D.SetIdentifier(0, Tok.getLocation());
2337 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002338 if (D.getContext() == Declarator::MemberContext)
2339 Diag(Tok, diag::err_expected_member_name_or_semi)
2340 << D.getDeclSpec().getSourceRange();
2341 else if (getLang().CPlusPlus)
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002342 Diag(Tok, diag::err_expected_unqualified_id);
2343 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002344 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002345 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002346 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002347 }
Mike Stump11289f42009-09-09 15:08:12 +00002348
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002349 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002350 assert(D.isPastIdentifier() &&
2351 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002352
Chris Lattneracd58a32006-08-06 17:24:14 +00002353 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002354 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002355 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2356 // In such a case, check if we actually have a function declarator; if it
2357 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002358 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2359 // When not in file scope, warn for ambiguous function declarators, just
2360 // in case the author intended it as a variable definition.
2361 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2362 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2363 break;
2364 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002365 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002366 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002367 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002368 } else {
2369 break;
2370 }
2371 }
2372}
2373
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002374/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2375/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002376/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002377/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2378///
2379/// direct-declarator:
2380/// '(' declarator ')'
2381/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002382/// direct-declarator '(' parameter-type-list ')'
2383/// direct-declarator '(' identifier-list[opt] ')'
2384/// [GNU] direct-declarator '(' parameter-forward-declarations
2385/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002386///
2387void Parser::ParseParenDeclarator(Declarator &D) {
2388 SourceLocation StartLoc = ConsumeParen();
2389 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002390
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002391 // Eat any attributes before we look at whether this is a grouping or function
2392 // declarator paren. If this is a grouping paren, the attribute applies to
2393 // the type being built up, for example:
2394 // int (__attribute__(()) *x)(long y)
2395 // If this ends up not being a grouping paren, the attribute applies to the
2396 // first argument, for example:
2397 // int (__attribute__(()) int x)
2398 // In either case, we need to eat any attributes to be able to determine what
2399 // sort of paren this is.
2400 //
2401 AttributeList *AttrList = 0;
2402 bool RequiresArg = false;
2403 if (Tok.is(tok::kw___attribute)) {
2404 AttrList = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +00002405
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002406 // We require that the argument list (if this is a non-grouping paren) be
2407 // present even if the attribute list was empty.
2408 RequiresArg = true;
2409 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002410 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002411 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2412 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2413 Tok.is(tok::kw___ptr64)) {
2414 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2415 }
Mike Stump11289f42009-09-09 15:08:12 +00002416
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002417 // If we haven't past the identifier yet (or where the identifier would be
2418 // stored, if this is an abstract declarator), then this is probably just
2419 // grouping parens. However, if this could be an abstract-declarator, then
2420 // this could also be the start of function arguments (consider 'void()').
2421 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002422
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002423 if (!D.mayOmitIdentifier()) {
2424 // If this can't be an abstract-declarator, this *must* be a grouping
2425 // paren, because we haven't seen the identifier yet.
2426 isGrouping = true;
2427 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002428 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002429 isDeclarationSpecifier()) { // 'int(int)' is a function.
2430 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2431 // considered to be a type, not a K&R identifier-list.
2432 isGrouping = false;
2433 } else {
2434 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2435 isGrouping = true;
2436 }
Mike Stump11289f42009-09-09 15:08:12 +00002437
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002438 // If this is a grouping paren, handle:
2439 // direct-declarator: '(' declarator ')'
2440 // direct-declarator: '(' attributes declarator ')'
2441 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002442 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002443 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002444 if (AttrList)
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002445 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002446
Sebastian Redlbd150f42008-11-21 19:14:01 +00002447 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002448 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002449 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002450
2451 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002452 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002453 return;
2454 }
Mike Stump11289f42009-09-09 15:08:12 +00002455
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002456 // Okay, if this wasn't a grouping paren, it must be the start of a function
2457 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002458 // identifier (and remember where it would have been), then call into
2459 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002460 D.SetIdentifier(0, Tok.getLocation());
2461
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002462 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002463}
2464
2465/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2466/// declarator D up to a paren, which indicates that we are parsing function
2467/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002468///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002469/// If AttrList is non-null, then the caller parsed those arguments immediately
2470/// after the open paren - they should be considered to be the first argument of
2471/// a parameter. If RequiresArg is true, then the first argument of the
2472/// function is required to be present and required to not be an identifier
2473/// list.
2474///
Chris Lattneracd58a32006-08-06 17:24:14 +00002475/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002476/// parameter-type-list: [C99 6.7.5]
2477/// parameter-list
2478/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002479/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002480///
2481/// parameter-list: [C99 6.7.5]
2482/// parameter-declaration
2483/// parameter-list ',' parameter-declaration
2484///
2485/// parameter-declaration: [C99 6.7.5]
2486/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002487/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002488/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002489/// declaration-specifiers abstract-declarator[opt]
2490/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002491/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002492/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002493///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002494/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002495/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002496///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002497void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2498 AttributeList *AttrList,
2499 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002500 // lparen is already consumed!
2501 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002502
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002503 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002504 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002505 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002506 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002507 delete AttrList;
2508 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002509
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002510 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2511 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002512
2513 // cv-qualifier-seq[opt].
2514 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002515 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002516 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002517 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002518 llvm::SmallVector<TypeTy*, 2> Exceptions;
2519 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002520 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002521 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002522 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002523 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002524
2525 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002526 if (Tok.is(tok::kw_throw)) {
2527 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002528 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002529 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002530 hasAnyExceptionSpec);
2531 assert(Exceptions.size() == ExceptionRanges.size() &&
2532 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002533 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002534 }
2535
Chris Lattner371ed4e2008-04-06 06:57:35 +00002536 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002537 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002538 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002539 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002540 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002541 /*arglist*/ 0, 0,
2542 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002543 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002544 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002545 Exceptions.data(),
2546 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002547 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002548 LParenLoc, RParenLoc, D),
2549 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002550 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002551 }
2552
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002553 // Alternatively, this parameter list may be an identifier list form for a
2554 // K&R-style function: void foo(a,b,c)
Steve Naroffb0486722009-01-28 19:16:40 +00002555 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff3b6a4bd2009-01-30 14:23:32 +00002556 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002557 // K&R identifier lists can't have typedefs as identifiers, per
2558 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002559 if (RequiresArg) {
2560 Diag(Tok, diag::err_argument_required_after_attribute);
2561 delete AttrList;
2562 }
Steve Naroffb0486722009-01-28 19:16:40 +00002563 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2564 // normal declarators, not for abstract-declarators.
2565 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002566 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002567 }
Mike Stump11289f42009-09-09 15:08:12 +00002568
Chris Lattner371ed4e2008-04-06 06:57:35 +00002569 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00002570
Chris Lattner371ed4e2008-04-06 06:57:35 +00002571 // Build up an array of information about the parsed arguments.
2572 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002573
2574 // Enter function-declaration scope, limiting any declarators to the
2575 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00002576 ParseScope PrototypeScope(this,
2577 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00002578
Chris Lattner371ed4e2008-04-06 06:57:35 +00002579 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002580 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00002581 while (1) {
2582 if (Tok.is(tok::ellipsis)) {
2583 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002584 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002585 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00002586 }
Mike Stump11289f42009-09-09 15:08:12 +00002587
Chris Lattner371ed4e2008-04-06 06:57:35 +00002588 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002589
Chris Lattner371ed4e2008-04-06 06:57:35 +00002590 // Parse the declaration-specifiers.
2591 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002592
2593 // If the caller parsed attributes for the first argument, add them now.
2594 if (AttrList) {
2595 DS.AddAttributes(AttrList);
2596 AttrList = 0; // Only apply the attributes to the first parameter.
2597 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002598 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002599
Chris Lattner371ed4e2008-04-06 06:57:35 +00002600 // Parse the declarator. This is "PrototypeContext", because we must
2601 // accept either 'declarator' or 'abstract-declarator' here.
2602 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2603 ParseDeclarator(ParmDecl);
2604
2605 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002606 if (Tok.is(tok::kw___attribute)) {
2607 SourceLocation Loc;
2608 AttributeList *AttrList = ParseAttributes(&Loc);
2609 ParmDecl.AddAttributes(AttrList, Loc);
2610 }
Mike Stump11289f42009-09-09 15:08:12 +00002611
Chris Lattner371ed4e2008-04-06 06:57:35 +00002612 // Remember this parsed parameter in ParamInfo.
2613 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00002614
Douglas Gregor4d87df52008-12-16 21:30:33 +00002615 // DefArgToks is used when the parsing of default arguments needs
2616 // to be delayed.
2617 CachedTokens *DefArgToks = 0;
2618
Chris Lattner371ed4e2008-04-06 06:57:35 +00002619 // If no parameter was specified, verify that *something* was specified,
2620 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002621 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2622 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00002623 // Completely missing, emit error.
2624 Diag(DSStart, diag::err_missing_param);
2625 } else {
2626 // Otherwise, we have something. Add it and let semantic analysis try
2627 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00002628
Chris Lattner371ed4e2008-04-06 06:57:35 +00002629 // Inform the actions module about the parameter declarator, so it gets
2630 // added to the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002631 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002632
2633 // Parse the default argument, if any. We parse the default
2634 // arguments in all dialects; the semantic analysis in
2635 // ActOnParamDefaultArgument will reject the default argument in
2636 // C.
2637 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00002638 SourceLocation EqualLoc = Tok.getLocation();
2639
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002640 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00002641 if (D.getContext() == Declarator::MemberContext) {
2642 // If we're inside a class definition, cache the tokens
2643 // corresponding to the default argument. We'll actually parse
2644 // them when we see the end of the class definition.
2645 // FIXME: Templates will require something similar.
2646 // FIXME: Can we use a smart pointer for Toks?
2647 DefArgToks = new CachedTokens;
2648
Mike Stump11289f42009-09-09 15:08:12 +00002649 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002650 tok::semi, false)) {
2651 delete DefArgToks;
2652 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00002653 Actions.ActOnParamDefaultArgumentError(Param);
2654 } else
Mike Stump11289f42009-09-09 15:08:12 +00002655 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00002656 (*DefArgToks)[1].getLocation());
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002657 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00002658 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00002659 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002660
Douglas Gregor4d87df52008-12-16 21:30:33 +00002661 OwningExprResult DefArgResult(ParseAssignmentExpression());
2662 if (DefArgResult.isInvalid()) {
2663 Actions.ActOnParamDefaultArgumentError(Param);
2664 SkipUntil(tok::comma, tok::r_paren, true, true);
2665 } else {
2666 // Inform the actions module about the default argument
2667 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002668 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00002669 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002670 }
2671 }
Mike Stump11289f42009-09-09 15:08:12 +00002672
2673 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2674 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002675 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00002676 }
2677
2678 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002679 if (Tok.isNot(tok::comma)) {
2680 if (Tok.is(tok::ellipsis)) {
2681 IsVariadic = true;
2682 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2683
2684 if (!getLang().CPlusPlus) {
2685 // We have ellipsis without a preceding ',', which is ill-formed
2686 // in C. Complain and provide the fix.
2687 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2688 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2689 }
2690 }
2691
2692 break;
2693 }
Mike Stump11289f42009-09-09 15:08:12 +00002694
Chris Lattner371ed4e2008-04-06 06:57:35 +00002695 // Consume the comma.
2696 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00002697 }
Mike Stump11289f42009-09-09 15:08:12 +00002698
Chris Lattner371ed4e2008-04-06 06:57:35 +00002699 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002700 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00002701
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002702 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002703 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2704 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002705
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002706 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002707 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002708 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002709 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002710 llvm::SmallVector<TypeTy*, 2> Exceptions;
2711 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002712 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002713 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00002714 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002715 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002716 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002717
2718 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002719 if (Tok.is(tok::kw_throw)) {
2720 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002721 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002722 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002723 hasAnyExceptionSpec);
2724 assert(Exceptions.size() == ExceptionRanges.size() &&
2725 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002726 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002727 }
2728
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002729 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002730 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002731 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002732 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002733 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002734 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002735 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002736 Exceptions.data(),
2737 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002738 Exceptions.size(),
2739 LParenLoc, RParenLoc, D),
2740 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002741}
Chris Lattneracd58a32006-08-06 17:24:14 +00002742
Chris Lattner6c940e62008-04-06 06:34:08 +00002743/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2744/// we found a K&R-style identifier list instead of a type argument list. The
2745/// current token is known to be the first identifier in the list.
2746///
2747/// identifier-list: [C99 6.7.5]
2748/// identifier
2749/// identifier-list ',' identifier
2750///
2751void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2752 Declarator &D) {
2753 // Build up an array of information about the parsed arguments.
2754 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2755 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00002756
Chris Lattner6c940e62008-04-06 06:34:08 +00002757 // If there was no identifier specified for the declarator, either we are in
2758 // an abstract-declarator, or we are in a parameter declarator which was found
2759 // to be abstract. In abstract-declarators, identifier lists are not valid:
2760 // diagnose this.
2761 if (!D.getIdentifier())
2762 Diag(Tok, diag::ext_ident_list_in_param);
2763
2764 // Tok is known to be the first identifier in the list. Remember this
2765 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00002766 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00002767 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner83f095c2009-03-28 19:18:32 +00002768 Tok.getLocation(),
2769 DeclPtrTy()));
Mike Stump11289f42009-09-09 15:08:12 +00002770
Chris Lattner9186f552008-04-06 06:39:19 +00002771 ConsumeToken(); // eat the first identifier.
Mike Stump11289f42009-09-09 15:08:12 +00002772
Chris Lattner6c940e62008-04-06 06:34:08 +00002773 while (Tok.is(tok::comma)) {
2774 // Eat the comma.
2775 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002776
Chris Lattner9186f552008-04-06 06:39:19 +00002777 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00002778 if (Tok.isNot(tok::identifier)) {
2779 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00002780 SkipUntil(tok::r_paren);
2781 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00002782 }
Chris Lattner67b450c2008-04-06 06:47:48 +00002783
Chris Lattner6c940e62008-04-06 06:34:08 +00002784 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00002785
2786 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00002787 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerebad6a22008-11-19 07:37:42 +00002788 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00002789
Chris Lattner6c940e62008-04-06 06:34:08 +00002790 // Verify that the argument identifier has not already been mentioned.
2791 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002792 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00002793 } else {
2794 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00002795 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00002796 Tok.getLocation(),
2797 DeclPtrTy()));
Chris Lattner9186f552008-04-06 06:39:19 +00002798 }
Mike Stump11289f42009-09-09 15:08:12 +00002799
Chris Lattner6c940e62008-04-06 06:34:08 +00002800 // Eat the identifier.
2801 ConsumeToken();
2802 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002803
2804 // If we have the closing ')', eat it and we're done.
2805 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2806
Chris Lattner9186f552008-04-06 06:39:19 +00002807 // Remember that we parsed a function type, and remember the attributes. This
2808 // function type is always a K&R style function type, which is not varargs and
2809 // has no prototype.
2810 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002811 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00002812 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002813 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002814 /*exception*/false,
2815 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002816 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002817 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00002818}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002819
Chris Lattnere8074e62006-08-06 18:30:15 +00002820/// [C90] direct-declarator '[' constant-expression[opt] ']'
2821/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2822/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2823/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2824/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2825void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00002826 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00002827
Chris Lattner84a11622008-12-18 07:27:21 +00002828 // C array syntax has many features, but by-far the most common is [] and [4].
2829 // This code does a fast path to handle some of the most obvious cases.
2830 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002831 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002832 // Remember that we parsed the empty array type.
2833 OwningExprResult NumElements(Actions);
Douglas Gregor04318252009-07-06 15:59:29 +00002834 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2835 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002836 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002837 return;
2838 } else if (Tok.getKind() == tok::numeric_constant &&
2839 GetLookAheadToken(1).is(tok::r_square)) {
2840 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00002841 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00002842 ConsumeToken();
2843
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002844 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002845
2846 // If there was an error parsing the assignment-expression, recover.
2847 if (ExprRes.isInvalid())
2848 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00002849
Chris Lattner84a11622008-12-18 07:27:21 +00002850 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00002851 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2852 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002853 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002854 return;
2855 }
Mike Stump11289f42009-09-09 15:08:12 +00002856
Chris Lattnere8074e62006-08-06 18:30:15 +00002857 // If valid, this location is the position where we read the 'static' keyword.
2858 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00002859 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002860 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002861
Chris Lattnere8074e62006-08-06 18:30:15 +00002862 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002863 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00002864 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00002865 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00002866
Chris Lattnere8074e62006-08-06 18:30:15 +00002867 // If we haven't already read 'static', check to see if there is one after the
2868 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002869 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002870 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002871
Chris Lattnere8074e62006-08-06 18:30:15 +00002872 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00002873 bool isStar = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +00002874 OwningExprResult NumElements(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00002875
Chris Lattner521ff2b2008-04-06 05:26:30 +00002876 // Handle the case where we have '[*]' as the array size. However, a leading
2877 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2878 // the the token after the star is a ']'. Since stars in arrays are
2879 // infrequent, use of lookahead is not costly here.
2880 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00002881 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00002882
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002883 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00002884 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002885 StaticLoc = SourceLocation(); // Drop the static.
2886 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00002887 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00002888 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00002889 // Note, in C89, this production uses the constant-expr production instead
2890 // of assignment-expr. The only difference is that assignment-expr allows
2891 // things like '=' and '*='. Sema rejects these in C89 mode because they
2892 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00002893
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00002894 // Parse the constant-expression or assignment-expression now (depending
2895 // on dialect).
2896 if (getLang().CPlusPlus)
2897 NumElements = ParseConstantExpression();
2898 else
2899 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00002900 }
Mike Stump11289f42009-09-09 15:08:12 +00002901
Chris Lattner62591722006-08-12 18:40:58 +00002902 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002903 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00002904 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00002905 // If the expression was invalid, skip it.
2906 SkipUntil(tok::r_square);
2907 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00002908 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002909
2910 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2911
Chris Lattner84a11622008-12-18 07:27:21 +00002912 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00002913 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2914 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00002915 NumElements.release(),
2916 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002917 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00002918}
2919
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002920/// [GNU] typeof-specifier:
2921/// typeof ( expressions )
2922/// typeof ( type-name )
2923/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00002924///
2925void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00002926 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002927 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00002928 SourceLocation StartLoc = ConsumeToken();
2929
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002930 bool isCastExpr;
2931 TypeTy *CastTy;
2932 SourceRange CastRange;
2933 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2934 isCastExpr,
2935 CastTy,
2936 CastRange);
2937
2938 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002939 // FIXME: Not accurate, the range gets one token more than it should.
2940 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002941 else
2942 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00002943
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002944 if (isCastExpr) {
2945 if (!CastTy) {
2946 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002947 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00002948 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002949
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002950 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002951 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002952 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2953 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002954 DiagID, CastTy))
2955 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002956 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002957 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002958
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002959 // If we get here, the operand to the typeof was an expresion.
2960 if (Operand.isInvalid()) {
2961 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00002962 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00002963 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002964
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002965 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002966 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002967 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2968 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002969 DiagID, Operand.release()))
2970 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00002971}