blob: 66a69574433ed6d1f055cb30233434bfe0ea471b [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"
Douglas Gregorb53edfb2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Chris Lattner8a9a97a2009-12-10 00:21:05 +000018#include "RAIIObjectsForParser.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000019#include "llvm/ADT/SmallSet.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
Chris Lattnerf5fbd792006-08-10 23:56:11 +000026/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redld6434562009-05-29 18:02:33 +000031Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Chris Lattnerf5fbd792006-08-10 23:56:11 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
Chris Lattner1890ac82006-08-13 01:16:23 +000034 ParseSpecifierQualifierList(DS);
Sebastian Redld6434562009-05-29 18:02:33 +000035
Chris Lattnerf5fbd792006-08-10 23:56:11 +000036 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000039 if (Range)
40 *Range = DeclaratorInfo.getSourceRange();
41
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000042 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000043 return true;
44
45 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000046}
47
Alexis Hunt96d5c762009-11-21 08:43:09 +000048/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000049///
50/// [GNU] attributes:
51/// attribute
52/// attributes attribute
53///
54/// [GNU] attribute:
55/// '__attribute__' '(' '(' attribute-list ')' ')'
56///
57/// [GNU] attribute-list:
58/// attrib
59/// attribute_list ',' attrib
60///
61/// [GNU] attrib:
62/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000063/// attrib-name
64/// attrib-name '(' identifier ')'
65/// attrib-name '(' identifier ',' nonempty-expr-list ')'
66/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000067///
Steve Naroff0f2fe172007-06-01 17:11:19 +000068/// [GNU] attrib-name:
69/// identifier
70/// typespec
71/// typequal
72/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000073///
Steve Naroff0f2fe172007-06-01 17:11:19 +000074/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000075/// token lookahead. Comment from gcc: "If they start with an identifier
76/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +000077/// start with that identifier; otherwise they are an expression list."
78///
79/// At the moment, I am not doing 2 token lookahead. I am also unaware of
80/// any attributes that don't work (based on my limited testing). Most
81/// attributes are very simple in practice. Until we find a bug, I don't see
82/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000083
Alexis Hunt96d5c762009-11-21 08:43:09 +000084AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
85 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +000086
Steve Naroffb8371e12007-06-09 03:39:29 +000087 AttributeList *CurrAttr = 0;
Mike Stump11289f42009-09-09 15:08:12 +000088
Chris Lattner76c72282007-10-09 17:33:22 +000089 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +000090 ConsumeToken();
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
92 "attribute")) {
93 SkipUntil(tok::r_paren, true); // skip until ) or ;
94 return CurrAttr;
95 }
96 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
97 SkipUntil(tok::r_paren, true); // skip until ) or ;
98 return CurrAttr;
99 }
100 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000101 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
102 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000103
104 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
106 ConsumeToken();
107 continue;
108 }
109 // we have an identifier or declaration specifier (const, int, etc.)
110 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
111 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000112
Steve Naroff0f2fe172007-06-01 17:11:19 +0000113 // check if we have a "paramterized" attribute
Chris Lattner76c72282007-10-09 17:33:22 +0000114 if (Tok.is(tok::l_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000115 ConsumeParen(); // ignore the left paren loc for now
Mike Stump11289f42009-09-09 15:08:12 +0000116
Chris Lattner76c72282007-10-09 17:33:22 +0000117 if (Tok.is(tok::identifier)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000118 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
119 SourceLocation ParmLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000120
121 if (Tok.is(tok::r_paren)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000122 // __attribute__(( mode(byte) ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000123 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000124 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000125 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner76c72282007-10-09 17:33:22 +0000126 } else if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000127 ConsumeToken();
128 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000129 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000130 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000131
Steve Naroff0f2fe172007-06-01 17:11:19 +0000132 // now parse the non-empty comma separated list of expressions
133 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000134 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000135 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000136 ArgExprsOk = false;
137 SkipUntil(tok::r_paren);
138 break;
139 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000140 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000141 }
Chris Lattner76c72282007-10-09 17:33:22 +0000142 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000143 break;
144 ConsumeToken(); // Eat the comma, move to the next argument
145 }
Chris Lattner76c72282007-10-09 17:33:22 +0000146 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000147 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000148 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
149 AttrNameLoc, ParmName, ParmLoc,
150 ArgExprs.take(), ArgExprs.size(),
151 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000152 }
153 }
154 } else { // not an identifier
Nate Begemanf2758702009-06-26 06:32:41 +0000155 switch (Tok.getKind()) {
156 case tok::r_paren:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000157 // parse a possibly empty comma separated list of expressions
Steve Naroff0f2fe172007-06-01 17:11:19 +0000158 // __attribute__(( nonnull() ))
Steve Naroffb8371e12007-06-09 03:39:29 +0000159 ConsumeParen(); // ignore the right paren loc for now
Alexis Hunt96d5c762009-11-21 08:43:09 +0000160 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000161 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begemanf2758702009-06-26 06:32:41 +0000162 break;
163 case tok::kw_char:
164 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +0000165 case tok::kw_char16_t:
166 case tok::kw_char32_t:
Nate Begemanf2758702009-06-26 06:32:41 +0000167 case tok::kw_bool:
168 case tok::kw_short:
169 case tok::kw_int:
170 case tok::kw_long:
171 case tok::kw_signed:
172 case tok::kw_unsigned:
173 case tok::kw_float:
174 case tok::kw_double:
175 case tok::kw_void:
176 case tok::kw_typeof:
177 // If it's a builtin type name, eat it and expect a rparen
178 // __attribute__(( vec_type_hint(char) ))
179 ConsumeToken();
Alexis Hunt96d5c762009-11-21 08:43:09 +0000180 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Nate Begemanf2758702009-06-26 06:32:41 +0000181 0, SourceLocation(), 0, 0, CurrAttr);
182 if (Tok.is(tok::r_paren))
183 ConsumeParen();
184 break;
185 default:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000186 // __attribute__(( aligned(16) ))
Sebastian Redl511ed552008-11-25 22:21:31 +0000187 ExprVector ArgExprs(Actions);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000188 bool ArgExprsOk = true;
Mike Stump11289f42009-09-09 15:08:12 +0000189
Steve Naroff0f2fe172007-06-01 17:11:19 +0000190 // now parse the list of expressions
191 while (1) {
Sebastian Redl59b5e512008-12-11 21:36:32 +0000192 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +0000193 if (ArgExpr.isInvalid()) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000194 ArgExprsOk = false;
195 SkipUntil(tok::r_paren);
196 break;
197 } else {
Sebastian Redld9f7b1c2008-12-10 00:02:53 +0000198 ArgExprs.push_back(ArgExpr.release());
Steve Naroff0f2fe172007-06-01 17:11:19 +0000199 }
Chris Lattner76c72282007-10-09 17:33:22 +0000200 if (Tok.isNot(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000201 break;
202 ConsumeToken(); // Eat the comma, move to the next argument
203 }
204 // Match the ')'.
Chris Lattner76c72282007-10-09 17:33:22 +0000205 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Steve Naroffb8371e12007-06-09 03:39:29 +0000206 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redl511ed552008-11-25 22:21:31 +0000207 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000208 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
209 ArgExprs.size(),
Steve Naroffb8371e12007-06-09 03:39:29 +0000210 CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000211 }
Nate Begemanf2758702009-06-26 06:32:41 +0000212 break;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000213 }
214 }
215 } else {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000216 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Steve Naroffb8371e12007-06-09 03:39:29 +0000217 0, SourceLocation(), 0, 0, CurrAttr);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000218 }
219 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000220 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000221 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000222 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000223 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
224 SkipUntil(tok::r_paren, false);
225 }
226 if (EndLoc)
227 *EndLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000228 }
229 return CurrAttr;
230}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000231
Eli Friedman06de2b52009-06-08 07:21:15 +0000232/// ParseMicrosoftDeclSpec - Parse an __declspec construct
233///
234/// [MS] decl-specifier:
235/// __declspec ( extended-decl-modifier-seq )
236///
237/// [MS] extended-decl-modifier-seq:
238/// extended-decl-modifier[opt]
239/// extended-decl-modifier extended-decl-modifier-seq
240
Eli Friedman53339e02009-06-08 23:27:34 +0000241AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000242 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000243
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000244 ConsumeToken();
Eli Friedman06de2b52009-06-08 07:21:15 +0000245 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
246 "declspec")) {
247 SkipUntil(tok::r_paren, true); // skip until ) or ;
248 return CurrAttr;
249 }
Eli Friedman53339e02009-06-08 23:27:34 +0000250 while (Tok.getIdentifierInfo()) {
Eli Friedman06de2b52009-06-08 07:21:15 +0000251 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
252 SourceLocation AttrNameLoc = ConsumeToken();
253 if (Tok.is(tok::l_paren)) {
254 ConsumeParen();
255 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
256 // correctly.
257 OwningExprResult ArgExpr(ParseAssignmentExpression());
258 if (!ArgExpr.isInvalid()) {
259 ExprTy* ExprList = ArgExpr.take();
Alexis Hunt96d5c762009-11-21 08:43:09 +0000260 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman06de2b52009-06-08 07:21:15 +0000261 SourceLocation(), &ExprList, 1,
262 CurrAttr, true);
263 }
264 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
265 SkipUntil(tok::r_paren, false);
266 } else {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000267 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
268 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedman06de2b52009-06-08 07:21:15 +0000269 }
270 }
271 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
272 SkipUntil(tok::r_paren, false);
Eli Friedman53339e02009-06-08 23:27:34 +0000273 return CurrAttr;
274}
275
276AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
277 // Treat these like attributes
278 // FIXME: Allow Sema to distinguish between these and real attributes!
279 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
280 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) ||
281 Tok.is(tok::kw___w64)) {
282 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
283 SourceLocation AttrNameLoc = ConsumeToken();
284 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
285 // FIXME: Support these properly!
286 continue;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000287 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman53339e02009-06-08 23:27:34 +0000288 SourceLocation(), 0, 0, CurrAttr, true);
289 }
290 return CurrAttr;
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000291}
292
Chris Lattner53361ac2006-08-10 05:19:57 +0000293/// ParseDeclaration - Parse a full 'declaration', which consists of
294/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +0000295/// 'Context' should be a Declarator::TheContext value. This returns the
296/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +0000297///
298/// declaration: [C99 6.7]
299/// block-declaration ->
300/// simple-declaration
301/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +0000302/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000303/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +0000304/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +0000305/// [C++] using-declaration
Sebastian Redlf769df52009-03-24 22:27:57 +0000306/// [C++0x] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +0000307/// others... [FIXME]
308///
Chris Lattner49836b42009-04-02 04:16:50 +0000309Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000310 SourceLocation &DeclEnd,
311 CXX0XAttributeList Attr) {
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000312 DeclPtrTy SingleDecl;
Chris Lattnera5235172007-08-25 06:57:03 +0000313 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +0000314 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +0000315 case tok::kw_export:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000316 if (Attr.HasAttr)
317 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
318 << Attr.Range;
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000319 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000320 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000321 case tok::kw_namespace:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000322 if (Attr.HasAttr)
323 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
324 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000325 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000326 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +0000327 case tok::kw_using:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000328 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000329 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +0000330 case tok::kw_static_assert:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000331 if (Attr.HasAttr)
332 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
333 << Attr.Range;
Chris Lattner49836b42009-04-02 04:16:50 +0000334 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000335 break;
Chris Lattnera5235172007-08-25 06:57:03 +0000336 default:
Alexis Hunt96d5c762009-11-21 08:43:09 +0000337 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList);
Chris Lattnera5235172007-08-25 06:57:03 +0000338 }
Alexis Hunt96d5c762009-11-21 08:43:09 +0000339
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000340 // This routine returns a DeclGroup, if the thing we parsed only contains a
341 // single decl, convert it now.
342 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattnera5235172007-08-25 06:57:03 +0000343}
344
345/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
346/// declaration-specifiers init-declarator-list[opt] ';'
347///[C90/C++]init-declarator-list ';' [TODO]
348/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +0000349///
350/// If RequireSemi is false, this does not check for a ';' at the end of the
351/// declaration.
352Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +0000353 SourceLocation &DeclEnd,
354 AttributeList *Attr) {
Chris Lattner53361ac2006-08-10 05:19:57 +0000355 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +0000356 ParsingDeclSpec DS(*this);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000357 if (Attr)
358 DS.AddAttributes(Attr);
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000359 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
360 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump11289f42009-09-09 15:08:12 +0000361
Chris Lattner0e894622006-08-13 19:58:17 +0000362 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
363 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000364 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000365 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000366 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall28a6aea2009-11-04 02:18:39 +0000367 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000368 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370
John McCalld5a36322009-11-03 19:26:08 +0000371 DeclGroupPtrTy DG = ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false,
372 &DeclEnd);
373 return DG;
374}
Mike Stump11289f42009-09-09 15:08:12 +0000375
John McCalld5a36322009-11-03 19:26:08 +0000376/// ParseDeclGroup - Having concluded that this is either a function
377/// definition or a group of object declarations, actually parse the
378/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000379Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
380 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000381 bool AllowFunctionDefinitions,
382 SourceLocation *DeclEnd) {
383 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000384 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000385 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000386
John McCalld5a36322009-11-03 19:26:08 +0000387 // Bail out if the first declarator didn't seem well-formed.
388 if (!D.hasName() && !D.mayOmitIdentifier()) {
389 // Skip until ; or }.
390 SkipUntil(tok::r_brace, true, true);
391 if (Tok.is(tok::semi))
392 ConsumeToken();
393 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000394 }
Mike Stump11289f42009-09-09 15:08:12 +0000395
John McCalld5a36322009-11-03 19:26:08 +0000396 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
397 if (isDeclarationAfterDeclarator()) {
398 // Fall though. We have to check this first, though, because
399 // __attribute__ might be the start of a function definition in
400 // (extended) K&R C.
401 } else if (isStartOfFunctionDefinition()) {
402 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
403 Diag(Tok, diag::err_function_declared_typedef);
404
405 // Recover by treating the 'typedef' as spurious.
406 DS.ClearStorageClassSpecs();
407 }
408
409 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
410 return Actions.ConvertDeclToDeclGroup(TheDecl);
411 } else {
412 Diag(Tok, diag::err_expected_fn_body);
413 SkipUntil(tok::semi);
414 return DeclGroupPtrTy();
415 }
416 }
417
418 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
419 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000420 D.complete(FirstDecl);
John McCalld5a36322009-11-03 19:26:08 +0000421 if (FirstDecl.get())
422 DeclsInGroup.push_back(FirstDecl);
423
424 // If we don't have a comma, it is either the end of the list (a ';') or an
425 // error, bail out.
426 while (Tok.is(tok::comma)) {
427 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000428 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000429
430 // Parse the next declarator.
431 D.clear();
432
433 // Accept attributes in an init-declarator. In the first declarator in a
434 // declaration, these would be part of the declspec. In subsequent
435 // declarators, they become part of the declarator itself, so that they
436 // don't apply to declarators after *this* one. Examples:
437 // short __attribute__((common)) var; -> declspec
438 // short var __attribute__((common)); -> declarator
439 // short x, __attribute__((common)) var; -> declarator
440 if (Tok.is(tok::kw___attribute)) {
441 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000442 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld5a36322009-11-03 19:26:08 +0000443 D.AddAttributes(AttrList, Loc);
444 }
445
446 ParseDeclarator(D);
447
448 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000449 D.complete(ThisDecl);
John McCalld5a36322009-11-03 19:26:08 +0000450 if (ThisDecl.get())
451 DeclsInGroup.push_back(ThisDecl);
452 }
453
454 if (DeclEnd)
455 *DeclEnd = Tok.getLocation();
456
457 if (Context != Declarator::ForContext &&
458 ExpectAndConsume(tok::semi,
459 Context == Declarator::FileContext
460 ? diag::err_invalid_token_after_toplevel_declarator
461 : diag::err_expected_semi_declaration)) {
462 SkipUntil(tok::r_brace, true, true);
463 if (Tok.is(tok::semi))
464 ConsumeToken();
465 }
466
467 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
468 DeclsInGroup.data(),
469 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000470}
471
Douglas Gregor23996282009-05-12 21:31:51 +0000472/// \brief Parse 'declaration' after parsing 'declaration-specifiers
473/// declarator'. This method parses the remainder of the declaration
474/// (including any attributes or initializer, among other things) and
475/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000476///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000477/// init-declarator: [C99 6.7]
478/// declarator
479/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000480/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
481/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000482/// [C++] declarator initializer[opt]
483///
484/// [C++] initializer:
485/// [C++] '=' initializer-clause
486/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000487/// [C++0x] '=' 'default' [TODO]
488/// [C++0x] '=' 'delete'
489///
490/// According to the standard grammar, =default and =delete are function
491/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000492///
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000493Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
494 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000495 // If a simple-asm-expr is present, parse it.
496 if (Tok.is(tok::kw_asm)) {
497 SourceLocation Loc;
498 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
499 if (AsmLabel.isInvalid()) {
500 SkipUntil(tok::semi, true, true);
501 return DeclPtrTy();
502 }
Mike Stump11289f42009-09-09 15:08:12 +0000503
Douglas Gregor23996282009-05-12 21:31:51 +0000504 D.setAsmLabel(AsmLabel.release());
505 D.SetRangeEnd(Loc);
506 }
Mike Stump11289f42009-09-09 15:08:12 +0000507
Douglas Gregor23996282009-05-12 21:31:51 +0000508 // If attributes are present, parse them.
509 if (Tok.is(tok::kw___attribute)) {
510 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000511 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor23996282009-05-12 21:31:51 +0000512 D.AddAttributes(AttrList, Loc);
513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Douglas Gregor23996282009-05-12 21:31:51 +0000515 // Inform the current actions module that we just parsed this declarator.
Douglas Gregor450f00842009-09-25 18:43:00 +0000516 DeclPtrTy ThisDecl;
517 switch (TemplateInfo.Kind) {
518 case ParsedTemplateInfo::NonTemplate:
519 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
520 break;
521
522 case ParsedTemplateInfo::Template:
523 case ParsedTemplateInfo::ExplicitSpecialization:
524 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000525 Action::MultiTemplateParamsArg(Actions,
526 TemplateInfo.TemplateParams->data(),
527 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000528 D);
529 break;
530
531 case ParsedTemplateInfo::ExplicitInstantiation: {
532 Action::DeclResult ThisRes
533 = Actions.ActOnExplicitInstantiation(CurScope,
534 TemplateInfo.ExternLoc,
535 TemplateInfo.TemplateLoc,
536 D);
537 if (ThisRes.isInvalid()) {
538 SkipUntil(tok::semi, true, true);
539 return DeclPtrTy();
540 }
541
542 ThisDecl = ThisRes.get();
543 break;
544 }
545 }
Mike Stump11289f42009-09-09 15:08:12 +0000546
Douglas Gregor23996282009-05-12 21:31:51 +0000547 // Parse declarator '=' initializer.
548 if (Tok.is(tok::equal)) {
549 ConsumeToken();
550 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
551 SourceLocation DelLoc = ConsumeToken();
552 Actions.SetDeclDeleted(ThisDecl, DelLoc);
553 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000554 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
555 EnterScope(0);
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000556 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000557 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000558
Douglas Gregor23996282009-05-12 21:31:51 +0000559 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000560
John McCall1f4ee7b2009-12-19 09:28:58 +0000561 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000562 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000563 ExitScope();
564 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000565
Douglas Gregor23996282009-05-12 21:31:51 +0000566 if (Init.isInvalid()) {
567 SkipUntil(tok::semi, true, true);
568 return DeclPtrTy();
569 }
Anders Carlsson250aada2009-08-16 05:13:48 +0000570 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor23996282009-05-12 21:31:51 +0000571 }
572 } else if (Tok.is(tok::l_paren)) {
573 // Parse C++ direct initializer: '(' expression-list ')'
574 SourceLocation LParenLoc = ConsumeParen();
575 ExprVector Exprs(Actions);
576 CommaLocsTy CommaLocs;
577
Douglas Gregor613bf102009-12-22 17:47:17 +0000578 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
579 EnterScope(0);
580 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
581 }
582
Douglas Gregor23996282009-05-12 21:31:51 +0000583 if (ParseExpressionList(Exprs, CommaLocs)) {
584 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +0000585
586 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
587 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
588 ExitScope();
589 }
Douglas Gregor23996282009-05-12 21:31:51 +0000590 } else {
591 // Match the ')'.
592 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
593
594 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
595 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +0000596
597 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
598 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
599 ExitScope();
600 }
601
Douglas Gregor23996282009-05-12 21:31:51 +0000602 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
603 move_arg(Exprs),
Jay Foad7d0479f2009-05-21 09:52:38 +0000604 CommaLocs.data(), RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000605 }
606 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000607 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000608 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
609 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000610 }
611
612 return ThisDecl;
613}
614
Chris Lattner1890ac82006-08-13 01:16:23 +0000615/// ParseSpecifierQualifierList
616/// specifier-qualifier-list:
617/// type-specifier specifier-qualifier-list[opt]
618/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000619/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000620///
621void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
622 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
623 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000624 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000625
Chris Lattner1890ac82006-08-13 01:16:23 +0000626 // Validate declspec for type-name.
627 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000628 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
629 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000630 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000631
Chris Lattner1b22eed2006-11-28 05:12:07 +0000632 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000633 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000634 if (DS.getStorageClassSpecLoc().isValid())
635 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
636 else
637 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000638 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000639 }
Mike Stump11289f42009-09-09 15:08:12 +0000640
Chris Lattner1b22eed2006-11-28 05:12:07 +0000641 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000642 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000643 if (DS.isInlineSpecified())
644 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
645 if (DS.isVirtualSpecified())
646 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
647 if (DS.isExplicitSpecified())
648 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000649 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000650 }
651}
Chris Lattner53361ac2006-08-10 05:19:57 +0000652
Chris Lattner6cc055a2009-04-12 20:42:31 +0000653/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
654/// specified token is valid after the identifier in a declarator which
655/// immediately follows the declspec. For example, these things are valid:
656///
657/// int x [ 4]; // direct-declarator
658/// int x ( int y); // direct-declarator
659/// int(int x ) // direct-declarator
660/// int x ; // simple-declaration
661/// int x = 17; // init-declarator-list
662/// int x , y; // init-declarator-list
663/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000664/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000665/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000666///
667/// This is not, because 'x' does not immediately follow the declspec (though
668/// ')' happens to be valid anyway).
669/// int (x)
670///
671static bool isValidAfterIdentifierInDeclarator(const Token &T) {
672 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
673 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000674 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000675}
676
Chris Lattner20a0c612009-04-14 21:34:55 +0000677
678/// ParseImplicitInt - This method is called when we have an non-typename
679/// identifier in a declspec (which normally terminates the decl spec) when
680/// the declspec has no type specifier. In this case, the declspec is either
681/// malformed or is "implicit int" (in K&R and C89).
682///
683/// This method handles diagnosing this prettily and returns false if the
684/// declspec is done being processed. If it recovers and thinks there may be
685/// other pieces of declspec after it, it returns true.
686///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000687bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000688 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000689 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000690 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000691
Chris Lattner20a0c612009-04-14 21:34:55 +0000692 SourceLocation Loc = Tok.getLocation();
693 // If we see an identifier that is not a type name, we normally would
694 // parse it as the identifer being declared. However, when a typename
695 // is typo'd or the definition is not included, this will incorrectly
696 // parse the typename as the identifier name and fall over misparsing
697 // later parts of the diagnostic.
698 //
699 // As such, we try to do some look-ahead in cases where this would
700 // otherwise be an "implicit-int" case to see if this is invalid. For
701 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
702 // an identifier with implicit int, we'd get a parse error because the
703 // next token is obviously invalid for a type. Parse these as a case
704 // with an invalid type specifier.
705 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000706
Chris Lattner20a0c612009-04-14 21:34:55 +0000707 // Since we know that this either implicit int (which is rare) or an
708 // error, we'd do lookahead to try to do better recovery.
709 if (isValidAfterIdentifierInDeclarator(NextToken())) {
710 // If this token is valid for implicit int, e.g. "static x = 4", then
711 // we just avoid eating the identifier, so it will be parsed as the
712 // identifier in the declarator.
713 return false;
714 }
Mike Stump11289f42009-09-09 15:08:12 +0000715
Chris Lattner20a0c612009-04-14 21:34:55 +0000716 // Otherwise, if we don't consume this token, we are going to emit an
717 // error anyway. Try to recover from various common problems. Check
718 // to see if this was a reference to a tag name without a tag specified.
719 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000720 //
721 // C++ doesn't need this, and isTagName doesn't take SS.
722 if (SS == 0) {
723 const char *TagName = 0;
724 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000725
Chris Lattner20a0c612009-04-14 21:34:55 +0000726 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
727 default: break;
728 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
729 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
730 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
731 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
732 }
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000734 if (TagName) {
735 Diag(Loc, diag::err_use_of_tag_name_without_tag)
736 << Tok.getIdentifierInfo() << TagName
737 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000738
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000739 // Parse this as a tag as if the missing tag were present.
740 if (TagKind == tok::kw_enum)
741 ParseEnumSpecifier(Loc, DS, AS);
742 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000743 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000744 return true;
745 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000746 }
Mike Stump11289f42009-09-09 15:08:12 +0000747
Douglas Gregor15e56022009-10-13 23:27:22 +0000748 // This is almost certainly an invalid type name. Let the action emit a
749 // diagnostic and attempt to recover.
750 Action::TypeTy *T = 0;
751 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
752 CurScope, SS, T)) {
753 // The action emitted a diagnostic, so we don't have to.
754 if (T) {
755 // The action has suggested that the type T could be used. Set that as
756 // the type in the declaration specifiers, consume the would-be type
757 // name token, and we're done.
758 const char *PrevSpec;
759 unsigned DiagID;
760 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
761 false);
762 DS.SetRangeEnd(Tok.getLocation());
763 ConsumeToken();
764
765 // There may be other declaration specifiers after this.
766 return true;
767 }
768
769 // Fall through; the action had no suggestion for us.
770 } else {
771 // The action did not emit a diagnostic, so emit one now.
772 SourceRange R;
773 if (SS) R = SS->getRange();
774 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
Douglas Gregor15e56022009-10-13 23:27:22 +0000777 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000778 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000779 unsigned DiagID;
780 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000781 DS.SetRangeEnd(Tok.getLocation());
782 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000783
Chris Lattner20a0c612009-04-14 21:34:55 +0000784 // TODO: Could inject an invalid typedef decl in an enclosing scope to
785 // avoid rippling error messages on subsequent uses of the same type,
786 // could be useful if #include was forgotten.
787 return false;
788}
789
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000790/// \brief Determine the declaration specifier context from the declarator
791/// context.
792///
793/// \param Context the declarator context, which is one of the
794/// Declarator::TheContext enumerator values.
795Parser::DeclSpecContext
796Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
797 if (Context == Declarator::MemberContext)
798 return DSC_class;
799 if (Context == Declarator::FileContext)
800 return DSC_top_level;
801 return DSC_normal;
802}
803
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000804/// ParseDeclarationSpecifiers
805/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000806/// storage-class-specifier declaration-specifiers[opt]
807/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000808/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000809/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000810///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000811/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000812/// 'typedef'
813/// 'extern'
814/// 'static'
815/// 'auto'
816/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000817/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000818/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000819/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000820/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000821/// [C++] 'virtual'
822/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000823/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000824/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000825
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000826///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000827void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000828 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000829 AccessSpecifier AS,
830 DeclSpecContext DSContext) {
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000831 if (Tok.is(tok::code_completion)) {
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000832 Action::CodeCompletionContext CCC = Action::CCC_Namespace;
833 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
834 CCC = DSContext == DSC_class? Action::CCC_MemberTemplate
835 : Action::CCC_Template;
836 else if (DSContext == DSC_class)
837 CCC = Action::CCC_Class;
838
839 Actions.CodeCompleteOrdinaryName(CurScope, CCC);
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000840 ConsumeToken();
841 }
842
Chris Lattner2e232092008-03-13 06:29:04 +0000843 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000844 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000845 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000846 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000847 unsigned DiagID = 0;
848
Chris Lattner4d8f8732006-11-28 05:05:08 +0000849 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000850
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000851 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000852 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000853 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000854 // If this is not a declaration specifier token, we're done reading decl
855 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000856 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000857 return;
Mike Stump11289f42009-09-09 15:08:12 +0000858
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000859 case tok::coloncolon: // ::foo::bar
860 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregore861bac2009-08-25 22:51:20 +0000861 if (TryAnnotateCXXScopeToken(true))
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000862 continue;
863 goto DoneWithDeclSpec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000864
865 case tok::annot_cxxscope: {
866 if (DS.hasTypeSpecifier())
867 goto DoneWithDeclSpec;
868
John McCall9dab4e62009-12-12 11:40:51 +0000869 CXXScopeSpec SS;
870 SS.setScopeRep(Tok.getAnnotationValue());
871 SS.setRange(Tok.getAnnotationRange());
872
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000873 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000874 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000875 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000876 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000877 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000878 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000879
880 // C++ [class.qual]p2:
881 // In a lookup in which the constructor is an acceptable lookup
882 // result and the nested-name-specifier nominates a class C:
883 //
884 // - if the name specified after the
885 // nested-name-specifier, when looked up in C, is the
886 // injected-class-name of C (Clause 9), or
887 //
888 // - if the name specified after the nested-name-specifier
889 // is the same as the identifier or the
890 // simple-template-id's template-name in the last
891 // component of the nested-name-specifier,
892 //
893 // the name is instead considered to name the constructor of
894 // class C.
895 //
896 // Thus, if the template-name is actually the constructor
897 // name, then the code is ill-formed; this interpretation is
898 // reinforced by the NAD status of core issue 635.
899 TemplateIdAnnotation *TemplateId
900 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
901 if (DSContext == DSC_top_level && TemplateId->Name &&
902 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
903 if (isConstructorDeclarator()) {
904 // The user meant this to be an out-of-line constructor
905 // definition, but template arguments are not allowed
906 // there. Just allow this as a constructor; we'll
907 // complain about it later.
908 goto DoneWithDeclSpec;
909 }
910
911 // The user meant this to name a type, but it actually names
912 // a constructor with some extraneous template
913 // arguments. Complain, then parse it as a type as the user
914 // intended.
915 Diag(TemplateId->TemplateNameLoc,
916 diag::err_out_of_line_template_id_names_constructor)
917 << TemplateId->Name;
918 }
919
John McCall9dab4e62009-12-12 11:40:51 +0000920 DS.getTypeSpecScope() = SS;
921 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +0000922 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000923 "ParseOptionalCXXScopeSpecifier not working");
924 AnnotateTemplateIdTokenAsType(&SS);
925 continue;
926 }
927
Douglas Gregorc5790df2009-09-28 07:26:33 +0000928 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +0000929 DS.getTypeSpecScope() = SS;
930 ConsumeToken(); // The C++ scope.
Douglas Gregorc5790df2009-09-28 07:26:33 +0000931 if (Tok.getAnnotationValue())
932 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
933 PrevSpec, DiagID,
934 Tok.getAnnotationValue());
935 else
936 DS.SetTypeSpecError();
937 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
938 ConsumeToken(); // The typename
939 }
940
Douglas Gregor167fa622009-03-25 15:40:00 +0000941 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000942 goto DoneWithDeclSpec;
943
Douglas Gregor9de54ea2010-01-13 17:31:36 +0000944 // If we're in a context where the identifier could be a class name,
945 // check whether this is a constructor declaration.
946 if (DSContext == DSC_top_level &&
947 Actions.isCurrentClassName(*Next.getIdentifierInfo(), CurScope,
948 &SS)) {
949 if (isConstructorDeclarator())
950 goto DoneWithDeclSpec;
951
952 // As noted in C++ [class.qual]p2 (cited above), when the name
953 // of the class is qualified in a context where it could name
954 // a constructor, its a constructor name. However, we've
955 // looked at the declarator, and the user probably meant this
956 // to be a type. Complain that it isn't supposed to be treated
957 // as a type, then proceed to parse it as a type.
958 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
959 << Next.getIdentifierInfo();
960 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000961
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000962 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
963 Next.getLocation(), CurScope, &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000964
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000965 // If the referenced identifier is not a type, then this declspec is
966 // erroneous: We already checked about that it has no type specifier, and
967 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +0000968 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000969 if (TypeRep == 0) {
970 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000971 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000972 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000973 }
Mike Stump11289f42009-09-09 15:08:12 +0000974
John McCall9dab4e62009-12-12 11:40:51 +0000975 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000976 ConsumeToken(); // The C++ scope.
977
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000978 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000979 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000980 if (isInvalid)
981 break;
Mike Stump11289f42009-09-09 15:08:12 +0000982
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000983 DS.SetRangeEnd(Tok.getLocation());
984 ConsumeToken(); // The typename.
985
986 continue;
987 }
Mike Stump11289f42009-09-09 15:08:12 +0000988
Chris Lattnere387d9e2009-01-21 19:48:37 +0000989 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000990 if (Tok.getAnnotationValue())
991 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000992 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000993 else
994 DS.SetTypeSpecError();
Chris Lattnere387d9e2009-01-21 19:48:37 +0000995 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
996 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +0000997
Chris Lattnere387d9e2009-01-21 19:48:37 +0000998 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
999 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1000 // Objective-C interface. If we don't have Objective-C or a '<', this is
1001 // just a normal reference to a typedef name.
1002 if (!Tok.is(tok::less) || !getLang().ObjC1)
1003 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001004
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001005 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001006 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001007 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1008 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1009 LAngleLoc, EndProtoLoc);
1010 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1011 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001012
Chris Lattnere387d9e2009-01-21 19:48:37 +00001013 DS.SetRangeEnd(EndProtoLoc);
1014 continue;
1015 }
Mike Stump11289f42009-09-09 15:08:12 +00001016
Chris Lattner16fac4f2008-07-26 01:18:38 +00001017 // typedef-name
1018 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00001019 // In C++, check to see if this is a scope specifier like foo::bar::, if
1020 // so handle it as such. This is important for ctor parsing.
Douglas Gregore861bac2009-08-25 22:51:20 +00001021 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001022 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001023
Chris Lattner16fac4f2008-07-26 01:18:38 +00001024 // This identifier can only be a typedef name if we haven't already seen
1025 // a type-specifier. Without this check we misparse:
1026 // typedef int X; struct Y { short X; }; as 'short int'.
1027 if (DS.hasTypeSpecifier())
1028 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001029
Chris Lattner16fac4f2008-07-26 01:18:38 +00001030 // It has to be available as a typedef too!
Mike Stump11289f42009-09-09 15:08:12 +00001031 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00001032 Tok.getLocation(), CurScope);
Douglas Gregor8bf42052009-02-09 18:46:07 +00001033
Chris Lattner6cc055a2009-04-12 20:42:31 +00001034 // If this is not a typedef name, don't parse it as part of the declspec,
1035 // it must be an implicit int or an error.
1036 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001037 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001038 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00001039 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00001040
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001041 // If we're in a context where the identifier could be a class name,
1042 // check whether this is a constructor declaration.
1043 if (getLang().CPlusPlus && DSContext == DSC_class &&
Mike Stump11289f42009-09-09 15:08:12 +00001044 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001045 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00001046 goto DoneWithDeclSpec;
1047
Douglas Gregor9817f4a2009-02-09 15:09:02 +00001048 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001049 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001050 if (isInvalid)
1051 break;
Mike Stump11289f42009-09-09 15:08:12 +00001052
Chris Lattner16fac4f2008-07-26 01:18:38 +00001053 DS.SetRangeEnd(Tok.getLocation());
1054 ConsumeToken(); // The identifier
1055
1056 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1057 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1058 // Objective-C interface. If we don't have Objective-C or a '<', this is
1059 // just a normal reference to a typedef name.
1060 if (!Tok.is(tok::less) || !getLang().ObjC1)
1061 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001062
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001063 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001064 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001065 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1066 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1067 LAngleLoc, EndProtoLoc);
1068 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1069 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001070
Chris Lattner16fac4f2008-07-26 01:18:38 +00001071 DS.SetRangeEnd(EndProtoLoc);
1072
Steve Naroffcd5e7822008-09-22 10:28:57 +00001073 // Need to support trailing type qualifiers (e.g. "id<p> const").
1074 // If a type specifier follows, it will be diagnosed elsewhere.
1075 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00001076 }
Douglas Gregor7f741122009-02-25 19:37:18 +00001077
1078 // type-name
1079 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +00001080 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +00001081 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +00001082 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00001083 // This template-id does not refer to a type name, so we're
1084 // done with the type-specifiers.
1085 goto DoneWithDeclSpec;
1086 }
1087
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001088 // If we're in a context where the template-id could be a
1089 // constructor name or specialization, check whether this is a
1090 // constructor declaration.
1091 if (getLang().CPlusPlus && DSContext == DSC_class &&
1092 Actions.isCurrentClassName(*TemplateId->Name, CurScope) &&
1093 isConstructorDeclarator())
1094 goto DoneWithDeclSpec;
1095
Douglas Gregor7f741122009-02-25 19:37:18 +00001096 // Turn the template-id annotation token into a type annotation
1097 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001098 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001099 continue;
1100 }
1101
Chris Lattnere37e2332006-08-15 04:50:22 +00001102 // GNU attributes support.
1103 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001104 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001105 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001106
1107 // Microsoft declspec support.
1108 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001109 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001110 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001111
Steve Naroff44ac7772008-12-25 14:16:32 +00001112 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001113 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001114 // FIXME: Add handling here!
1115 break;
1116
1117 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001118 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001119 case tok::kw___cdecl:
1120 case tok::kw___stdcall:
1121 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001122 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1123 continue;
1124
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001125 // storage-class-specifier
1126 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001127 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1128 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001129 break;
1130 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001131 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001132 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001133 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1134 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001135 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001136 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001137 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001138 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001139 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001140 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001141 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001142 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001143 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1144 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001145 break;
1146 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001147 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001148 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1149 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001150 else
John McCall49bfce42009-08-03 20:12:06 +00001151 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1152 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001153 break;
1154 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001155 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1156 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001157 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001158 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001159 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1160 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001161 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001162 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001163 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001164 break;
Mike Stump11289f42009-09-09 15:08:12 +00001165
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001166 // function-specifier
1167 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001168 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001169 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001170 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001171 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001172 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001173 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001174 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001175 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001176
Anders Carlssoncd8db412009-05-06 04:46:28 +00001177 // friend
1178 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001179 if (DSContext == DSC_class)
1180 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1181 else {
1182 PrevSpec = ""; // not actually used by the diagnostic
1183 DiagID = diag::err_friend_invalid_in_context;
1184 isInvalid = true;
1185 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001186 break;
Mike Stump11289f42009-09-09 15:08:12 +00001187
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001188 // constexpr
1189 case tok::kw_constexpr:
1190 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1191 break;
1192
Chris Lattnere387d9e2009-01-21 19:48:37 +00001193 // type-specifier
1194 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001195 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1196 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001197 break;
1198 case tok::kw_long:
1199 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001200 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1201 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001202 else
John McCall49bfce42009-08-03 20:12:06 +00001203 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1204 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001205 break;
1206 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001207 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1208 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001209 break;
1210 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001211 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1212 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001213 break;
1214 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001215 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1216 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001217 break;
1218 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001219 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1220 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001221 break;
1222 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001223 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1224 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001225 break;
1226 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001227 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1228 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001229 break;
1230 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001231 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1232 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001233 break;
1234 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001235 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1236 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001237 break;
1238 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001239 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1240 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001241 break;
1242 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001243 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1244 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001245 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001246 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001247 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1248 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001249 break;
1250 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001251 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1252 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001253 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001254 case tok::kw_bool:
1255 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001256 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1257 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001258 break;
1259 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001260 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1261 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001262 break;
1263 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001264 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1265 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001266 break;
1267 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001268 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1269 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001270 break;
1271
1272 // class-specifier:
1273 case tok::kw_class:
1274 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001275 case tok::kw_union: {
1276 tok::TokenKind Kind = Tok.getKind();
1277 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001278 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001279 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001280 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001281
1282 // enum-specifier:
1283 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001284 ConsumeToken();
1285 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001286 continue;
1287
1288 // cv-qualifier:
1289 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001290 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1291 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001292 break;
1293 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001294 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1295 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001296 break;
1297 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001298 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1299 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001300 break;
1301
Douglas Gregor333489b2009-03-27 23:10:48 +00001302 // C++ typename-specifier:
1303 case tok::kw_typename:
1304 if (TryAnnotateTypeOrScopeToken())
1305 continue;
1306 break;
1307
Chris Lattnere387d9e2009-01-21 19:48:37 +00001308 // GNU typeof support.
1309 case tok::kw_typeof:
1310 ParseTypeofSpecifier(DS);
1311 continue;
1312
Anders Carlsson74948d02009-06-24 17:47:40 +00001313 case tok::kw_decltype:
1314 ParseDecltypeSpecifier(DS);
1315 continue;
1316
Steve Naroffcfdf6162008-06-05 00:02:44 +00001317 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001318 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001319 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1320 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001321 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001322 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001323
Chris Lattner0974b232008-07-26 00:20:22 +00001324 {
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001325 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001326 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001327 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1328 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1329 LAngleLoc, EndProtoLoc);
1330 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1331 ProtocolLocs.data(), LAngleLoc);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001332 DS.SetRangeEnd(EndProtoLoc);
1333
Chris Lattner6d29c102008-11-18 07:48:38 +00001334 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner3a4e4312009-04-03 18:38:42 +00001335 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001336 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001337 // Need to support trailing type qualifiers (e.g. "id<p> const").
1338 // If a type specifier follows, it will be diagnosed elsewhere.
1339 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001340 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001341 }
John McCall49bfce42009-08-03 20:12:06 +00001342 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001343 if (isInvalid) {
1344 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001345 assert(DiagID);
Chris Lattner6d29c102008-11-18 07:48:38 +00001346 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001347 }
Chris Lattner2e232092008-03-13 06:29:04 +00001348 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001349 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001350 }
1351}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001352
Chris Lattnera448d752009-01-06 06:59:53 +00001353/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001354/// primarily follow the C++ grammar with additions for C99 and GNU,
1355/// which together subsume the C grammar. Note that the C++
1356/// type-specifier also includes the C type-qualifier (for const,
1357/// volatile, and C99 restrict). Returns true if a type-specifier was
1358/// found (and parsed), false otherwise.
1359///
1360/// type-specifier: [C++ 7.1.5]
1361/// simple-type-specifier
1362/// class-specifier
1363/// enum-specifier
1364/// elaborated-type-specifier [TODO]
1365/// cv-qualifier
1366///
1367/// cv-qualifier: [C++ 7.1.5.1]
1368/// 'const'
1369/// 'volatile'
1370/// [C99] 'restrict'
1371///
1372/// simple-type-specifier: [ C++ 7.1.5.2]
1373/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1374/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1375/// 'char'
1376/// 'wchar_t'
1377/// 'bool'
1378/// 'short'
1379/// 'int'
1380/// 'long'
1381/// 'signed'
1382/// 'unsigned'
1383/// 'float'
1384/// 'double'
1385/// 'void'
1386/// [C99] '_Bool'
1387/// [C99] '_Complex'
1388/// [C99] '_Imaginary' // Removed in TC2?
1389/// [GNU] '_Decimal32'
1390/// [GNU] '_Decimal64'
1391/// [GNU] '_Decimal128'
1392/// [GNU] typeof-specifier
1393/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1394/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001395/// [C++0x] 'decltype' ( expression )
John McCall49bfce42009-08-03 20:12:06 +00001396bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001397 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001398 unsigned &DiagID,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001399 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001400 SourceLocation Loc = Tok.getLocation();
1401
1402 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001403 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001404 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001405 // Annotate typenames and C++ scope specifiers. If we get one, just
1406 // recurse to handle whatever we get.
1407 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001408 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1409 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001410 // Otherwise, not a type specifier.
1411 return false;
1412 case tok::coloncolon: // ::foo::bar
1413 if (NextToken().is(tok::kw_new) || // ::new
1414 NextToken().is(tok::kw_delete)) // ::delete
1415 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001416
Chris Lattner020bab92009-01-04 23:41:41 +00001417 // Annotate typenames and C++ scope specifiers. If we get one, just
1418 // recurse to handle whatever we get.
1419 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001420 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1421 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001422 // Otherwise, not a type specifier.
1423 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001424
Douglas Gregor450c75a2008-11-07 15:42:26 +00001425 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001426 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001427 if (Tok.getAnnotationValue())
1428 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001429 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001430 else
1431 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001432 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1433 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001434
Douglas Gregor450c75a2008-11-07 15:42:26 +00001435 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1436 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1437 // Objective-C interface. If we don't have Objective-C or a '<', this is
1438 // just a normal reference to a typedef name.
1439 if (!Tok.is(tok::less) || !getLang().ObjC1)
1440 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001441
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001442 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001443 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001444 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1445 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1446 LAngleLoc, EndProtoLoc);
1447 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1448 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001449
Douglas Gregor450c75a2008-11-07 15:42:26 +00001450 DS.SetRangeEnd(EndProtoLoc);
1451 return true;
1452 }
1453
1454 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001455 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001456 break;
1457 case tok::kw_long:
1458 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001459 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1460 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001461 else
John McCall49bfce42009-08-03 20:12:06 +00001462 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1463 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001464 break;
1465 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001466 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001467 break;
1468 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001469 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1470 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001471 break;
1472 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001473 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1474 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001475 break;
1476 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001477 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1478 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001479 break;
1480 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001481 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001482 break;
1483 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001484 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001485 break;
1486 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001487 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001488 break;
1489 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001490 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001491 break;
1492 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001493 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001494 break;
1495 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001496 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001497 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001498 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001499 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001500 break;
1501 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001502 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001503 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001504 case tok::kw_bool:
1505 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001506 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001507 break;
1508 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001509 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1510 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001511 break;
1512 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001513 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1514 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001515 break;
1516 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001517 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1518 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001519 break;
1520
1521 // class-specifier:
1522 case tok::kw_class:
1523 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001524 case tok::kw_union: {
1525 tok::TokenKind Kind = Tok.getKind();
1526 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001527 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001528 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001529 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001530
1531 // enum-specifier:
1532 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001533 ConsumeToken();
1534 ParseEnumSpecifier(Loc, DS);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001535 return true;
1536
1537 // cv-qualifier:
1538 case tok::kw_const:
1539 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001540 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001541 break;
1542 case tok::kw_volatile:
1543 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001544 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001545 break;
1546 case tok::kw_restrict:
1547 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001548 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001549 break;
1550
1551 // GNU typeof support.
1552 case tok::kw_typeof:
1553 ParseTypeofSpecifier(DS);
1554 return true;
1555
Anders Carlsson74948d02009-06-24 17:47:40 +00001556 // C++0x decltype support.
1557 case tok::kw_decltype:
1558 ParseDecltypeSpecifier(DS);
1559 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001560
Anders Carlssonbae27372009-06-26 23:44:14 +00001561 // C++0x auto support.
1562 case tok::kw_auto:
1563 if (!getLang().CPlusPlus0x)
1564 return false;
1565
John McCall49bfce42009-08-03 20:12:06 +00001566 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001567 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001568 case tok::kw___ptr64:
1569 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001570 case tok::kw___cdecl:
1571 case tok::kw___stdcall:
1572 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001573 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001574 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001575
Douglas Gregor450c75a2008-11-07 15:42:26 +00001576 default:
1577 // Not a type-specifier; do nothing.
1578 return false;
1579 }
1580
1581 // If the specifier combination wasn't legal, issue a diagnostic.
1582 if (isInvalid) {
1583 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001584 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001585 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001586 }
1587 DS.SetRangeEnd(Tok.getLocation());
1588 ConsumeToken(); // whatever we parsed above.
1589 return true;
1590}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001591
Chris Lattner70ae4912007-10-29 04:42:53 +00001592/// ParseStructDeclaration - Parse a struct declaration without the terminating
1593/// semicolon.
1594///
Chris Lattner90a26b02007-01-23 04:38:16 +00001595/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001596/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001597/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001598/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001599/// struct-declarator-list:
1600/// struct-declarator
1601/// struct-declarator-list ',' struct-declarator
1602/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1603/// struct-declarator:
1604/// declarator
1605/// [GNU] declarator attributes[opt]
1606/// declarator[opt] ':' constant-expression
1607/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1608///
Chris Lattnera12405b2008-04-10 06:46:29 +00001609void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001610ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001611 if (Tok.is(tok::kw___extension__)) {
1612 // __extension__ silences extension warnings in the subexpression.
1613 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001614 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001615 return ParseStructDeclaration(DS, Fields);
1616 }
Mike Stump11289f42009-09-09 15:08:12 +00001617
Steve Naroff97170802007-08-20 22:28:22 +00001618 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001619 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001620 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001621
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001622 // If there are no declarators, this is a free-standing declaration
1623 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001624 if (Tok.is(tok::semi)) {
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001625 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001626 return;
1627 }
1628
1629 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001630 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001631 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001632 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001633 FieldDeclarator DeclaratorInfo(DS);
1634
1635 // Attributes are only allowed here on successive declarators.
1636 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1637 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001638 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001639 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1640 }
Mike Stump11289f42009-09-09 15:08:12 +00001641
Steve Naroff97170802007-08-20 22:28:22 +00001642 /// struct-declarator: declarator
1643 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001644 if (Tok.isNot(tok::colon)) {
1645 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1646 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001647 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001648 }
Mike Stump11289f42009-09-09 15:08:12 +00001649
Chris Lattner76c72282007-10-09 17:33:22 +00001650 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001651 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001652 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001653 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001654 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001655 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001656 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001657 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001658
Steve Naroff97170802007-08-20 22:28:22 +00001659 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001660 if (Tok.is(tok::kw___attribute)) {
1661 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001662 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001663 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1664 }
1665
John McCallcfefb6d2009-11-03 02:38:08 +00001666 // We're done with this declarator; invoke the callback.
John McCall28a6aea2009-11-04 02:18:39 +00001667 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1668 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001669
Steve Naroff97170802007-08-20 22:28:22 +00001670 // If we don't have a comma, it is either the end of the list (a ';')
1671 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001672 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001673 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001674
Steve Naroff97170802007-08-20 22:28:22 +00001675 // Consume the comma.
1676 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001677
John McCallcfefb6d2009-11-03 02:38:08 +00001678 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001679 }
Steve Naroff97170802007-08-20 22:28:22 +00001680}
1681
1682/// ParseStructUnionBody
1683/// struct-contents:
1684/// struct-declaration-list
1685/// [EXT] empty
1686/// [GNU] "struct-declaration-list" without terminatoring ';'
1687/// struct-declaration-list:
1688/// struct-declaration
1689/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001690/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001691///
Chris Lattner1300fb92007-01-23 23:42:53 +00001692void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001693 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001694 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1695 PP.getSourceManager(),
1696 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001697
Chris Lattner90a26b02007-01-23 04:38:16 +00001698 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001699
Douglas Gregor658b9552009-01-09 22:42:13 +00001700 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001701 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1702
Chris Lattner7b9ace62007-01-23 20:11:08 +00001703 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1704 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001705 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001706 Diag(Tok, diag::ext_empty_struct_union_enum)
1707 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001708
Chris Lattner83f095c2009-03-28 19:18:32 +00001709 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001710
Chris Lattner7b9ace62007-01-23 20:11:08 +00001711 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001712 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001713 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001714
Chris Lattner736ed5d2007-06-09 05:59:07 +00001715 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001716 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001717 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001718 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001719 ConsumeToken();
1720 continue;
1721 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001722
1723 // Parse all the comma separated declarators.
1724 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001725
John McCallcfefb6d2009-11-03 02:38:08 +00001726 if (!Tok.is(tok::at)) {
1727 struct CFieldCallback : FieldCallback {
1728 Parser &P;
1729 DeclPtrTy TagDecl;
1730 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1731
1732 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1733 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1734 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1735
1736 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001737 // Install the declarator into the current TagDecl.
John McCall5e6253b2009-11-03 21:13:47 +00001738 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1739 FD.D.getDeclSpec().getSourceRange().getBegin(),
1740 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001741 FieldDecls.push_back(Field);
1742 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001743 }
John McCallcfefb6d2009-11-03 02:38:08 +00001744 } Callback(*this, TagDecl, FieldDecls);
1745
1746 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001747 } else { // Handle @defs
1748 ConsumeToken();
1749 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1750 Diag(Tok, diag::err_unexpected_at);
1751 SkipUntil(tok::semi, true, true);
1752 continue;
1753 }
1754 ConsumeToken();
1755 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1756 if (!Tok.is(tok::identifier)) {
1757 Diag(Tok, diag::err_expected_ident);
1758 SkipUntil(tok::semi, true, true);
1759 continue;
1760 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001761 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump11289f42009-09-09 15:08:12 +00001762 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001763 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001764 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1765 ConsumeToken();
1766 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001767 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001768
Chris Lattner76c72282007-10-09 17:33:22 +00001769 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001770 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001771 } else if (Tok.is(tok::r_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001772 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001773 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001774 } else {
1775 Diag(Tok, diag::err_expected_semi_decl_list);
1776 // Skip to end of block or statement
1777 SkipUntil(tok::r_brace, true, true);
1778 }
1779 }
Mike Stump11289f42009-09-09 15:08:12 +00001780
Steve Naroff33a1e802007-10-29 21:38:07 +00001781 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001782
Steve Naroffb8371e12007-06-09 03:39:29 +00001783 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +00001784 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001785 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +00001786 AttrList = ParseGNUAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +00001787
1788 Actions.ActOnFields(CurScope,
Jay Foad7d0479f2009-05-21 09:52:38 +00001789 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001790 LBraceLoc, RBraceLoc,
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001791 AttrList);
1792 StructScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001793 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001794}
1795
1796
Chris Lattner3b561a32006-08-13 00:12:11 +00001797/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001798/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001799/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001800///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001801/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1802/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001803/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001804/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001805///
1806/// [C++] elaborated-type-specifier:
1807/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1808///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001809void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1810 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001811 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001812 if (Tok.is(tok::code_completion)) {
1813 // Code completion for an enum name.
1814 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1815 ConsumeToken();
1816 }
1817
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001818 AttributeList *Attr = 0;
1819 // If attributes exist after tag, parse them.
1820 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +00001821 Attr = ParseGNUAttributes();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001822
1823 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001824 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001825 if (Tok.isNot(tok::identifier)) {
1826 Diag(Tok, diag::err_expected_ident);
1827 if (Tok.isNot(tok::l_brace)) {
1828 // Has no name and is not a definition.
1829 // Skip the rest of this declarator, up until the comma or semicolon.
1830 SkipUntil(tok::comma, true);
1831 return;
1832 }
1833 }
1834 }
Mike Stump11289f42009-09-09 15:08:12 +00001835
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001836 // Must have either 'enum name' or 'enum {...}'.
1837 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1838 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001839
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001840 // Skip the rest of this declarator, up until the comma or semicolon.
1841 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001842 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001843 }
Mike Stump11289f42009-09-09 15:08:12 +00001844
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001845 // If an identifier is present, consume and remember it.
1846 IdentifierInfo *Name = 0;
1847 SourceLocation NameLoc;
1848 if (Tok.is(tok::identifier)) {
1849 Name = Tok.getIdentifierInfo();
1850 NameLoc = ConsumeToken();
1851 }
Mike Stump11289f42009-09-09 15:08:12 +00001852
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001853 // There are three options here. If we have 'enum foo;', then this is a
1854 // forward declaration. If we have 'enum foo {...' then this is a
1855 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1856 //
1857 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1858 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1859 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1860 //
John McCall9bb74a52009-07-31 02:45:11 +00001861 Action::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001862 if (Tok.is(tok::l_brace))
John McCall9bb74a52009-07-31 02:45:11 +00001863 TUK = Action::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001864 else if (Tok.is(tok::semi))
John McCall9bb74a52009-07-31 02:45:11 +00001865 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001866 else
John McCall9bb74a52009-07-31 02:45:11 +00001867 TUK = Action::TUK_Reference;
Douglas Gregord6ab8742009-05-28 23:31:59 +00001868 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001869 bool IsDependent = false;
John McCall9bb74a52009-07-31 02:45:11 +00001870 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001871 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregor27bdf00f2009-07-23 16:36:45 +00001872 Action::MultiTemplateParamsArg(Actions),
John McCall7f41d982009-09-11 04:59:25 +00001873 Owned, IsDependent);
1874 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump11289f42009-09-09 15:08:12 +00001875
Chris Lattner76c72282007-10-09 17:33:22 +00001876 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001877 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001878
Chris Lattner3b561a32006-08-13 00:12:11 +00001879 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +00001880 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001881 unsigned DiagID;
1882 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001883 TagDecl.getAs<void>(), Owned))
John McCall49bfce42009-08-03 20:12:06 +00001884 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00001885}
1886
Chris Lattnerc1915e22007-01-25 07:29:02 +00001887/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1888/// enumerator-list:
1889/// enumerator
1890/// enumerator-list ',' enumerator
1891/// enumerator:
1892/// enumeration-constant
1893/// enumeration-constant '=' constant-expression
1894/// enumeration-constant:
1895/// identifier
1896///
Chris Lattner83f095c2009-03-28 19:18:32 +00001897void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001898 // Enter the scope of the enum body and start the definition.
1899 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001900 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00001901
Chris Lattnerc1915e22007-01-25 07:29:02 +00001902 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001903
Chris Lattner37256fb2007-08-27 17:24:30 +00001904 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00001905 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001906 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump11289f42009-09-09 15:08:12 +00001907
Chris Lattner83f095c2009-03-28 19:18:32 +00001908 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001909
Chris Lattner83f095c2009-03-28 19:18:32 +00001910 DeclPtrTy LastEnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001911
Chris Lattnerc1915e22007-01-25 07:29:02 +00001912 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001913 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001914 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1915 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001916
Chris Lattnerc1915e22007-01-25 07:29:02 +00001917 SourceLocation EqualLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001918 OwningExprResult AssignedVal(Actions);
Chris Lattner76c72282007-10-09 17:33:22 +00001919 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001920 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001921 AssignedVal = ParseConstantExpression();
1922 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00001923 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001924 }
Mike Stump11289f42009-09-09 15:08:12 +00001925
Chris Lattnerc1915e22007-01-25 07:29:02 +00001926 // Install the enumerator constant into EnumDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00001927 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1928 LastEnumConstDecl,
1929 IdentLoc, Ident,
1930 EqualLoc,
1931 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00001932 EnumConstantDecls.push_back(EnumConstDecl);
1933 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001934
Chris Lattner76c72282007-10-09 17:33:22 +00001935 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001936 break;
1937 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001938
1939 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00001940 !(getLang().C99 || getLang().CPlusPlus0x))
1941 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1942 << getLang().CPlusPlus
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001943 << CodeModificationHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001944 }
Mike Stump11289f42009-09-09 15:08:12 +00001945
Chris Lattnerc1915e22007-01-25 07:29:02 +00001946 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00001947 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001948
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001949 AttributeList *Attr = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001950 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001951 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +00001952 Attr = ParseGNUAttributes(); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001953
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001954 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1955 EnumConstantDecls.data(), EnumConstantDecls.size(),
1956 CurScope, Attr);
Mike Stump11289f42009-09-09 15:08:12 +00001957
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001958 EnumScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001959 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001960}
Chris Lattner3b561a32006-08-13 00:12:11 +00001961
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001962/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001963/// start of a type-qualifier-list.
1964bool Parser::isTypeQualifier() const {
1965 switch (Tok.getKind()) {
1966 default: return false;
1967 // type-qualifier
1968 case tok::kw_const:
1969 case tok::kw_volatile:
1970 case tok::kw_restrict:
1971 return true;
1972 }
1973}
1974
1975/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001976/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001977bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001978 switch (Tok.getKind()) {
1979 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001980
Chris Lattner020bab92009-01-04 23:41:41 +00001981 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001982 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001983 // Annotate typenames and C++ scope specifiers. If we get one, just
1984 // recurse to handle whatever we get.
1985 if (TryAnnotateTypeOrScopeToken())
1986 return isTypeSpecifierQualifier();
1987 // Otherwise, not a type specifier.
1988 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001989
Chris Lattner020bab92009-01-04 23:41:41 +00001990 case tok::coloncolon: // ::foo::bar
1991 if (NextToken().is(tok::kw_new) || // ::new
1992 NextToken().is(tok::kw_delete)) // ::delete
1993 return false;
1994
1995 // Annotate typenames and C++ scope specifiers. If we get one, just
1996 // recurse to handle whatever we get.
1997 if (TryAnnotateTypeOrScopeToken())
1998 return isTypeSpecifierQualifier();
1999 // Otherwise, not a type specifier.
2000 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002001
Chris Lattnere37e2332006-08-15 04:50:22 +00002002 // GNU attributes support.
2003 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00002004 // GNU typeof support.
2005 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002006
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002007 // type-specifiers
2008 case tok::kw_short:
2009 case tok::kw_long:
2010 case tok::kw_signed:
2011 case tok::kw_unsigned:
2012 case tok::kw__Complex:
2013 case tok::kw__Imaginary:
2014 case tok::kw_void:
2015 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002016 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002017 case tok::kw_char16_t:
2018 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002019 case tok::kw_int:
2020 case tok::kw_float:
2021 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002022 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002023 case tok::kw__Bool:
2024 case tok::kw__Decimal32:
2025 case tok::kw__Decimal64:
2026 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00002027
Chris Lattner861a2262008-04-13 18:59:07 +00002028 // struct-or-union-specifier (C99) or class-specifier (C++)
2029 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002030 case tok::kw_struct:
2031 case tok::kw_union:
2032 // enum-specifier
2033 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002034
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002035 // type-qualifier
2036 case tok::kw_const:
2037 case tok::kw_volatile:
2038 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002039
2040 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002041 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002042 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002043
Chris Lattner409bf7d2008-10-20 00:25:30 +00002044 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2045 case tok::less:
2046 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002047
Steve Naroff44ac7772008-12-25 14:16:32 +00002048 case tok::kw___cdecl:
2049 case tok::kw___stdcall:
2050 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00002051 case tok::kw___w64:
2052 case tok::kw___ptr64:
2053 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00002054 }
2055}
2056
Chris Lattneracd58a32006-08-06 17:24:14 +00002057/// isDeclarationSpecifier() - Return true if the current token is part of a
2058/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002059bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00002060 switch (Tok.getKind()) {
2061 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00002062
Chris Lattner020bab92009-01-04 23:41:41 +00002063 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00002064 // Unfortunate hack to support "Class.factoryMethod" notation.
2065 if (getLang().ObjC1 && NextToken().is(tok::period))
2066 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00002067 // Fall through
Steve Naroff9527bbf2009-03-09 21:12:44 +00002068
Douglas Gregor333489b2009-03-27 23:10:48 +00002069 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00002070 // Annotate typenames and C++ scope specifiers. If we get one, just
2071 // recurse to handle whatever we get.
2072 if (TryAnnotateTypeOrScopeToken())
2073 return isDeclarationSpecifier();
2074 // Otherwise, not a declaration specifier.
2075 return false;
2076 case tok::coloncolon: // ::foo::bar
2077 if (NextToken().is(tok::kw_new) || // ::new
2078 NextToken().is(tok::kw_delete)) // ::delete
2079 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002080
Chris Lattner020bab92009-01-04 23:41:41 +00002081 // Annotate typenames and C++ scope specifiers. If we get one, just
2082 // recurse to handle whatever we get.
2083 if (TryAnnotateTypeOrScopeToken())
2084 return isDeclarationSpecifier();
2085 // Otherwise, not a declaration specifier.
2086 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002087
Chris Lattneracd58a32006-08-06 17:24:14 +00002088 // storage-class-specifier
2089 case tok::kw_typedef:
2090 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00002091 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002092 case tok::kw_static:
2093 case tok::kw_auto:
2094 case tok::kw_register:
2095 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002096
Chris Lattneracd58a32006-08-06 17:24:14 +00002097 // type-specifiers
2098 case tok::kw_short:
2099 case tok::kw_long:
2100 case tok::kw_signed:
2101 case tok::kw_unsigned:
2102 case tok::kw__Complex:
2103 case tok::kw__Imaginary:
2104 case tok::kw_void:
2105 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002106 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002107 case tok::kw_char16_t:
2108 case tok::kw_char32_t:
2109
Chris Lattneracd58a32006-08-06 17:24:14 +00002110 case tok::kw_int:
2111 case tok::kw_float:
2112 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002113 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002114 case tok::kw__Bool:
2115 case tok::kw__Decimal32:
2116 case tok::kw__Decimal64:
2117 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00002118
Chris Lattner861a2262008-04-13 18:59:07 +00002119 // struct-or-union-specifier (C99) or class-specifier (C++)
2120 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002121 case tok::kw_struct:
2122 case tok::kw_union:
2123 // enum-specifier
2124 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002125
Chris Lattneracd58a32006-08-06 17:24:14 +00002126 // type-qualifier
2127 case tok::kw_const:
2128 case tok::kw_volatile:
2129 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002130
Chris Lattneracd58a32006-08-06 17:24:14 +00002131 // function-specifier
2132 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002133 case tok::kw_virtual:
2134 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002135
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002136 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002137 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002138
Chris Lattner599e47e2007-08-09 17:01:07 +00002139 // GNU typeof support.
2140 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002141
Chris Lattner599e47e2007-08-09 17:01:07 +00002142 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002143 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002144 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002145
Chris Lattner8b2ec162008-07-26 03:38:44 +00002146 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2147 case tok::less:
2148 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002149
Steve Narofff192fab2009-01-06 19:34:12 +00002150 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002151 case tok::kw___cdecl:
2152 case tok::kw___stdcall:
2153 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00002154 case tok::kw___w64:
2155 case tok::kw___ptr64:
2156 case tok::kw___forceinline:
2157 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002158 }
2159}
2160
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002161bool Parser::isConstructorDeclarator() {
2162 TentativeParsingAction TPA(*this);
2163
2164 // Parse the C++ scope specifier.
2165 CXXScopeSpec SS;
2166 ParseOptionalCXXScopeSpecifier(SS, 0, true);
2167
2168 // Parse the constructor name.
2169 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2170 // We already know that we have a constructor name; just consume
2171 // the token.
2172 ConsumeToken();
2173 } else {
2174 TPA.Revert();
2175 return false;
2176 }
2177
2178 // Current class name must be followed by a left parentheses.
2179 if (Tok.isNot(tok::l_paren)) {
2180 TPA.Revert();
2181 return false;
2182 }
2183 ConsumeParen();
2184
2185 // A right parentheses or ellipsis signals that we have a constructor.
2186 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2187 TPA.Revert();
2188 return true;
2189 }
2190
2191 // If we need to, enter the specified scope.
2192 DeclaratorScopeObj DeclScopeObj(*this, SS);
2193 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(CurScope, SS))
2194 DeclScopeObj.EnterDeclaratorScope();
2195
2196 // Check whether the next token(s) are part of a declaration
2197 // specifier, in which case we have the start of a parameter and,
2198 // therefore, we know that this is a constructor.
2199 bool IsConstructor = isDeclarationSpecifier();
2200 TPA.Revert();
2201 return IsConstructor;
2202}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002203
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002204/// ParseTypeQualifierListOpt
2205/// type-qualifier-list: [C99 6.7.5]
2206/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002207/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002208/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002209/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Alexis Hunt96d5c762009-11-21 08:43:09 +00002210/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2211/// if CXX0XAttributesAllowed = true
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002212///
Alexis Hunt96d5c762009-11-21 08:43:09 +00002213void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2214 bool CXX0XAttributesAllowed) {
2215 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2216 SourceLocation Loc = Tok.getLocation();
2217 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2218 if (CXX0XAttributesAllowed)
2219 DS.AddAttributes(Attr.AttrList);
2220 else
2221 Diag(Loc, diag::err_attributes_not_allowed);
2222 }
2223
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002224 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002225 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002226 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002227 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002228 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002229
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002230 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002231 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002232 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2233 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002234 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002235 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002236 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2237 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002238 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002239 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002240 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2241 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002242 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002243 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002244 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002245 case tok::kw___cdecl:
2246 case tok::kw___stdcall:
2247 case tok::kw___fastcall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002248 if (GNUAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002249 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2250 continue;
2251 }
2252 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002253 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002254 if (GNUAttributesAllowed) {
2255 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002256 continue; // do *not* consume the next token!
2257 }
2258 // otherwise, FALL THROUGH!
2259 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002260 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002261 // If this is not a type-qualifier token, we're done reading type
2262 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002263 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002264 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002265 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002266
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002267 // If the specifier combination wasn't legal, issue a diagnostic.
2268 if (isInvalid) {
2269 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002270 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002271 }
2272 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002273 }
2274}
2275
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002276
2277/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2278///
2279void Parser::ParseDeclarator(Declarator &D) {
2280 /// This implements the 'declarator' production in the C grammar, then checks
2281 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002282 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002283}
2284
Sebastian Redlbd150f42008-11-21 19:14:01 +00002285/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2286/// is parsed by the function passed to it. Pass null, and the direct-declarator
2287/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002288/// ptr-operator production.
2289///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002290/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2291/// [C] pointer[opt] direct-declarator
2292/// [C++] direct-declarator
2293/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002294///
2295/// pointer: [C99 6.7.5]
2296/// '*' type-qualifier-list[opt]
2297/// '*' type-qualifier-list[opt] pointer
2298///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002299/// ptr-operator:
2300/// '*' cv-qualifier-seq[opt]
2301/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002302/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002303/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002304/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002305/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002306void Parser::ParseDeclaratorInternal(Declarator &D,
2307 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002308 if (Diags.hasAllExtensionsSilenced())
2309 D.setExtension();
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002310 // C++ member pointers start with a '::' or a nested-name.
2311 // Member pointers get special handling, since there's no place for the
2312 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002313 if (getLang().CPlusPlus &&
2314 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2315 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002316 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00002317 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump11289f42009-09-09 15:08:12 +00002318 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002319 // The scope spec really belongs to the direct-declarator.
2320 D.getCXXScopeSpec() = SS;
2321 if (DirectDeclParser)
2322 (this->*DirectDeclParser)(D);
2323 return;
2324 }
2325
2326 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002327 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002328 DeclSpec DS;
2329 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002330 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002331
2332 // Recurse to parse whatever is left.
2333 ParseDeclaratorInternal(D, DirectDeclParser);
2334
2335 // Sema will have to catch (syntactically invalid) pointers into global
2336 // scope. It has to catch pointers into namespace scope anyway.
2337 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002338 Loc, DS.TakeAttributes()),
2339 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002340 return;
2341 }
2342 }
2343
2344 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002345 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002346 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002347 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002348 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002349 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002350 if (DirectDeclParser)
2351 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002352 return;
2353 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002354
Sebastian Redled0f3b02009-03-15 22:02:01 +00002355 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2356 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002357 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002358 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002359
Chris Lattner9eac9312009-03-27 04:18:06 +00002360 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002361 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002362 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002363
Bill Wendling3708c182007-05-27 10:15:43 +00002364 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002365 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002366
Bill Wendling3708c182007-05-27 10:15:43 +00002367 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002368 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002369 if (Kind == tok::star)
2370 // Remember that we parsed a pointer type, and remember the type-quals.
2371 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002372 DS.TakeAttributes()),
2373 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002374 else
2375 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002376 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002377 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002378 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002379 } else {
2380 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002381 DeclSpec DS;
2382
Sebastian Redl3b27be62009-03-23 00:00:23 +00002383 // Complain about rvalue references in C++03, but then go on and build
2384 // the declarator.
2385 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2386 Diag(Loc, diag::err_rvalue_reference);
2387
Bill Wendling93efb222007-06-02 23:28:54 +00002388 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2389 // cv-qualifiers are introduced through the use of a typedef or of a
2390 // template type argument, in which case the cv-qualifiers are ignored.
2391 //
2392 // [GNU] Retricted references are allowed.
2393 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002394 // [C++0x] Attributes on references are not allowed.
2395 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002396 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002397
2398 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2399 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2400 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002401 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002402 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2403 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002404 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002405 }
Bill Wendling3708c182007-05-27 10:15:43 +00002406
2407 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002408 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002409
Douglas Gregor66583c52008-11-03 15:51:28 +00002410 if (D.getNumTypeObjects() > 0) {
2411 // C++ [dcl.ref]p4: There shall be no references to references.
2412 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2413 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002414 if (const IdentifierInfo *II = D.getIdentifier())
2415 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2416 << II;
2417 else
2418 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2419 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002420
Sebastian Redlbd150f42008-11-21 19:14:01 +00002421 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002422 // can go ahead and build the (technically ill-formed)
2423 // declarator: reference collapsing will take care of it.
2424 }
2425 }
2426
Bill Wendling3708c182007-05-27 10:15:43 +00002427 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002428 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002429 DS.TakeAttributes(),
2430 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002431 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002432 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002433}
2434
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002435/// ParseDirectDeclarator
2436/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002437/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002438/// '(' declarator ')'
2439/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002440/// [C90] direct-declarator '[' constant-expression[opt] ']'
2441/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2442/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2443/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2444/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002445/// direct-declarator '(' parameter-type-list ')'
2446/// direct-declarator '(' identifier-list[opt] ')'
2447/// [GNU] direct-declarator '(' parameter-forward-declarations
2448/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002449/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2450/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002451/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002452///
2453/// declarator-id: [C++ 8]
2454/// id-expression
2455/// '::'[opt] nested-name-specifier[opt] type-name
2456///
2457/// id-expression: [C++ 5.1]
2458/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002459/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002460///
2461/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002462/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002463/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002464/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002465/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002466/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002467///
Chris Lattneracd58a32006-08-06 17:24:14 +00002468void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002469 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002470
Douglas Gregor7861a802009-11-03 01:35:08 +00002471 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2472 // ParseDeclaratorInternal might already have parsed the scope.
2473 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2474 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2475 true);
2476 if (afterCXXScope) {
John McCall2b058ef2009-12-11 20:04:54 +00002477 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2478 // Change the declaration context for name lookup, until this function
2479 // is exited (and the declarator has been parsed).
2480 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor7861a802009-11-03 01:35:08 +00002481 }
2482
2483 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2484 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2485 // We found something that indicates the start of an unqualified-id.
2486 // Parse that unqualified-id.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002487 bool AllowConstructorName
2488 = ((D.getCXXScopeSpec().isSet() &&
2489 D.getContext() == Declarator::FileContext) ||
2490 (!D.getCXXScopeSpec().isSet() &&
2491 D.getContext() == Declarator::MemberContext)) &&
2492 !D.getDeclSpec().hasTypeSpecifier();
Douglas Gregor7861a802009-11-03 01:35:08 +00002493 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2494 /*EnteringContext=*/true,
2495 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002496 AllowConstructorName,
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002497 /*ObjectType=*/0,
Douglas Gregor7861a802009-11-03 01:35:08 +00002498 D.getName())) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002499 D.SetIdentifier(0, Tok.getLocation());
2500 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002501 } else {
2502 // Parsed the unqualified-id; update range information and move along.
2503 if (D.getSourceRange().getBegin().isInvalid())
2504 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2505 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002506 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002507 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002508 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002509 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002510 assert(!getLang().CPlusPlus &&
2511 "There's a C++-specific check for tok::identifier above");
2512 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2513 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2514 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002515 goto PastIdentifier;
2516 }
2517
2518 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002519 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002520 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002521 // Example: 'char (*X)' or 'int (*XX)(void)'
2522 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002523
2524 // If the declarator was parenthesized, we entered the declarator
2525 // scope when parsing the parenthesized declarator, then exited
2526 // the scope already. Re-enter the scope, if we need to.
2527 if (D.getCXXScopeSpec().isSet()) {
2528 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2529 // Change the declaration context for name lookup, until this function
2530 // is exited (and the declarator has been parsed).
2531 DeclScopeObj.EnterDeclaratorScope();
2532 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002533 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002534 // This could be something simple like "int" (in which case the declarator
2535 // portion is empty), if an abstract-declarator is allowed.
2536 D.SetIdentifier(0, Tok.getLocation());
2537 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002538 if (D.getContext() == Declarator::MemberContext)
2539 Diag(Tok, diag::err_expected_member_name_or_semi)
2540 << D.getDeclSpec().getSourceRange();
2541 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002542 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002543 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002544 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002545 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002546 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002547 }
Mike Stump11289f42009-09-09 15:08:12 +00002548
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002549 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002550 assert(D.isPastIdentifier() &&
2551 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002552
Alexis Hunt96d5c762009-11-21 08:43:09 +00002553 // Don't parse attributes unless we have an identifier.
2554 if (D.getIdentifier() && getLang().CPlusPlus
2555 && isCXX0XAttributeSpecifier(true)) {
2556 SourceLocation AttrEndLoc;
2557 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2558 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2559 }
2560
Chris Lattneracd58a32006-08-06 17:24:14 +00002561 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002562 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002563 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2564 // In such a case, check if we actually have a function declarator; if it
2565 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002566 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2567 // When not in file scope, warn for ambiguous function declarators, just
2568 // in case the author intended it as a variable definition.
2569 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2570 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2571 break;
2572 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002573 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002574 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002575 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002576 } else {
2577 break;
2578 }
2579 }
2580}
2581
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002582/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2583/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002584/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002585/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2586///
2587/// direct-declarator:
2588/// '(' declarator ')'
2589/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002590/// direct-declarator '(' parameter-type-list ')'
2591/// direct-declarator '(' identifier-list[opt] ')'
2592/// [GNU] direct-declarator '(' parameter-forward-declarations
2593/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002594///
2595void Parser::ParseParenDeclarator(Declarator &D) {
2596 SourceLocation StartLoc = ConsumeParen();
2597 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002598
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002599 // Eat any attributes before we look at whether this is a grouping or function
2600 // declarator paren. If this is a grouping paren, the attribute applies to
2601 // the type being built up, for example:
2602 // int (__attribute__(()) *x)(long y)
2603 // If this ends up not being a grouping paren, the attribute applies to the
2604 // first argument, for example:
2605 // int (__attribute__(()) int x)
2606 // In either case, we need to eat any attributes to be able to determine what
2607 // sort of paren this is.
2608 //
2609 AttributeList *AttrList = 0;
2610 bool RequiresArg = false;
2611 if (Tok.is(tok::kw___attribute)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002612 AttrList = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +00002613
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002614 // We require that the argument list (if this is a non-grouping paren) be
2615 // present even if the attribute list was empty.
2616 RequiresArg = true;
2617 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002618 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002619 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2620 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2621 Tok.is(tok::kw___ptr64)) {
2622 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2623 }
Mike Stump11289f42009-09-09 15:08:12 +00002624
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002625 // If we haven't past the identifier yet (or where the identifier would be
2626 // stored, if this is an abstract declarator), then this is probably just
2627 // grouping parens. However, if this could be an abstract-declarator, then
2628 // this could also be the start of function arguments (consider 'void()').
2629 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002630
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002631 if (!D.mayOmitIdentifier()) {
2632 // If this can't be an abstract-declarator, this *must* be a grouping
2633 // paren, because we haven't seen the identifier yet.
2634 isGrouping = true;
2635 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002636 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002637 isDeclarationSpecifier()) { // 'int(int)' is a function.
2638 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2639 // considered to be a type, not a K&R identifier-list.
2640 isGrouping = false;
2641 } else {
2642 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2643 isGrouping = true;
2644 }
Mike Stump11289f42009-09-09 15:08:12 +00002645
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002646 // If this is a grouping paren, handle:
2647 // direct-declarator: '(' declarator ')'
2648 // direct-declarator: '(' attributes declarator ')'
2649 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002650 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002651 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002652 if (AttrList)
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002653 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002654
Sebastian Redlbd150f42008-11-21 19:14:01 +00002655 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002656 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002657 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002658
2659 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002660 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002661 return;
2662 }
Mike Stump11289f42009-09-09 15:08:12 +00002663
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002664 // Okay, if this wasn't a grouping paren, it must be the start of a function
2665 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002666 // identifier (and remember where it would have been), then call into
2667 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002668 D.SetIdentifier(0, Tok.getLocation());
2669
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002670 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002671}
2672
2673/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2674/// declarator D up to a paren, which indicates that we are parsing function
2675/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002676///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002677/// If AttrList is non-null, then the caller parsed those arguments immediately
2678/// after the open paren - they should be considered to be the first argument of
2679/// a parameter. If RequiresArg is true, then the first argument of the
2680/// function is required to be present and required to not be an identifier
2681/// list.
2682///
Chris Lattneracd58a32006-08-06 17:24:14 +00002683/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002684/// parameter-type-list: [C99 6.7.5]
2685/// parameter-list
2686/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002687/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002688///
2689/// parameter-list: [C99 6.7.5]
2690/// parameter-declaration
2691/// parameter-list ',' parameter-declaration
2692///
2693/// parameter-declaration: [C99 6.7.5]
2694/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002695/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002696/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002697/// declaration-specifiers abstract-declarator[opt]
2698/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002699/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002700/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002701///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002702/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002703/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002704///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002705void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2706 AttributeList *AttrList,
2707 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002708 // lparen is already consumed!
2709 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002710
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002711 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002712 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002713 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002714 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002715 delete AttrList;
2716 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002717
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002718 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2719 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002720
2721 // cv-qualifier-seq[opt].
2722 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002723 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002724 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002725 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002726 llvm::SmallVector<TypeTy*, 2> Exceptions;
2727 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002728 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002729 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002730 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002731 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002732
2733 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002734 if (Tok.is(tok::kw_throw)) {
2735 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002736 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002737 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002738 hasAnyExceptionSpec);
2739 assert(Exceptions.size() == ExceptionRanges.size() &&
2740 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002741 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002742 }
2743
Chris Lattner371ed4e2008-04-06 06:57:35 +00002744 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002745 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002746 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002747 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002748 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002749 /*arglist*/ 0, 0,
2750 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002751 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002752 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002753 Exceptions.data(),
2754 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002755 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002756 LParenLoc, RParenLoc, D),
2757 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002758 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002759 }
2760
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002761 // Alternatively, this parameter list may be an identifier list form for a
2762 // K&R-style function: void foo(a,b,c)
Steve Naroffb0486722009-01-28 19:16:40 +00002763 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff3b6a4bd2009-01-30 14:23:32 +00002764 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002765 // K&R identifier lists can't have typedefs as identifiers, per
2766 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002767 if (RequiresArg) {
2768 Diag(Tok, diag::err_argument_required_after_attribute);
2769 delete AttrList;
2770 }
Steve Naroffb0486722009-01-28 19:16:40 +00002771 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2772 // normal declarators, not for abstract-declarators.
2773 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002774 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002775 }
Mike Stump11289f42009-09-09 15:08:12 +00002776
Chris Lattner371ed4e2008-04-06 06:57:35 +00002777 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00002778
Chris Lattner371ed4e2008-04-06 06:57:35 +00002779 // Build up an array of information about the parsed arguments.
2780 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002781
2782 // Enter function-declaration scope, limiting any declarators to the
2783 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00002784 ParseScope PrototypeScope(this,
2785 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00002786
Chris Lattner371ed4e2008-04-06 06:57:35 +00002787 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002788 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00002789 while (1) {
2790 if (Tok.is(tok::ellipsis)) {
2791 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002792 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002793 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00002794 }
Mike Stump11289f42009-09-09 15:08:12 +00002795
Chris Lattner371ed4e2008-04-06 06:57:35 +00002796 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002797
Chris Lattner371ed4e2008-04-06 06:57:35 +00002798 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00002799 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002800 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002801
2802 // If the caller parsed attributes for the first argument, add them now.
2803 if (AttrList) {
2804 DS.AddAttributes(AttrList);
2805 AttrList = 0; // Only apply the attributes to the first parameter.
2806 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002807 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002808
Chris Lattner371ed4e2008-04-06 06:57:35 +00002809 // Parse the declarator. This is "PrototypeContext", because we must
2810 // accept either 'declarator' or 'abstract-declarator' here.
2811 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2812 ParseDeclarator(ParmDecl);
2813
2814 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002815 if (Tok.is(tok::kw___attribute)) {
2816 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002817 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002818 ParmDecl.AddAttributes(AttrList, Loc);
2819 }
Mike Stump11289f42009-09-09 15:08:12 +00002820
Chris Lattner371ed4e2008-04-06 06:57:35 +00002821 // Remember this parsed parameter in ParamInfo.
2822 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00002823
Douglas Gregor4d87df52008-12-16 21:30:33 +00002824 // DefArgToks is used when the parsing of default arguments needs
2825 // to be delayed.
2826 CachedTokens *DefArgToks = 0;
2827
Chris Lattner371ed4e2008-04-06 06:57:35 +00002828 // If no parameter was specified, verify that *something* was specified,
2829 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002830 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2831 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00002832 // Completely missing, emit error.
2833 Diag(DSStart, diag::err_missing_param);
2834 } else {
2835 // Otherwise, we have something. Add it and let semantic analysis try
2836 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00002837
Chris Lattner371ed4e2008-04-06 06:57:35 +00002838 // Inform the actions module about the parameter declarator, so it gets
2839 // added to the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002840 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002841
2842 // Parse the default argument, if any. We parse the default
2843 // arguments in all dialects; the semantic analysis in
2844 // ActOnParamDefaultArgument will reject the default argument in
2845 // C.
2846 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00002847 SourceLocation EqualLoc = Tok.getLocation();
2848
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002849 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00002850 if (D.getContext() == Declarator::MemberContext) {
2851 // If we're inside a class definition, cache the tokens
2852 // corresponding to the default argument. We'll actually parse
2853 // them when we see the end of the class definition.
2854 // FIXME: Templates will require something similar.
2855 // FIXME: Can we use a smart pointer for Toks?
2856 DefArgToks = new CachedTokens;
2857
Mike Stump11289f42009-09-09 15:08:12 +00002858 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002859 tok::semi, false)) {
2860 delete DefArgToks;
2861 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00002862 Actions.ActOnParamDefaultArgumentError(Param);
2863 } else
Mike Stump11289f42009-09-09 15:08:12 +00002864 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00002865 (*DefArgToks)[1].getLocation());
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002866 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00002867 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00002868 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002869
Douglas Gregor4d87df52008-12-16 21:30:33 +00002870 OwningExprResult DefArgResult(ParseAssignmentExpression());
2871 if (DefArgResult.isInvalid()) {
2872 Actions.ActOnParamDefaultArgumentError(Param);
2873 SkipUntil(tok::comma, tok::r_paren, true, true);
2874 } else {
2875 // Inform the actions module about the default argument
2876 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002877 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00002878 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002879 }
2880 }
Mike Stump11289f42009-09-09 15:08:12 +00002881
2882 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2883 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002884 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00002885 }
2886
2887 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002888 if (Tok.isNot(tok::comma)) {
2889 if (Tok.is(tok::ellipsis)) {
2890 IsVariadic = true;
2891 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2892
2893 if (!getLang().CPlusPlus) {
2894 // We have ellipsis without a preceding ',', which is ill-formed
2895 // in C. Complain and provide the fix.
2896 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2897 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2898 }
2899 }
2900
2901 break;
2902 }
Mike Stump11289f42009-09-09 15:08:12 +00002903
Chris Lattner371ed4e2008-04-06 06:57:35 +00002904 // Consume the comma.
2905 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00002906 }
Mike Stump11289f42009-09-09 15:08:12 +00002907
Chris Lattner371ed4e2008-04-06 06:57:35 +00002908 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002909 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00002910
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002911 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002912 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2913 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002914
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002915 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002916 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002917 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002918 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002919 llvm::SmallVector<TypeTy*, 2> Exceptions;
2920 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002921
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002922 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002923 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00002924 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002925 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002926 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002927
2928 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002929 if (Tok.is(tok::kw_throw)) {
2930 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002931 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002932 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002933 hasAnyExceptionSpec);
2934 assert(Exceptions.size() == ExceptionRanges.size() &&
2935 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002936 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002937 }
2938
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002939 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002940 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002941 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002942 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002943 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002944 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002945 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002946 Exceptions.data(),
2947 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002948 Exceptions.size(),
2949 LParenLoc, RParenLoc, D),
2950 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002951}
Chris Lattneracd58a32006-08-06 17:24:14 +00002952
Chris Lattner6c940e62008-04-06 06:34:08 +00002953/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2954/// we found a K&R-style identifier list instead of a type argument list. The
2955/// current token is known to be the first identifier in the list.
2956///
2957/// identifier-list: [C99 6.7.5]
2958/// identifier
2959/// identifier-list ',' identifier
2960///
2961void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2962 Declarator &D) {
2963 // Build up an array of information about the parsed arguments.
2964 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2965 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00002966
Chris Lattner6c940e62008-04-06 06:34:08 +00002967 // If there was no identifier specified for the declarator, either we are in
2968 // an abstract-declarator, or we are in a parameter declarator which was found
2969 // to be abstract. In abstract-declarators, identifier lists are not valid:
2970 // diagnose this.
2971 if (!D.getIdentifier())
2972 Diag(Tok, diag::ext_ident_list_in_param);
2973
2974 // Tok is known to be the first identifier in the list. Remember this
2975 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00002976 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00002977 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner83f095c2009-03-28 19:18:32 +00002978 Tok.getLocation(),
2979 DeclPtrTy()));
Mike Stump11289f42009-09-09 15:08:12 +00002980
Chris Lattner9186f552008-04-06 06:39:19 +00002981 ConsumeToken(); // eat the first identifier.
Mike Stump11289f42009-09-09 15:08:12 +00002982
Chris Lattner6c940e62008-04-06 06:34:08 +00002983 while (Tok.is(tok::comma)) {
2984 // Eat the comma.
2985 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002986
Chris Lattner9186f552008-04-06 06:39:19 +00002987 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00002988 if (Tok.isNot(tok::identifier)) {
2989 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00002990 SkipUntil(tok::r_paren);
2991 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00002992 }
Chris Lattner67b450c2008-04-06 06:47:48 +00002993
Chris Lattner6c940e62008-04-06 06:34:08 +00002994 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00002995
2996 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00002997 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerebad6a22008-11-19 07:37:42 +00002998 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00002999
Chris Lattner6c940e62008-04-06 06:34:08 +00003000 // Verify that the argument identifier has not already been mentioned.
3001 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00003002 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00003003 } else {
3004 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00003005 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00003006 Tok.getLocation(),
3007 DeclPtrTy()));
Chris Lattner9186f552008-04-06 06:39:19 +00003008 }
Mike Stump11289f42009-09-09 15:08:12 +00003009
Chris Lattner6c940e62008-04-06 06:34:08 +00003010 // Eat the identifier.
3011 ConsumeToken();
3012 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003013
3014 // If we have the closing ')', eat it and we're done.
3015 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3016
Chris Lattner9186f552008-04-06 06:39:19 +00003017 // Remember that we parsed a function type, and remember the attributes. This
3018 // function type is always a K&R style function type, which is not varargs and
3019 // has no prototype.
3020 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00003021 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00003022 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00003023 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00003024 /*exception*/false,
3025 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00003026 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003027 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00003028}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00003029
Chris Lattnere8074e62006-08-06 18:30:15 +00003030/// [C90] direct-declarator '[' constant-expression[opt] ']'
3031/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3032/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3033/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3034/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3035void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00003036 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00003037
Chris Lattner84a11622008-12-18 07:27:21 +00003038 // C array syntax has many features, but by-far the most common is [] and [4].
3039 // This code does a fast path to handle some of the most obvious cases.
3040 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003041 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003042 //FIXME: Use these
3043 CXX0XAttributeList Attr;
3044 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3045 Attr = ParseCXX0XAttributes();
3046 }
3047
Chris Lattner84a11622008-12-18 07:27:21 +00003048 // Remember that we parsed the empty array type.
3049 OwningExprResult NumElements(Actions);
Douglas Gregor04318252009-07-06 15:59:29 +00003050 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3051 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003052 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003053 return;
3054 } else if (Tok.getKind() == tok::numeric_constant &&
3055 GetLookAheadToken(1).is(tok::r_square)) {
3056 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00003057 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00003058 ConsumeToken();
3059
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003060 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00003061 //FIXME: Use these
3062 CXX0XAttributeList Attr;
3063 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3064 Attr = ParseCXX0XAttributes();
3065 }
Chris Lattner84a11622008-12-18 07:27:21 +00003066
3067 // If there was an error parsing the assignment-expression, recover.
3068 if (ExprRes.isInvalid())
3069 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00003070
Chris Lattner84a11622008-12-18 07:27:21 +00003071 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00003072 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3073 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003074 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00003075 return;
3076 }
Mike Stump11289f42009-09-09 15:08:12 +00003077
Chris Lattnere8074e62006-08-06 18:30:15 +00003078 // If valid, this location is the position where we read the 'static' keyword.
3079 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00003080 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003081 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003082
Chris Lattnere8074e62006-08-06 18:30:15 +00003083 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003084 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00003085 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00003086 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00003087
Chris Lattnere8074e62006-08-06 18:30:15 +00003088 // If we haven't already read 'static', check to see if there is one after the
3089 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003090 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00003091 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003092
Chris Lattnere8074e62006-08-06 18:30:15 +00003093 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00003094 bool isStar = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +00003095 OwningExprResult NumElements(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00003096
Chris Lattner521ff2b2008-04-06 05:26:30 +00003097 // Handle the case where we have '[*]' as the array size. However, a leading
3098 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3099 // the the token after the star is a ']'. Since stars in arrays are
3100 // infrequent, use of lookahead is not costly here.
3101 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00003102 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00003103
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003104 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00003105 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00003106 StaticLoc = SourceLocation(); // Drop the static.
3107 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00003108 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00003109 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00003110 // Note, in C89, this production uses the constant-expr production instead
3111 // of assignment-expr. The only difference is that assignment-expr allows
3112 // things like '=' and '*='. Sema rejects these in C89 mode because they
3113 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00003114
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00003115 // Parse the constant-expression or assignment-expression now (depending
3116 // on dialect).
3117 if (getLang().CPlusPlus)
3118 NumElements = ParseConstantExpression();
3119 else
3120 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00003121 }
Mike Stump11289f42009-09-09 15:08:12 +00003122
Chris Lattner62591722006-08-12 18:40:58 +00003123 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003124 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00003125 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00003126 // If the expression was invalid, skip it.
3127 SkipUntil(tok::r_square);
3128 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00003129 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003130
3131 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3132
Alexis Hunt96d5c762009-11-21 08:43:09 +00003133 //FIXME: Use these
3134 CXX0XAttributeList Attr;
3135 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3136 Attr = ParseCXX0XAttributes();
3137 }
3138
Chris Lattner84a11622008-12-18 07:27:21 +00003139 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00003140 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3141 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00003142 NumElements.release(),
3143 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003144 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00003145}
3146
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003147/// [GNU] typeof-specifier:
3148/// typeof ( expressions )
3149/// typeof ( type-name )
3150/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003151///
3152void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003153 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003154 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003155 SourceLocation StartLoc = ConsumeToken();
3156
John McCalle8595032010-01-13 20:03:27 +00003157 const bool hasParens = Tok.is(tok::l_paren);
3158
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003159 bool isCastExpr;
3160 TypeTy *CastTy;
3161 SourceRange CastRange;
3162 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3163 isCastExpr,
3164 CastTy,
3165 CastRange);
John McCalle8595032010-01-13 20:03:27 +00003166 if (hasParens)
3167 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003168
3169 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003170 // FIXME: Not accurate, the range gets one token more than it should.
3171 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003172 else
3173 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003174
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003175 if (isCastExpr) {
3176 if (!CastTy) {
3177 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003178 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003179 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003180
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003181 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003182 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003183 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3184 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003185 DiagID, CastTy))
3186 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003187 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003188 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003189
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003190 // If we get here, the operand to the typeof was an expresion.
3191 if (Operand.isInvalid()) {
3192 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003193 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003194 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003195
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003196 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003197 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003198 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3199 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003200 DiagID, Operand.release()))
3201 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003202}