blob: e905553fd818b1d7bd7228eb48e1e38096c37893 [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,
John McCalld5a36322009-11-03 19:26:08 +0000339 SourceLocation &DeclEnd) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000340 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000341 ParsingDeclSpec DS(*this);
Chris Lattner53361ac2006-08-10 05:19:57 +0000342 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000343
Chris Lattner0e894622006-08-13 19:58:17 +0000344 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
345 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000346 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000347 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000348 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall28a6aea2009-11-04 02:18:39 +0000349 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000350 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000351 }
Mike Stump11289f42009-09-09 15:08:12 +0000352
John McCalld5a36322009-11-03 19:26:08 +0000353 DeclGroupPtrTy DG = ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false,
354 &DeclEnd);
355 return DG;
356}
Mike Stump11289f42009-09-09 15:08:12 +0000357
John McCalld5a36322009-11-03 19:26:08 +0000358/// ParseDeclGroup - Having concluded that this is either a function
359/// definition or a group of object declarations, actually parse the
360/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000361Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
362 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000363 bool AllowFunctionDefinitions,
364 SourceLocation *DeclEnd) {
365 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000366 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000367 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000368
John McCalld5a36322009-11-03 19:26:08 +0000369 // Bail out if the first declarator didn't seem well-formed.
370 if (!D.hasName() && !D.mayOmitIdentifier()) {
371 // Skip until ; or }.
372 SkipUntil(tok::r_brace, true, true);
373 if (Tok.is(tok::semi))
374 ConsumeToken();
375 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000376 }
Mike Stump11289f42009-09-09 15:08:12 +0000377
John McCalld5a36322009-11-03 19:26:08 +0000378 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
379 if (isDeclarationAfterDeclarator()) {
380 // Fall though. We have to check this first, though, because
381 // __attribute__ might be the start of a function definition in
382 // (extended) K&R C.
383 } else if (isStartOfFunctionDefinition()) {
384 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
385 Diag(Tok, diag::err_function_declared_typedef);
386
387 // Recover by treating the 'typedef' as spurious.
388 DS.ClearStorageClassSpecs();
389 }
390
391 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
392 return Actions.ConvertDeclToDeclGroup(TheDecl);
393 } else {
394 Diag(Tok, diag::err_expected_fn_body);
395 SkipUntil(tok::semi);
396 return DeclGroupPtrTy();
397 }
398 }
399
400 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
401 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000402 D.complete(FirstDecl);
John McCalld5a36322009-11-03 19:26:08 +0000403 if (FirstDecl.get())
404 DeclsInGroup.push_back(FirstDecl);
405
406 // If we don't have a comma, it is either the end of the list (a ';') or an
407 // error, bail out.
408 while (Tok.is(tok::comma)) {
409 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000410 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000411
412 // Parse the next declarator.
413 D.clear();
414
415 // Accept attributes in an init-declarator. In the first declarator in a
416 // declaration, these would be part of the declspec. In subsequent
417 // declarators, they become part of the declarator itself, so that they
418 // don't apply to declarators after *this* one. Examples:
419 // short __attribute__((common)) var; -> declspec
420 // short var __attribute__((common)); -> declarator
421 // short x, __attribute__((common)) var; -> declarator
422 if (Tok.is(tok::kw___attribute)) {
423 SourceLocation Loc;
424 AttributeList *AttrList = ParseAttributes(&Loc);
425 D.AddAttributes(AttrList, Loc);
426 }
427
428 ParseDeclarator(D);
429
430 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000431 D.complete(ThisDecl);
John McCalld5a36322009-11-03 19:26:08 +0000432 if (ThisDecl.get())
433 DeclsInGroup.push_back(ThisDecl);
434 }
435
436 if (DeclEnd)
437 *DeclEnd = Tok.getLocation();
438
439 if (Context != Declarator::ForContext &&
440 ExpectAndConsume(tok::semi,
441 Context == Declarator::FileContext
442 ? diag::err_invalid_token_after_toplevel_declarator
443 : diag::err_expected_semi_declaration)) {
444 SkipUntil(tok::r_brace, true, true);
445 if (Tok.is(tok::semi))
446 ConsumeToken();
447 }
448
449 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
450 DeclsInGroup.data(),
451 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000452}
453
Douglas Gregor23996282009-05-12 21:31:51 +0000454/// \brief Parse 'declaration' after parsing 'declaration-specifiers
455/// declarator'. This method parses the remainder of the declaration
456/// (including any attributes or initializer, among other things) and
457/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000458///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000459/// init-declarator: [C99 6.7]
460/// declarator
461/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000462/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
463/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000464/// [C++] declarator initializer[opt]
465///
466/// [C++] initializer:
467/// [C++] '=' initializer-clause
468/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000469/// [C++0x] '=' 'default' [TODO]
470/// [C++0x] '=' 'delete'
471///
472/// According to the standard grammar, =default and =delete are function
473/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000474///
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000475Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
476 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000477 // If a simple-asm-expr is present, parse it.
478 if (Tok.is(tok::kw_asm)) {
479 SourceLocation Loc;
480 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
481 if (AsmLabel.isInvalid()) {
482 SkipUntil(tok::semi, true, true);
483 return DeclPtrTy();
484 }
Mike Stump11289f42009-09-09 15:08:12 +0000485
Douglas Gregor23996282009-05-12 21:31:51 +0000486 D.setAsmLabel(AsmLabel.release());
487 D.SetRangeEnd(Loc);
488 }
Mike Stump11289f42009-09-09 15:08:12 +0000489
Douglas Gregor23996282009-05-12 21:31:51 +0000490 // If attributes are present, parse them.
491 if (Tok.is(tok::kw___attribute)) {
492 SourceLocation Loc;
493 AttributeList *AttrList = ParseAttributes(&Loc);
494 D.AddAttributes(AttrList, Loc);
495 }
Mike Stump11289f42009-09-09 15:08:12 +0000496
Douglas Gregor23996282009-05-12 21:31:51 +0000497 // Inform the current actions module that we just parsed this declarator.
Douglas Gregor450f00842009-09-25 18:43:00 +0000498 DeclPtrTy ThisDecl;
499 switch (TemplateInfo.Kind) {
500 case ParsedTemplateInfo::NonTemplate:
501 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
502 break;
503
504 case ParsedTemplateInfo::Template:
505 case ParsedTemplateInfo::ExplicitSpecialization:
506 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000507 Action::MultiTemplateParamsArg(Actions,
508 TemplateInfo.TemplateParams->data(),
509 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000510 D);
511 break;
512
513 case ParsedTemplateInfo::ExplicitInstantiation: {
514 Action::DeclResult ThisRes
515 = Actions.ActOnExplicitInstantiation(CurScope,
516 TemplateInfo.ExternLoc,
517 TemplateInfo.TemplateLoc,
518 D);
519 if (ThisRes.isInvalid()) {
520 SkipUntil(tok::semi, true, true);
521 return DeclPtrTy();
522 }
523
524 ThisDecl = ThisRes.get();
525 break;
526 }
527 }
Mike Stump11289f42009-09-09 15:08:12 +0000528
Douglas Gregor23996282009-05-12 21:31:51 +0000529 // Parse declarator '=' initializer.
530 if (Tok.is(tok::equal)) {
531 ConsumeToken();
532 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
533 SourceLocation DelLoc = ConsumeToken();
534 Actions.SetDeclDeleted(ThisDecl, DelLoc);
535 } else {
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000536 if (getLang().CPlusPlus)
537 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
538
Douglas Gregor23996282009-05-12 21:31:51 +0000539 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000540
541 if (getLang().CPlusPlus)
542 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
543
Douglas Gregor23996282009-05-12 21:31:51 +0000544 if (Init.isInvalid()) {
545 SkipUntil(tok::semi, true, true);
546 return DeclPtrTy();
547 }
Anders Carlsson250aada2009-08-16 05:13:48 +0000548 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor23996282009-05-12 21:31:51 +0000549 }
550 } else if (Tok.is(tok::l_paren)) {
551 // Parse C++ direct initializer: '(' expression-list ')'
552 SourceLocation LParenLoc = ConsumeParen();
553 ExprVector Exprs(Actions);
554 CommaLocsTy CommaLocs;
555
556 if (ParseExpressionList(Exprs, CommaLocs)) {
557 SkipUntil(tok::r_paren);
558 } else {
559 // Match the ')'.
560 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
561
562 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
563 "Unexpected number of commas!");
564 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
565 move_arg(Exprs),
Jay Foad7d0479f2009-05-21 09:52:38 +0000566 CommaLocs.data(), RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000567 }
568 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000569 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000570 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
571 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000572 }
573
574 return ThisDecl;
575}
576
Chris Lattner1890ac82006-08-13 01:16:23 +0000577/// ParseSpecifierQualifierList
578/// specifier-qualifier-list:
579/// type-specifier specifier-qualifier-list[opt]
580/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000581/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000582///
583void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
584 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
585 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000586 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000587
Chris Lattner1890ac82006-08-13 01:16:23 +0000588 // Validate declspec for type-name.
589 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000590 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
591 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000592 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000593
Chris Lattner1b22eed2006-11-28 05:12:07 +0000594 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000595 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000596 if (DS.getStorageClassSpecLoc().isValid())
597 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
598 else
599 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000600 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000601 }
Mike Stump11289f42009-09-09 15:08:12 +0000602
Chris Lattner1b22eed2006-11-28 05:12:07 +0000603 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000604 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000605 if (DS.isInlineSpecified())
606 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
607 if (DS.isVirtualSpecified())
608 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
609 if (DS.isExplicitSpecified())
610 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000611 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000612 }
613}
Chris Lattner53361ac2006-08-10 05:19:57 +0000614
Chris Lattner6cc055a2009-04-12 20:42:31 +0000615/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
616/// specified token is valid after the identifier in a declarator which
617/// immediately follows the declspec. For example, these things are valid:
618///
619/// int x [ 4]; // direct-declarator
620/// int x ( int y); // direct-declarator
621/// int(int x ) // direct-declarator
622/// int x ; // simple-declaration
623/// int x = 17; // init-declarator-list
624/// int x , y; // init-declarator-list
625/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000626/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000627/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000628///
629/// This is not, because 'x' does not immediately follow the declspec (though
630/// ')' happens to be valid anyway).
631/// int (x)
632///
633static bool isValidAfterIdentifierInDeclarator(const Token &T) {
634 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
635 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000636 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000637}
638
Chris Lattner20a0c612009-04-14 21:34:55 +0000639
640/// ParseImplicitInt - This method is called when we have an non-typename
641/// identifier in a declspec (which normally terminates the decl spec) when
642/// the declspec has no type specifier. In this case, the declspec is either
643/// malformed or is "implicit int" (in K&R and C89).
644///
645/// This method handles diagnosing this prettily and returns false if the
646/// declspec is done being processed. If it recovers and thinks there may be
647/// other pieces of declspec after it, it returns true.
648///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000649bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000650 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000651 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000652 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000653
Chris Lattner20a0c612009-04-14 21:34:55 +0000654 SourceLocation Loc = Tok.getLocation();
655 // If we see an identifier that is not a type name, we normally would
656 // parse it as the identifer being declared. However, when a typename
657 // is typo'd or the definition is not included, this will incorrectly
658 // parse the typename as the identifier name and fall over misparsing
659 // later parts of the diagnostic.
660 //
661 // As such, we try to do some look-ahead in cases where this would
662 // otherwise be an "implicit-int" case to see if this is invalid. For
663 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
664 // an identifier with implicit int, we'd get a parse error because the
665 // next token is obviously invalid for a type. Parse these as a case
666 // with an invalid type specifier.
667 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000668
Chris Lattner20a0c612009-04-14 21:34:55 +0000669 // Since we know that this either implicit int (which is rare) or an
670 // error, we'd do lookahead to try to do better recovery.
671 if (isValidAfterIdentifierInDeclarator(NextToken())) {
672 // If this token is valid for implicit int, e.g. "static x = 4", then
673 // we just avoid eating the identifier, so it will be parsed as the
674 // identifier in the declarator.
675 return false;
676 }
Mike Stump11289f42009-09-09 15:08:12 +0000677
Chris Lattner20a0c612009-04-14 21:34:55 +0000678 // Otherwise, if we don't consume this token, we are going to emit an
679 // error anyway. Try to recover from various common problems. Check
680 // to see if this was a reference to a tag name without a tag specified.
681 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000682 //
683 // C++ doesn't need this, and isTagName doesn't take SS.
684 if (SS == 0) {
685 const char *TagName = 0;
686 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000687
Chris Lattner20a0c612009-04-14 21:34:55 +0000688 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
689 default: break;
690 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
691 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
692 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
693 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
694 }
Mike Stump11289f42009-09-09 15:08:12 +0000695
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000696 if (TagName) {
697 Diag(Loc, diag::err_use_of_tag_name_without_tag)
698 << Tok.getIdentifierInfo() << TagName
699 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000700
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000701 // Parse this as a tag as if the missing tag were present.
702 if (TagKind == tok::kw_enum)
703 ParseEnumSpecifier(Loc, DS, AS);
704 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000705 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000706 return true;
707 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000708 }
Mike Stump11289f42009-09-09 15:08:12 +0000709
Douglas Gregor15e56022009-10-13 23:27:22 +0000710 // This is almost certainly an invalid type name. Let the action emit a
711 // diagnostic and attempt to recover.
712 Action::TypeTy *T = 0;
713 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
714 CurScope, SS, T)) {
715 // The action emitted a diagnostic, so we don't have to.
716 if (T) {
717 // The action has suggested that the type T could be used. Set that as
718 // the type in the declaration specifiers, consume the would-be type
719 // name token, and we're done.
720 const char *PrevSpec;
721 unsigned DiagID;
722 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
723 false);
724 DS.SetRangeEnd(Tok.getLocation());
725 ConsumeToken();
726
727 // There may be other declaration specifiers after this.
728 return true;
729 }
730
731 // Fall through; the action had no suggestion for us.
732 } else {
733 // The action did not emit a diagnostic, so emit one now.
734 SourceRange R;
735 if (SS) R = SS->getRange();
736 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
737 }
Mike Stump11289f42009-09-09 15:08:12 +0000738
Douglas Gregor15e56022009-10-13 23:27:22 +0000739 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000740 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000741 unsigned DiagID;
742 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000743 DS.SetRangeEnd(Tok.getLocation());
744 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000745
Chris Lattner20a0c612009-04-14 21:34:55 +0000746 // TODO: Could inject an invalid typedef decl in an enclosing scope to
747 // avoid rippling error messages on subsequent uses of the same type,
748 // could be useful if #include was forgotten.
749 return false;
750}
751
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000752/// ParseDeclarationSpecifiers
753/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000754/// storage-class-specifier declaration-specifiers[opt]
755/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000756/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000757/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000758///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000759/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000760/// 'typedef'
761/// 'extern'
762/// 'static'
763/// 'auto'
764/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000765/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000766/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000767/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000768/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000769/// [C++] 'virtual'
770/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000771/// 'friend': [C++ dcl.friend]
772
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000773///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000774void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000775 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000776 AccessSpecifier AS,
777 DeclSpecContext DSContext) {
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000778 if (Tok.is(tok::code_completion)) {
779 Actions.CodeCompleteOrdinaryName(CurScope);
780 ConsumeToken();
781 }
782
Chris Lattner2e232092008-03-13 06:29:04 +0000783 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000784 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000785 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000786 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000787 unsigned DiagID = 0;
788
Chris Lattner4d8f8732006-11-28 05:05:08 +0000789 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000790
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000791 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000792 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000793 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000794 // If this is not a declaration specifier token, we're done reading decl
795 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000796 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000797 return;
Mike Stump11289f42009-09-09 15:08:12 +0000798
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000799 case tok::coloncolon: // ::foo::bar
800 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregore861bac2009-08-25 22:51:20 +0000801 if (TryAnnotateCXXScopeToken(true))
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000802 continue;
803 goto DoneWithDeclSpec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000804
805 case tok::annot_cxxscope: {
806 if (DS.hasTypeSpecifier())
807 goto DoneWithDeclSpec;
808
809 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000810 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000811 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000812 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000813 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000814 // We have a qualified template-id, e.g., N::A<int>
815 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000816 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Mike Stump11289f42009-09-09 15:08:12 +0000817 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000818 "ParseOptionalCXXScopeSpecifier not working");
819 AnnotateTemplateIdTokenAsType(&SS);
820 continue;
821 }
822
Douglas Gregorc5790df2009-09-28 07:26:33 +0000823 if (Next.is(tok::annot_typename)) {
824 // FIXME: is this scope-specifier getting dropped?
825 ConsumeToken(); // the scope-specifier
826 if (Tok.getAnnotationValue())
827 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
828 PrevSpec, DiagID,
829 Tok.getAnnotationValue());
830 else
831 DS.SetTypeSpecError();
832 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
833 ConsumeToken(); // The typename
834 }
835
Douglas Gregor167fa622009-03-25 15:40:00 +0000836 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000837 goto DoneWithDeclSpec;
838
839 CXXScopeSpec SS;
Douglas Gregorc23500e2009-03-26 23:56:24 +0000840 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000841 SS.setRange(Tok.getAnnotationRange());
842
843 // If the next token is the name of the class type that the C++ scope
844 // denotes, followed by a '(', then this is a constructor declaration.
845 // We're done with the decl-specifiers.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000846 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000847 CurScope, &SS) &&
848 GetLookAheadToken(2).is(tok::l_paren))
849 goto DoneWithDeclSpec;
850
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000851 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
852 Next.getLocation(), CurScope, &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000853
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000854 // If the referenced identifier is not a type, then this declspec is
855 // erroneous: We already checked about that it has no type specifier, and
856 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +0000857 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000858 if (TypeRep == 0) {
859 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000860 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000861 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000862 }
Mike Stump11289f42009-09-09 15:08:12 +0000863
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000864 ConsumeToken(); // The C++ scope.
865
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000866 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000867 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000868 if (isInvalid)
869 break;
Mike Stump11289f42009-09-09 15:08:12 +0000870
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000871 DS.SetRangeEnd(Tok.getLocation());
872 ConsumeToken(); // The typename.
873
874 continue;
875 }
Mike Stump11289f42009-09-09 15:08:12 +0000876
Chris Lattnere387d9e2009-01-21 19:48:37 +0000877 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000878 if (Tok.getAnnotationValue())
879 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000880 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000881 else
882 DS.SetTypeSpecError();
Chris Lattnere387d9e2009-01-21 19:48:37 +0000883 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
884 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +0000885
Chris Lattnere387d9e2009-01-21 19:48:37 +0000886 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
887 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
888 // Objective-C interface. If we don't have Objective-C or a '<', this is
889 // just a normal reference to a typedef name.
890 if (!Tok.is(tok::less) || !getLang().ObjC1)
891 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000892
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000893 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000894 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000895 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
896 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
897 LAngleLoc, EndProtoLoc);
898 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
899 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000900
Chris Lattnere387d9e2009-01-21 19:48:37 +0000901 DS.SetRangeEnd(EndProtoLoc);
902 continue;
903 }
Mike Stump11289f42009-09-09 15:08:12 +0000904
Chris Lattner16fac4f2008-07-26 01:18:38 +0000905 // typedef-name
906 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000907 // In C++, check to see if this is a scope specifier like foo::bar::, if
908 // so handle it as such. This is important for ctor parsing.
Douglas Gregore861bac2009-08-25 22:51:20 +0000909 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner78ecd4f2009-01-21 19:19:26 +0000910 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000911
Chris Lattner16fac4f2008-07-26 01:18:38 +0000912 // This identifier can only be a typedef name if we haven't already seen
913 // a type-specifier. Without this check we misparse:
914 // typedef int X; struct Y { short X; }; as 'short int'.
915 if (DS.hasTypeSpecifier())
916 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +0000917
Chris Lattner16fac4f2008-07-26 01:18:38 +0000918 // It has to be available as a typedef too!
Mike Stump11289f42009-09-09 15:08:12 +0000919 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000920 Tok.getLocation(), CurScope);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000921
Chris Lattner6cc055a2009-04-12 20:42:31 +0000922 // If this is not a typedef name, don't parse it as part of the declspec,
923 // it must be an implicit int or an error.
924 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000925 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000926 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +0000927 }
Douglas Gregor8bf42052009-02-09 18:46:07 +0000928
Douglas Gregor61956c42008-10-31 09:07:45 +0000929 // C++: If the identifier is actually the name of the class type
930 // being defined and the next token is a '(', then this is a
931 // constructor declaration. We're done with the decl-specifiers
932 // and will treat this token as an identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000933 if (getLang().CPlusPlus &&
934 (CurScope->isClassScope() ||
935 (CurScope->isTemplateParamScope() &&
Douglas Gregor5ed5ae42009-08-21 18:42:58 +0000936 CurScope->getParent()->isClassScope())) &&
Mike Stump11289f42009-09-09 15:08:12 +0000937 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor61956c42008-10-31 09:07:45 +0000938 NextToken().getKind() == tok::l_paren)
939 goto DoneWithDeclSpec;
940
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000941 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000942 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +0000943 if (isInvalid)
944 break;
Mike Stump11289f42009-09-09 15:08:12 +0000945
Chris Lattner16fac4f2008-07-26 01:18:38 +0000946 DS.SetRangeEnd(Tok.getLocation());
947 ConsumeToken(); // The identifier
948
949 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
950 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
951 // Objective-C interface. If we don't have Objective-C or a '<', this is
952 // just a normal reference to a typedef name.
953 if (!Tok.is(tok::less) || !getLang().ObjC1)
954 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000955
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000956 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000957 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000958 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
959 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
960 LAngleLoc, EndProtoLoc);
961 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
962 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000963
Chris Lattner16fac4f2008-07-26 01:18:38 +0000964 DS.SetRangeEnd(EndProtoLoc);
965
Steve Naroffcd5e7822008-09-22 10:28:57 +0000966 // Need to support trailing type qualifiers (e.g. "id<p> const").
967 // If a type specifier follows, it will be diagnosed elsewhere.
968 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000969 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000970
971 // type-name
972 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +0000973 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000974 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000975 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000976 // This template-id does not refer to a type name, so we're
977 // done with the type-specifiers.
978 goto DoneWithDeclSpec;
979 }
980
981 // Turn the template-id annotation token into a type annotation
982 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000983 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +0000984 continue;
985 }
986
Chris Lattnere37e2332006-08-15 04:50:22 +0000987 // GNU attributes support.
988 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000989 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000990 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000991
992 // Microsoft declspec support.
993 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +0000994 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000995 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000996
Steve Naroff44ac7772008-12-25 14:16:32 +0000997 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +0000998 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +0000999 // FIXME: Add handling here!
1000 break;
1001
1002 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001003 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001004 case tok::kw___cdecl:
1005 case tok::kw___stdcall:
1006 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001007 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1008 continue;
1009
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001010 // storage-class-specifier
1011 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001012 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1013 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001014 break;
1015 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001016 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001017 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001018 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1019 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001020 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001021 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001022 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001023 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001024 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001025 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001026 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001027 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001028 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1029 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001030 break;
1031 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001032 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001033 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1034 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001035 else
John McCall49bfce42009-08-03 20:12:06 +00001036 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1037 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001038 break;
1039 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001040 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1041 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001042 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001043 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001044 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1045 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001046 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001047 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001048 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001049 break;
Mike Stump11289f42009-09-09 15:08:12 +00001050
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001051 // function-specifier
1052 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001053 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001054 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001055 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001056 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001057 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001058 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001059 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001060 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001061
Anders Carlssoncd8db412009-05-06 04:46:28 +00001062 // friend
1063 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001064 if (DSContext == DSC_class)
1065 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1066 else {
1067 PrevSpec = ""; // not actually used by the diagnostic
1068 DiagID = diag::err_friend_invalid_in_context;
1069 isInvalid = true;
1070 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001071 break;
Mike Stump11289f42009-09-09 15:08:12 +00001072
Chris Lattnere387d9e2009-01-21 19:48:37 +00001073 // type-specifier
1074 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001075 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1076 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001077 break;
1078 case tok::kw_long:
1079 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001080 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1081 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001082 else
John McCall49bfce42009-08-03 20:12:06 +00001083 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1084 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001085 break;
1086 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001087 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1088 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001089 break;
1090 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001091 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1092 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001093 break;
1094 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001095 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1096 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001097 break;
1098 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001099 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1100 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001101 break;
1102 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001103 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1104 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001105 break;
1106 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001107 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1108 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001109 break;
1110 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001111 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1112 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001113 break;
1114 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001115 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1116 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001117 break;
1118 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001119 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1120 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001121 break;
1122 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001123 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1124 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001125 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001126 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001127 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1128 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001129 break;
1130 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001131 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1132 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001133 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001134 case tok::kw_bool:
1135 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001136 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1137 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001138 break;
1139 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001140 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1141 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001142 break;
1143 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001144 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1145 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001146 break;
1147 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001148 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1149 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001150 break;
1151
1152 // class-specifier:
1153 case tok::kw_class:
1154 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001155 case tok::kw_union: {
1156 tok::TokenKind Kind = Tok.getKind();
1157 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001158 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001159 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001160 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001161
1162 // enum-specifier:
1163 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001164 ConsumeToken();
1165 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001166 continue;
1167
1168 // cv-qualifier:
1169 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001170 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1171 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001172 break;
1173 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001174 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1175 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001176 break;
1177 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001178 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1179 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001180 break;
1181
Douglas Gregor333489b2009-03-27 23:10:48 +00001182 // C++ typename-specifier:
1183 case tok::kw_typename:
1184 if (TryAnnotateTypeOrScopeToken())
1185 continue;
1186 break;
1187
Chris Lattnere387d9e2009-01-21 19:48:37 +00001188 // GNU typeof support.
1189 case tok::kw_typeof:
1190 ParseTypeofSpecifier(DS);
1191 continue;
1192
Anders Carlsson74948d02009-06-24 17:47:40 +00001193 case tok::kw_decltype:
1194 ParseDecltypeSpecifier(DS);
1195 continue;
1196
Steve Naroffcfdf6162008-06-05 00:02:44 +00001197 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001198 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001199 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1200 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001201 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001202 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001203
Chris Lattner0974b232008-07-26 00:20:22 +00001204 {
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001205 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001206 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001207 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1208 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1209 LAngleLoc, EndProtoLoc);
1210 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1211 ProtocolLocs.data(), LAngleLoc);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001212 DS.SetRangeEnd(EndProtoLoc);
1213
Chris Lattner6d29c102008-11-18 07:48:38 +00001214 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner3a4e4312009-04-03 18:38:42 +00001215 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001216 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001217 // Need to support trailing type qualifiers (e.g. "id<p> const").
1218 // If a type specifier follows, it will be diagnosed elsewhere.
1219 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001220 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001221 }
John McCall49bfce42009-08-03 20:12:06 +00001222 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001223 if (isInvalid) {
1224 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001225 assert(DiagID);
Chris Lattner6d29c102008-11-18 07:48:38 +00001226 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001227 }
Chris Lattner2e232092008-03-13 06:29:04 +00001228 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001229 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001230 }
1231}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001232
Chris Lattnera448d752009-01-06 06:59:53 +00001233/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001234/// primarily follow the C++ grammar with additions for C99 and GNU,
1235/// which together subsume the C grammar. Note that the C++
1236/// type-specifier also includes the C type-qualifier (for const,
1237/// volatile, and C99 restrict). Returns true if a type-specifier was
1238/// found (and parsed), false otherwise.
1239///
1240/// type-specifier: [C++ 7.1.5]
1241/// simple-type-specifier
1242/// class-specifier
1243/// enum-specifier
1244/// elaborated-type-specifier [TODO]
1245/// cv-qualifier
1246///
1247/// cv-qualifier: [C++ 7.1.5.1]
1248/// 'const'
1249/// 'volatile'
1250/// [C99] 'restrict'
1251///
1252/// simple-type-specifier: [ C++ 7.1.5.2]
1253/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1254/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1255/// 'char'
1256/// 'wchar_t'
1257/// 'bool'
1258/// 'short'
1259/// 'int'
1260/// 'long'
1261/// 'signed'
1262/// 'unsigned'
1263/// 'float'
1264/// 'double'
1265/// 'void'
1266/// [C99] '_Bool'
1267/// [C99] '_Complex'
1268/// [C99] '_Imaginary' // Removed in TC2?
1269/// [GNU] '_Decimal32'
1270/// [GNU] '_Decimal64'
1271/// [GNU] '_Decimal128'
1272/// [GNU] typeof-specifier
1273/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1274/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001275/// [C++0x] 'decltype' ( expression )
John McCall49bfce42009-08-03 20:12:06 +00001276bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001277 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001278 unsigned &DiagID,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001279 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001280 SourceLocation Loc = Tok.getLocation();
1281
1282 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001283 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001284 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001285 // Annotate typenames and C++ scope specifiers. If we get one, just
1286 // recurse to handle whatever we get.
1287 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001288 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1289 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001290 // Otherwise, not a type specifier.
1291 return false;
1292 case tok::coloncolon: // ::foo::bar
1293 if (NextToken().is(tok::kw_new) || // ::new
1294 NextToken().is(tok::kw_delete)) // ::delete
1295 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001296
Chris Lattner020bab92009-01-04 23:41:41 +00001297 // Annotate typenames and C++ scope specifiers. If we get one, just
1298 // recurse to handle whatever we get.
1299 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001300 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1301 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001302 // Otherwise, not a type specifier.
1303 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001304
Douglas Gregor450c75a2008-11-07 15:42:26 +00001305 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001306 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001307 if (Tok.getAnnotationValue())
1308 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001309 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001310 else
1311 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001312 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1313 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001314
Douglas Gregor450c75a2008-11-07 15:42:26 +00001315 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1316 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1317 // Objective-C interface. If we don't have Objective-C or a '<', this is
1318 // just a normal reference to a typedef name.
1319 if (!Tok.is(tok::less) || !getLang().ObjC1)
1320 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001321
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001322 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001323 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001324 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1325 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1326 LAngleLoc, EndProtoLoc);
1327 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1328 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001329
Douglas Gregor450c75a2008-11-07 15:42:26 +00001330 DS.SetRangeEnd(EndProtoLoc);
1331 return true;
1332 }
1333
1334 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001335 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001336 break;
1337 case tok::kw_long:
1338 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001339 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1340 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001341 else
John McCall49bfce42009-08-03 20:12:06 +00001342 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1343 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001344 break;
1345 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001346 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001347 break;
1348 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001349 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1350 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001351 break;
1352 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001353 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1354 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001355 break;
1356 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001357 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1358 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001359 break;
1360 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001361 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001362 break;
1363 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001364 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001365 break;
1366 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001367 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001368 break;
1369 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001370 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001371 break;
1372 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001373 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001374 break;
1375 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001376 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001377 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001378 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001379 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001380 break;
1381 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001382 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001383 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001384 case tok::kw_bool:
1385 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001386 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001387 break;
1388 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001389 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1390 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001391 break;
1392 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001393 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1394 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001395 break;
1396 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001397 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1398 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001399 break;
1400
1401 // class-specifier:
1402 case tok::kw_class:
1403 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001404 case tok::kw_union: {
1405 tok::TokenKind Kind = Tok.getKind();
1406 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001407 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001408 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001409 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001410
1411 // enum-specifier:
1412 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001413 ConsumeToken();
1414 ParseEnumSpecifier(Loc, DS);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001415 return true;
1416
1417 // cv-qualifier:
1418 case tok::kw_const:
1419 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001420 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001421 break;
1422 case tok::kw_volatile:
1423 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001424 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001425 break;
1426 case tok::kw_restrict:
1427 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001428 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001429 break;
1430
1431 // GNU typeof support.
1432 case tok::kw_typeof:
1433 ParseTypeofSpecifier(DS);
1434 return true;
1435
Anders Carlsson74948d02009-06-24 17:47:40 +00001436 // C++0x decltype support.
1437 case tok::kw_decltype:
1438 ParseDecltypeSpecifier(DS);
1439 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001440
Anders Carlssonbae27372009-06-26 23:44:14 +00001441 // C++0x auto support.
1442 case tok::kw_auto:
1443 if (!getLang().CPlusPlus0x)
1444 return false;
1445
John McCall49bfce42009-08-03 20:12:06 +00001446 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001447 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001448 case tok::kw___ptr64:
1449 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001450 case tok::kw___cdecl:
1451 case tok::kw___stdcall:
1452 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001453 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001454 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001455
Douglas Gregor450c75a2008-11-07 15:42:26 +00001456 default:
1457 // Not a type-specifier; do nothing.
1458 return false;
1459 }
1460
1461 // If the specifier combination wasn't legal, issue a diagnostic.
1462 if (isInvalid) {
1463 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001464 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001465 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001466 }
1467 DS.SetRangeEnd(Tok.getLocation());
1468 ConsumeToken(); // whatever we parsed above.
1469 return true;
1470}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001471
Chris Lattner70ae4912007-10-29 04:42:53 +00001472/// ParseStructDeclaration - Parse a struct declaration without the terminating
1473/// semicolon.
1474///
Chris Lattner90a26b02007-01-23 04:38:16 +00001475/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001476/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001477/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001478/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001479/// struct-declarator-list:
1480/// struct-declarator
1481/// struct-declarator-list ',' struct-declarator
1482/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1483/// struct-declarator:
1484/// declarator
1485/// [GNU] declarator attributes[opt]
1486/// declarator[opt] ':' constant-expression
1487/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1488///
Chris Lattnera12405b2008-04-10 06:46:29 +00001489void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001490ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001491 if (Tok.is(tok::kw___extension__)) {
1492 // __extension__ silences extension warnings in the subexpression.
1493 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001494 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001495 return ParseStructDeclaration(DS, Fields);
1496 }
Mike Stump11289f42009-09-09 15:08:12 +00001497
Steve Naroff97170802007-08-20 22:28:22 +00001498 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001499 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001500 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001501
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001502 // If there are no declarators, this is a free-standing declaration
1503 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001504 if (Tok.is(tok::semi)) {
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001505 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001506 return;
1507 }
1508
1509 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001510 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001511 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001512 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001513 FieldDeclarator DeclaratorInfo(DS);
1514
1515 // Attributes are only allowed here on successive declarators.
1516 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1517 SourceLocation Loc;
1518 AttributeList *AttrList = ParseAttributes(&Loc);
1519 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1520 }
Mike Stump11289f42009-09-09 15:08:12 +00001521
Steve Naroff97170802007-08-20 22:28:22 +00001522 /// struct-declarator: declarator
1523 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +00001524 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +00001525 ParseDeclarator(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00001526
Chris Lattner76c72282007-10-09 17:33:22 +00001527 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001528 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001529 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001530 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001531 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001532 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001533 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001534 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001535
Steve Naroff97170802007-08-20 22:28:22 +00001536 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001537 if (Tok.is(tok::kw___attribute)) {
1538 SourceLocation Loc;
1539 AttributeList *AttrList = ParseAttributes(&Loc);
1540 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1541 }
1542
John McCallcfefb6d2009-11-03 02:38:08 +00001543 // We're done with this declarator; invoke the callback.
John McCall28a6aea2009-11-04 02:18:39 +00001544 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1545 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001546
Steve Naroff97170802007-08-20 22:28:22 +00001547 // If we don't have a comma, it is either the end of the list (a ';')
1548 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001549 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001550 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001551
Steve Naroff97170802007-08-20 22:28:22 +00001552 // Consume the comma.
1553 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001554
John McCallcfefb6d2009-11-03 02:38:08 +00001555 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001556 }
Steve Naroff97170802007-08-20 22:28:22 +00001557}
1558
1559/// ParseStructUnionBody
1560/// struct-contents:
1561/// struct-declaration-list
1562/// [EXT] empty
1563/// [GNU] "struct-declaration-list" without terminatoring ';'
1564/// struct-declaration-list:
1565/// struct-declaration
1566/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001567/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001568///
Chris Lattner1300fb92007-01-23 23:42:53 +00001569void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001570 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001571 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1572 PP.getSourceManager(),
1573 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001574
Chris Lattner90a26b02007-01-23 04:38:16 +00001575 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001576
Douglas Gregor658b9552009-01-09 22:42:13 +00001577 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001578 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1579
Chris Lattner7b9ace62007-01-23 20:11:08 +00001580 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1581 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001582 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001583 Diag(Tok, diag::ext_empty_struct_union_enum)
1584 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001585
Chris Lattner83f095c2009-03-28 19:18:32 +00001586 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001587
Chris Lattner7b9ace62007-01-23 20:11:08 +00001588 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001589 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001590 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001591
Chris Lattner736ed5d2007-06-09 05:59:07 +00001592 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001593 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001594 Diag(Tok, diag::ext_extra_struct_semi)
1595 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner36e46a22007-06-09 05:49:55 +00001596 ConsumeToken();
1597 continue;
1598 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001599
1600 // Parse all the comma separated declarators.
1601 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001602
John McCallcfefb6d2009-11-03 02:38:08 +00001603 if (!Tok.is(tok::at)) {
1604 struct CFieldCallback : FieldCallback {
1605 Parser &P;
1606 DeclPtrTy TagDecl;
1607 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1608
1609 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1610 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1611 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1612
1613 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001614 // Install the declarator into the current TagDecl.
John McCall5e6253b2009-11-03 21:13:47 +00001615 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1616 FD.D.getDeclSpec().getSourceRange().getBegin(),
1617 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001618 FieldDecls.push_back(Field);
1619 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001620 }
John McCallcfefb6d2009-11-03 02:38:08 +00001621 } Callback(*this, TagDecl, FieldDecls);
1622
1623 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001624 } else { // Handle @defs
1625 ConsumeToken();
1626 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1627 Diag(Tok, diag::err_unexpected_at);
1628 SkipUntil(tok::semi, true, true);
1629 continue;
1630 }
1631 ConsumeToken();
1632 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1633 if (!Tok.is(tok::identifier)) {
1634 Diag(Tok, diag::err_expected_ident);
1635 SkipUntil(tok::semi, true, true);
1636 continue;
1637 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001638 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump11289f42009-09-09 15:08:12 +00001639 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001640 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001641 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1642 ConsumeToken();
1643 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001644 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001645
Chris Lattner76c72282007-10-09 17:33:22 +00001646 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001647 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001648 } else if (Tok.is(tok::r_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001649 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001650 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001651 } else {
1652 Diag(Tok, diag::err_expected_semi_decl_list);
1653 // Skip to end of block or statement
1654 SkipUntil(tok::r_brace, true, true);
1655 }
1656 }
Mike Stump11289f42009-09-09 15:08:12 +00001657
Steve Naroff33a1e802007-10-29 21:38:07 +00001658 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001659
Steve Naroffb8371e12007-06-09 03:39:29 +00001660 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +00001661 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001662 if (Tok.is(tok::kw___attribute))
Daniel Dunbare4ac7a42008-10-03 16:42:10 +00001663 AttrList = ParseAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +00001664
1665 Actions.ActOnFields(CurScope,
Jay Foad7d0479f2009-05-21 09:52:38 +00001666 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001667 LBraceLoc, RBraceLoc,
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001668 AttrList);
1669 StructScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001670 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001671}
1672
1673
Chris Lattner3b561a32006-08-13 00:12:11 +00001674/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001675/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001676/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001677///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001678/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1679/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001680/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001681/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001682///
1683/// [C++] elaborated-type-specifier:
1684/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1685///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001686void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1687 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001688 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001689 if (Tok.is(tok::code_completion)) {
1690 // Code completion for an enum name.
1691 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1692 ConsumeToken();
1693 }
1694
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001695 AttributeList *Attr = 0;
1696 // If attributes exist after tag, parse them.
1697 if (Tok.is(tok::kw___attribute))
1698 Attr = ParseAttributes();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001699
1700 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001701 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001702 if (Tok.isNot(tok::identifier)) {
1703 Diag(Tok, diag::err_expected_ident);
1704 if (Tok.isNot(tok::l_brace)) {
1705 // Has no name and is not a definition.
1706 // Skip the rest of this declarator, up until the comma or semicolon.
1707 SkipUntil(tok::comma, true);
1708 return;
1709 }
1710 }
1711 }
Mike Stump11289f42009-09-09 15:08:12 +00001712
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001713 // Must have either 'enum name' or 'enum {...}'.
1714 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1715 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001716
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001717 // Skip the rest of this declarator, up until the comma or semicolon.
1718 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001719 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001720 }
Mike Stump11289f42009-09-09 15:08:12 +00001721
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001722 // If an identifier is present, consume and remember it.
1723 IdentifierInfo *Name = 0;
1724 SourceLocation NameLoc;
1725 if (Tok.is(tok::identifier)) {
1726 Name = Tok.getIdentifierInfo();
1727 NameLoc = ConsumeToken();
1728 }
Mike Stump11289f42009-09-09 15:08:12 +00001729
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001730 // There are three options here. If we have 'enum foo;', then this is a
1731 // forward declaration. If we have 'enum foo {...' then this is a
1732 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1733 //
1734 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1735 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1736 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1737 //
John McCall9bb74a52009-07-31 02:45:11 +00001738 Action::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001739 if (Tok.is(tok::l_brace))
John McCall9bb74a52009-07-31 02:45:11 +00001740 TUK = Action::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001741 else if (Tok.is(tok::semi))
John McCall9bb74a52009-07-31 02:45:11 +00001742 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001743 else
John McCall9bb74a52009-07-31 02:45:11 +00001744 TUK = Action::TUK_Reference;
Douglas Gregord6ab8742009-05-28 23:31:59 +00001745 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001746 bool IsDependent = false;
John McCall9bb74a52009-07-31 02:45:11 +00001747 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001748 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregor27bdf00f2009-07-23 16:36:45 +00001749 Action::MultiTemplateParamsArg(Actions),
John McCall7f41d982009-09-11 04:59:25 +00001750 Owned, IsDependent);
1751 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump11289f42009-09-09 15:08:12 +00001752
Chris Lattner76c72282007-10-09 17:33:22 +00001753 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001754 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001755
Chris Lattner3b561a32006-08-13 00:12:11 +00001756 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +00001757 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001758 unsigned DiagID;
1759 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001760 TagDecl.getAs<void>(), Owned))
John McCall49bfce42009-08-03 20:12:06 +00001761 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00001762}
1763
Chris Lattnerc1915e22007-01-25 07:29:02 +00001764/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1765/// enumerator-list:
1766/// enumerator
1767/// enumerator-list ',' enumerator
1768/// enumerator:
1769/// enumeration-constant
1770/// enumeration-constant '=' constant-expression
1771/// enumeration-constant:
1772/// identifier
1773///
Chris Lattner83f095c2009-03-28 19:18:32 +00001774void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001775 // Enter the scope of the enum body and start the definition.
1776 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001777 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00001778
Chris Lattnerc1915e22007-01-25 07:29:02 +00001779 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001780
Chris Lattner37256fb2007-08-27 17:24:30 +00001781 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00001782 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001783 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump11289f42009-09-09 15:08:12 +00001784
Chris Lattner83f095c2009-03-28 19:18:32 +00001785 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001786
Chris Lattner83f095c2009-03-28 19:18:32 +00001787 DeclPtrTy LastEnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001788
Chris Lattnerc1915e22007-01-25 07:29:02 +00001789 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001790 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001791 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1792 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001793
Chris Lattnerc1915e22007-01-25 07:29:02 +00001794 SourceLocation EqualLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001795 OwningExprResult AssignedVal(Actions);
Chris Lattner76c72282007-10-09 17:33:22 +00001796 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001797 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001798 AssignedVal = ParseConstantExpression();
1799 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00001800 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001801 }
Mike Stump11289f42009-09-09 15:08:12 +00001802
Chris Lattnerc1915e22007-01-25 07:29:02 +00001803 // Install the enumerator constant into EnumDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00001804 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1805 LastEnumConstDecl,
1806 IdentLoc, Ident,
1807 EqualLoc,
1808 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00001809 EnumConstantDecls.push_back(EnumConstDecl);
1810 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001811
Chris Lattner76c72282007-10-09 17:33:22 +00001812 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001813 break;
1814 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001815
1816 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00001817 !(getLang().C99 || getLang().CPlusPlus0x))
1818 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1819 << getLang().CPlusPlus
1820 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattnerc1915e22007-01-25 07:29:02 +00001821 }
Mike Stump11289f42009-09-09 15:08:12 +00001822
Chris Lattnerc1915e22007-01-25 07:29:02 +00001823 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00001824 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001825
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001826 AttributeList *Attr = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001827 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001828 if (Tok.is(tok::kw___attribute))
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001829 Attr = ParseAttributes();
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001830
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001831 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1832 EnumConstantDecls.data(), EnumConstantDecls.size(),
1833 CurScope, Attr);
Mike Stump11289f42009-09-09 15:08:12 +00001834
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001835 EnumScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001836 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001837}
Chris Lattner3b561a32006-08-13 00:12:11 +00001838
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001839/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001840/// start of a type-qualifier-list.
1841bool Parser::isTypeQualifier() const {
1842 switch (Tok.getKind()) {
1843 default: return false;
1844 // type-qualifier
1845 case tok::kw_const:
1846 case tok::kw_volatile:
1847 case tok::kw_restrict:
1848 return true;
1849 }
1850}
1851
1852/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001853/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001854bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001855 switch (Tok.getKind()) {
1856 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001857
Chris Lattner020bab92009-01-04 23:41:41 +00001858 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001859 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001860 // Annotate typenames and C++ scope specifiers. If we get one, just
1861 // recurse to handle whatever we get.
1862 if (TryAnnotateTypeOrScopeToken())
1863 return isTypeSpecifierQualifier();
1864 // Otherwise, not a type specifier.
1865 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001866
Chris Lattner020bab92009-01-04 23:41:41 +00001867 case tok::coloncolon: // ::foo::bar
1868 if (NextToken().is(tok::kw_new) || // ::new
1869 NextToken().is(tok::kw_delete)) // ::delete
1870 return false;
1871
1872 // Annotate typenames and C++ scope specifiers. If we get one, just
1873 // recurse to handle whatever we get.
1874 if (TryAnnotateTypeOrScopeToken())
1875 return isTypeSpecifierQualifier();
1876 // Otherwise, not a type specifier.
1877 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001878
Chris Lattnere37e2332006-08-15 04:50:22 +00001879 // GNU attributes support.
1880 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00001881 // GNU typeof support.
1882 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00001883
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001884 // type-specifiers
1885 case tok::kw_short:
1886 case tok::kw_long:
1887 case tok::kw_signed:
1888 case tok::kw_unsigned:
1889 case tok::kw__Complex:
1890 case tok::kw__Imaginary:
1891 case tok::kw_void:
1892 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001893 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001894 case tok::kw_char16_t:
1895 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001896 case tok::kw_int:
1897 case tok::kw_float:
1898 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001899 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001900 case tok::kw__Bool:
1901 case tok::kw__Decimal32:
1902 case tok::kw__Decimal64:
1903 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00001904
Chris Lattner861a2262008-04-13 18:59:07 +00001905 // struct-or-union-specifier (C99) or class-specifier (C++)
1906 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001907 case tok::kw_struct:
1908 case tok::kw_union:
1909 // enum-specifier
1910 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00001911
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001912 // type-qualifier
1913 case tok::kw_const:
1914 case tok::kw_volatile:
1915 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001916
1917 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001918 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001919 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001920
Chris Lattner409bf7d2008-10-20 00:25:30 +00001921 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1922 case tok::less:
1923 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00001924
Steve Naroff44ac7772008-12-25 14:16:32 +00001925 case tok::kw___cdecl:
1926 case tok::kw___stdcall:
1927 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001928 case tok::kw___w64:
1929 case tok::kw___ptr64:
1930 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001931 }
1932}
1933
Chris Lattneracd58a32006-08-06 17:24:14 +00001934/// isDeclarationSpecifier() - Return true if the current token is part of a
1935/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001936bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00001937 switch (Tok.getKind()) {
1938 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001939
Chris Lattner020bab92009-01-04 23:41:41 +00001940 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00001941 // Unfortunate hack to support "Class.factoryMethod" notation.
1942 if (getLang().ObjC1 && NextToken().is(tok::period))
1943 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001944 // Fall through
Steve Naroff9527bbf2009-03-09 21:12:44 +00001945
Douglas Gregor333489b2009-03-27 23:10:48 +00001946 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001947 // Annotate typenames and C++ scope specifiers. If we get one, just
1948 // recurse to handle whatever we get.
1949 if (TryAnnotateTypeOrScopeToken())
1950 return isDeclarationSpecifier();
1951 // Otherwise, not a declaration specifier.
1952 return false;
1953 case tok::coloncolon: // ::foo::bar
1954 if (NextToken().is(tok::kw_new) || // ::new
1955 NextToken().is(tok::kw_delete)) // ::delete
1956 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001957
Chris Lattner020bab92009-01-04 23:41:41 +00001958 // Annotate typenames and C++ scope specifiers. If we get one, just
1959 // recurse to handle whatever we get.
1960 if (TryAnnotateTypeOrScopeToken())
1961 return isDeclarationSpecifier();
1962 // Otherwise, not a declaration specifier.
1963 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001964
Chris Lattneracd58a32006-08-06 17:24:14 +00001965 // storage-class-specifier
1966 case tok::kw_typedef:
1967 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00001968 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00001969 case tok::kw_static:
1970 case tok::kw_auto:
1971 case tok::kw_register:
1972 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00001973
Chris Lattneracd58a32006-08-06 17:24:14 +00001974 // type-specifiers
1975 case tok::kw_short:
1976 case tok::kw_long:
1977 case tok::kw_signed:
1978 case tok::kw_unsigned:
1979 case tok::kw__Complex:
1980 case tok::kw__Imaginary:
1981 case tok::kw_void:
1982 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001983 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001984 case tok::kw_char16_t:
1985 case tok::kw_char32_t:
1986
Chris Lattneracd58a32006-08-06 17:24:14 +00001987 case tok::kw_int:
1988 case tok::kw_float:
1989 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001990 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00001991 case tok::kw__Bool:
1992 case tok::kw__Decimal32:
1993 case tok::kw__Decimal64:
1994 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00001995
Chris Lattner861a2262008-04-13 18:59:07 +00001996 // struct-or-union-specifier (C99) or class-specifier (C++)
1997 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00001998 case tok::kw_struct:
1999 case tok::kw_union:
2000 // enum-specifier
2001 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002002
Chris Lattneracd58a32006-08-06 17:24:14 +00002003 // type-qualifier
2004 case tok::kw_const:
2005 case tok::kw_volatile:
2006 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002007
Chris Lattneracd58a32006-08-06 17:24:14 +00002008 // function-specifier
2009 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002010 case tok::kw_virtual:
2011 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002012
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002013 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002014 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002015
Chris Lattner599e47e2007-08-09 17:01:07 +00002016 // GNU typeof support.
2017 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002018
Chris Lattner599e47e2007-08-09 17:01:07 +00002019 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002020 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002021 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002022
Chris Lattner8b2ec162008-07-26 03:38:44 +00002023 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2024 case tok::less:
2025 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002026
Steve Narofff192fab2009-01-06 19:34:12 +00002027 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002028 case tok::kw___cdecl:
2029 case tok::kw___stdcall:
2030 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00002031 case tok::kw___w64:
2032 case tok::kw___ptr64:
2033 case tok::kw___forceinline:
2034 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002035 }
2036}
2037
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002038
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002039/// ParseTypeQualifierListOpt
2040/// type-qualifier-list: [C99 6.7.5]
2041/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002042/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002043/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002044/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002045///
Chris Lattnercf0bab22008-12-18 07:02:59 +00002046void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002047 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002048 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002049 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002050 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002051 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002052
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002053 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002054 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002055 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2056 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002057 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002058 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002059 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2060 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002061 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002062 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002063 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2064 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002065 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002066 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002067 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002068 case tok::kw___cdecl:
2069 case tok::kw___stdcall:
2070 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00002071 if (AttributesAllowed) {
2072 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2073 continue;
2074 }
2075 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002076 case tok::kw___attribute:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002077 if (AttributesAllowed) {
2078 DS.AddAttributes(ParseAttributes());
2079 continue; // do *not* consume the next token!
2080 }
2081 // otherwise, FALL THROUGH!
2082 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002083 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002084 // If this is not a type-qualifier token, we're done reading type
2085 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002086 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002087 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002088 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002089
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002090 // If the specifier combination wasn't legal, issue a diagnostic.
2091 if (isInvalid) {
2092 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002093 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002094 }
2095 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002096 }
2097}
2098
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002099
2100/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2101///
2102void Parser::ParseDeclarator(Declarator &D) {
2103 /// This implements the 'declarator' production in the C grammar, then checks
2104 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002105 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002106}
2107
Sebastian Redlbd150f42008-11-21 19:14:01 +00002108/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2109/// is parsed by the function passed to it. Pass null, and the direct-declarator
2110/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002111/// ptr-operator production.
2112///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002113/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2114/// [C] pointer[opt] direct-declarator
2115/// [C++] direct-declarator
2116/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002117///
2118/// pointer: [C99 6.7.5]
2119/// '*' type-qualifier-list[opt]
2120/// '*' type-qualifier-list[opt] pointer
2121///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002122/// ptr-operator:
2123/// '*' cv-qualifier-seq[opt]
2124/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002125/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002126/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002127/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002128/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002129void Parser::ParseDeclaratorInternal(Declarator &D,
2130 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002131 if (Diags.hasAllExtensionsSilenced())
2132 D.setExtension();
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002133 // C++ member pointers start with a '::' or a nested-name.
2134 // Member pointers get special handling, since there's no place for the
2135 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002136 if (getLang().CPlusPlus &&
2137 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2138 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002139 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00002140 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump11289f42009-09-09 15:08:12 +00002141 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002142 // The scope spec really belongs to the direct-declarator.
2143 D.getCXXScopeSpec() = SS;
2144 if (DirectDeclParser)
2145 (this->*DirectDeclParser)(D);
2146 return;
2147 }
2148
2149 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002150 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002151 DeclSpec DS;
2152 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002153 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002154
2155 // Recurse to parse whatever is left.
2156 ParseDeclaratorInternal(D, DirectDeclParser);
2157
2158 // Sema will have to catch (syntactically invalid) pointers into global
2159 // scope. It has to catch pointers into namespace scope anyway.
2160 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002161 Loc, DS.TakeAttributes()),
2162 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002163 return;
2164 }
2165 }
2166
2167 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002168 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002169 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002170 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002171 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002172 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002173 if (DirectDeclParser)
2174 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002175 return;
2176 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002177
Sebastian Redled0f3b02009-03-15 22:02:01 +00002178 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2179 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002180 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002181 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002182
Chris Lattner9eac9312009-03-27 04:18:06 +00002183 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002184 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002185 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002186
Bill Wendling3708c182007-05-27 10:15:43 +00002187 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002188 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002189
Bill Wendling3708c182007-05-27 10:15:43 +00002190 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002191 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002192 if (Kind == tok::star)
2193 // Remember that we parsed a pointer type, and remember the type-quals.
2194 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002195 DS.TakeAttributes()),
2196 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002197 else
2198 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002199 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002200 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002201 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002202 } else {
2203 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002204 DeclSpec DS;
2205
Sebastian Redl3b27be62009-03-23 00:00:23 +00002206 // Complain about rvalue references in C++03, but then go on and build
2207 // the declarator.
2208 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2209 Diag(Loc, diag::err_rvalue_reference);
2210
Bill Wendling93efb222007-06-02 23:28:54 +00002211 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2212 // cv-qualifiers are introduced through the use of a typedef or of a
2213 // template type argument, in which case the cv-qualifiers are ignored.
2214 //
2215 // [GNU] Retricted references are allowed.
2216 // [GNU] Attributes on references are allowed.
2217 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002218 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002219
2220 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2221 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2222 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002223 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002224 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2225 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002226 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002227 }
Bill Wendling3708c182007-05-27 10:15:43 +00002228
2229 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002230 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002231
Douglas Gregor66583c52008-11-03 15:51:28 +00002232 if (D.getNumTypeObjects() > 0) {
2233 // C++ [dcl.ref]p4: There shall be no references to references.
2234 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2235 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002236 if (const IdentifierInfo *II = D.getIdentifier())
2237 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2238 << II;
2239 else
2240 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2241 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002242
Sebastian Redlbd150f42008-11-21 19:14:01 +00002243 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002244 // can go ahead and build the (technically ill-formed)
2245 // declarator: reference collapsing will take care of it.
2246 }
2247 }
2248
Bill Wendling3708c182007-05-27 10:15:43 +00002249 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002250 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002251 DS.TakeAttributes(),
2252 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002253 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002254 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002255}
2256
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002257/// ParseDirectDeclarator
2258/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002259/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002260/// '(' declarator ')'
2261/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002262/// [C90] direct-declarator '[' constant-expression[opt] ']'
2263/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2264/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2265/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2266/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002267/// direct-declarator '(' parameter-type-list ')'
2268/// direct-declarator '(' identifier-list[opt] ')'
2269/// [GNU] direct-declarator '(' parameter-forward-declarations
2270/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002271/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2272/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002273/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002274///
2275/// declarator-id: [C++ 8]
2276/// id-expression
2277/// '::'[opt] nested-name-specifier[opt] type-name
2278///
2279/// id-expression: [C++ 5.1]
2280/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002281/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002282///
2283/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002284/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002285/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002286/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002287/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002288/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002289///
Chris Lattneracd58a32006-08-06 17:24:14 +00002290void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002291 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002292
Douglas Gregor7861a802009-11-03 01:35:08 +00002293 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2294 // ParseDeclaratorInternal might already have parsed the scope.
2295 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2296 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2297 true);
2298 if (afterCXXScope) {
2299 // Change the declaration context for name lookup, until this function
2300 // is exited (and the declarator has been parsed).
2301 DeclScopeObj.EnterDeclaratorScope();
2302 }
2303
2304 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2305 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2306 // We found something that indicates the start of an unqualified-id.
2307 // Parse that unqualified-id.
2308 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2309 /*EnteringContext=*/true,
2310 /*AllowDestructorName=*/true,
2311 /*AllowConstructorName=*/!D.getDeclSpec().hasTypeSpecifier(),
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002312 /*ObjectType=*/0,
Douglas Gregor7861a802009-11-03 01:35:08 +00002313 D.getName())) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002314 D.SetIdentifier(0, Tok.getLocation());
2315 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002316 } else {
2317 // Parsed the unqualified-id; update range information and move along.
2318 if (D.getSourceRange().getBegin().isInvalid())
2319 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2320 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002321 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002322 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002323 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002324 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002325 assert(!getLang().CPlusPlus &&
2326 "There's a C++-specific check for tok::identifier above");
2327 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2328 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2329 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002330 goto PastIdentifier;
2331 }
2332
2333 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002334 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002335 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002336 // Example: 'char (*X)' or 'int (*XX)(void)'
2337 ParseParenDeclarator(D);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002338 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002339 // This could be something simple like "int" (in which case the declarator
2340 // portion is empty), if an abstract-declarator is allowed.
2341 D.SetIdentifier(0, Tok.getLocation());
2342 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002343 if (D.getContext() == Declarator::MemberContext)
2344 Diag(Tok, diag::err_expected_member_name_or_semi)
2345 << D.getDeclSpec().getSourceRange();
2346 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002347 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002348 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002349 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002350 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002351 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002352 }
Mike Stump11289f42009-09-09 15:08:12 +00002353
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002354 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002355 assert(D.isPastIdentifier() &&
2356 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002357
Chris Lattneracd58a32006-08-06 17:24:14 +00002358 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002359 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002360 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2361 // In such a case, check if we actually have a function declarator; if it
2362 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002363 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2364 // When not in file scope, warn for ambiguous function declarators, just
2365 // in case the author intended it as a variable definition.
2366 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2367 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2368 break;
2369 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002370 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002371 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002372 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002373 } else {
2374 break;
2375 }
2376 }
2377}
2378
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002379/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2380/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002381/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002382/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2383///
2384/// direct-declarator:
2385/// '(' declarator ')'
2386/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002387/// direct-declarator '(' parameter-type-list ')'
2388/// direct-declarator '(' identifier-list[opt] ')'
2389/// [GNU] direct-declarator '(' parameter-forward-declarations
2390/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002391///
2392void Parser::ParseParenDeclarator(Declarator &D) {
2393 SourceLocation StartLoc = ConsumeParen();
2394 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002395
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002396 // Eat any attributes before we look at whether this is a grouping or function
2397 // declarator paren. If this is a grouping paren, the attribute applies to
2398 // the type being built up, for example:
2399 // int (__attribute__(()) *x)(long y)
2400 // If this ends up not being a grouping paren, the attribute applies to the
2401 // first argument, for example:
2402 // int (__attribute__(()) int x)
2403 // In either case, we need to eat any attributes to be able to determine what
2404 // sort of paren this is.
2405 //
2406 AttributeList *AttrList = 0;
2407 bool RequiresArg = false;
2408 if (Tok.is(tok::kw___attribute)) {
2409 AttrList = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +00002410
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002411 // We require that the argument list (if this is a non-grouping paren) be
2412 // present even if the attribute list was empty.
2413 RequiresArg = true;
2414 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002415 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002416 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2417 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2418 Tok.is(tok::kw___ptr64)) {
2419 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2420 }
Mike Stump11289f42009-09-09 15:08:12 +00002421
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002422 // If we haven't past the identifier yet (or where the identifier would be
2423 // stored, if this is an abstract declarator), then this is probably just
2424 // grouping parens. However, if this could be an abstract-declarator, then
2425 // this could also be the start of function arguments (consider 'void()').
2426 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002427
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002428 if (!D.mayOmitIdentifier()) {
2429 // If this can't be an abstract-declarator, this *must* be a grouping
2430 // paren, because we haven't seen the identifier yet.
2431 isGrouping = true;
2432 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002433 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002434 isDeclarationSpecifier()) { // 'int(int)' is a function.
2435 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2436 // considered to be a type, not a K&R identifier-list.
2437 isGrouping = false;
2438 } else {
2439 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2440 isGrouping = true;
2441 }
Mike Stump11289f42009-09-09 15:08:12 +00002442
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002443 // If this is a grouping paren, handle:
2444 // direct-declarator: '(' declarator ')'
2445 // direct-declarator: '(' attributes declarator ')'
2446 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002447 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002448 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002449 if (AttrList)
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002450 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002451
Sebastian Redlbd150f42008-11-21 19:14:01 +00002452 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002453 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002454 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002455
2456 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002457 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002458 return;
2459 }
Mike Stump11289f42009-09-09 15:08:12 +00002460
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002461 // Okay, if this wasn't a grouping paren, it must be the start of a function
2462 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002463 // identifier (and remember where it would have been), then call into
2464 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002465 D.SetIdentifier(0, Tok.getLocation());
2466
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002467 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002468}
2469
2470/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2471/// declarator D up to a paren, which indicates that we are parsing function
2472/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002473///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002474/// If AttrList is non-null, then the caller parsed those arguments immediately
2475/// after the open paren - they should be considered to be the first argument of
2476/// a parameter. If RequiresArg is true, then the first argument of the
2477/// function is required to be present and required to not be an identifier
2478/// list.
2479///
Chris Lattneracd58a32006-08-06 17:24:14 +00002480/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002481/// parameter-type-list: [C99 6.7.5]
2482/// parameter-list
2483/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002484/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002485///
2486/// parameter-list: [C99 6.7.5]
2487/// parameter-declaration
2488/// parameter-list ',' parameter-declaration
2489///
2490/// parameter-declaration: [C99 6.7.5]
2491/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002492/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002493/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002494/// declaration-specifiers abstract-declarator[opt]
2495/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002496/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002497/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002498///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002499/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002500/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002501///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002502void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2503 AttributeList *AttrList,
2504 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002505 // lparen is already consumed!
2506 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002507
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002508 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002509 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002510 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002511 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002512 delete AttrList;
2513 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002514
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002515 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2516 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002517
2518 // cv-qualifier-seq[opt].
2519 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002520 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002521 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002522 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002523 llvm::SmallVector<TypeTy*, 2> Exceptions;
2524 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002525 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002526 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002527 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002528 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002529
2530 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002531 if (Tok.is(tok::kw_throw)) {
2532 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002533 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002534 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002535 hasAnyExceptionSpec);
2536 assert(Exceptions.size() == ExceptionRanges.size() &&
2537 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002538 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002539 }
2540
Chris Lattner371ed4e2008-04-06 06:57:35 +00002541 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002542 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002543 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002544 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002545 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002546 /*arglist*/ 0, 0,
2547 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002548 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002549 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002550 Exceptions.data(),
2551 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002552 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002553 LParenLoc, RParenLoc, D),
2554 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002555 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002556 }
2557
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002558 // Alternatively, this parameter list may be an identifier list form for a
2559 // K&R-style function: void foo(a,b,c)
Steve Naroffb0486722009-01-28 19:16:40 +00002560 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff3b6a4bd2009-01-30 14:23:32 +00002561 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002562 // K&R identifier lists can't have typedefs as identifiers, per
2563 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002564 if (RequiresArg) {
2565 Diag(Tok, diag::err_argument_required_after_attribute);
2566 delete AttrList;
2567 }
Steve Naroffb0486722009-01-28 19:16:40 +00002568 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2569 // normal declarators, not for abstract-declarators.
2570 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002571 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002572 }
Mike Stump11289f42009-09-09 15:08:12 +00002573
Chris Lattner371ed4e2008-04-06 06:57:35 +00002574 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00002575
Chris Lattner371ed4e2008-04-06 06:57:35 +00002576 // Build up an array of information about the parsed arguments.
2577 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002578
2579 // Enter function-declaration scope, limiting any declarators to the
2580 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00002581 ParseScope PrototypeScope(this,
2582 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00002583
Chris Lattner371ed4e2008-04-06 06:57:35 +00002584 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002585 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00002586 while (1) {
2587 if (Tok.is(tok::ellipsis)) {
2588 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002589 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002590 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00002591 }
Mike Stump11289f42009-09-09 15:08:12 +00002592
Chris Lattner371ed4e2008-04-06 06:57:35 +00002593 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002594
Chris Lattner371ed4e2008-04-06 06:57:35 +00002595 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00002596 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002597 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002598
2599 // If the caller parsed attributes for the first argument, add them now.
2600 if (AttrList) {
2601 DS.AddAttributes(AttrList);
2602 AttrList = 0; // Only apply the attributes to the first parameter.
2603 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002604 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002605
Chris Lattner371ed4e2008-04-06 06:57:35 +00002606 // Parse the declarator. This is "PrototypeContext", because we must
2607 // accept either 'declarator' or 'abstract-declarator' here.
2608 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2609 ParseDeclarator(ParmDecl);
2610
2611 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002612 if (Tok.is(tok::kw___attribute)) {
2613 SourceLocation Loc;
2614 AttributeList *AttrList = ParseAttributes(&Loc);
2615 ParmDecl.AddAttributes(AttrList, Loc);
2616 }
Mike Stump11289f42009-09-09 15:08:12 +00002617
Chris Lattner371ed4e2008-04-06 06:57:35 +00002618 // Remember this parsed parameter in ParamInfo.
2619 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00002620
Douglas Gregor4d87df52008-12-16 21:30:33 +00002621 // DefArgToks is used when the parsing of default arguments needs
2622 // to be delayed.
2623 CachedTokens *DefArgToks = 0;
2624
Chris Lattner371ed4e2008-04-06 06:57:35 +00002625 // If no parameter was specified, verify that *something* was specified,
2626 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002627 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2628 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00002629 // Completely missing, emit error.
2630 Diag(DSStart, diag::err_missing_param);
2631 } else {
2632 // Otherwise, we have something. Add it and let semantic analysis try
2633 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00002634
Chris Lattner371ed4e2008-04-06 06:57:35 +00002635 // Inform the actions module about the parameter declarator, so it gets
2636 // added to the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002637 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002638
2639 // Parse the default argument, if any. We parse the default
2640 // arguments in all dialects; the semantic analysis in
2641 // ActOnParamDefaultArgument will reject the default argument in
2642 // C.
2643 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00002644 SourceLocation EqualLoc = Tok.getLocation();
2645
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002646 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00002647 if (D.getContext() == Declarator::MemberContext) {
2648 // If we're inside a class definition, cache the tokens
2649 // corresponding to the default argument. We'll actually parse
2650 // them when we see the end of the class definition.
2651 // FIXME: Templates will require something similar.
2652 // FIXME: Can we use a smart pointer for Toks?
2653 DefArgToks = new CachedTokens;
2654
Mike Stump11289f42009-09-09 15:08:12 +00002655 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002656 tok::semi, false)) {
2657 delete DefArgToks;
2658 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00002659 Actions.ActOnParamDefaultArgumentError(Param);
2660 } else
Mike Stump11289f42009-09-09 15:08:12 +00002661 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00002662 (*DefArgToks)[1].getLocation());
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002663 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00002664 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00002665 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002666
Douglas Gregor4d87df52008-12-16 21:30:33 +00002667 OwningExprResult DefArgResult(ParseAssignmentExpression());
2668 if (DefArgResult.isInvalid()) {
2669 Actions.ActOnParamDefaultArgumentError(Param);
2670 SkipUntil(tok::comma, tok::r_paren, true, true);
2671 } else {
2672 // Inform the actions module about the default argument
2673 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002674 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00002675 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002676 }
2677 }
Mike Stump11289f42009-09-09 15:08:12 +00002678
2679 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2680 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002681 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00002682 }
2683
2684 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002685 if (Tok.isNot(tok::comma)) {
2686 if (Tok.is(tok::ellipsis)) {
2687 IsVariadic = true;
2688 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2689
2690 if (!getLang().CPlusPlus) {
2691 // We have ellipsis without a preceding ',', which is ill-formed
2692 // in C. Complain and provide the fix.
2693 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2694 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2695 }
2696 }
2697
2698 break;
2699 }
Mike Stump11289f42009-09-09 15:08:12 +00002700
Chris Lattner371ed4e2008-04-06 06:57:35 +00002701 // Consume the comma.
2702 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00002703 }
Mike Stump11289f42009-09-09 15:08:12 +00002704
Chris Lattner371ed4e2008-04-06 06:57:35 +00002705 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002706 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00002707
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002708 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002709 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2710 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002711
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002712 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002713 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002714 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002715 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002716 llvm::SmallVector<TypeTy*, 2> Exceptions;
2717 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002718 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002719 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00002720 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002721 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002722 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002723
2724 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002725 if (Tok.is(tok::kw_throw)) {
2726 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002727 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002728 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002729 hasAnyExceptionSpec);
2730 assert(Exceptions.size() == ExceptionRanges.size() &&
2731 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002732 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002733 }
2734
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002735 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002736 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002737 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002738 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002739 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002740 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002741 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002742 Exceptions.data(),
2743 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002744 Exceptions.size(),
2745 LParenLoc, RParenLoc, D),
2746 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002747}
Chris Lattneracd58a32006-08-06 17:24:14 +00002748
Chris Lattner6c940e62008-04-06 06:34:08 +00002749/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2750/// we found a K&R-style identifier list instead of a type argument list. The
2751/// current token is known to be the first identifier in the list.
2752///
2753/// identifier-list: [C99 6.7.5]
2754/// identifier
2755/// identifier-list ',' identifier
2756///
2757void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2758 Declarator &D) {
2759 // Build up an array of information about the parsed arguments.
2760 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2761 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00002762
Chris Lattner6c940e62008-04-06 06:34:08 +00002763 // If there was no identifier specified for the declarator, either we are in
2764 // an abstract-declarator, or we are in a parameter declarator which was found
2765 // to be abstract. In abstract-declarators, identifier lists are not valid:
2766 // diagnose this.
2767 if (!D.getIdentifier())
2768 Diag(Tok, diag::ext_ident_list_in_param);
2769
2770 // Tok is known to be the first identifier in the list. Remember this
2771 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00002772 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00002773 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner83f095c2009-03-28 19:18:32 +00002774 Tok.getLocation(),
2775 DeclPtrTy()));
Mike Stump11289f42009-09-09 15:08:12 +00002776
Chris Lattner9186f552008-04-06 06:39:19 +00002777 ConsumeToken(); // eat the first identifier.
Mike Stump11289f42009-09-09 15:08:12 +00002778
Chris Lattner6c940e62008-04-06 06:34:08 +00002779 while (Tok.is(tok::comma)) {
2780 // Eat the comma.
2781 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002782
Chris Lattner9186f552008-04-06 06:39:19 +00002783 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00002784 if (Tok.isNot(tok::identifier)) {
2785 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00002786 SkipUntil(tok::r_paren);
2787 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00002788 }
Chris Lattner67b450c2008-04-06 06:47:48 +00002789
Chris Lattner6c940e62008-04-06 06:34:08 +00002790 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00002791
2792 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00002793 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerebad6a22008-11-19 07:37:42 +00002794 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00002795
Chris Lattner6c940e62008-04-06 06:34:08 +00002796 // Verify that the argument identifier has not already been mentioned.
2797 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002798 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00002799 } else {
2800 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00002801 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00002802 Tok.getLocation(),
2803 DeclPtrTy()));
Chris Lattner9186f552008-04-06 06:39:19 +00002804 }
Mike Stump11289f42009-09-09 15:08:12 +00002805
Chris Lattner6c940e62008-04-06 06:34:08 +00002806 // Eat the identifier.
2807 ConsumeToken();
2808 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002809
2810 // If we have the closing ')', eat it and we're done.
2811 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2812
Chris Lattner9186f552008-04-06 06:39:19 +00002813 // Remember that we parsed a function type, and remember the attributes. This
2814 // function type is always a K&R style function type, which is not varargs and
2815 // has no prototype.
2816 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002817 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00002818 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002819 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002820 /*exception*/false,
2821 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002822 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002823 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00002824}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002825
Chris Lattnere8074e62006-08-06 18:30:15 +00002826/// [C90] direct-declarator '[' constant-expression[opt] ']'
2827/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2828/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2829/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2830/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2831void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00002832 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00002833
Chris Lattner84a11622008-12-18 07:27:21 +00002834 // C array syntax has many features, but by-far the most common is [] and [4].
2835 // This code does a fast path to handle some of the most obvious cases.
2836 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002837 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002838 // Remember that we parsed the empty array type.
2839 OwningExprResult NumElements(Actions);
Douglas Gregor04318252009-07-06 15:59:29 +00002840 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2841 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002842 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002843 return;
2844 } else if (Tok.getKind() == tok::numeric_constant &&
2845 GetLookAheadToken(1).is(tok::r_square)) {
2846 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00002847 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00002848 ConsumeToken();
2849
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002850 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002851
2852 // If there was an error parsing the assignment-expression, recover.
2853 if (ExprRes.isInvalid())
2854 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00002855
Chris Lattner84a11622008-12-18 07:27:21 +00002856 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00002857 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2858 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002859 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002860 return;
2861 }
Mike Stump11289f42009-09-09 15:08:12 +00002862
Chris Lattnere8074e62006-08-06 18:30:15 +00002863 // If valid, this location is the position where we read the 'static' keyword.
2864 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00002865 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002866 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002867
Chris Lattnere8074e62006-08-06 18:30:15 +00002868 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002869 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00002870 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00002871 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00002872
Chris Lattnere8074e62006-08-06 18:30:15 +00002873 // If we haven't already read 'static', check to see if there is one after the
2874 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002875 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002876 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002877
Chris Lattnere8074e62006-08-06 18:30:15 +00002878 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00002879 bool isStar = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +00002880 OwningExprResult NumElements(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00002881
Chris Lattner521ff2b2008-04-06 05:26:30 +00002882 // Handle the case where we have '[*]' as the array size. However, a leading
2883 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2884 // the the token after the star is a ']'. Since stars in arrays are
2885 // infrequent, use of lookahead is not costly here.
2886 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00002887 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00002888
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002889 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00002890 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002891 StaticLoc = SourceLocation(); // Drop the static.
2892 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00002893 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00002894 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00002895 // Note, in C89, this production uses the constant-expr production instead
2896 // of assignment-expr. The only difference is that assignment-expr allows
2897 // things like '=' and '*='. Sema rejects these in C89 mode because they
2898 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00002899
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00002900 // Parse the constant-expression or assignment-expression now (depending
2901 // on dialect).
2902 if (getLang().CPlusPlus)
2903 NumElements = ParseConstantExpression();
2904 else
2905 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00002906 }
Mike Stump11289f42009-09-09 15:08:12 +00002907
Chris Lattner62591722006-08-12 18:40:58 +00002908 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002909 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00002910 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00002911 // If the expression was invalid, skip it.
2912 SkipUntil(tok::r_square);
2913 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00002914 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002915
2916 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2917
Chris Lattner84a11622008-12-18 07:27:21 +00002918 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00002919 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2920 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00002921 NumElements.release(),
2922 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002923 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00002924}
2925
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002926/// [GNU] typeof-specifier:
2927/// typeof ( expressions )
2928/// typeof ( type-name )
2929/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00002930///
2931void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00002932 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002933 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00002934 SourceLocation StartLoc = ConsumeToken();
2935
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002936 bool isCastExpr;
2937 TypeTy *CastTy;
2938 SourceRange CastRange;
2939 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2940 isCastExpr,
2941 CastTy,
2942 CastRange);
2943
2944 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002945 // FIXME: Not accurate, the range gets one token more than it should.
2946 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002947 else
2948 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00002949
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002950 if (isCastExpr) {
2951 if (!CastTy) {
2952 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002953 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00002954 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002955
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002956 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002957 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002958 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2959 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002960 DiagID, CastTy))
2961 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002962 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002963 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002964
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002965 // If we get here, the operand to the typeof was an expresion.
2966 if (Operand.isInvalid()) {
2967 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00002968 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00002969 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002970
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002971 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002972 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002973 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2974 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002975 DiagID, Operand.release()))
2976 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00002977}