blob: 4ca5b48e8b671b25f5a05e9af2e4a2d2cc0609a2 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner60f36222009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner1a76a3c2007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Chris Lattnerf02ef3e2008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000018#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000019using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.7: Declarations.
23//===----------------------------------------------------------------------===//
24
Chris Lattnerf5fbd792006-08-10 23:56:11 +000025/// ParseTypeName
26/// type-name: [C99 6.7.6]
27/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000028///
29/// Called type-id in C++.
Sebastian Redld6434562009-05-29 18:02:33 +000030Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000031 // Parse the common declaration-specifiers piece.
32 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000033 ParseSpecifierQualifierList(DS);
Sebastian Redld6434562009-05-29 18:02:33 +000034
Chris Lattnerf5fbd792006-08-10 23:56:11 +000035 // Parse the abstract-declarator, if present.
36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
37 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000038 if (Range)
39 *Range = DeclaratorInfo.getSourceRange();
40
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000041 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000042 return true;
43
44 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000045}
46
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000047/// ParseAttributes - Parse a non-empty attributes list.
48///
49/// [GNU] attributes:
50/// attribute
51/// attributes attribute
52///
53/// [GNU] attribute:
54/// '__attribute__' '(' '(' attribute-list ')' ')'
55///
56/// [GNU] attribute-list:
57/// attrib
58/// attribute_list ',' attrib
59///
60/// [GNU] attrib:
61/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000062/// attrib-name
63/// attrib-name '(' identifier ')'
64/// attrib-name '(' identifier ',' nonempty-expr-list ')'
65/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000066///
Steve Naroff0f2fe172007-06-01 17:11:19 +000067/// [GNU] attrib-name:
68/// identifier
69/// typespec
70/// typequal
71/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000072///
Steve Naroff0f2fe172007-06-01 17:11:19 +000073/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000074/// token lookahead. Comment from gcc: "If they start with an identifier
75/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +000076/// start with that identifier; otherwise they are an expression list."
77///
78/// At the moment, I am not doing 2 token lookahead. I am also unaware of
79/// any attributes that don't work (based on my limited testing). Most
80/// attributes are very simple in practice. Until we find a bug, I don't see
81/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000082
Sebastian Redlf6591ca2009-02-09 18:23:29 +000083AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner76c72282007-10-09 17:33:22 +000084 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000085
Steve Naroffb8371e12007-06-09 03:39:29 +000086 AttributeList *CurrAttr = 0;
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner76c72282007-10-09 17:33:22 +000088 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000089 ConsumeToken();
90 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
91 "attribute")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
96 SkipUntil(tok::r_paren, true); // skip until ) or ;
97 return CurrAttr;
98 }
99 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000100 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
101 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000102
103 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000104 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
105 ConsumeToken();
106 continue;
107 }
108 // we have an identifier or declaration specifier (const, int, etc.)
109 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
110 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000111
Steve Naroff0f2fe172007-06-01 17:11:19 +0000112 // check if we have a "paramterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000113 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000114 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000115
Chris Lattner76c72282007-10-09 17:33:22 +0000116 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000117 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
118 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000119
120 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000121 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000122 ConsumeParen(); // ignore the right paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000123 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000124 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000125 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000126 ConsumeToken();
127 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000128 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000129 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000130
Steve Naroff0f2fe172007-06-01 17:11:19 +0000131 // now parse the non-empty comma separated list of expressions
132 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000133 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000134 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000135 ArgExprsOk = false;
136 SkipUntil(tok::r_paren);
137 break;
138 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000139 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000140 }
Chris Lattner76c72282007-10-09 17:33:22 +0000141 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000142 break;
143 ConsumeToken(); // Eat the comma, move to the next argument
144 }
Chris Lattner76c72282007-10-09 17:33:22 +0000145 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000146 ConsumeParen(); // ignore the right paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000147 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redl511ed552008-11-25 22:21:31 +0000148 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000149 }
150 }
151 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000152 switch (Tok.getKind()) {
153 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000154 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000155 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000156 ConsumeParen(); // ignore the right paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000157 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000158 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begemanf2758702009-06-26 06:32:41 +0000159 break;
160 case tok::kw_char:
161 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000162 case tok::kw_char16_t:
163 case tok::kw_char32_t:
Nate Begemanf2758702009-06-26 06:32:41 +0000164 case tok::kw_bool:
165 case tok::kw_short:
166 case tok::kw_int:
167 case tok::kw_long:
168 case tok::kw_signed:
169 case tok::kw_unsigned:
170 case tok::kw_float:
171 case tok::kw_double:
172 case tok::kw_void:
173 case tok::kw_typeof:
174 // If it's a builtin type name, eat it and expect a rparen
175 // __attribute__(( vec_type_hint(char) ))
176 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000177 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Nate Begemanf2758702009-06-26 06:32:41 +0000178 0, SourceLocation(), 0, 0, CurrAttr);
179 if (Tok.is(tok::r_paren))
180 ConsumeParen();
181 break;
182 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000183 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000184 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000185 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000186
Steve Naroff0f2fe172007-06-01 17:11:19 +0000187 // now parse the list of expressions
188 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000189 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000190 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000191 ArgExprsOk = false;
192 SkipUntil(tok::r_paren);
193 break;
194 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000195 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000196 }
Chris Lattner76c72282007-10-09 17:33:22 +0000197 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000198 break;
199 ConsumeToken(); // Eat the comma, move to the next argument
200 }
201 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000202 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000203 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl511ed552008-11-25 22:21:31 +0000204 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
205 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Steve Naroffb8371e12007-06-09 03:39:29 +0000206 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000207 }
Nate Begemanf2758702009-06-26 06:32:41 +0000208 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000209 }
210 }
211 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000212 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000213 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000214 }
215 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000216 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000217 SkipUntil(tok::r_paren, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000218 SourceLocation Loc = Tok.getLocation();;
219 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
220 SkipUntil(tok::r_paren, false);
221 }
222 if (EndLoc)
223 *EndLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000224 }
225 return CurrAttr;
226}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000227
Eli Friedman06de2b52009-06-08 07:21:15 +0000228/// ParseMicrosoftDeclSpec - Parse an __declspec construct
229///
230/// [MS] decl-specifier:
231/// __declspec ( extended-decl-modifier-seq )
232///
233/// [MS] extended-decl-modifier-seq:
234/// extended-decl-modifier[opt]
235/// extended-decl-modifier extended-decl-modifier-seq
236
Eli Friedman53339e02009-06-08 23:27:34 +0000237AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000238 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000239
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000240 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000241 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
242 "declspec")) {
243 SkipUntil(tok::r_paren, true); // skip until ) or ;
244 return CurrAttr;
245 }
Eli Friedman53339e02009-06-08 23:27:34 +0000246 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000247 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
248 SourceLocation AttrNameLoc = ConsumeToken();
249 if (Tok.is(tok::l_paren)) {
250 ConsumeParen();
251 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
252 // correctly.
253 OwningExprResult ArgExpr(ParseAssignmentExpression());
254 if (!ArgExpr.isInvalid()) {
255 ExprTy* ExprList = ArgExpr.take();
256 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
257 SourceLocation(), &ExprList, 1,
258 CurrAttr, true);
259 }
260 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
261 SkipUntil(tok::r_paren, false);
262 } else {
263 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, SourceLocation(),
264 0, 0, CurrAttr, true);
265 }
266 }
267 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
268 SkipUntil(tok::r_paren, false);
Eli Friedman53339e02009-06-08 23:27:34 +0000269 return CurrAttr;
270}
271
272AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
273 // Treat these like attributes
274 // FIXME: Allow Sema to distinguish between these and real attributes!
275 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
276 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) ||
277 Tok.is(tok::kw___w64)) {
278 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
279 SourceLocation AttrNameLoc = ConsumeToken();
280 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
281 // FIXME: Support these properly!
282 continue;
283 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
284 SourceLocation(), 0, 0, CurrAttr, true);
285 }
286 return CurrAttr;
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000287}
288
Chris Lattner53361ac2006-08-10 05:19:57 +0000289/// ParseDeclaration - Parse a full 'declaration', which consists of
290/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000291/// 'Context' should be a Declarator::TheContext value. This returns the
292/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000293///
294/// declaration: [C99 6.7]
295/// block-declaration ->
296/// simple-declaration
297/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000298/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000299/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000300/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000301/// [C++] using-declaration
Sebastian Redlf769df52009-03-24 22:27:57 +0000302/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000303/// others... [FIXME]
304///
Chris Lattner49836b42009-04-02 04:16:50 +0000305Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
306 SourceLocation &DeclEnd) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000307 DeclPtrTy SingleDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000308 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000309 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000310 case tok::kw_export:
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000311 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000312 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000313 case tok::kw_namespace:
Chris Lattner49836b42009-04-02 04:16:50 +0000314 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000315 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000316 case tok::kw_using:
Chris Lattner49836b42009-04-02 04:16:50 +0000317 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000318 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000319 case tok::kw_static_assert:
Chris Lattner49836b42009-04-02 04:16:50 +0000320 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000321 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000322 default:
Chris Lattner49836b42009-04-02 04:16:50 +0000323 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattnera5235172007-08-25 06:57:03 +0000324 }
Mike Stump11289f42009-09-09 15:08:12 +0000325
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000326 // This routine returns a DeclGroup, if the thing we parsed only contains a
327 // single decl, convert it now.
328 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000329}
330
331/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
332/// declaration-specifiers init-declarator-list[opt] ';'
333///[C90/C++]init-declarator-list ';' [TODO]
334/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000335///
336/// If RequireSemi is false, this does not check for a ';' at the end of the
337/// declaration.
338Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Chris Lattner49836b42009-04-02 04:16:50 +0000339 SourceLocation &DeclEnd,
Chris Lattner32dc41c2009-03-29 17:27:48 +0000340 bool RequireSemi) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000341 // Parse the common declaration-specifiers piece.
342 DeclSpec DS;
343 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000344
Chris Lattner0e894622006-08-13 19:58:17 +0000345 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
346 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000347 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000348 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000349 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
350 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000351 }
Mike Stump11289f42009-09-09 15:08:12 +0000352
Chris Lattner53361ac2006-08-10 05:19:57 +0000353 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
354 ParseDeclarator(DeclaratorInfo);
Mike Stump11289f42009-09-09 15:08:12 +0000355
Chris Lattnerefb0f112009-03-29 17:18:04 +0000356 DeclGroupPtrTy DG =
357 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000358
Chris Lattner49836b42009-04-02 04:16:50 +0000359 DeclEnd = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattner32dc41c2009-03-29 17:27:48 +0000361 // If the client wants to check what comes after the declaration, just return
362 // immediately without checking anything!
363 if (!RequireSemi) return DG;
Mike Stump11289f42009-09-09 15:08:12 +0000364
Chris Lattnerefb0f112009-03-29 17:18:04 +0000365 if (Tok.is(tok::semi)) {
366 ConsumeToken();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000367 return DG;
368 }
Mike Stump11289f42009-09-09 15:08:12 +0000369
John McCallef50e992009-07-31 02:20:35 +0000370 Diag(Tok, diag::err_expected_semi_declaration);
Chris Lattnerefb0f112009-03-29 17:18:04 +0000371 // Skip to end of block or statement
372 SkipUntil(tok::r_brace, true, true);
373 if (Tok.is(tok::semi))
374 ConsumeToken();
375 return DG;
Chris Lattner53361ac2006-08-10 05:19:57 +0000376}
377
Douglas Gregor23996282009-05-12 21:31:51 +0000378/// \brief Parse 'declaration' after parsing 'declaration-specifiers
379/// declarator'. This method parses the remainder of the declaration
380/// (including any attributes or initializer, among other things) and
381/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000382///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000383/// init-declarator: [C99 6.7]
384/// declarator
385/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000386/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
387/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000388/// [C++] declarator initializer[opt]
389///
390/// [C++] initializer:
391/// [C++] '=' initializer-clause
392/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000393/// [C++0x] '=' 'default' [TODO]
394/// [C++0x] '=' 'delete'
395///
396/// According to the standard grammar, =default and =delete are function
397/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000398///
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000399Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
400 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000401 // If a simple-asm-expr is present, parse it.
402 if (Tok.is(tok::kw_asm)) {
403 SourceLocation Loc;
404 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
405 if (AsmLabel.isInvalid()) {
406 SkipUntil(tok::semi, true, true);
407 return DeclPtrTy();
408 }
Mike Stump11289f42009-09-09 15:08:12 +0000409
Douglas Gregor23996282009-05-12 21:31:51 +0000410 D.setAsmLabel(AsmLabel.release());
411 D.SetRangeEnd(Loc);
412 }
Mike Stump11289f42009-09-09 15:08:12 +0000413
Douglas Gregor23996282009-05-12 21:31:51 +0000414 // If attributes are present, parse them.
415 if (Tok.is(tok::kw___attribute)) {
416 SourceLocation Loc;
417 AttributeList *AttrList = ParseAttributes(&Loc);
418 D.AddAttributes(AttrList, Loc);
419 }
Mike Stump11289f42009-09-09 15:08:12 +0000420
Douglas Gregor23996282009-05-12 21:31:51 +0000421 // Inform the current actions module that we just parsed this declarator.
Mike Stump11289f42009-09-09 15:08:12 +0000422 DeclPtrTy ThisDecl = TemplateInfo.TemplateParams?
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000423 Actions.ActOnTemplateDeclarator(CurScope,
424 Action::MultiTemplateParamsArg(Actions,
425 TemplateInfo.TemplateParams->data(),
426 TemplateInfo.TemplateParams->size()),
427 D)
428 : Actions.ActOnDeclarator(CurScope, D);
Mike Stump11289f42009-09-09 15:08:12 +0000429
Douglas Gregor23996282009-05-12 21:31:51 +0000430 // Parse declarator '=' initializer.
431 if (Tok.is(tok::equal)) {
432 ConsumeToken();
433 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
434 SourceLocation DelLoc = ConsumeToken();
435 Actions.SetDeclDeleted(ThisDecl, DelLoc);
436 } else {
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000437 if (getLang().CPlusPlus)
438 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
439
Douglas Gregor23996282009-05-12 21:31:51 +0000440 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000441
442 if (getLang().CPlusPlus)
443 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
444
Douglas Gregor23996282009-05-12 21:31:51 +0000445 if (Init.isInvalid()) {
446 SkipUntil(tok::semi, true, true);
447 return DeclPtrTy();
448 }
Anders Carlsson250aada2009-08-16 05:13:48 +0000449 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor23996282009-05-12 21:31:51 +0000450 }
451 } else if (Tok.is(tok::l_paren)) {
452 // Parse C++ direct initializer: '(' expression-list ')'
453 SourceLocation LParenLoc = ConsumeParen();
454 ExprVector Exprs(Actions);
455 CommaLocsTy CommaLocs;
456
457 if (ParseExpressionList(Exprs, CommaLocs)) {
458 SkipUntil(tok::r_paren);
459 } else {
460 // Match the ')'.
461 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
462
463 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
464 "Unexpected number of commas!");
465 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
466 move_arg(Exprs),
Jay Foad7d0479f2009-05-21 09:52:38 +0000467 CommaLocs.data(), RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000468 }
469 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000470 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000471 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
472 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000473 }
474
475 return ThisDecl;
476}
477
478/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
479/// parsing 'declaration-specifiers declarator'. This method is split out this
480/// way to handle the ambiguity between top-level function-definitions and
481/// declarations.
482///
483/// init-declarator-list: [C99 6.7]
484/// init-declarator
485/// init-declarator-list ',' init-declarator
486///
487/// According to the standard grammar, =default and =delete are function
488/// definitions, but that definitely doesn't fit with the parser here.
489///
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000490Parser::DeclGroupPtrTy Parser::
Chris Lattner2dacc3f2006-10-16 00:33:54 +0000491ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000492 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
493 // that we parse together here.
494 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Mike Stump11289f42009-09-09 15:08:12 +0000495
Chris Lattner53361ac2006-08-10 05:19:57 +0000496 // At this point, we know that it is not a function definition. Parse the
497 // rest of the init-declarator-list.
498 while (1) {
Douglas Gregor23996282009-05-12 21:31:51 +0000499 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
500 if (ThisDecl.get())
501 DeclsInGroup.push_back(ThisDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000502
Chris Lattner53361ac2006-08-10 05:19:57 +0000503 // If we don't have a comma, it is either the end of the list (a ';') or an
504 // error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +0000505 if (Tok.isNot(tok::comma))
Chris Lattner53361ac2006-08-10 05:19:57 +0000506 break;
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattner53361ac2006-08-10 05:19:57 +0000508 // Consume the comma.
509 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000510
Chris Lattner53361ac2006-08-10 05:19:57 +0000511 // Parse the next declarator.
512 D.clear();
Mike Stump11289f42009-09-09 15:08:12 +0000513
Chris Lattner29e6f2b2008-10-20 04:57:38 +0000514 // Accept attributes in an init-declarator. In the first declarator in a
515 // declaration, these would be part of the declspec. In subsequent
516 // declarators, they become part of the declarator itself, so that they
517 // don't apply to declarators after *this* one. Examples:
518 // short __attribute__((common)) var; -> declspec
519 // short var __attribute__((common)); -> declarator
520 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000521 if (Tok.is(tok::kw___attribute)) {
522 SourceLocation Loc;
523 AttributeList *AttrList = ParseAttributes(&Loc);
524 D.AddAttributes(AttrList, Loc);
525 }
Mike Stump11289f42009-09-09 15:08:12 +0000526
Chris Lattner53361ac2006-08-10 05:19:57 +0000527 ParseDeclarator(D);
528 }
Mike Stump11289f42009-09-09 15:08:12 +0000529
Eli Friedman55b9ecb2009-05-29 01:49:24 +0000530 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
531 DeclsInGroup.data(),
Chris Lattnerefb0f112009-03-29 17:18:04 +0000532 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000533}
534
Chris Lattner1890ac82006-08-13 01:16:23 +0000535/// ParseSpecifierQualifierList
536/// specifier-qualifier-list:
537/// type-specifier specifier-qualifier-list[opt]
538/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000539/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000540///
541void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
542 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
543 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000544 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000545
Chris Lattner1890ac82006-08-13 01:16:23 +0000546 // Validate declspec for type-name.
547 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000548 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
549 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000550 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000551
Chris Lattner1b22eed2006-11-28 05:12:07 +0000552 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000553 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000554 if (DS.getStorageClassSpecLoc().isValid())
555 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
556 else
557 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000558 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000559 }
Mike Stump11289f42009-09-09 15:08:12 +0000560
Chris Lattner1b22eed2006-11-28 05:12:07 +0000561 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000562 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000563 if (DS.isInlineSpecified())
564 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
565 if (DS.isVirtualSpecified())
566 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
567 if (DS.isExplicitSpecified())
568 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000569 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000570 }
571}
Chris Lattner53361ac2006-08-10 05:19:57 +0000572
Chris Lattner6cc055a2009-04-12 20:42:31 +0000573/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
574/// specified token is valid after the identifier in a declarator which
575/// immediately follows the declspec. For example, these things are valid:
576///
577/// int x [ 4]; // direct-declarator
578/// int x ( int y); // direct-declarator
579/// int(int x ) // direct-declarator
580/// int x ; // simple-declaration
581/// int x = 17; // init-declarator-list
582/// int x , y; // init-declarator-list
583/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000584/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000585/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000586///
587/// This is not, because 'x' does not immediately follow the declspec (though
588/// ')' happens to be valid anyway).
589/// int (x)
590///
591static bool isValidAfterIdentifierInDeclarator(const Token &T) {
592 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
593 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000594 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000595}
596
Chris Lattner20a0c612009-04-14 21:34:55 +0000597
598/// ParseImplicitInt - This method is called when we have an non-typename
599/// identifier in a declspec (which normally terminates the decl spec) when
600/// the declspec has no type specifier. In this case, the declspec is either
601/// malformed or is "implicit int" (in K&R and C89).
602///
603/// This method handles diagnosing this prettily and returns false if the
604/// declspec is done being processed. If it recovers and thinks there may be
605/// other pieces of declspec after it, it returns true.
606///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000607bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000608 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000609 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000610 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000611
Chris Lattner20a0c612009-04-14 21:34:55 +0000612 SourceLocation Loc = Tok.getLocation();
613 // If we see an identifier that is not a type name, we normally would
614 // parse it as the identifer being declared. However, when a typename
615 // is typo'd or the definition is not included, this will incorrectly
616 // parse the typename as the identifier name and fall over misparsing
617 // later parts of the diagnostic.
618 //
619 // As such, we try to do some look-ahead in cases where this would
620 // otherwise be an "implicit-int" case to see if this is invalid. For
621 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
622 // an identifier with implicit int, we'd get a parse error because the
623 // next token is obviously invalid for a type. Parse these as a case
624 // with an invalid type specifier.
625 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000626
Chris Lattner20a0c612009-04-14 21:34:55 +0000627 // Since we know that this either implicit int (which is rare) or an
628 // error, we'd do lookahead to try to do better recovery.
629 if (isValidAfterIdentifierInDeclarator(NextToken())) {
630 // If this token is valid for implicit int, e.g. "static x = 4", then
631 // we just avoid eating the identifier, so it will be parsed as the
632 // identifier in the declarator.
633 return false;
634 }
Mike Stump11289f42009-09-09 15:08:12 +0000635
Chris Lattner20a0c612009-04-14 21:34:55 +0000636 // Otherwise, if we don't consume this token, we are going to emit an
637 // error anyway. Try to recover from various common problems. Check
638 // to see if this was a reference to a tag name without a tag specified.
639 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000640 //
641 // C++ doesn't need this, and isTagName doesn't take SS.
642 if (SS == 0) {
643 const char *TagName = 0;
644 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000645
Chris Lattner20a0c612009-04-14 21:34:55 +0000646 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
647 default: break;
648 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
649 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
650 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
651 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
652 }
Mike Stump11289f42009-09-09 15:08:12 +0000653
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000654 if (TagName) {
655 Diag(Loc, diag::err_use_of_tag_name_without_tag)
656 << Tok.getIdentifierInfo() << TagName
657 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000658
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000659 // Parse this as a tag as if the missing tag were present.
660 if (TagKind == tok::kw_enum)
661 ParseEnumSpecifier(Loc, DS, AS);
662 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000663 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000664 return true;
665 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000666 }
Mike Stump11289f42009-09-09 15:08:12 +0000667
Chris Lattner20a0c612009-04-14 21:34:55 +0000668 // Since this is almost certainly an invalid type name, emit a
669 // diagnostic that says it, eat the token, and mark the declspec as
670 // invalid.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000671 SourceRange R;
672 if (SS) R = SS->getRange();
Mike Stump11289f42009-09-09 15:08:12 +0000673
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000674 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
Chris Lattner20a0c612009-04-14 21:34:55 +0000675 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000676 unsigned DiagID;
677 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000678 DS.SetRangeEnd(Tok.getLocation());
679 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000680
Chris Lattner20a0c612009-04-14 21:34:55 +0000681 // TODO: Could inject an invalid typedef decl in an enclosing scope to
682 // avoid rippling error messages on subsequent uses of the same type,
683 // could be useful if #include was forgotten.
684 return false;
685}
686
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000687/// ParseDeclarationSpecifiers
688/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000689/// storage-class-specifier declaration-specifiers[opt]
690/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000691/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000692/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000693///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000694/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000695/// 'typedef'
696/// 'extern'
697/// 'static'
698/// 'auto'
699/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000700/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000701/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000702/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000703/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000704/// [C++] 'virtual'
705/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000706/// 'friend': [C++ dcl.friend]
707
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000708///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000709void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000710 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000711 AccessSpecifier AS,
712 DeclSpecContext DSContext) {
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000713 if (Tok.is(tok::code_completion)) {
714 Actions.CodeCompleteOrdinaryName(CurScope);
715 ConsumeToken();
716 }
717
Chris Lattner2e232092008-03-13 06:29:04 +0000718 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000719 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000720 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000721 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000722 unsigned DiagID = 0;
723
Chris Lattner4d8f8732006-11-28 05:05:08 +0000724 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000725
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000726 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000727 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000728 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000729 // If this is not a declaration specifier token, we're done reading decl
730 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000731 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000732 return;
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000734 case tok::coloncolon: // ::foo::bar
735 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregore861bac2009-08-25 22:51:20 +0000736 if (TryAnnotateCXXScopeToken(true))
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000737 continue;
738 goto DoneWithDeclSpec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000739
740 case tok::annot_cxxscope: {
741 if (DS.hasTypeSpecifier())
742 goto DoneWithDeclSpec;
743
744 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000745 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000746 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000747 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000748 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000749 // We have a qualified template-id, e.g., N::A<int>
750 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +0000751 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Mike Stump11289f42009-09-09 15:08:12 +0000752 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000753 "ParseOptionalCXXScopeSpecifier not working");
754 AnnotateTemplateIdTokenAsType(&SS);
755 continue;
756 }
757
758 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000759 goto DoneWithDeclSpec;
760
761 CXXScopeSpec SS;
Douglas Gregorc23500e2009-03-26 23:56:24 +0000762 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000763 SS.setRange(Tok.getAnnotationRange());
764
765 // If the next token is the name of the class type that the C++ scope
766 // denotes, followed by a '(', then this is a constructor declaration.
767 // We're done with the decl-specifiers.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000768 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000769 CurScope, &SS) &&
770 GetLookAheadToken(2).is(tok::l_paren))
771 goto DoneWithDeclSpec;
772
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000773 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
774 Next.getLocation(), CurScope, &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000775
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000776 // If the referenced identifier is not a type, then this declspec is
777 // erroneous: We already checked about that it has no type specifier, and
778 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +0000779 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000780 if (TypeRep == 0) {
781 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000782 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000783 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000784 }
Mike Stump11289f42009-09-09 15:08:12 +0000785
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000786 ConsumeToken(); // The C++ scope.
787
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000788 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000789 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000790 if (isInvalid)
791 break;
Mike Stump11289f42009-09-09 15:08:12 +0000792
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000793 DS.SetRangeEnd(Tok.getLocation());
794 ConsumeToken(); // The typename.
795
796 continue;
797 }
Mike Stump11289f42009-09-09 15:08:12 +0000798
Chris Lattnere387d9e2009-01-21 19:48:37 +0000799 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000800 if (Tok.getAnnotationValue())
801 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000802 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000803 else
804 DS.SetTypeSpecError();
Chris Lattnere387d9e2009-01-21 19:48:37 +0000805 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
806 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +0000807
Chris Lattnere387d9e2009-01-21 19:48:37 +0000808 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
809 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
810 // Objective-C interface. If we don't have Objective-C or a '<', this is
811 // just a normal reference to a typedef name.
812 if (!Tok.is(tok::less) || !getLang().ObjC1)
813 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000814
Chris Lattnere387d9e2009-01-21 19:48:37 +0000815 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000816 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattnere387d9e2009-01-21 19:48:37 +0000817 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek58d81902009-06-30 22:19:00 +0000818 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Mike Stump11289f42009-09-09 15:08:12 +0000819
Chris Lattnere387d9e2009-01-21 19:48:37 +0000820 DS.SetRangeEnd(EndProtoLoc);
821 continue;
822 }
Mike Stump11289f42009-09-09 15:08:12 +0000823
Chris Lattner16fac4f2008-07-26 01:18:38 +0000824 // typedef-name
825 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000826 // In C++, check to see if this is a scope specifier like foo::bar::, if
827 // so handle it as such. This is important for ctor parsing.
Douglas Gregore861bac2009-08-25 22:51:20 +0000828 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner78ecd4f2009-01-21 19:19:26 +0000829 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000830
Chris Lattner16fac4f2008-07-26 01:18:38 +0000831 // This identifier can only be a typedef name if we haven't already seen
832 // a type-specifier. Without this check we misparse:
833 // typedef int X; struct Y { short X; }; as 'short int'.
834 if (DS.hasTypeSpecifier())
835 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +0000836
Chris Lattner16fac4f2008-07-26 01:18:38 +0000837 // It has to be available as a typedef too!
Mike Stump11289f42009-09-09 15:08:12 +0000838 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000839 Tok.getLocation(), CurScope);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000840
Chris Lattner6cc055a2009-04-12 20:42:31 +0000841 // If this is not a typedef name, don't parse it as part of the declspec,
842 // it must be an implicit int or an error.
843 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000844 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000845 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +0000846 }
Douglas Gregor8bf42052009-02-09 18:46:07 +0000847
Douglas Gregor61956c42008-10-31 09:07:45 +0000848 // C++: If the identifier is actually the name of the class type
849 // being defined and the next token is a '(', then this is a
850 // constructor declaration. We're done with the decl-specifiers
851 // and will treat this token as an identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000852 if (getLang().CPlusPlus &&
853 (CurScope->isClassScope() ||
854 (CurScope->isTemplateParamScope() &&
Douglas Gregor5ed5ae42009-08-21 18:42:58 +0000855 CurScope->getParent()->isClassScope())) &&
Mike Stump11289f42009-09-09 15:08:12 +0000856 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor61956c42008-10-31 09:07:45 +0000857 NextToken().getKind() == tok::l_paren)
858 goto DoneWithDeclSpec;
859
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000860 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000861 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +0000862 if (isInvalid)
863 break;
Mike Stump11289f42009-09-09 15:08:12 +0000864
Chris Lattner16fac4f2008-07-26 01:18:38 +0000865 DS.SetRangeEnd(Tok.getLocation());
866 ConsumeToken(); // The identifier
867
868 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
869 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
870 // Objective-C interface. If we don't have Objective-C or a '<', this is
871 // just a normal reference to a typedef name.
872 if (!Tok.is(tok::less) || !getLang().ObjC1)
873 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000874
Chris Lattner16fac4f2008-07-26 01:18:38 +0000875 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000876 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +0000877 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek58d81902009-06-30 22:19:00 +0000878 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Mike Stump11289f42009-09-09 15:08:12 +0000879
Chris Lattner16fac4f2008-07-26 01:18:38 +0000880 DS.SetRangeEnd(EndProtoLoc);
881
Steve Naroffcd5e7822008-09-22 10:28:57 +0000882 // Need to support trailing type qualifiers (e.g. "id<p> const").
883 // If a type specifier follows, it will be diagnosed elsewhere.
884 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000885 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000886
887 // type-name
888 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +0000889 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000890 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000891 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000892 // This template-id does not refer to a type name, so we're
893 // done with the type-specifiers.
894 goto DoneWithDeclSpec;
895 }
896
897 // Turn the template-id annotation token into a type annotation
898 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000899 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +0000900 continue;
901 }
902
Chris Lattnere37e2332006-08-15 04:50:22 +0000903 // GNU attributes support.
904 case tok::kw___attribute:
Steve Naroff0f05a7a2007-06-09 23:38:17 +0000905 DS.AddAttributes(ParseAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +0000906 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000907
908 // Microsoft declspec support.
909 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +0000910 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000911 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000912
Steve Naroff44ac7772008-12-25 14:16:32 +0000913 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +0000914 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +0000915 // FIXME: Add handling here!
916 break;
917
918 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +0000919 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +0000920 case tok::kw___cdecl:
921 case tok::kw___stdcall:
922 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +0000923 DS.AddAttributes(ParseMicrosoftTypeAttributes());
924 continue;
925
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000926 // storage-class-specifier
927 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +0000928 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
929 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000930 break;
931 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +0000932 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +0000933 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +0000934 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
935 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000936 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +0000937 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +0000938 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +0000939 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +0000940 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000941 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +0000942 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +0000943 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +0000944 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
945 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000946 break;
947 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +0000948 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +0000949 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
950 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +0000951 else
John McCall49bfce42009-08-03 20:12:06 +0000952 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
953 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000954 break;
955 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +0000956 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
957 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000958 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000959 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +0000960 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
961 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000962 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000963 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +0000964 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000965 break;
Mike Stump11289f42009-09-09 15:08:12 +0000966
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000967 // function-specifier
968 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +0000969 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000970 break;
Douglas Gregor61956c42008-10-31 09:07:45 +0000971 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +0000972 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +0000973 break;
Douglas Gregor61956c42008-10-31 09:07:45 +0000974 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +0000975 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +0000976 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +0000977
Anders Carlssoncd8db412009-05-06 04:46:28 +0000978 // friend
979 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +0000980 if (DSContext == DSC_class)
981 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
982 else {
983 PrevSpec = ""; // not actually used by the diagnostic
984 DiagID = diag::err_friend_invalid_in_context;
985 isInvalid = true;
986 }
Anders Carlssoncd8db412009-05-06 04:46:28 +0000987 break;
Mike Stump11289f42009-09-09 15:08:12 +0000988
Chris Lattnere387d9e2009-01-21 19:48:37 +0000989 // type-specifier
990 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +0000991 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
992 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +0000993 break;
994 case tok::kw_long:
995 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +0000996 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
997 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +0000998 else
John McCall49bfce42009-08-03 20:12:06 +0000999 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1000 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001001 break;
1002 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001003 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1004 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001005 break;
1006 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001007 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1008 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001009 break;
1010 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001011 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1012 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001013 break;
1014 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001015 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1016 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001017 break;
1018 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001019 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1020 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001021 break;
1022 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001023 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1024 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001025 break;
1026 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001027 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1028 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001029 break;
1030 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001031 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1032 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001033 break;
1034 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001035 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1036 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001037 break;
1038 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001039 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1040 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001041 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001042 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001043 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1044 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001045 break;
1046 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001047 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1048 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001049 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001050 case tok::kw_bool:
1051 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001052 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1053 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001054 break;
1055 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001056 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1057 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001058 break;
1059 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001060 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1061 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001062 break;
1063 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001064 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1065 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001066 break;
1067
1068 // class-specifier:
1069 case tok::kw_class:
1070 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001071 case tok::kw_union: {
1072 tok::TokenKind Kind = Tok.getKind();
1073 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001074 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001075 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001076 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001077
1078 // enum-specifier:
1079 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001080 ConsumeToken();
1081 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001082 continue;
1083
1084 // cv-qualifier:
1085 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001086 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1087 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001088 break;
1089 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001090 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1091 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001092 break;
1093 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001094 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1095 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001096 break;
1097
Douglas Gregor333489b2009-03-27 23:10:48 +00001098 // C++ typename-specifier:
1099 case tok::kw_typename:
1100 if (TryAnnotateTypeOrScopeToken())
1101 continue;
1102 break;
1103
Chris Lattnere387d9e2009-01-21 19:48:37 +00001104 // GNU typeof support.
1105 case tok::kw_typeof:
1106 ParseTypeofSpecifier(DS);
1107 continue;
1108
Anders Carlsson74948d02009-06-24 17:47:40 +00001109 case tok::kw_decltype:
1110 ParseDecltypeSpecifier(DS);
1111 continue;
1112
Steve Naroffcfdf6162008-06-05 00:02:44 +00001113 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001114 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001115 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1116 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001117 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001118 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001119
Chris Lattner0974b232008-07-26 00:20:22 +00001120 {
1121 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001122 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Chris Lattner3bbae002008-07-26 04:03:38 +00001123 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek58d81902009-06-30 22:19:00 +00001124 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Chris Lattner16fac4f2008-07-26 01:18:38 +00001125 DS.SetRangeEnd(EndProtoLoc);
1126
Chris Lattner6d29c102008-11-18 07:48:38 +00001127 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner3a4e4312009-04-03 18:38:42 +00001128 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001129 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001130 // Need to support trailing type qualifiers (e.g. "id<p> const").
1131 // If a type specifier follows, it will be diagnosed elsewhere.
1132 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001133 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001134 }
John McCall49bfce42009-08-03 20:12:06 +00001135 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001136 if (isInvalid) {
1137 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001138 assert(DiagID);
Chris Lattner6d29c102008-11-18 07:48:38 +00001139 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001140 }
Chris Lattner2e232092008-03-13 06:29:04 +00001141 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001142 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001143 }
1144}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001145
Chris Lattnera448d752009-01-06 06:59:53 +00001146/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001147/// primarily follow the C++ grammar with additions for C99 and GNU,
1148/// which together subsume the C grammar. Note that the C++
1149/// type-specifier also includes the C type-qualifier (for const,
1150/// volatile, and C99 restrict). Returns true if a type-specifier was
1151/// found (and parsed), false otherwise.
1152///
1153/// type-specifier: [C++ 7.1.5]
1154/// simple-type-specifier
1155/// class-specifier
1156/// enum-specifier
1157/// elaborated-type-specifier [TODO]
1158/// cv-qualifier
1159///
1160/// cv-qualifier: [C++ 7.1.5.1]
1161/// 'const'
1162/// 'volatile'
1163/// [C99] 'restrict'
1164///
1165/// simple-type-specifier: [ C++ 7.1.5.2]
1166/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1167/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1168/// 'char'
1169/// 'wchar_t'
1170/// 'bool'
1171/// 'short'
1172/// 'int'
1173/// 'long'
1174/// 'signed'
1175/// 'unsigned'
1176/// 'float'
1177/// 'double'
1178/// 'void'
1179/// [C99] '_Bool'
1180/// [C99] '_Complex'
1181/// [C99] '_Imaginary' // Removed in TC2?
1182/// [GNU] '_Decimal32'
1183/// [GNU] '_Decimal64'
1184/// [GNU] '_Decimal128'
1185/// [GNU] typeof-specifier
1186/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1187/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001188/// [C++0x] 'decltype' ( expression )
John McCall49bfce42009-08-03 20:12:06 +00001189bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001190 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001191 unsigned &DiagID,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001192 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001193 SourceLocation Loc = Tok.getLocation();
1194
1195 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001196 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001197 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001198 // Annotate typenames and C++ scope specifiers. If we get one, just
1199 // recurse to handle whatever we get.
1200 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001201 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1202 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001203 // Otherwise, not a type specifier.
1204 return false;
1205 case tok::coloncolon: // ::foo::bar
1206 if (NextToken().is(tok::kw_new) || // ::new
1207 NextToken().is(tok::kw_delete)) // ::delete
1208 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001209
Chris Lattner020bab92009-01-04 23:41:41 +00001210 // Annotate typenames and C++ scope specifiers. If we get one, just
1211 // recurse to handle whatever we get.
1212 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001213 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1214 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001215 // Otherwise, not a type specifier.
1216 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001217
Douglas Gregor450c75a2008-11-07 15:42:26 +00001218 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001219 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001220 if (Tok.getAnnotationValue())
1221 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001222 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001223 else
1224 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001225 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1226 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001227
Douglas Gregor450c75a2008-11-07 15:42:26 +00001228 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1229 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1230 // Objective-C interface. If we don't have Objective-C or a '<', this is
1231 // just a normal reference to a typedef name.
1232 if (!Tok.is(tok::less) || !getLang().ObjC1)
1233 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001234
Douglas Gregor450c75a2008-11-07 15:42:26 +00001235 SourceLocation EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001236 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001237 ParseObjCProtocolReferences(ProtocolDecl, false, EndProtoLoc);
Ted Kremenek58d81902009-06-30 22:19:00 +00001238 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size());
Mike Stump11289f42009-09-09 15:08:12 +00001239
Douglas Gregor450c75a2008-11-07 15:42:26 +00001240 DS.SetRangeEnd(EndProtoLoc);
1241 return true;
1242 }
1243
1244 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001245 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001246 break;
1247 case tok::kw_long:
1248 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001249 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1250 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001251 else
John McCall49bfce42009-08-03 20:12:06 +00001252 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1253 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001254 break;
1255 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001256 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001257 break;
1258 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001259 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1260 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001261 break;
1262 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001263 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1264 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001265 break;
1266 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001267 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1268 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001269 break;
1270 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001271 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001272 break;
1273 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001274 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001275 break;
1276 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001277 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001278 break;
1279 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001280 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001281 break;
1282 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001283 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001284 break;
1285 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001286 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001287 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001288 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001289 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001290 break;
1291 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001292 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001293 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001294 case tok::kw_bool:
1295 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001296 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001297 break;
1298 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001299 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1300 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001301 break;
1302 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001303 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1304 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001305 break;
1306 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001307 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1308 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001309 break;
1310
1311 // class-specifier:
1312 case tok::kw_class:
1313 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001314 case tok::kw_union: {
1315 tok::TokenKind Kind = Tok.getKind();
1316 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001317 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001318 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001319 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001320
1321 // enum-specifier:
1322 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001323 ConsumeToken();
1324 ParseEnumSpecifier(Loc, DS);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001325 return true;
1326
1327 // cv-qualifier:
1328 case tok::kw_const:
1329 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001330 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001331 break;
1332 case tok::kw_volatile:
1333 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001334 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001335 break;
1336 case tok::kw_restrict:
1337 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001338 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001339 break;
1340
1341 // GNU typeof support.
1342 case tok::kw_typeof:
1343 ParseTypeofSpecifier(DS);
1344 return true;
1345
Anders Carlsson74948d02009-06-24 17:47:40 +00001346 // C++0x decltype support.
1347 case tok::kw_decltype:
1348 ParseDecltypeSpecifier(DS);
1349 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001350
Anders Carlssonbae27372009-06-26 23:44:14 +00001351 // C++0x auto support.
1352 case tok::kw_auto:
1353 if (!getLang().CPlusPlus0x)
1354 return false;
1355
John McCall49bfce42009-08-03 20:12:06 +00001356 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001357 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001358 case tok::kw___ptr64:
1359 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001360 case tok::kw___cdecl:
1361 case tok::kw___stdcall:
1362 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001363 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001364 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001365
Douglas Gregor450c75a2008-11-07 15:42:26 +00001366 default:
1367 // Not a type-specifier; do nothing.
1368 return false;
1369 }
1370
1371 // If the specifier combination wasn't legal, issue a diagnostic.
1372 if (isInvalid) {
1373 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001374 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001375 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001376 }
1377 DS.SetRangeEnd(Tok.getLocation());
1378 ConsumeToken(); // whatever we parsed above.
1379 return true;
1380}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001381
Chris Lattner70ae4912007-10-29 04:42:53 +00001382/// ParseStructDeclaration - Parse a struct declaration without the terminating
1383/// semicolon.
1384///
Chris Lattner90a26b02007-01-23 04:38:16 +00001385/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001386/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001387/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001388/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001389/// struct-declarator-list:
1390/// struct-declarator
1391/// struct-declarator-list ',' struct-declarator
1392/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1393/// struct-declarator:
1394/// declarator
1395/// [GNU] declarator attributes[opt]
1396/// declarator[opt] ':' constant-expression
1397/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1398///
Chris Lattnera12405b2008-04-10 06:46:29 +00001399void Parser::
1400ParseStructDeclaration(DeclSpec &DS,
1401 llvm::SmallVectorImpl<FieldDeclarator> &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001402 if (Tok.is(tok::kw___extension__)) {
1403 // __extension__ silences extension warnings in the subexpression.
1404 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001405 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001406 return ParseStructDeclaration(DS, Fields);
1407 }
Mike Stump11289f42009-09-09 15:08:12 +00001408
Steve Naroff97170802007-08-20 22:28:22 +00001409 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001410 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001411 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001412
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001413 // If there are no declarators, this is a free-standing declaration
1414 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001415 if (Tok.is(tok::semi)) {
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001416 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001417 return;
1418 }
1419
1420 // Read struct-declarators until we find the semicolon.
Chris Lattner5c7fce42008-04-10 16:37:40 +00001421 Fields.push_back(FieldDeclarator(DS));
Steve Naroff97170802007-08-20 22:28:22 +00001422 while (1) {
Chris Lattnera12405b2008-04-10 06:46:29 +00001423 FieldDeclarator &DeclaratorInfo = Fields.back();
Mike Stump11289f42009-09-09 15:08:12 +00001424
Steve Naroff97170802007-08-20 22:28:22 +00001425 /// struct-declarator: declarator
1426 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner76c72282007-10-09 17:33:22 +00001427 if (Tok.isNot(tok::colon))
Chris Lattnera12405b2008-04-10 06:46:29 +00001428 ParseDeclarator(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00001429
Chris Lattner76c72282007-10-09 17:33:22 +00001430 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001431 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001432 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001433 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001434 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001435 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001436 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001437 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001438
Steve Naroff97170802007-08-20 22:28:22 +00001439 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001440 if (Tok.is(tok::kw___attribute)) {
1441 SourceLocation Loc;
1442 AttributeList *AttrList = ParseAttributes(&Loc);
1443 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1444 }
1445
Steve Naroff97170802007-08-20 22:28:22 +00001446 // If we don't have a comma, it is either the end of the list (a ';')
1447 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001448 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001449 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001450
Steve Naroff97170802007-08-20 22:28:22 +00001451 // Consume the comma.
1452 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001453
Steve Naroff97170802007-08-20 22:28:22 +00001454 // Parse the next declarator.
Chris Lattner5c7fce42008-04-10 16:37:40 +00001455 Fields.push_back(FieldDeclarator(DS));
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001456
Steve Naroff97170802007-08-20 22:28:22 +00001457 // Attributes are only allowed on the second declarator.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001458 if (Tok.is(tok::kw___attribute)) {
1459 SourceLocation Loc;
1460 AttributeList *AttrList = ParseAttributes(&Loc);
1461 Fields.back().D.AddAttributes(AttrList, Loc);
1462 }
Steve Naroff97170802007-08-20 22:28:22 +00001463 }
Steve Naroff97170802007-08-20 22:28:22 +00001464}
1465
1466/// ParseStructUnionBody
1467/// struct-contents:
1468/// struct-declaration-list
1469/// [EXT] empty
1470/// [GNU] "struct-declaration-list" without terminatoring ';'
1471/// struct-declaration-list:
1472/// struct-declaration
1473/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001474/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001475///
Chris Lattner1300fb92007-01-23 23:42:53 +00001476void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001477 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001478 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1479 PP.getSourceManager(),
1480 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001481
Chris Lattner90a26b02007-01-23 04:38:16 +00001482 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001483
Douglas Gregor658b9552009-01-09 22:42:13 +00001484 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001485 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1486
Chris Lattner7b9ace62007-01-23 20:11:08 +00001487 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1488 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001489 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001490 Diag(Tok, diag::ext_empty_struct_union_enum)
1491 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001492
Chris Lattner83f095c2009-03-28 19:18:32 +00001493 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001494 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
1495
Chris Lattner7b9ace62007-01-23 20:11:08 +00001496 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001497 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001498 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001499
Chris Lattner736ed5d2007-06-09 05:59:07 +00001500 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001501 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001502 Diag(Tok, diag::ext_extra_struct_semi)
1503 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Chris Lattner36e46a22007-06-09 05:49:55 +00001504 ConsumeToken();
1505 continue;
1506 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001507
1508 // Parse all the comma separated declarators.
1509 DeclSpec DS;
1510 FieldDeclarators.clear();
Chris Lattner535b8302008-06-21 19:39:06 +00001511 if (!Tok.is(tok::at)) {
1512 ParseStructDeclaration(DS, FieldDeclarators);
Mike Stump11289f42009-09-09 15:08:12 +00001513
Chris Lattner535b8302008-06-21 19:39:06 +00001514 // Convert them all to fields.
1515 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
1516 FieldDeclarator &FD = FieldDeclarators[i];
Douglas Gregor66a985d2009-08-26 14:27:30 +00001517 DeclPtrTy Field;
Chris Lattner535b8302008-06-21 19:39:06 +00001518 // Install the declarator into the current TagDecl.
Douglas Gregor66a985d2009-08-26 14:27:30 +00001519 if (FD.D.getExtension()) {
1520 // Silences extension warnings
1521 ExtensionRAIIObject O(Diags);
1522 Field = Actions.ActOnField(CurScope, TagDecl,
1523 DS.getSourceRange().getBegin(),
1524 FD.D, FD.BitfieldSize);
1525 } else {
1526 Field = Actions.ActOnField(CurScope, TagDecl,
1527 DS.getSourceRange().getBegin(),
1528 FD.D, FD.BitfieldSize);
1529 }
Chris Lattner535b8302008-06-21 19:39:06 +00001530 FieldDecls.push_back(Field);
1531 }
1532 } else { // Handle @defs
1533 ConsumeToken();
1534 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1535 Diag(Tok, diag::err_unexpected_at);
1536 SkipUntil(tok::semi, true, true);
1537 continue;
1538 }
1539 ConsumeToken();
1540 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1541 if (!Tok.is(tok::identifier)) {
1542 Diag(Tok, diag::err_expected_ident);
1543 SkipUntil(tok::semi, true, true);
1544 continue;
1545 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001546 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump11289f42009-09-09 15:08:12 +00001547 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001548 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001549 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1550 ConsumeToken();
1551 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001552 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001553
Chris Lattner76c72282007-10-09 17:33:22 +00001554 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001555 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001556 } else if (Tok.is(tok::r_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001557 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001558 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001559 } else {
1560 Diag(Tok, diag::err_expected_semi_decl_list);
1561 // Skip to end of block or statement
1562 SkipUntil(tok::r_brace, true, true);
1563 }
1564 }
Mike Stump11289f42009-09-09 15:08:12 +00001565
Steve Naroff33a1e802007-10-29 21:38:07 +00001566 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001567
Steve Naroffb8371e12007-06-09 03:39:29 +00001568 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +00001569 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001570 if (Tok.is(tok::kw___attribute))
Daniel Dunbare4ac7a42008-10-03 16:42:10 +00001571 AttrList = ParseAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +00001572
1573 Actions.ActOnFields(CurScope,
Jay Foad7d0479f2009-05-21 09:52:38 +00001574 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001575 LBraceLoc, RBraceLoc,
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001576 AttrList);
1577 StructScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001578 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001579}
1580
1581
Chris Lattner3b561a32006-08-13 00:12:11 +00001582/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001583/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001584/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001585///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001586/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1587/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001588/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001589/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001590///
1591/// [C++] elaborated-type-specifier:
1592/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1593///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001594void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1595 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001596 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001597 if (Tok.is(tok::code_completion)) {
1598 // Code completion for an enum name.
1599 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1600 ConsumeToken();
1601 }
1602
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001603 AttributeList *Attr = 0;
1604 // If attributes exist after tag, parse them.
1605 if (Tok.is(tok::kw___attribute))
1606 Attr = ParseAttributes();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001607
1608 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001609 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001610 if (Tok.isNot(tok::identifier)) {
1611 Diag(Tok, diag::err_expected_ident);
1612 if (Tok.isNot(tok::l_brace)) {
1613 // Has no name and is not a definition.
1614 // Skip the rest of this declarator, up until the comma or semicolon.
1615 SkipUntil(tok::comma, true);
1616 return;
1617 }
1618 }
1619 }
Mike Stump11289f42009-09-09 15:08:12 +00001620
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001621 // Must have either 'enum name' or 'enum {...}'.
1622 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1623 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001624
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001625 // Skip the rest of this declarator, up until the comma or semicolon.
1626 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001627 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001628 }
Mike Stump11289f42009-09-09 15:08:12 +00001629
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001630 // If an identifier is present, consume and remember it.
1631 IdentifierInfo *Name = 0;
1632 SourceLocation NameLoc;
1633 if (Tok.is(tok::identifier)) {
1634 Name = Tok.getIdentifierInfo();
1635 NameLoc = ConsumeToken();
1636 }
Mike Stump11289f42009-09-09 15:08:12 +00001637
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001638 // There are three options here. If we have 'enum foo;', then this is a
1639 // forward declaration. If we have 'enum foo {...' then this is a
1640 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1641 //
1642 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1643 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1644 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1645 //
John McCall9bb74a52009-07-31 02:45:11 +00001646 Action::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001647 if (Tok.is(tok::l_brace))
John McCall9bb74a52009-07-31 02:45:11 +00001648 TUK = Action::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001649 else if (Tok.is(tok::semi))
John McCall9bb74a52009-07-31 02:45:11 +00001650 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001651 else
John McCall9bb74a52009-07-31 02:45:11 +00001652 TUK = Action::TUK_Reference;
Douglas Gregord6ab8742009-05-28 23:31:59 +00001653 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001654 bool IsDependent = false;
John McCall9bb74a52009-07-31 02:45:11 +00001655 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001656 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregor27bdf00f2009-07-23 16:36:45 +00001657 Action::MultiTemplateParamsArg(Actions),
John McCall7f41d982009-09-11 04:59:25 +00001658 Owned, IsDependent);
1659 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump11289f42009-09-09 15:08:12 +00001660
Chris Lattner76c72282007-10-09 17:33:22 +00001661 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001662 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001663
Chris Lattner3b561a32006-08-13 00:12:11 +00001664 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +00001665 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001666 unsigned DiagID;
1667 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001668 TagDecl.getAs<void>(), Owned))
John McCall49bfce42009-08-03 20:12:06 +00001669 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00001670}
1671
Chris Lattnerc1915e22007-01-25 07:29:02 +00001672/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1673/// enumerator-list:
1674/// enumerator
1675/// enumerator-list ',' enumerator
1676/// enumerator:
1677/// enumeration-constant
1678/// enumeration-constant '=' constant-expression
1679/// enumeration-constant:
1680/// identifier
1681///
Chris Lattner83f095c2009-03-28 19:18:32 +00001682void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001683 // Enter the scope of the enum body and start the definition.
1684 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001685 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00001686
Chris Lattnerc1915e22007-01-25 07:29:02 +00001687 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001688
Chris Lattner37256fb2007-08-27 17:24:30 +00001689 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00001690 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001691 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump11289f42009-09-09 15:08:12 +00001692
Chris Lattner83f095c2009-03-28 19:18:32 +00001693 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001694
Chris Lattner83f095c2009-03-28 19:18:32 +00001695 DeclPtrTy LastEnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001696
Chris Lattnerc1915e22007-01-25 07:29:02 +00001697 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001698 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001699 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1700 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001701
Chris Lattnerc1915e22007-01-25 07:29:02 +00001702 SourceLocation EqualLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001703 OwningExprResult AssignedVal(Actions);
Chris Lattner76c72282007-10-09 17:33:22 +00001704 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001705 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001706 AssignedVal = ParseConstantExpression();
1707 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00001708 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001709 }
Mike Stump11289f42009-09-09 15:08:12 +00001710
Chris Lattnerc1915e22007-01-25 07:29:02 +00001711 // Install the enumerator constant into EnumDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00001712 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1713 LastEnumConstDecl,
1714 IdentLoc, Ident,
1715 EqualLoc,
1716 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00001717 EnumConstantDecls.push_back(EnumConstDecl);
1718 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001719
Chris Lattner76c72282007-10-09 17:33:22 +00001720 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001721 break;
1722 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001723
1724 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00001725 !(getLang().C99 || getLang().CPlusPlus0x))
1726 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1727 << getLang().CPlusPlus
1728 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Chris Lattnerc1915e22007-01-25 07:29:02 +00001729 }
Mike Stump11289f42009-09-09 15:08:12 +00001730
Chris Lattnerc1915e22007-01-25 07:29:02 +00001731 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00001732 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001733
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001734 AttributeList *Attr = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001735 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001736 if (Tok.is(tok::kw___attribute))
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001737 Attr = ParseAttributes();
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001738
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001739 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1740 EnumConstantDecls.data(), EnumConstantDecls.size(),
1741 CurScope, Attr);
Mike Stump11289f42009-09-09 15:08:12 +00001742
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001743 EnumScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001744 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001745}
Chris Lattner3b561a32006-08-13 00:12:11 +00001746
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001747/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001748/// start of a type-qualifier-list.
1749bool Parser::isTypeQualifier() const {
1750 switch (Tok.getKind()) {
1751 default: return false;
1752 // type-qualifier
1753 case tok::kw_const:
1754 case tok::kw_volatile:
1755 case tok::kw_restrict:
1756 return true;
1757 }
1758}
1759
1760/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001761/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001762bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001763 switch (Tok.getKind()) {
1764 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001765
Chris Lattner020bab92009-01-04 23:41:41 +00001766 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001767 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001768 // Annotate typenames and C++ scope specifiers. If we get one, just
1769 // recurse to handle whatever we get.
1770 if (TryAnnotateTypeOrScopeToken())
1771 return isTypeSpecifierQualifier();
1772 // Otherwise, not a type specifier.
1773 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001774
Chris Lattner020bab92009-01-04 23:41:41 +00001775 case tok::coloncolon: // ::foo::bar
1776 if (NextToken().is(tok::kw_new) || // ::new
1777 NextToken().is(tok::kw_delete)) // ::delete
1778 return false;
1779
1780 // Annotate typenames and C++ scope specifiers. If we get one, just
1781 // recurse to handle whatever we get.
1782 if (TryAnnotateTypeOrScopeToken())
1783 return isTypeSpecifierQualifier();
1784 // Otherwise, not a type specifier.
1785 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001786
Chris Lattnere37e2332006-08-15 04:50:22 +00001787 // GNU attributes support.
1788 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00001789 // GNU typeof support.
1790 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00001791
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001792 // type-specifiers
1793 case tok::kw_short:
1794 case tok::kw_long:
1795 case tok::kw_signed:
1796 case tok::kw_unsigned:
1797 case tok::kw__Complex:
1798 case tok::kw__Imaginary:
1799 case tok::kw_void:
1800 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001801 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001802 case tok::kw_char16_t:
1803 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001804 case tok::kw_int:
1805 case tok::kw_float:
1806 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001807 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001808 case tok::kw__Bool:
1809 case tok::kw__Decimal32:
1810 case tok::kw__Decimal64:
1811 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00001812
Chris Lattner861a2262008-04-13 18:59:07 +00001813 // struct-or-union-specifier (C99) or class-specifier (C++)
1814 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001815 case tok::kw_struct:
1816 case tok::kw_union:
1817 // enum-specifier
1818 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00001819
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001820 // type-qualifier
1821 case tok::kw_const:
1822 case tok::kw_volatile:
1823 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001824
1825 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001826 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001827 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001828
Chris Lattner409bf7d2008-10-20 00:25:30 +00001829 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1830 case tok::less:
1831 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00001832
Steve Naroff44ac7772008-12-25 14:16:32 +00001833 case tok::kw___cdecl:
1834 case tok::kw___stdcall:
1835 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001836 case tok::kw___w64:
1837 case tok::kw___ptr64:
1838 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001839 }
1840}
1841
Chris Lattneracd58a32006-08-06 17:24:14 +00001842/// isDeclarationSpecifier() - Return true if the current token is part of a
1843/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001844bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00001845 switch (Tok.getKind()) {
1846 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001847
Chris Lattner020bab92009-01-04 23:41:41 +00001848 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00001849 // Unfortunate hack to support "Class.factoryMethod" notation.
1850 if (getLang().ObjC1 && NextToken().is(tok::period))
1851 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001852 // Fall through
Steve Naroff9527bbf2009-03-09 21:12:44 +00001853
Douglas Gregor333489b2009-03-27 23:10:48 +00001854 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001855 // Annotate typenames and C++ scope specifiers. If we get one, just
1856 // recurse to handle whatever we get.
1857 if (TryAnnotateTypeOrScopeToken())
1858 return isDeclarationSpecifier();
1859 // Otherwise, not a declaration specifier.
1860 return false;
1861 case tok::coloncolon: // ::foo::bar
1862 if (NextToken().is(tok::kw_new) || // ::new
1863 NextToken().is(tok::kw_delete)) // ::delete
1864 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001865
Chris Lattner020bab92009-01-04 23:41:41 +00001866 // Annotate typenames and C++ scope specifiers. If we get one, just
1867 // recurse to handle whatever we get.
1868 if (TryAnnotateTypeOrScopeToken())
1869 return isDeclarationSpecifier();
1870 // Otherwise, not a declaration specifier.
1871 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001872
Chris Lattneracd58a32006-08-06 17:24:14 +00001873 // storage-class-specifier
1874 case tok::kw_typedef:
1875 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00001876 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00001877 case tok::kw_static:
1878 case tok::kw_auto:
1879 case tok::kw_register:
1880 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00001881
Chris Lattneracd58a32006-08-06 17:24:14 +00001882 // type-specifiers
1883 case tok::kw_short:
1884 case tok::kw_long:
1885 case tok::kw_signed:
1886 case tok::kw_unsigned:
1887 case tok::kw__Complex:
1888 case tok::kw__Imaginary:
1889 case tok::kw_void:
1890 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001891 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001892 case tok::kw_char16_t:
1893 case tok::kw_char32_t:
1894
Chris Lattneracd58a32006-08-06 17:24:14 +00001895 case tok::kw_int:
1896 case tok::kw_float:
1897 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001898 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00001899 case tok::kw__Bool:
1900 case tok::kw__Decimal32:
1901 case tok::kw__Decimal64:
1902 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00001903
Chris Lattner861a2262008-04-13 18:59:07 +00001904 // struct-or-union-specifier (C99) or class-specifier (C++)
1905 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00001906 case tok::kw_struct:
1907 case tok::kw_union:
1908 // enum-specifier
1909 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00001910
Chris Lattneracd58a32006-08-06 17:24:14 +00001911 // type-qualifier
1912 case tok::kw_const:
1913 case tok::kw_volatile:
1914 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00001915
Chris Lattneracd58a32006-08-06 17:24:14 +00001916 // function-specifier
1917 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00001918 case tok::kw_virtual:
1919 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00001920
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001921 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001922 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001923
Chris Lattner599e47e2007-08-09 17:01:07 +00001924 // GNU typeof support.
1925 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00001926
Chris Lattner599e47e2007-08-09 17:01:07 +00001927 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00001928 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00001929 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001930
Chris Lattner8b2ec162008-07-26 03:38:44 +00001931 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1932 case tok::less:
1933 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00001934
Steve Narofff192fab2009-01-06 19:34:12 +00001935 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00001936 case tok::kw___cdecl:
1937 case tok::kw___stdcall:
1938 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001939 case tok::kw___w64:
1940 case tok::kw___ptr64:
1941 case tok::kw___forceinline:
1942 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00001943 }
1944}
1945
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001946
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001947/// ParseTypeQualifierListOpt
1948/// type-qualifier-list: [C99 6.7.5]
1949/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00001950/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001951/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00001952/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001953///
Chris Lattnercf0bab22008-12-18 07:02:59 +00001954void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001955 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00001956 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001957 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001958 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00001959 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001960
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001961 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001962 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001963 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
1964 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001965 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001966 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001967 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1968 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001969 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001970 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001971 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1972 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001973 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001974 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001975 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001976 case tok::kw___cdecl:
1977 case tok::kw___stdcall:
1978 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001979 if (AttributesAllowed) {
1980 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1981 continue;
1982 }
1983 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00001984 case tok::kw___attribute:
Chris Lattnercf0bab22008-12-18 07:02:59 +00001985 if (AttributesAllowed) {
1986 DS.AddAttributes(ParseAttributes());
1987 continue; // do *not* consume the next token!
1988 }
1989 // otherwise, FALL THROUGH!
1990 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00001991 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00001992 // If this is not a type-qualifier token, we're done reading type
1993 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00001994 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00001995 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001996 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00001997
Chris Lattnerd9c3c592006-08-05 06:26:47 +00001998 // If the specifier combination wasn't legal, issue a diagnostic.
1999 if (isInvalid) {
2000 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002001 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002002 }
2003 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002004 }
2005}
2006
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002007
2008/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2009///
2010void Parser::ParseDeclarator(Declarator &D) {
2011 /// This implements the 'declarator' production in the C grammar, then checks
2012 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002013 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002014}
2015
Sebastian Redlbd150f42008-11-21 19:14:01 +00002016/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2017/// is parsed by the function passed to it. Pass null, and the direct-declarator
2018/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002019/// ptr-operator production.
2020///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002021/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2022/// [C] pointer[opt] direct-declarator
2023/// [C++] direct-declarator
2024/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002025///
2026/// pointer: [C99 6.7.5]
2027/// '*' type-qualifier-list[opt]
2028/// '*' type-qualifier-list[opt] pointer
2029///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002030/// ptr-operator:
2031/// '*' cv-qualifier-seq[opt]
2032/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002033/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002034/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002035/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002036/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002037void Parser::ParseDeclaratorInternal(Declarator &D,
2038 DirectDeclParseFunction DirectDeclParser) {
Bill Wendling3708c182007-05-27 10:15:43 +00002039
Douglas Gregor66a985d2009-08-26 14:27:30 +00002040 if (Diags.hasAllExtensionsSilenced())
2041 D.setExtension();
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002042 // C++ member pointers start with a '::' or a nested-name.
2043 // Member pointers get special handling, since there's no place for the
2044 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002045 if (getLang().CPlusPlus &&
2046 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2047 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002048 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00002049 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump11289f42009-09-09 15:08:12 +00002050 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002051 // The scope spec really belongs to the direct-declarator.
2052 D.getCXXScopeSpec() = SS;
2053 if (DirectDeclParser)
2054 (this->*DirectDeclParser)(D);
2055 return;
2056 }
2057
2058 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002059 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002060 DeclSpec DS;
2061 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002062 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002063
2064 // Recurse to parse whatever is left.
2065 ParseDeclaratorInternal(D, DirectDeclParser);
2066
2067 // Sema will have to catch (syntactically invalid) pointers into global
2068 // scope. It has to catch pointers into namespace scope anyway.
2069 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002070 Loc, DS.TakeAttributes()),
2071 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002072 return;
2073 }
2074 }
2075
2076 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002077 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002078 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002079 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002080 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002081 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002082 if (DirectDeclParser)
2083 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002084 return;
2085 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002086
Sebastian Redled0f3b02009-03-15 22:02:01 +00002087 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2088 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002089 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002090 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002091
Chris Lattner9eac9312009-03-27 04:18:06 +00002092 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002093 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002094 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002095
Bill Wendling3708c182007-05-27 10:15:43 +00002096 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002097 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002098
Bill Wendling3708c182007-05-27 10:15:43 +00002099 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002100 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002101 if (Kind == tok::star)
2102 // Remember that we parsed a pointer type, and remember the type-quals.
2103 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002104 DS.TakeAttributes()),
2105 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002106 else
2107 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002108 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002109 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002110 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002111 } else {
2112 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002113 DeclSpec DS;
2114
Sebastian Redl3b27be62009-03-23 00:00:23 +00002115 // Complain about rvalue references in C++03, but then go on and build
2116 // the declarator.
2117 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2118 Diag(Loc, diag::err_rvalue_reference);
2119
Bill Wendling93efb222007-06-02 23:28:54 +00002120 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2121 // cv-qualifiers are introduced through the use of a typedef or of a
2122 // template type argument, in which case the cv-qualifiers are ignored.
2123 //
2124 // [GNU] Retricted references are allowed.
2125 // [GNU] Attributes on references are allowed.
2126 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002127 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002128
2129 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2130 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2131 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002132 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002133 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2134 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002135 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002136 }
Bill Wendling3708c182007-05-27 10:15:43 +00002137
2138 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002139 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002140
Douglas Gregor66583c52008-11-03 15:51:28 +00002141 if (D.getNumTypeObjects() > 0) {
2142 // C++ [dcl.ref]p4: There shall be no references to references.
2143 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2144 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002145 if (const IdentifierInfo *II = D.getIdentifier())
2146 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2147 << II;
2148 else
2149 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2150 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002151
Sebastian Redlbd150f42008-11-21 19:14:01 +00002152 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002153 // can go ahead and build the (technically ill-formed)
2154 // declarator: reference collapsing will take care of it.
2155 }
2156 }
2157
Bill Wendling3708c182007-05-27 10:15:43 +00002158 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002159 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002160 DS.TakeAttributes(),
2161 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002162 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002163 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002164}
2165
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002166/// ParseDirectDeclarator
2167/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002168/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002169/// '(' declarator ')'
2170/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002171/// [C90] direct-declarator '[' constant-expression[opt] ']'
2172/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2173/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2174/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2175/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002176/// direct-declarator '(' parameter-type-list ')'
2177/// direct-declarator '(' identifier-list[opt] ')'
2178/// [GNU] direct-declarator '(' parameter-forward-declarations
2179/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002180/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2181/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002182/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002183///
2184/// declarator-id: [C++ 8]
2185/// id-expression
2186/// '::'[opt] nested-name-specifier[opt] type-name
2187///
2188/// id-expression: [C++ 5.1]
2189/// unqualified-id
2190/// qualified-id [TODO]
2191///
2192/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002193/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002194/// operator-function-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002195/// conversion-function-id [TODO]
Mike Stump11289f42009-09-09 15:08:12 +00002196/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002197/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002198///
Chris Lattneracd58a32006-08-06 17:24:14 +00002199void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002200 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002201
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002202 if (getLang().CPlusPlus) {
2203 if (D.mayHaveIdentifier()) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002204 // ParseDeclaratorInternal might already have parsed the scope.
2205 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
Mike Stump11289f42009-09-09 15:08:12 +00002206 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
Douglas Gregorb7bfe792009-09-02 22:59:36 +00002207 true);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002208 if (afterCXXScope) {
2209 // Change the declaration context for name lookup, until this function
2210 // is exited (and the declarator has been parsed).
2211 DeclScopeObj.EnterDeclaratorScope();
2212 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002213
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002214 if (Tok.is(tok::identifier)) {
2215 assert(Tok.getIdentifierInfo() && "Not an identifier?");
Anders Carlssona0886932009-04-30 22:41:11 +00002216
2217 // If this identifier is the name of the current class, it's a
Mike Stump11289f42009-09-09 15:08:12 +00002218 // constructor name.
Anders Carlssona0886932009-04-30 22:41:11 +00002219 if (!D.getDeclSpec().hasTypeSpecifier() &&
2220 Actions.isCurrentClassName(*Tok.getIdentifierInfo(),CurScope)) {
Douglas Gregordce892e2009-07-06 16:40:48 +00002221 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Anders Carlssona0886932009-04-30 22:41:11 +00002222 D.setConstructor(Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregordce892e2009-07-06 16:40:48 +00002223 Tok.getLocation(), CurScope, SS),
Anders Carlssona0886932009-04-30 22:41:11 +00002224 Tok.getLocation());
2225 // This is a normal identifier.
2226 } else
2227 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002228 ConsumeToken();
2229 goto PastIdentifier;
Douglas Gregor7f741122009-02-25 19:37:18 +00002230 } else if (Tok.is(tok::annot_template_id)) {
Mike Stump11289f42009-09-09 15:08:12 +00002231 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00002232 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
2233
2234 // FIXME: Could this template-id name a constructor?
2235
2236 // FIXME: This is an egregious hack, where we silently ignore
2237 // the specialization (which should be a function template
2238 // specialization name) and use the name instead. This hack
2239 // will go away when we have support for function
2240 // specializations.
2241 D.SetIdentifier(TemplateId->Name, Tok.getLocation());
2242 TemplateId->Destroy();
2243 ConsumeToken();
2244 goto PastIdentifier;
Douglas Gregor1dc98262008-12-26 15:00:45 +00002245 } else if (Tok.is(tok::kw_operator)) {
2246 SourceLocation OperatorLoc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002247 SourceLocation EndLoc;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002248
Douglas Gregor1dc98262008-12-26 15:00:45 +00002249 // First try the name of an overloaded operator
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002250 if (OverloadedOperatorKind Op = TryParseOperatorFunctionId(&EndLoc)) {
2251 D.setOverloadedOperator(Op, OperatorLoc, EndLoc);
Douglas Gregor1dc98262008-12-26 15:00:45 +00002252 } else {
2253 // This must be a conversion function (C++ [class.conv.fct]).
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002254 if (TypeTy *ConvType = ParseConversionFunctionId(&EndLoc))
2255 D.setConversionFunction(ConvType, OperatorLoc, EndLoc);
2256 else {
Douglas Gregor1dc98262008-12-26 15:00:45 +00002257 D.SetIdentifier(0, Tok.getLocation());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002258 }
Douglas Gregor1dc98262008-12-26 15:00:45 +00002259 }
2260 goto PastIdentifier;
2261 } else if (Tok.is(tok::tilde)) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002262 // This should be a C++ destructor.
2263 SourceLocation TildeLoc = ConsumeToken();
Douglas Gregor5e0962f2009-08-26 18:27:52 +00002264 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002265 // FIXME: Inaccurate.
2266 SourceLocation NameLoc = Tok.getLocation();
Douglas Gregord54dfb82009-02-25 23:52:28 +00002267 SourceLocation EndLoc;
Douglas Gregordce892e2009-07-06 16:40:48 +00002268 CXXScopeSpec *SS = afterCXXScope? &D.getCXXScopeSpec() : 0;
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +00002269 TypeResult Type = ParseClassName(EndLoc, SS, true);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002270 if (Type.isInvalid())
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002271 D.SetIdentifier(0, TildeLoc);
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002272 else
2273 D.setDestructor(Type.get(), TildeLoc, NameLoc);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002274 } else {
Fariborz Jahanian4041dfc2009-07-20 17:43:15 +00002275 Diag(Tok, diag::err_destructor_class_name);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002276 D.SetIdentifier(0, TildeLoc);
2277 }
2278 goto PastIdentifier;
2279 }
2280
2281 // If we reached this point, token is not identifier and not '~'.
2282
2283 if (afterCXXScope) {
2284 Diag(Tok, diag::err_expected_unqualified_id);
2285 D.SetIdentifier(0, Tok.getLocation());
2286 D.setInvalidType(true);
2287 goto PastIdentifier;
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002288 }
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002289 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002290 }
2291
2292 // If we reached this point, we are either in C/ObjC or the token didn't
2293 // satisfy any of the C++-specific checks.
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002294 if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
2295 assert(!getLang().CPlusPlus &&
2296 "There's a C++-specific check for tok::identifier above");
2297 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2298 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2299 ConsumeToken();
2300 } else if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002301 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002302 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002303 // Example: 'char (*X)' or 'int (*XX)(void)'
2304 ParseParenDeclarator(D);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002305 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002306 // This could be something simple like "int" (in which case the declarator
2307 // portion is empty), if an abstract-declarator is allowed.
2308 D.SetIdentifier(0, Tok.getLocation());
2309 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002310 if (D.getContext() == Declarator::MemberContext)
2311 Diag(Tok, diag::err_expected_member_name_or_semi)
2312 << D.getDeclSpec().getSourceRange();
2313 else if (getLang().CPlusPlus)
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002314 Diag(Tok, diag::err_expected_unqualified_id);
2315 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002316 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002317 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002318 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002319 }
Mike Stump11289f42009-09-09 15:08:12 +00002320
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002321 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002322 assert(D.isPastIdentifier() &&
2323 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002324
Chris Lattneracd58a32006-08-06 17:24:14 +00002325 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002326 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002327 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2328 // In such a case, check if we actually have a function declarator; if it
2329 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002330 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2331 // When not in file scope, warn for ambiguous function declarators, just
2332 // in case the author intended it as a variable definition.
2333 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2334 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2335 break;
2336 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002337 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002338 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002339 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002340 } else {
2341 break;
2342 }
2343 }
2344}
2345
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002346/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2347/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002348/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002349/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2350///
2351/// direct-declarator:
2352/// '(' declarator ')'
2353/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002354/// direct-declarator '(' parameter-type-list ')'
2355/// direct-declarator '(' identifier-list[opt] ')'
2356/// [GNU] direct-declarator '(' parameter-forward-declarations
2357/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002358///
2359void Parser::ParseParenDeclarator(Declarator &D) {
2360 SourceLocation StartLoc = ConsumeParen();
2361 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002362
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002363 // Eat any attributes before we look at whether this is a grouping or function
2364 // declarator paren. If this is a grouping paren, the attribute applies to
2365 // the type being built up, for example:
2366 // int (__attribute__(()) *x)(long y)
2367 // If this ends up not being a grouping paren, the attribute applies to the
2368 // first argument, for example:
2369 // int (__attribute__(()) int x)
2370 // In either case, we need to eat any attributes to be able to determine what
2371 // sort of paren this is.
2372 //
2373 AttributeList *AttrList = 0;
2374 bool RequiresArg = false;
2375 if (Tok.is(tok::kw___attribute)) {
2376 AttrList = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +00002377
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002378 // We require that the argument list (if this is a non-grouping paren) be
2379 // present even if the attribute list was empty.
2380 RequiresArg = true;
2381 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002382 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002383 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2384 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2385 Tok.is(tok::kw___ptr64)) {
2386 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2387 }
Mike Stump11289f42009-09-09 15:08:12 +00002388
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002389 // If we haven't past the identifier yet (or where the identifier would be
2390 // stored, if this is an abstract declarator), then this is probably just
2391 // grouping parens. However, if this could be an abstract-declarator, then
2392 // this could also be the start of function arguments (consider 'void()').
2393 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002394
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002395 if (!D.mayOmitIdentifier()) {
2396 // If this can't be an abstract-declarator, this *must* be a grouping
2397 // paren, because we haven't seen the identifier yet.
2398 isGrouping = true;
2399 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002400 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002401 isDeclarationSpecifier()) { // 'int(int)' is a function.
2402 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2403 // considered to be a type, not a K&R identifier-list.
2404 isGrouping = false;
2405 } else {
2406 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2407 isGrouping = true;
2408 }
Mike Stump11289f42009-09-09 15:08:12 +00002409
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002410 // If this is a grouping paren, handle:
2411 // direct-declarator: '(' declarator ')'
2412 // direct-declarator: '(' attributes declarator ')'
2413 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002414 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002415 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002416 if (AttrList)
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002417 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002418
Sebastian Redlbd150f42008-11-21 19:14:01 +00002419 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002420 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002421 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002422
2423 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002424 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002425 return;
2426 }
Mike Stump11289f42009-09-09 15:08:12 +00002427
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002428 // Okay, if this wasn't a grouping paren, it must be the start of a function
2429 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002430 // identifier (and remember where it would have been), then call into
2431 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002432 D.SetIdentifier(0, Tok.getLocation());
2433
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002434 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002435}
2436
2437/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2438/// declarator D up to a paren, which indicates that we are parsing function
2439/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002440///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002441/// If AttrList is non-null, then the caller parsed those arguments immediately
2442/// after the open paren - they should be considered to be the first argument of
2443/// a parameter. If RequiresArg is true, then the first argument of the
2444/// function is required to be present and required to not be an identifier
2445/// list.
2446///
Chris Lattneracd58a32006-08-06 17:24:14 +00002447/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002448/// parameter-type-list: [C99 6.7.5]
2449/// parameter-list
2450/// parameter-list ',' '...'
2451///
2452/// parameter-list: [C99 6.7.5]
2453/// parameter-declaration
2454/// parameter-list ',' parameter-declaration
2455///
2456/// parameter-declaration: [C99 6.7.5]
2457/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002458/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002459/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002460/// declaration-specifiers abstract-declarator[opt]
2461/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002462/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002463/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002464///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002465/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002466/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002467///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002468void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2469 AttributeList *AttrList,
2470 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002471 // lparen is already consumed!
2472 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002473
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002474 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002475 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002476 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002477 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002478 delete AttrList;
2479 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002480
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002481 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2482 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002483
2484 // cv-qualifier-seq[opt].
2485 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002486 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002487 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002488 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002489 llvm::SmallVector<TypeTy*, 2> Exceptions;
2490 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002491 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002492 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002493 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002494 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002495
2496 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002497 if (Tok.is(tok::kw_throw)) {
2498 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002499 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002500 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002501 hasAnyExceptionSpec);
2502 assert(Exceptions.size() == ExceptionRanges.size() &&
2503 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002504 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002505 }
2506
Chris Lattner371ed4e2008-04-06 06:57:35 +00002507 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002508 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002509 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002510 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002511 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002512 /*arglist*/ 0, 0,
2513 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002514 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002515 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002516 Exceptions.data(),
2517 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002518 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002519 LParenLoc, RParenLoc, D),
2520 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002521 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002522 }
2523
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002524 // Alternatively, this parameter list may be an identifier list form for a
2525 // K&R-style function: void foo(a,b,c)
Steve Naroffb0486722009-01-28 19:16:40 +00002526 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff3b6a4bd2009-01-30 14:23:32 +00002527 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002528 // K&R identifier lists can't have typedefs as identifiers, per
2529 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002530 if (RequiresArg) {
2531 Diag(Tok, diag::err_argument_required_after_attribute);
2532 delete AttrList;
2533 }
Steve Naroffb0486722009-01-28 19:16:40 +00002534 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2535 // normal declarators, not for abstract-declarators.
2536 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002537 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002538 }
Mike Stump11289f42009-09-09 15:08:12 +00002539
Chris Lattner371ed4e2008-04-06 06:57:35 +00002540 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00002541
Chris Lattner371ed4e2008-04-06 06:57:35 +00002542 // Build up an array of information about the parsed arguments.
2543 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002544
2545 // Enter function-declaration scope, limiting any declarators to the
2546 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00002547 ParseScope PrototypeScope(this,
2548 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00002549
Chris Lattner371ed4e2008-04-06 06:57:35 +00002550 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002551 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00002552 while (1) {
2553 if (Tok.is(tok::ellipsis)) {
2554 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002555 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002556 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00002557 }
Mike Stump11289f42009-09-09 15:08:12 +00002558
Chris Lattner371ed4e2008-04-06 06:57:35 +00002559 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002560
Chris Lattner371ed4e2008-04-06 06:57:35 +00002561 // Parse the declaration-specifiers.
2562 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002563
2564 // If the caller parsed attributes for the first argument, add them now.
2565 if (AttrList) {
2566 DS.AddAttributes(AttrList);
2567 AttrList = 0; // Only apply the attributes to the first parameter.
2568 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002569 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002570
Chris Lattner371ed4e2008-04-06 06:57:35 +00002571 // Parse the declarator. This is "PrototypeContext", because we must
2572 // accept either 'declarator' or 'abstract-declarator' here.
2573 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2574 ParseDeclarator(ParmDecl);
2575
2576 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002577 if (Tok.is(tok::kw___attribute)) {
2578 SourceLocation Loc;
2579 AttributeList *AttrList = ParseAttributes(&Loc);
2580 ParmDecl.AddAttributes(AttrList, Loc);
2581 }
Mike Stump11289f42009-09-09 15:08:12 +00002582
Chris Lattner371ed4e2008-04-06 06:57:35 +00002583 // Remember this parsed parameter in ParamInfo.
2584 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00002585
Douglas Gregor4d87df52008-12-16 21:30:33 +00002586 // DefArgToks is used when the parsing of default arguments needs
2587 // to be delayed.
2588 CachedTokens *DefArgToks = 0;
2589
Chris Lattner371ed4e2008-04-06 06:57:35 +00002590 // If no parameter was specified, verify that *something* was specified,
2591 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002592 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2593 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00002594 // Completely missing, emit error.
2595 Diag(DSStart, diag::err_missing_param);
2596 } else {
2597 // Otherwise, we have something. Add it and let semantic analysis try
2598 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00002599
Chris Lattner371ed4e2008-04-06 06:57:35 +00002600 // Inform the actions module about the parameter declarator, so it gets
2601 // added to the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002602 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002603
2604 // Parse the default argument, if any. We parse the default
2605 // arguments in all dialects; the semantic analysis in
2606 // ActOnParamDefaultArgument will reject the default argument in
2607 // C.
2608 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00002609 SourceLocation EqualLoc = Tok.getLocation();
2610
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002611 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00002612 if (D.getContext() == Declarator::MemberContext) {
2613 // If we're inside a class definition, cache the tokens
2614 // corresponding to the default argument. We'll actually parse
2615 // them when we see the end of the class definition.
2616 // FIXME: Templates will require something similar.
2617 // FIXME: Can we use a smart pointer for Toks?
2618 DefArgToks = new CachedTokens;
2619
Mike Stump11289f42009-09-09 15:08:12 +00002620 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002621 tok::semi, false)) {
2622 delete DefArgToks;
2623 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00002624 Actions.ActOnParamDefaultArgumentError(Param);
2625 } else
Mike Stump11289f42009-09-09 15:08:12 +00002626 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00002627 (*DefArgToks)[1].getLocation());
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002628 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00002629 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00002630 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002631
Douglas Gregor4d87df52008-12-16 21:30:33 +00002632 OwningExprResult DefArgResult(ParseAssignmentExpression());
2633 if (DefArgResult.isInvalid()) {
2634 Actions.ActOnParamDefaultArgumentError(Param);
2635 SkipUntil(tok::comma, tok::r_paren, true, true);
2636 } else {
2637 // Inform the actions module about the default argument
2638 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002639 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00002640 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002641 }
2642 }
Mike Stump11289f42009-09-09 15:08:12 +00002643
2644 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2645 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002646 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00002647 }
2648
2649 // If the next token is a comma, consume it and keep reading arguments.
2650 if (Tok.isNot(tok::comma)) break;
Mike Stump11289f42009-09-09 15:08:12 +00002651
Chris Lattner371ed4e2008-04-06 06:57:35 +00002652 // Consume the comma.
2653 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00002654 }
Mike Stump11289f42009-09-09 15:08:12 +00002655
Chris Lattner371ed4e2008-04-06 06:57:35 +00002656 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002657 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00002658
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002659 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002660 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2661 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002662
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002663 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002664 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002665 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002666 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002667 llvm::SmallVector<TypeTy*, 2> Exceptions;
2668 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002669 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002670 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00002671 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002672 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002673 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002674
2675 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002676 if (Tok.is(tok::kw_throw)) {
2677 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002678 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002679 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002680 hasAnyExceptionSpec);
2681 assert(Exceptions.size() == ExceptionRanges.size() &&
2682 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002683 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002684 }
2685
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002686 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002687 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002688 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002689 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002690 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002691 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002692 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002693 Exceptions.data(),
2694 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002695 Exceptions.size(),
2696 LParenLoc, RParenLoc, D),
2697 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002698}
Chris Lattneracd58a32006-08-06 17:24:14 +00002699
Chris Lattner6c940e62008-04-06 06:34:08 +00002700/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2701/// we found a K&R-style identifier list instead of a type argument list. The
2702/// current token is known to be the first identifier in the list.
2703///
2704/// identifier-list: [C99 6.7.5]
2705/// identifier
2706/// identifier-list ',' identifier
2707///
2708void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2709 Declarator &D) {
2710 // Build up an array of information about the parsed arguments.
2711 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2712 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00002713
Chris Lattner6c940e62008-04-06 06:34:08 +00002714 // If there was no identifier specified for the declarator, either we are in
2715 // an abstract-declarator, or we are in a parameter declarator which was found
2716 // to be abstract. In abstract-declarators, identifier lists are not valid:
2717 // diagnose this.
2718 if (!D.getIdentifier())
2719 Diag(Tok, diag::ext_ident_list_in_param);
2720
2721 // Tok is known to be the first identifier in the list. Remember this
2722 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00002723 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00002724 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner83f095c2009-03-28 19:18:32 +00002725 Tok.getLocation(),
2726 DeclPtrTy()));
Mike Stump11289f42009-09-09 15:08:12 +00002727
Chris Lattner9186f552008-04-06 06:39:19 +00002728 ConsumeToken(); // eat the first identifier.
Mike Stump11289f42009-09-09 15:08:12 +00002729
Chris Lattner6c940e62008-04-06 06:34:08 +00002730 while (Tok.is(tok::comma)) {
2731 // Eat the comma.
2732 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002733
Chris Lattner9186f552008-04-06 06:39:19 +00002734 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00002735 if (Tok.isNot(tok::identifier)) {
2736 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00002737 SkipUntil(tok::r_paren);
2738 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00002739 }
Chris Lattner67b450c2008-04-06 06:47:48 +00002740
Chris Lattner6c940e62008-04-06 06:34:08 +00002741 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00002742
2743 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00002744 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerebad6a22008-11-19 07:37:42 +00002745 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00002746
Chris Lattner6c940e62008-04-06 06:34:08 +00002747 // Verify that the argument identifier has not already been mentioned.
2748 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002749 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00002750 } else {
2751 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00002752 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00002753 Tok.getLocation(),
2754 DeclPtrTy()));
Chris Lattner9186f552008-04-06 06:39:19 +00002755 }
Mike Stump11289f42009-09-09 15:08:12 +00002756
Chris Lattner6c940e62008-04-06 06:34:08 +00002757 // Eat the identifier.
2758 ConsumeToken();
2759 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002760
2761 // If we have the closing ')', eat it and we're done.
2762 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2763
Chris Lattner9186f552008-04-06 06:39:19 +00002764 // Remember that we parsed a function type, and remember the attributes. This
2765 // function type is always a K&R style function type, which is not varargs and
2766 // has no prototype.
2767 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002768 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00002769 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002770 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002771 /*exception*/false,
2772 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002773 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002774 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00002775}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002776
Chris Lattnere8074e62006-08-06 18:30:15 +00002777/// [C90] direct-declarator '[' constant-expression[opt] ']'
2778/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2779/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2780/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2781/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2782void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00002783 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00002784
Chris Lattner84a11622008-12-18 07:27:21 +00002785 // C array syntax has many features, but by-far the most common is [] and [4].
2786 // This code does a fast path to handle some of the most obvious cases.
2787 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002788 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002789 // Remember that we parsed the empty array type.
2790 OwningExprResult NumElements(Actions);
Douglas Gregor04318252009-07-06 15:59:29 +00002791 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2792 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002793 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002794 return;
2795 } else if (Tok.getKind() == tok::numeric_constant &&
2796 GetLookAheadToken(1).is(tok::r_square)) {
2797 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00002798 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00002799 ConsumeToken();
2800
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002801 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002802
2803 // If there was an error parsing the assignment-expression, recover.
2804 if (ExprRes.isInvalid())
2805 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00002806
Chris Lattner84a11622008-12-18 07:27:21 +00002807 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00002808 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2809 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002810 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002811 return;
2812 }
Mike Stump11289f42009-09-09 15:08:12 +00002813
Chris Lattnere8074e62006-08-06 18:30:15 +00002814 // If valid, this location is the position where we read the 'static' keyword.
2815 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00002816 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002817 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002818
Chris Lattnere8074e62006-08-06 18:30:15 +00002819 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002820 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00002821 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00002822 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00002823
Chris Lattnere8074e62006-08-06 18:30:15 +00002824 // If we haven't already read 'static', check to see if there is one after the
2825 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002826 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002827 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002828
Chris Lattnere8074e62006-08-06 18:30:15 +00002829 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00002830 bool isStar = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +00002831 OwningExprResult NumElements(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00002832
Chris Lattner521ff2b2008-04-06 05:26:30 +00002833 // Handle the case where we have '[*]' as the array size. However, a leading
2834 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2835 // the the token after the star is a ']'. Since stars in arrays are
2836 // infrequent, use of lookahead is not costly here.
2837 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00002838 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00002839
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002840 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00002841 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002842 StaticLoc = SourceLocation(); // Drop the static.
2843 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00002844 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00002845 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00002846 // Note, in C89, this production uses the constant-expr production instead
2847 // of assignment-expr. The only difference is that assignment-expr allows
2848 // things like '=' and '*='. Sema rejects these in C89 mode because they
2849 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00002850
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00002851 // Parse the constant-expression or assignment-expression now (depending
2852 // on dialect).
2853 if (getLang().CPlusPlus)
2854 NumElements = ParseConstantExpression();
2855 else
2856 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00002857 }
Mike Stump11289f42009-09-09 15:08:12 +00002858
Chris Lattner62591722006-08-12 18:40:58 +00002859 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002860 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00002861 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00002862 // If the expression was invalid, skip it.
2863 SkipUntil(tok::r_square);
2864 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00002865 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002866
2867 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2868
Chris Lattner84a11622008-12-18 07:27:21 +00002869 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00002870 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2871 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00002872 NumElements.release(),
2873 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002874 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00002875}
2876
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002877/// [GNU] typeof-specifier:
2878/// typeof ( expressions )
2879/// typeof ( type-name )
2880/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00002881///
2882void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00002883 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002884 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00002885 SourceLocation StartLoc = ConsumeToken();
2886
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002887 bool isCastExpr;
2888 TypeTy *CastTy;
2889 SourceRange CastRange;
2890 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2891 isCastExpr,
2892 CastTy,
2893 CastRange);
2894
2895 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002896 // FIXME: Not accurate, the range gets one token more than it should.
2897 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002898 else
2899 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00002900
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002901 if (isCastExpr) {
2902 if (!CastTy) {
2903 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002904 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00002905 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002906
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002907 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002908 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002909 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2910 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002911 DiagID, CastTy))
2912 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00002913 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002914 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002915
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002916 // If we get here, the operand to the typeof was an expresion.
2917 if (Operand.isInvalid()) {
2918 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00002919 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00002920 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002921
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002922 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002923 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00002924 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2925 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002926 DiagID, Operand.release()))
2927 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00002928}