blob: 43c92456d29130abfd4ceb1aa95749b6ae1b057f [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);
Chris Lattner53361ac2006-08-10 05:19:57 +0000359 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000360
Chris Lattner0e894622006-08-13 19:58:17 +0000361 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
362 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +0000363 if (Tok.is(tok::semi)) {
Chris Lattner0e894622006-08-13 19:58:17 +0000364 ConsumeToken();
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000365 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall28a6aea2009-11-04 02:18:39 +0000366 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000367 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +0000368 }
Mike Stump11289f42009-09-09 15:08:12 +0000369
John McCalld5a36322009-11-03 19:26:08 +0000370 DeclGroupPtrTy DG = ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false,
371 &DeclEnd);
372 return DG;
373}
Mike Stump11289f42009-09-09 15:08:12 +0000374
John McCalld5a36322009-11-03 19:26:08 +0000375/// ParseDeclGroup - Having concluded that this is either a function
376/// definition or a group of object declarations, actually parse the
377/// result.
John McCall28a6aea2009-11-04 02:18:39 +0000378Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
379 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +0000380 bool AllowFunctionDefinitions,
381 SourceLocation *DeclEnd) {
382 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +0000383 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +0000384 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +0000385
John McCalld5a36322009-11-03 19:26:08 +0000386 // Bail out if the first declarator didn't seem well-formed.
387 if (!D.hasName() && !D.mayOmitIdentifier()) {
388 // Skip until ; or }.
389 SkipUntil(tok::r_brace, true, true);
390 if (Tok.is(tok::semi))
391 ConsumeToken();
392 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +0000393 }
Mike Stump11289f42009-09-09 15:08:12 +0000394
John McCalld5a36322009-11-03 19:26:08 +0000395 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
396 if (isDeclarationAfterDeclarator()) {
397 // Fall though. We have to check this first, though, because
398 // __attribute__ might be the start of a function definition in
399 // (extended) K&R C.
400 } else if (isStartOfFunctionDefinition()) {
401 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
402 Diag(Tok, diag::err_function_declared_typedef);
403
404 // Recover by treating the 'typedef' as spurious.
405 DS.ClearStorageClassSpecs();
406 }
407
408 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
409 return Actions.ConvertDeclToDeclGroup(TheDecl);
410 } else {
411 Diag(Tok, diag::err_expected_fn_body);
412 SkipUntil(tok::semi);
413 return DeclGroupPtrTy();
414 }
415 }
416
417 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
418 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000419 D.complete(FirstDecl);
John McCalld5a36322009-11-03 19:26:08 +0000420 if (FirstDecl.get())
421 DeclsInGroup.push_back(FirstDecl);
422
423 // If we don't have a comma, it is either the end of the list (a ';') or an
424 // error, bail out.
425 while (Tok.is(tok::comma)) {
426 // Consume the comma.
Chris Lattnerefb0f112009-03-29 17:18:04 +0000427 ConsumeToken();
John McCalld5a36322009-11-03 19:26:08 +0000428
429 // Parse the next declarator.
430 D.clear();
431
432 // Accept attributes in an init-declarator. In the first declarator in a
433 // declaration, these would be part of the declspec. In subsequent
434 // declarators, they become part of the declarator itself, so that they
435 // don't apply to declarators after *this* one. Examples:
436 // short __attribute__((common)) var; -> declspec
437 // short var __attribute__((common)); -> declarator
438 // short x, __attribute__((common)) var; -> declarator
439 if (Tok.is(tok::kw___attribute)) {
440 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000441 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld5a36322009-11-03 19:26:08 +0000442 D.AddAttributes(AttrList, Loc);
443 }
444
445 ParseDeclarator(D);
446
447 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall28a6aea2009-11-04 02:18:39 +0000448 D.complete(ThisDecl);
John McCalld5a36322009-11-03 19:26:08 +0000449 if (ThisDecl.get())
450 DeclsInGroup.push_back(ThisDecl);
451 }
452
453 if (DeclEnd)
454 *DeclEnd = Tok.getLocation();
455
456 if (Context != Declarator::ForContext &&
457 ExpectAndConsume(tok::semi,
458 Context == Declarator::FileContext
459 ? diag::err_invalid_token_after_toplevel_declarator
460 : diag::err_expected_semi_declaration)) {
461 SkipUntil(tok::r_brace, true, true);
462 if (Tok.is(tok::semi))
463 ConsumeToken();
464 }
465
466 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
467 DeclsInGroup.data(),
468 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +0000469}
470
Douglas Gregor23996282009-05-12 21:31:51 +0000471/// \brief Parse 'declaration' after parsing 'declaration-specifiers
472/// declarator'. This method parses the remainder of the declaration
473/// (including any attributes or initializer, among other things) and
474/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000475///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000476/// init-declarator: [C99 6.7]
477/// declarator
478/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +0000479/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
480/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +0000481/// [C++] declarator initializer[opt]
482///
483/// [C++] initializer:
484/// [C++] '=' initializer-clause
485/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +0000486/// [C++0x] '=' 'default' [TODO]
487/// [C++0x] '=' 'delete'
488///
489/// According to the standard grammar, =default and =delete are function
490/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +0000491///
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000492Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
493 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +0000494 // If a simple-asm-expr is present, parse it.
495 if (Tok.is(tok::kw_asm)) {
496 SourceLocation Loc;
497 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
498 if (AsmLabel.isInvalid()) {
499 SkipUntil(tok::semi, true, true);
500 return DeclPtrTy();
501 }
Mike Stump11289f42009-09-09 15:08:12 +0000502
Douglas Gregor23996282009-05-12 21:31:51 +0000503 D.setAsmLabel(AsmLabel.release());
504 D.SetRangeEnd(Loc);
505 }
Mike Stump11289f42009-09-09 15:08:12 +0000506
Douglas Gregor23996282009-05-12 21:31:51 +0000507 // If attributes are present, parse them.
508 if (Tok.is(tok::kw___attribute)) {
509 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000510 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor23996282009-05-12 21:31:51 +0000511 D.AddAttributes(AttrList, Loc);
512 }
Mike Stump11289f42009-09-09 15:08:12 +0000513
Douglas Gregor23996282009-05-12 21:31:51 +0000514 // Inform the current actions module that we just parsed this declarator.
Douglas Gregor450f00842009-09-25 18:43:00 +0000515 DeclPtrTy ThisDecl;
516 switch (TemplateInfo.Kind) {
517 case ParsedTemplateInfo::NonTemplate:
518 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
519 break;
520
521 case ParsedTemplateInfo::Template:
522 case ParsedTemplateInfo::ExplicitSpecialization:
523 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregorb52fabb2009-06-23 23:11:28 +0000524 Action::MultiTemplateParamsArg(Actions,
525 TemplateInfo.TemplateParams->data(),
526 TemplateInfo.TemplateParams->size()),
Douglas Gregor450f00842009-09-25 18:43:00 +0000527 D);
528 break;
529
530 case ParsedTemplateInfo::ExplicitInstantiation: {
531 Action::DeclResult ThisRes
532 = Actions.ActOnExplicitInstantiation(CurScope,
533 TemplateInfo.ExternLoc,
534 TemplateInfo.TemplateLoc,
535 D);
536 if (ThisRes.isInvalid()) {
537 SkipUntil(tok::semi, true, true);
538 return DeclPtrTy();
539 }
540
541 ThisDecl = ThisRes.get();
542 break;
543 }
544 }
Mike Stump11289f42009-09-09 15:08:12 +0000545
Douglas Gregor23996282009-05-12 21:31:51 +0000546 // Parse declarator '=' initializer.
547 if (Tok.is(tok::equal)) {
548 ConsumeToken();
549 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
550 SourceLocation DelLoc = ConsumeToken();
551 Actions.SetDeclDeleted(ThisDecl, DelLoc);
552 } else {
John McCall1f4ee7b2009-12-19 09:28:58 +0000553 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
554 EnterScope(0);
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000555 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000556 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000557
Douglas Gregor23996282009-05-12 21:31:51 +0000558 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000559
John McCall1f4ee7b2009-12-19 09:28:58 +0000560 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000561 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +0000562 ExitScope();
563 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000564
Douglas Gregor23996282009-05-12 21:31:51 +0000565 if (Init.isInvalid()) {
566 SkipUntil(tok::semi, true, true);
567 return DeclPtrTy();
568 }
Anders Carlsson250aada2009-08-16 05:13:48 +0000569 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor23996282009-05-12 21:31:51 +0000570 }
571 } else if (Tok.is(tok::l_paren)) {
572 // Parse C++ direct initializer: '(' expression-list ')'
573 SourceLocation LParenLoc = ConsumeParen();
574 ExprVector Exprs(Actions);
575 CommaLocsTy CommaLocs;
576
577 if (ParseExpressionList(Exprs, CommaLocs)) {
578 SkipUntil(tok::r_paren);
579 } else {
580 // Match the ')'.
581 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
582
583 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
584 "Unexpected number of commas!");
585 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
586 move_arg(Exprs),
Jay Foad7d0479f2009-05-21 09:52:38 +0000587 CommaLocs.data(), RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000588 }
589 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000590 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000591 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
592 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000593 }
594
595 return ThisDecl;
596}
597
Chris Lattner1890ac82006-08-13 01:16:23 +0000598/// ParseSpecifierQualifierList
599/// specifier-qualifier-list:
600/// type-specifier specifier-qualifier-list[opt]
601/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000602/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000603///
604void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
605 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
606 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000607 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000608
Chris Lattner1890ac82006-08-13 01:16:23 +0000609 // Validate declspec for type-name.
610 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000611 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
612 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000613 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000614
Chris Lattner1b22eed2006-11-28 05:12:07 +0000615 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000616 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000617 if (DS.getStorageClassSpecLoc().isValid())
618 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
619 else
620 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000621 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000622 }
Mike Stump11289f42009-09-09 15:08:12 +0000623
Chris Lattner1b22eed2006-11-28 05:12:07 +0000624 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000625 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000626 if (DS.isInlineSpecified())
627 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
628 if (DS.isVirtualSpecified())
629 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
630 if (DS.isExplicitSpecified())
631 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000632 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000633 }
634}
Chris Lattner53361ac2006-08-10 05:19:57 +0000635
Chris Lattner6cc055a2009-04-12 20:42:31 +0000636/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
637/// specified token is valid after the identifier in a declarator which
638/// immediately follows the declspec. For example, these things are valid:
639///
640/// int x [ 4]; // direct-declarator
641/// int x ( int y); // direct-declarator
642/// int(int x ) // direct-declarator
643/// int x ; // simple-declaration
644/// int x = 17; // init-declarator-list
645/// int x , y; // init-declarator-list
646/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000647/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000648/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000649///
650/// This is not, because 'x' does not immediately follow the declspec (though
651/// ')' happens to be valid anyway).
652/// int (x)
653///
654static bool isValidAfterIdentifierInDeclarator(const Token &T) {
655 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
656 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000657 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000658}
659
Chris Lattner20a0c612009-04-14 21:34:55 +0000660
661/// ParseImplicitInt - This method is called when we have an non-typename
662/// identifier in a declspec (which normally terminates the decl spec) when
663/// the declspec has no type specifier. In this case, the declspec is either
664/// malformed or is "implicit int" (in K&R and C89).
665///
666/// This method handles diagnosing this prettily and returns false if the
667/// declspec is done being processed. If it recovers and thinks there may be
668/// other pieces of declspec after it, it returns true.
669///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000670bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000671 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000672 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000673 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattner20a0c612009-04-14 21:34:55 +0000675 SourceLocation Loc = Tok.getLocation();
676 // If we see an identifier that is not a type name, we normally would
677 // parse it as the identifer being declared. However, when a typename
678 // is typo'd or the definition is not included, this will incorrectly
679 // parse the typename as the identifier name and fall over misparsing
680 // later parts of the diagnostic.
681 //
682 // As such, we try to do some look-ahead in cases where this would
683 // otherwise be an "implicit-int" case to see if this is invalid. For
684 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
685 // an identifier with implicit int, we'd get a parse error because the
686 // next token is obviously invalid for a type. Parse these as a case
687 // with an invalid type specifier.
688 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000689
Chris Lattner20a0c612009-04-14 21:34:55 +0000690 // Since we know that this either implicit int (which is rare) or an
691 // error, we'd do lookahead to try to do better recovery.
692 if (isValidAfterIdentifierInDeclarator(NextToken())) {
693 // If this token is valid for implicit int, e.g. "static x = 4", then
694 // we just avoid eating the identifier, so it will be parsed as the
695 // identifier in the declarator.
696 return false;
697 }
Mike Stump11289f42009-09-09 15:08:12 +0000698
Chris Lattner20a0c612009-04-14 21:34:55 +0000699 // Otherwise, if we don't consume this token, we are going to emit an
700 // error anyway. Try to recover from various common problems. Check
701 // to see if this was a reference to a tag name without a tag specified.
702 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000703 //
704 // C++ doesn't need this, and isTagName doesn't take SS.
705 if (SS == 0) {
706 const char *TagName = 0;
707 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000708
Chris Lattner20a0c612009-04-14 21:34:55 +0000709 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
710 default: break;
711 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
712 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
713 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
714 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
715 }
Mike Stump11289f42009-09-09 15:08:12 +0000716
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000717 if (TagName) {
718 Diag(Loc, diag::err_use_of_tag_name_without_tag)
719 << Tok.getIdentifierInfo() << TagName
720 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000721
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000722 // Parse this as a tag as if the missing tag were present.
723 if (TagKind == tok::kw_enum)
724 ParseEnumSpecifier(Loc, DS, AS);
725 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000726 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000727 return true;
728 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000729 }
Mike Stump11289f42009-09-09 15:08:12 +0000730
Douglas Gregor15e56022009-10-13 23:27:22 +0000731 // This is almost certainly an invalid type name. Let the action emit a
732 // diagnostic and attempt to recover.
733 Action::TypeTy *T = 0;
734 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
735 CurScope, SS, T)) {
736 // The action emitted a diagnostic, so we don't have to.
737 if (T) {
738 // The action has suggested that the type T could be used. Set that as
739 // the type in the declaration specifiers, consume the would-be type
740 // name token, and we're done.
741 const char *PrevSpec;
742 unsigned DiagID;
743 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
744 false);
745 DS.SetRangeEnd(Tok.getLocation());
746 ConsumeToken();
747
748 // There may be other declaration specifiers after this.
749 return true;
750 }
751
752 // Fall through; the action had no suggestion for us.
753 } else {
754 // The action did not emit a diagnostic, so emit one now.
755 SourceRange R;
756 if (SS) R = SS->getRange();
757 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
758 }
Mike Stump11289f42009-09-09 15:08:12 +0000759
Douglas Gregor15e56022009-10-13 23:27:22 +0000760 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000761 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000762 unsigned DiagID;
763 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000764 DS.SetRangeEnd(Tok.getLocation());
765 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000766
Chris Lattner20a0c612009-04-14 21:34:55 +0000767 // TODO: Could inject an invalid typedef decl in an enclosing scope to
768 // avoid rippling error messages on subsequent uses of the same type,
769 // could be useful if #include was forgotten.
770 return false;
771}
772
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000773/// ParseDeclarationSpecifiers
774/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000775/// storage-class-specifier declaration-specifiers[opt]
776/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000777/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000778/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000779///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000780/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000781/// 'typedef'
782/// 'extern'
783/// 'static'
784/// 'auto'
785/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000786/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000787/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000788/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000789/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000790/// [C++] 'virtual'
791/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000792/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000793/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000794
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000795///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000796void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000797 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000798 AccessSpecifier AS,
799 DeclSpecContext DSContext) {
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000800 if (Tok.is(tok::code_completion)) {
801 Actions.CodeCompleteOrdinaryName(CurScope);
802 ConsumeToken();
803 }
804
Chris Lattner2e232092008-03-13 06:29:04 +0000805 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000806 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000807 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000808 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000809 unsigned DiagID = 0;
810
Chris Lattner4d8f8732006-11-28 05:05:08 +0000811 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000812
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000813 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000814 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000815 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000816 // If this is not a declaration specifier token, we're done reading decl
817 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000818 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000819 return;
Mike Stump11289f42009-09-09 15:08:12 +0000820
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000821 case tok::coloncolon: // ::foo::bar
822 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregore861bac2009-08-25 22:51:20 +0000823 if (TryAnnotateCXXScopeToken(true))
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000824 continue;
825 goto DoneWithDeclSpec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000826
827 case tok::annot_cxxscope: {
828 if (DS.hasTypeSpecifier())
829 goto DoneWithDeclSpec;
830
John McCall9dab4e62009-12-12 11:40:51 +0000831 CXXScopeSpec SS;
832 SS.setScopeRep(Tok.getAnnotationValue());
833 SS.setRange(Tok.getAnnotationRange());
834
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000835 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000836 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000837 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000838 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000839 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000840 // We have a qualified template-id, e.g., N::A<int>
John McCall9dab4e62009-12-12 11:40:51 +0000841 DS.getTypeSpecScope() = SS;
842 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +0000843 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000844 "ParseOptionalCXXScopeSpecifier not working");
845 AnnotateTemplateIdTokenAsType(&SS);
846 continue;
847 }
848
Douglas Gregorc5790df2009-09-28 07:26:33 +0000849 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +0000850 DS.getTypeSpecScope() = SS;
851 ConsumeToken(); // The C++ scope.
Douglas Gregorc5790df2009-09-28 07:26:33 +0000852 if (Tok.getAnnotationValue())
853 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
854 PrevSpec, DiagID,
855 Tok.getAnnotationValue());
856 else
857 DS.SetTypeSpecError();
858 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
859 ConsumeToken(); // The typename
860 }
861
Douglas Gregor167fa622009-03-25 15:40:00 +0000862 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000863 goto DoneWithDeclSpec;
864
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000865 // If the next token is the name of the class type that the C++ scope
866 // denotes, followed by a '(', then this is a constructor declaration.
867 // We're done with the decl-specifiers.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000868 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000869 CurScope, &SS) &&
870 GetLookAheadToken(2).is(tok::l_paren))
871 goto DoneWithDeclSpec;
872
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000873 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
874 Next.getLocation(), CurScope, &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000875
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000876 // If the referenced identifier is not a type, then this declspec is
877 // erroneous: We already checked about that it has no type specifier, and
878 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +0000879 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000880 if (TypeRep == 0) {
881 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000882 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000883 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000884 }
Mike Stump11289f42009-09-09 15:08:12 +0000885
John McCall9dab4e62009-12-12 11:40:51 +0000886 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000887 ConsumeToken(); // The C++ scope.
888
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000889 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000890 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000891 if (isInvalid)
892 break;
Mike Stump11289f42009-09-09 15:08:12 +0000893
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000894 DS.SetRangeEnd(Tok.getLocation());
895 ConsumeToken(); // The typename.
896
897 continue;
898 }
Mike Stump11289f42009-09-09 15:08:12 +0000899
Chris Lattnere387d9e2009-01-21 19:48:37 +0000900 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000901 if (Tok.getAnnotationValue())
902 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000903 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000904 else
905 DS.SetTypeSpecError();
Chris Lattnere387d9e2009-01-21 19:48:37 +0000906 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
907 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +0000908
Chris Lattnere387d9e2009-01-21 19:48:37 +0000909 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
910 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
911 // Objective-C interface. If we don't have Objective-C or a '<', this is
912 // just a normal reference to a typedef name.
913 if (!Tok.is(tok::less) || !getLang().ObjC1)
914 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000915
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000916 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000917 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000918 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
919 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
920 LAngleLoc, EndProtoLoc);
921 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
922 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000923
Chris Lattnere387d9e2009-01-21 19:48:37 +0000924 DS.SetRangeEnd(EndProtoLoc);
925 continue;
926 }
Mike Stump11289f42009-09-09 15:08:12 +0000927
Chris Lattner16fac4f2008-07-26 01:18:38 +0000928 // typedef-name
929 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000930 // In C++, check to see if this is a scope specifier like foo::bar::, if
931 // so handle it as such. This is important for ctor parsing.
Douglas Gregore861bac2009-08-25 22:51:20 +0000932 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner78ecd4f2009-01-21 19:19:26 +0000933 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000934
Chris Lattner16fac4f2008-07-26 01:18:38 +0000935 // This identifier can only be a typedef name if we haven't already seen
936 // a type-specifier. Without this check we misparse:
937 // typedef int X; struct Y { short X; }; as 'short int'.
938 if (DS.hasTypeSpecifier())
939 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +0000940
Chris Lattner16fac4f2008-07-26 01:18:38 +0000941 // It has to be available as a typedef too!
Mike Stump11289f42009-09-09 15:08:12 +0000942 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000943 Tok.getLocation(), CurScope);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000944
Chris Lattner6cc055a2009-04-12 20:42:31 +0000945 // If this is not a typedef name, don't parse it as part of the declspec,
946 // it must be an implicit int or an error.
947 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000948 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000949 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +0000950 }
Douglas Gregor8bf42052009-02-09 18:46:07 +0000951
Douglas Gregor61956c42008-10-31 09:07:45 +0000952 // C++: If the identifier is actually the name of the class type
953 // being defined and the next token is a '(', then this is a
954 // constructor declaration. We're done with the decl-specifiers
955 // and will treat this token as an identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000956 if (getLang().CPlusPlus &&
957 (CurScope->isClassScope() ||
958 (CurScope->isTemplateParamScope() &&
Douglas Gregor5ed5ae42009-08-21 18:42:58 +0000959 CurScope->getParent()->isClassScope())) &&
Mike Stump11289f42009-09-09 15:08:12 +0000960 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor61956c42008-10-31 09:07:45 +0000961 NextToken().getKind() == tok::l_paren)
962 goto DoneWithDeclSpec;
963
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000964 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000965 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +0000966 if (isInvalid)
967 break;
Mike Stump11289f42009-09-09 15:08:12 +0000968
Chris Lattner16fac4f2008-07-26 01:18:38 +0000969 DS.SetRangeEnd(Tok.getLocation());
970 ConsumeToken(); // The identifier
971
972 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
973 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
974 // Objective-C interface. If we don't have Objective-C or a '<', this is
975 // just a normal reference to a typedef name.
976 if (!Tok.is(tok::less) || !getLang().ObjC1)
977 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000978
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000979 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000980 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000981 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
982 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
983 LAngleLoc, EndProtoLoc);
984 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
985 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000986
Chris Lattner16fac4f2008-07-26 01:18:38 +0000987 DS.SetRangeEnd(EndProtoLoc);
988
Steve Naroffcd5e7822008-09-22 10:28:57 +0000989 // Need to support trailing type qualifiers (e.g. "id<p> const").
990 // If a type specifier follows, it will be diagnosed elsewhere.
991 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000992 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000993
994 // type-name
995 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +0000996 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000997 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000998 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000999 // This template-id does not refer to a type name, so we're
1000 // done with the type-specifiers.
1001 goto DoneWithDeclSpec;
1002 }
1003
1004 // Turn the template-id annotation token into a type annotation
1005 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001006 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001007 continue;
1008 }
1009
Chris Lattnere37e2332006-08-15 04:50:22 +00001010 // GNU attributes support.
1011 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001012 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001013 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001014
1015 // Microsoft declspec support.
1016 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001017 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001018 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001019
Steve Naroff44ac7772008-12-25 14:16:32 +00001020 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001021 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001022 // FIXME: Add handling here!
1023 break;
1024
1025 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001026 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001027 case tok::kw___cdecl:
1028 case tok::kw___stdcall:
1029 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001030 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1031 continue;
1032
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001033 // storage-class-specifier
1034 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001035 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1036 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001037 break;
1038 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001039 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001040 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001041 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1042 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001043 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001044 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001045 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001046 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001047 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001048 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001049 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001050 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001051 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1052 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001053 break;
1054 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001055 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001056 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1057 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001058 else
John McCall49bfce42009-08-03 20:12:06 +00001059 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1060 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001061 break;
1062 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001063 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1064 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001065 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001066 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001067 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1068 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001069 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001070 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001071 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001072 break;
Mike Stump11289f42009-09-09 15:08:12 +00001073
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001074 // function-specifier
1075 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001076 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001077 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001078 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001079 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001080 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001081 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001082 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001083 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001084
Anders Carlssoncd8db412009-05-06 04:46:28 +00001085 // friend
1086 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001087 if (DSContext == DSC_class)
1088 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1089 else {
1090 PrevSpec = ""; // not actually used by the diagnostic
1091 DiagID = diag::err_friend_invalid_in_context;
1092 isInvalid = true;
1093 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001094 break;
Mike Stump11289f42009-09-09 15:08:12 +00001095
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001096 // constexpr
1097 case tok::kw_constexpr:
1098 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1099 break;
1100
Chris Lattnere387d9e2009-01-21 19:48:37 +00001101 // type-specifier
1102 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001103 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1104 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001105 break;
1106 case tok::kw_long:
1107 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001108 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1109 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001110 else
John McCall49bfce42009-08-03 20:12:06 +00001111 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1112 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001113 break;
1114 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001115 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1116 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001117 break;
1118 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001119 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1120 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001121 break;
1122 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001123 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1124 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001125 break;
1126 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001127 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1128 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001129 break;
1130 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001131 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1132 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001133 break;
1134 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001135 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1136 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001137 break;
1138 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001139 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1140 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001141 break;
1142 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001143 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1144 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001145 break;
1146 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001147 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1148 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001149 break;
1150 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001151 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1152 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001153 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001154 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001155 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1156 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001157 break;
1158 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001159 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1160 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001161 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001162 case tok::kw_bool:
1163 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001164 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1165 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001166 break;
1167 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001168 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1169 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001170 break;
1171 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001172 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1173 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001174 break;
1175 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001176 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1177 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001178 break;
1179
1180 // class-specifier:
1181 case tok::kw_class:
1182 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001183 case tok::kw_union: {
1184 tok::TokenKind Kind = Tok.getKind();
1185 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001186 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001187 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001188 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001189
1190 // enum-specifier:
1191 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001192 ConsumeToken();
1193 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001194 continue;
1195
1196 // cv-qualifier:
1197 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001198 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1199 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001200 break;
1201 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001202 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1203 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001204 break;
1205 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001206 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1207 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001208 break;
1209
Douglas Gregor333489b2009-03-27 23:10:48 +00001210 // C++ typename-specifier:
1211 case tok::kw_typename:
1212 if (TryAnnotateTypeOrScopeToken())
1213 continue;
1214 break;
1215
Chris Lattnere387d9e2009-01-21 19:48:37 +00001216 // GNU typeof support.
1217 case tok::kw_typeof:
1218 ParseTypeofSpecifier(DS);
1219 continue;
1220
Anders Carlsson74948d02009-06-24 17:47:40 +00001221 case tok::kw_decltype:
1222 ParseDecltypeSpecifier(DS);
1223 continue;
1224
Steve Naroffcfdf6162008-06-05 00:02:44 +00001225 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001226 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001227 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1228 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001229 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001230 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001231
Chris Lattner0974b232008-07-26 00:20:22 +00001232 {
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001233 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001234 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001235 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1236 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1237 LAngleLoc, EndProtoLoc);
1238 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1239 ProtocolLocs.data(), LAngleLoc);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001240 DS.SetRangeEnd(EndProtoLoc);
1241
Chris Lattner6d29c102008-11-18 07:48:38 +00001242 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner3a4e4312009-04-03 18:38:42 +00001243 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001244 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001245 // Need to support trailing type qualifiers (e.g. "id<p> const").
1246 // If a type specifier follows, it will be diagnosed elsewhere.
1247 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001248 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001249 }
John McCall49bfce42009-08-03 20:12:06 +00001250 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001251 if (isInvalid) {
1252 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001253 assert(DiagID);
Chris Lattner6d29c102008-11-18 07:48:38 +00001254 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001255 }
Chris Lattner2e232092008-03-13 06:29:04 +00001256 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001257 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001258 }
1259}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001260
Chris Lattnera448d752009-01-06 06:59:53 +00001261/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001262/// primarily follow the C++ grammar with additions for C99 and GNU,
1263/// which together subsume the C grammar. Note that the C++
1264/// type-specifier also includes the C type-qualifier (for const,
1265/// volatile, and C99 restrict). Returns true if a type-specifier was
1266/// found (and parsed), false otherwise.
1267///
1268/// type-specifier: [C++ 7.1.5]
1269/// simple-type-specifier
1270/// class-specifier
1271/// enum-specifier
1272/// elaborated-type-specifier [TODO]
1273/// cv-qualifier
1274///
1275/// cv-qualifier: [C++ 7.1.5.1]
1276/// 'const'
1277/// 'volatile'
1278/// [C99] 'restrict'
1279///
1280/// simple-type-specifier: [ C++ 7.1.5.2]
1281/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1282/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1283/// 'char'
1284/// 'wchar_t'
1285/// 'bool'
1286/// 'short'
1287/// 'int'
1288/// 'long'
1289/// 'signed'
1290/// 'unsigned'
1291/// 'float'
1292/// 'double'
1293/// 'void'
1294/// [C99] '_Bool'
1295/// [C99] '_Complex'
1296/// [C99] '_Imaginary' // Removed in TC2?
1297/// [GNU] '_Decimal32'
1298/// [GNU] '_Decimal64'
1299/// [GNU] '_Decimal128'
1300/// [GNU] typeof-specifier
1301/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1302/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001303/// [C++0x] 'decltype' ( expression )
John McCall49bfce42009-08-03 20:12:06 +00001304bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001305 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001306 unsigned &DiagID,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001307 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001308 SourceLocation Loc = Tok.getLocation();
1309
1310 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001311 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001312 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001313 // Annotate typenames and C++ scope specifiers. If we get one, just
1314 // recurse to handle whatever we get.
1315 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001316 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1317 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001318 // Otherwise, not a type specifier.
1319 return false;
1320 case tok::coloncolon: // ::foo::bar
1321 if (NextToken().is(tok::kw_new) || // ::new
1322 NextToken().is(tok::kw_delete)) // ::delete
1323 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001324
Chris Lattner020bab92009-01-04 23:41:41 +00001325 // Annotate typenames and C++ scope specifiers. If we get one, just
1326 // recurse to handle whatever we get.
1327 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001328 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1329 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001330 // Otherwise, not a type specifier.
1331 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001332
Douglas Gregor450c75a2008-11-07 15:42:26 +00001333 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001334 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001335 if (Tok.getAnnotationValue())
1336 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001337 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001338 else
1339 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001340 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1341 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001342
Douglas Gregor450c75a2008-11-07 15:42:26 +00001343 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1344 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1345 // Objective-C interface. If we don't have Objective-C or a '<', this is
1346 // just a normal reference to a typedef name.
1347 if (!Tok.is(tok::less) || !getLang().ObjC1)
1348 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001349
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001350 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001351 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001352 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1353 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1354 LAngleLoc, EndProtoLoc);
1355 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1356 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001357
Douglas Gregor450c75a2008-11-07 15:42:26 +00001358 DS.SetRangeEnd(EndProtoLoc);
1359 return true;
1360 }
1361
1362 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001363 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001364 break;
1365 case tok::kw_long:
1366 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001367 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1368 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001369 else
John McCall49bfce42009-08-03 20:12:06 +00001370 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1371 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001372 break;
1373 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001374 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001375 break;
1376 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001377 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1378 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001379 break;
1380 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001381 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1382 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001383 break;
1384 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001385 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1386 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001387 break;
1388 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001389 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001390 break;
1391 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001392 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001393 break;
1394 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001395 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001396 break;
1397 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001398 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001399 break;
1400 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001401 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001402 break;
1403 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001404 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001405 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001406 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001407 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001408 break;
1409 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001410 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001411 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001412 case tok::kw_bool:
1413 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001414 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001415 break;
1416 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001417 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1418 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001419 break;
1420 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001421 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1422 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001423 break;
1424 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001425 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1426 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001427 break;
1428
1429 // class-specifier:
1430 case tok::kw_class:
1431 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001432 case tok::kw_union: {
1433 tok::TokenKind Kind = Tok.getKind();
1434 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001435 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001436 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001437 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001438
1439 // enum-specifier:
1440 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001441 ConsumeToken();
1442 ParseEnumSpecifier(Loc, DS);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001443 return true;
1444
1445 // cv-qualifier:
1446 case tok::kw_const:
1447 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001448 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001449 break;
1450 case tok::kw_volatile:
1451 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001452 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001453 break;
1454 case tok::kw_restrict:
1455 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001456 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001457 break;
1458
1459 // GNU typeof support.
1460 case tok::kw_typeof:
1461 ParseTypeofSpecifier(DS);
1462 return true;
1463
Anders Carlsson74948d02009-06-24 17:47:40 +00001464 // C++0x decltype support.
1465 case tok::kw_decltype:
1466 ParseDecltypeSpecifier(DS);
1467 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001468
Anders Carlssonbae27372009-06-26 23:44:14 +00001469 // C++0x auto support.
1470 case tok::kw_auto:
1471 if (!getLang().CPlusPlus0x)
1472 return false;
1473
John McCall49bfce42009-08-03 20:12:06 +00001474 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001475 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001476 case tok::kw___ptr64:
1477 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001478 case tok::kw___cdecl:
1479 case tok::kw___stdcall:
1480 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001481 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001482 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001483
Douglas Gregor450c75a2008-11-07 15:42:26 +00001484 default:
1485 // Not a type-specifier; do nothing.
1486 return false;
1487 }
1488
1489 // If the specifier combination wasn't legal, issue a diagnostic.
1490 if (isInvalid) {
1491 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001492 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001493 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001494 }
1495 DS.SetRangeEnd(Tok.getLocation());
1496 ConsumeToken(); // whatever we parsed above.
1497 return true;
1498}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001499
Chris Lattner70ae4912007-10-29 04:42:53 +00001500/// ParseStructDeclaration - Parse a struct declaration without the terminating
1501/// semicolon.
1502///
Chris Lattner90a26b02007-01-23 04:38:16 +00001503/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001504/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001505/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001506/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001507/// struct-declarator-list:
1508/// struct-declarator
1509/// struct-declarator-list ',' struct-declarator
1510/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1511/// struct-declarator:
1512/// declarator
1513/// [GNU] declarator attributes[opt]
1514/// declarator[opt] ':' constant-expression
1515/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1516///
Chris Lattnera12405b2008-04-10 06:46:29 +00001517void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001518ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001519 if (Tok.is(tok::kw___extension__)) {
1520 // __extension__ silences extension warnings in the subexpression.
1521 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001522 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001523 return ParseStructDeclaration(DS, Fields);
1524 }
Mike Stump11289f42009-09-09 15:08:12 +00001525
Steve Naroff97170802007-08-20 22:28:22 +00001526 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001527 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001528 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001529
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001530 // If there are no declarators, this is a free-standing declaration
1531 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001532 if (Tok.is(tok::semi)) {
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001533 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001534 return;
1535 }
1536
1537 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001538 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001539 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001540 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001541 FieldDeclarator DeclaratorInfo(DS);
1542
1543 // Attributes are only allowed here on successive declarators.
1544 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1545 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001546 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001547 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1548 }
Mike Stump11289f42009-09-09 15:08:12 +00001549
Steve Naroff97170802007-08-20 22:28:22 +00001550 /// struct-declarator: declarator
1551 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001552 if (Tok.isNot(tok::colon)) {
1553 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1554 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001555 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001556 }
Mike Stump11289f42009-09-09 15:08:12 +00001557
Chris Lattner76c72282007-10-09 17:33:22 +00001558 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001559 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001560 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001561 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001562 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001563 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001564 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001565 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001566
Steve Naroff97170802007-08-20 22:28:22 +00001567 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001568 if (Tok.is(tok::kw___attribute)) {
1569 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001570 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001571 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1572 }
1573
John McCallcfefb6d2009-11-03 02:38:08 +00001574 // We're done with this declarator; invoke the callback.
John McCall28a6aea2009-11-04 02:18:39 +00001575 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1576 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001577
Steve Naroff97170802007-08-20 22:28:22 +00001578 // If we don't have a comma, it is either the end of the list (a ';')
1579 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001580 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001581 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001582
Steve Naroff97170802007-08-20 22:28:22 +00001583 // Consume the comma.
1584 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001585
John McCallcfefb6d2009-11-03 02:38:08 +00001586 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001587 }
Steve Naroff97170802007-08-20 22:28:22 +00001588}
1589
1590/// ParseStructUnionBody
1591/// struct-contents:
1592/// struct-declaration-list
1593/// [EXT] empty
1594/// [GNU] "struct-declaration-list" without terminatoring ';'
1595/// struct-declaration-list:
1596/// struct-declaration
1597/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001598/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001599///
Chris Lattner1300fb92007-01-23 23:42:53 +00001600void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001601 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001602 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1603 PP.getSourceManager(),
1604 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001605
Chris Lattner90a26b02007-01-23 04:38:16 +00001606 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001607
Douglas Gregor658b9552009-01-09 22:42:13 +00001608 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001609 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1610
Chris Lattner7b9ace62007-01-23 20:11:08 +00001611 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1612 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001613 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001614 Diag(Tok, diag::ext_empty_struct_union_enum)
1615 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001616
Chris Lattner83f095c2009-03-28 19:18:32 +00001617 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001618
Chris Lattner7b9ace62007-01-23 20:11:08 +00001619 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001620 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001621 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001622
Chris Lattner736ed5d2007-06-09 05:59:07 +00001623 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001624 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001625 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001626 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001627 ConsumeToken();
1628 continue;
1629 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001630
1631 // Parse all the comma separated declarators.
1632 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001633
John McCallcfefb6d2009-11-03 02:38:08 +00001634 if (!Tok.is(tok::at)) {
1635 struct CFieldCallback : FieldCallback {
1636 Parser &P;
1637 DeclPtrTy TagDecl;
1638 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1639
1640 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1641 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1642 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1643
1644 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001645 // Install the declarator into the current TagDecl.
John McCall5e6253b2009-11-03 21:13:47 +00001646 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1647 FD.D.getDeclSpec().getSourceRange().getBegin(),
1648 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001649 FieldDecls.push_back(Field);
1650 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001651 }
John McCallcfefb6d2009-11-03 02:38:08 +00001652 } Callback(*this, TagDecl, FieldDecls);
1653
1654 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001655 } else { // Handle @defs
1656 ConsumeToken();
1657 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1658 Diag(Tok, diag::err_unexpected_at);
1659 SkipUntil(tok::semi, true, true);
1660 continue;
1661 }
1662 ConsumeToken();
1663 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1664 if (!Tok.is(tok::identifier)) {
1665 Diag(Tok, diag::err_expected_ident);
1666 SkipUntil(tok::semi, true, true);
1667 continue;
1668 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001669 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump11289f42009-09-09 15:08:12 +00001670 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001671 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001672 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1673 ConsumeToken();
1674 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001675 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001676
Chris Lattner76c72282007-10-09 17:33:22 +00001677 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001678 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001679 } else if (Tok.is(tok::r_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001680 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001681 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001682 } else {
1683 Diag(Tok, diag::err_expected_semi_decl_list);
1684 // Skip to end of block or statement
1685 SkipUntil(tok::r_brace, true, true);
1686 }
1687 }
Mike Stump11289f42009-09-09 15:08:12 +00001688
Steve Naroff33a1e802007-10-29 21:38:07 +00001689 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001690
Steve Naroffb8371e12007-06-09 03:39:29 +00001691 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +00001692 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001693 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +00001694 AttrList = ParseGNUAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +00001695
1696 Actions.ActOnFields(CurScope,
Jay Foad7d0479f2009-05-21 09:52:38 +00001697 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001698 LBraceLoc, RBraceLoc,
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001699 AttrList);
1700 StructScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001701 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001702}
1703
1704
Chris Lattner3b561a32006-08-13 00:12:11 +00001705/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001706/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001707/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001708///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001709/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1710/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001711/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001712/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001713///
1714/// [C++] elaborated-type-specifier:
1715/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1716///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001717void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1718 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001719 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001720 if (Tok.is(tok::code_completion)) {
1721 // Code completion for an enum name.
1722 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1723 ConsumeToken();
1724 }
1725
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001726 AttributeList *Attr = 0;
1727 // If attributes exist after tag, parse them.
1728 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +00001729 Attr = ParseGNUAttributes();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001730
1731 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001732 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001733 if (Tok.isNot(tok::identifier)) {
1734 Diag(Tok, diag::err_expected_ident);
1735 if (Tok.isNot(tok::l_brace)) {
1736 // Has no name and is not a definition.
1737 // Skip the rest of this declarator, up until the comma or semicolon.
1738 SkipUntil(tok::comma, true);
1739 return;
1740 }
1741 }
1742 }
Mike Stump11289f42009-09-09 15:08:12 +00001743
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001744 // Must have either 'enum name' or 'enum {...}'.
1745 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1746 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001747
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001748 // Skip the rest of this declarator, up until the comma or semicolon.
1749 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001750 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001751 }
Mike Stump11289f42009-09-09 15:08:12 +00001752
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001753 // If an identifier is present, consume and remember it.
1754 IdentifierInfo *Name = 0;
1755 SourceLocation NameLoc;
1756 if (Tok.is(tok::identifier)) {
1757 Name = Tok.getIdentifierInfo();
1758 NameLoc = ConsumeToken();
1759 }
Mike Stump11289f42009-09-09 15:08:12 +00001760
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001761 // There are three options here. If we have 'enum foo;', then this is a
1762 // forward declaration. If we have 'enum foo {...' then this is a
1763 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1764 //
1765 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1766 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1767 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1768 //
John McCall9bb74a52009-07-31 02:45:11 +00001769 Action::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001770 if (Tok.is(tok::l_brace))
John McCall9bb74a52009-07-31 02:45:11 +00001771 TUK = Action::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001772 else if (Tok.is(tok::semi))
John McCall9bb74a52009-07-31 02:45:11 +00001773 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001774 else
John McCall9bb74a52009-07-31 02:45:11 +00001775 TUK = Action::TUK_Reference;
Douglas Gregord6ab8742009-05-28 23:31:59 +00001776 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001777 bool IsDependent = false;
John McCall9bb74a52009-07-31 02:45:11 +00001778 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001779 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregor27bdf00f2009-07-23 16:36:45 +00001780 Action::MultiTemplateParamsArg(Actions),
John McCall7f41d982009-09-11 04:59:25 +00001781 Owned, IsDependent);
1782 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump11289f42009-09-09 15:08:12 +00001783
Chris Lattner76c72282007-10-09 17:33:22 +00001784 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001785 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001786
Chris Lattner3b561a32006-08-13 00:12:11 +00001787 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +00001788 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001789 unsigned DiagID;
1790 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001791 TagDecl.getAs<void>(), Owned))
John McCall49bfce42009-08-03 20:12:06 +00001792 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00001793}
1794
Chris Lattnerc1915e22007-01-25 07:29:02 +00001795/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1796/// enumerator-list:
1797/// enumerator
1798/// enumerator-list ',' enumerator
1799/// enumerator:
1800/// enumeration-constant
1801/// enumeration-constant '=' constant-expression
1802/// enumeration-constant:
1803/// identifier
1804///
Chris Lattner83f095c2009-03-28 19:18:32 +00001805void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001806 // Enter the scope of the enum body and start the definition.
1807 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001808 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00001809
Chris Lattnerc1915e22007-01-25 07:29:02 +00001810 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001811
Chris Lattner37256fb2007-08-27 17:24:30 +00001812 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00001813 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001814 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump11289f42009-09-09 15:08:12 +00001815
Chris Lattner83f095c2009-03-28 19:18:32 +00001816 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001817
Chris Lattner83f095c2009-03-28 19:18:32 +00001818 DeclPtrTy LastEnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001819
Chris Lattnerc1915e22007-01-25 07:29:02 +00001820 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001821 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001822 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1823 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001824
Chris Lattnerc1915e22007-01-25 07:29:02 +00001825 SourceLocation EqualLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001826 OwningExprResult AssignedVal(Actions);
Chris Lattner76c72282007-10-09 17:33:22 +00001827 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001828 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001829 AssignedVal = ParseConstantExpression();
1830 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00001831 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001832 }
Mike Stump11289f42009-09-09 15:08:12 +00001833
Chris Lattnerc1915e22007-01-25 07:29:02 +00001834 // Install the enumerator constant into EnumDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00001835 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1836 LastEnumConstDecl,
1837 IdentLoc, Ident,
1838 EqualLoc,
1839 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00001840 EnumConstantDecls.push_back(EnumConstDecl);
1841 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001842
Chris Lattner76c72282007-10-09 17:33:22 +00001843 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001844 break;
1845 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001846
1847 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00001848 !(getLang().C99 || getLang().CPlusPlus0x))
1849 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1850 << getLang().CPlusPlus
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001851 << CodeModificationHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001852 }
Mike Stump11289f42009-09-09 15:08:12 +00001853
Chris Lattnerc1915e22007-01-25 07:29:02 +00001854 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00001855 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001856
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001857 AttributeList *Attr = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001858 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001859 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +00001860 Attr = ParseGNUAttributes(); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001861
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001862 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1863 EnumConstantDecls.data(), EnumConstantDecls.size(),
1864 CurScope, Attr);
Mike Stump11289f42009-09-09 15:08:12 +00001865
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001866 EnumScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001867 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001868}
Chris Lattner3b561a32006-08-13 00:12:11 +00001869
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001870/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001871/// start of a type-qualifier-list.
1872bool Parser::isTypeQualifier() const {
1873 switch (Tok.getKind()) {
1874 default: return false;
1875 // type-qualifier
1876 case tok::kw_const:
1877 case tok::kw_volatile:
1878 case tok::kw_restrict:
1879 return true;
1880 }
1881}
1882
1883/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001884/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001885bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001886 switch (Tok.getKind()) {
1887 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001888
Chris Lattner020bab92009-01-04 23:41:41 +00001889 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001890 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001891 // Annotate typenames and C++ scope specifiers. If we get one, just
1892 // recurse to handle whatever we get.
1893 if (TryAnnotateTypeOrScopeToken())
1894 return isTypeSpecifierQualifier();
1895 // Otherwise, not a type specifier.
1896 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001897
Chris Lattner020bab92009-01-04 23:41:41 +00001898 case tok::coloncolon: // ::foo::bar
1899 if (NextToken().is(tok::kw_new) || // ::new
1900 NextToken().is(tok::kw_delete)) // ::delete
1901 return false;
1902
1903 // Annotate typenames and C++ scope specifiers. If we get one, just
1904 // recurse to handle whatever we get.
1905 if (TryAnnotateTypeOrScopeToken())
1906 return isTypeSpecifierQualifier();
1907 // Otherwise, not a type specifier.
1908 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001909
Chris Lattnere37e2332006-08-15 04:50:22 +00001910 // GNU attributes support.
1911 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00001912 // GNU typeof support.
1913 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00001914
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001915 // type-specifiers
1916 case tok::kw_short:
1917 case tok::kw_long:
1918 case tok::kw_signed:
1919 case tok::kw_unsigned:
1920 case tok::kw__Complex:
1921 case tok::kw__Imaginary:
1922 case tok::kw_void:
1923 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001924 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001925 case tok::kw_char16_t:
1926 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001927 case tok::kw_int:
1928 case tok::kw_float:
1929 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001930 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001931 case tok::kw__Bool:
1932 case tok::kw__Decimal32:
1933 case tok::kw__Decimal64:
1934 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00001935
Chris Lattner861a2262008-04-13 18:59:07 +00001936 // struct-or-union-specifier (C99) or class-specifier (C++)
1937 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001938 case tok::kw_struct:
1939 case tok::kw_union:
1940 // enum-specifier
1941 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00001942
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001943 // type-qualifier
1944 case tok::kw_const:
1945 case tok::kw_volatile:
1946 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001947
1948 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001949 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001950 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001951
Chris Lattner409bf7d2008-10-20 00:25:30 +00001952 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1953 case tok::less:
1954 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00001955
Steve Naroff44ac7772008-12-25 14:16:32 +00001956 case tok::kw___cdecl:
1957 case tok::kw___stdcall:
1958 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001959 case tok::kw___w64:
1960 case tok::kw___ptr64:
1961 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001962 }
1963}
1964
Chris Lattneracd58a32006-08-06 17:24:14 +00001965/// isDeclarationSpecifier() - Return true if the current token is part of a
1966/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001967bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00001968 switch (Tok.getKind()) {
1969 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001970
Chris Lattner020bab92009-01-04 23:41:41 +00001971 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00001972 // Unfortunate hack to support "Class.factoryMethod" notation.
1973 if (getLang().ObjC1 && NextToken().is(tok::period))
1974 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001975 // Fall through
Steve Naroff9527bbf2009-03-09 21:12:44 +00001976
Douglas Gregor333489b2009-03-27 23:10:48 +00001977 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001978 // Annotate typenames and C++ scope specifiers. If we get one, just
1979 // recurse to handle whatever we get.
1980 if (TryAnnotateTypeOrScopeToken())
1981 return isDeclarationSpecifier();
1982 // Otherwise, not a declaration specifier.
1983 return false;
1984 case tok::coloncolon: // ::foo::bar
1985 if (NextToken().is(tok::kw_new) || // ::new
1986 NextToken().is(tok::kw_delete)) // ::delete
1987 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001988
Chris Lattner020bab92009-01-04 23:41:41 +00001989 // Annotate typenames and C++ scope specifiers. If we get one, just
1990 // recurse to handle whatever we get.
1991 if (TryAnnotateTypeOrScopeToken())
1992 return isDeclarationSpecifier();
1993 // Otherwise, not a declaration specifier.
1994 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001995
Chris Lattneracd58a32006-08-06 17:24:14 +00001996 // storage-class-specifier
1997 case tok::kw_typedef:
1998 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00001999 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00002000 case tok::kw_static:
2001 case tok::kw_auto:
2002 case tok::kw_register:
2003 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002004
Chris Lattneracd58a32006-08-06 17:24:14 +00002005 // type-specifiers
2006 case tok::kw_short:
2007 case tok::kw_long:
2008 case tok::kw_signed:
2009 case tok::kw_unsigned:
2010 case tok::kw__Complex:
2011 case tok::kw__Imaginary:
2012 case tok::kw_void:
2013 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002014 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002015 case tok::kw_char16_t:
2016 case tok::kw_char32_t:
2017
Chris Lattneracd58a32006-08-06 17:24:14 +00002018 case tok::kw_int:
2019 case tok::kw_float:
2020 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002021 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002022 case tok::kw__Bool:
2023 case tok::kw__Decimal32:
2024 case tok::kw__Decimal64:
2025 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00002026
Chris Lattner861a2262008-04-13 18:59:07 +00002027 // struct-or-union-specifier (C99) or class-specifier (C++)
2028 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002029 case tok::kw_struct:
2030 case tok::kw_union:
2031 // enum-specifier
2032 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002033
Chris Lattneracd58a32006-08-06 17:24:14 +00002034 // type-qualifier
2035 case tok::kw_const:
2036 case tok::kw_volatile:
2037 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002038
Chris Lattneracd58a32006-08-06 17:24:14 +00002039 // function-specifier
2040 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002041 case tok::kw_virtual:
2042 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002043
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002044 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002045 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002046
Chris Lattner599e47e2007-08-09 17:01:07 +00002047 // GNU typeof support.
2048 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002049
Chris Lattner599e47e2007-08-09 17:01:07 +00002050 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002051 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002052 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002053
Chris Lattner8b2ec162008-07-26 03:38:44 +00002054 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2055 case tok::less:
2056 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002057
Steve Narofff192fab2009-01-06 19:34:12 +00002058 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002059 case tok::kw___cdecl:
2060 case tok::kw___stdcall:
2061 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00002062 case tok::kw___w64:
2063 case tok::kw___ptr64:
2064 case tok::kw___forceinline:
2065 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002066 }
2067}
2068
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002069
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002070/// ParseTypeQualifierListOpt
2071/// type-qualifier-list: [C99 6.7.5]
2072/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002073/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002074/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002075/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Alexis Hunt96d5c762009-11-21 08:43:09 +00002076/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2077/// if CXX0XAttributesAllowed = true
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002078///
Alexis Hunt96d5c762009-11-21 08:43:09 +00002079void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2080 bool CXX0XAttributesAllowed) {
2081 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2082 SourceLocation Loc = Tok.getLocation();
2083 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2084 if (CXX0XAttributesAllowed)
2085 DS.AddAttributes(Attr.AttrList);
2086 else
2087 Diag(Loc, diag::err_attributes_not_allowed);
2088 }
2089
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002090 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002091 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002092 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002093 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002094 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002095
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002096 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002097 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002098 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2099 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002100 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002101 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002102 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2103 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002104 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002105 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002106 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2107 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002108 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002109 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002110 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002111 case tok::kw___cdecl:
2112 case tok::kw___stdcall:
2113 case tok::kw___fastcall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002114 if (GNUAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002115 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2116 continue;
2117 }
2118 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002119 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002120 if (GNUAttributesAllowed) {
2121 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002122 continue; // do *not* consume the next token!
2123 }
2124 // otherwise, FALL THROUGH!
2125 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002126 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002127 // If this is not a type-qualifier token, we're done reading type
2128 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002129 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002130 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002131 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002132
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002133 // If the specifier combination wasn't legal, issue a diagnostic.
2134 if (isInvalid) {
2135 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002136 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002137 }
2138 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002139 }
2140}
2141
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002142
2143/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2144///
2145void Parser::ParseDeclarator(Declarator &D) {
2146 /// This implements the 'declarator' production in the C grammar, then checks
2147 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002148 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002149}
2150
Sebastian Redlbd150f42008-11-21 19:14:01 +00002151/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2152/// is parsed by the function passed to it. Pass null, and the direct-declarator
2153/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002154/// ptr-operator production.
2155///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002156/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2157/// [C] pointer[opt] direct-declarator
2158/// [C++] direct-declarator
2159/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002160///
2161/// pointer: [C99 6.7.5]
2162/// '*' type-qualifier-list[opt]
2163/// '*' type-qualifier-list[opt] pointer
2164///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002165/// ptr-operator:
2166/// '*' cv-qualifier-seq[opt]
2167/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002168/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002169/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002170/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002171/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002172void Parser::ParseDeclaratorInternal(Declarator &D,
2173 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002174 if (Diags.hasAllExtensionsSilenced())
2175 D.setExtension();
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002176 // C++ member pointers start with a '::' or a nested-name.
2177 // Member pointers get special handling, since there's no place for the
2178 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002179 if (getLang().CPlusPlus &&
2180 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2181 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002182 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00002183 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump11289f42009-09-09 15:08:12 +00002184 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002185 // The scope spec really belongs to the direct-declarator.
2186 D.getCXXScopeSpec() = SS;
2187 if (DirectDeclParser)
2188 (this->*DirectDeclParser)(D);
2189 return;
2190 }
2191
2192 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002193 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002194 DeclSpec DS;
2195 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002196 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002197
2198 // Recurse to parse whatever is left.
2199 ParseDeclaratorInternal(D, DirectDeclParser);
2200
2201 // Sema will have to catch (syntactically invalid) pointers into global
2202 // scope. It has to catch pointers into namespace scope anyway.
2203 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002204 Loc, DS.TakeAttributes()),
2205 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002206 return;
2207 }
2208 }
2209
2210 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002211 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002212 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002213 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002214 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002215 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002216 if (DirectDeclParser)
2217 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002218 return;
2219 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002220
Sebastian Redled0f3b02009-03-15 22:02:01 +00002221 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2222 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002223 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002224 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002225
Chris Lattner9eac9312009-03-27 04:18:06 +00002226 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002227 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002228 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002229
Bill Wendling3708c182007-05-27 10:15:43 +00002230 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002231 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002232
Bill Wendling3708c182007-05-27 10:15:43 +00002233 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002234 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002235 if (Kind == tok::star)
2236 // Remember that we parsed a pointer type, and remember the type-quals.
2237 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002238 DS.TakeAttributes()),
2239 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002240 else
2241 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002242 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002243 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002244 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002245 } else {
2246 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002247 DeclSpec DS;
2248
Sebastian Redl3b27be62009-03-23 00:00:23 +00002249 // Complain about rvalue references in C++03, but then go on and build
2250 // the declarator.
2251 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2252 Diag(Loc, diag::err_rvalue_reference);
2253
Bill Wendling93efb222007-06-02 23:28:54 +00002254 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2255 // cv-qualifiers are introduced through the use of a typedef or of a
2256 // template type argument, in which case the cv-qualifiers are ignored.
2257 //
2258 // [GNU] Retricted references are allowed.
2259 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002260 // [C++0x] Attributes on references are not allowed.
2261 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002262 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002263
2264 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2265 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2266 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002267 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002268 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2269 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002270 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002271 }
Bill Wendling3708c182007-05-27 10:15:43 +00002272
2273 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002274 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002275
Douglas Gregor66583c52008-11-03 15:51:28 +00002276 if (D.getNumTypeObjects() > 0) {
2277 // C++ [dcl.ref]p4: There shall be no references to references.
2278 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2279 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002280 if (const IdentifierInfo *II = D.getIdentifier())
2281 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2282 << II;
2283 else
2284 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2285 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002286
Sebastian Redlbd150f42008-11-21 19:14:01 +00002287 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002288 // can go ahead and build the (technically ill-formed)
2289 // declarator: reference collapsing will take care of it.
2290 }
2291 }
2292
Bill Wendling3708c182007-05-27 10:15:43 +00002293 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002294 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002295 DS.TakeAttributes(),
2296 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002297 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002298 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002299}
2300
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002301/// ParseDirectDeclarator
2302/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002303/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002304/// '(' declarator ')'
2305/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002306/// [C90] direct-declarator '[' constant-expression[opt] ']'
2307/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2308/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2309/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2310/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002311/// direct-declarator '(' parameter-type-list ')'
2312/// direct-declarator '(' identifier-list[opt] ')'
2313/// [GNU] direct-declarator '(' parameter-forward-declarations
2314/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002315/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2316/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002317/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002318///
2319/// declarator-id: [C++ 8]
2320/// id-expression
2321/// '::'[opt] nested-name-specifier[opt] type-name
2322///
2323/// id-expression: [C++ 5.1]
2324/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002325/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002326///
2327/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002328/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002329/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002330/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002331/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002332/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002333///
Chris Lattneracd58a32006-08-06 17:24:14 +00002334void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002335 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002336
Douglas Gregor7861a802009-11-03 01:35:08 +00002337 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2338 // ParseDeclaratorInternal might already have parsed the scope.
2339 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2340 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2341 true);
2342 if (afterCXXScope) {
John McCall2b058ef2009-12-11 20:04:54 +00002343 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2344 // Change the declaration context for name lookup, until this function
2345 // is exited (and the declarator has been parsed).
2346 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor7861a802009-11-03 01:35:08 +00002347 }
2348
2349 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2350 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2351 // We found something that indicates the start of an unqualified-id.
2352 // Parse that unqualified-id.
2353 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2354 /*EnteringContext=*/true,
2355 /*AllowDestructorName=*/true,
2356 /*AllowConstructorName=*/!D.getDeclSpec().hasTypeSpecifier(),
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002357 /*ObjectType=*/0,
Douglas Gregor7861a802009-11-03 01:35:08 +00002358 D.getName())) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002359 D.SetIdentifier(0, Tok.getLocation());
2360 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002361 } else {
2362 // Parsed the unqualified-id; update range information and move along.
2363 if (D.getSourceRange().getBegin().isInvalid())
2364 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2365 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002366 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002367 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002368 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002369 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002370 assert(!getLang().CPlusPlus &&
2371 "There's a C++-specific check for tok::identifier above");
2372 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2373 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2374 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002375 goto PastIdentifier;
2376 }
2377
2378 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002379 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002380 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002381 // Example: 'char (*X)' or 'int (*XX)(void)'
2382 ParseParenDeclarator(D);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002383 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002384 // This could be something simple like "int" (in which case the declarator
2385 // portion is empty), if an abstract-declarator is allowed.
2386 D.SetIdentifier(0, Tok.getLocation());
2387 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002388 if (D.getContext() == Declarator::MemberContext)
2389 Diag(Tok, diag::err_expected_member_name_or_semi)
2390 << D.getDeclSpec().getSourceRange();
2391 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002392 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002393 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002394 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002395 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002396 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002397 }
Mike Stump11289f42009-09-09 15:08:12 +00002398
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002399 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002400 assert(D.isPastIdentifier() &&
2401 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002402
Alexis Hunt96d5c762009-11-21 08:43:09 +00002403 // Don't parse attributes unless we have an identifier.
2404 if (D.getIdentifier() && getLang().CPlusPlus
2405 && isCXX0XAttributeSpecifier(true)) {
2406 SourceLocation AttrEndLoc;
2407 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2408 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2409 }
2410
Chris Lattneracd58a32006-08-06 17:24:14 +00002411 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002412 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002413 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2414 // In such a case, check if we actually have a function declarator; if it
2415 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002416 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2417 // When not in file scope, warn for ambiguous function declarators, just
2418 // in case the author intended it as a variable definition.
2419 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2420 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2421 break;
2422 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002423 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002424 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002425 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002426 } else {
2427 break;
2428 }
2429 }
2430}
2431
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002432/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2433/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002434/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002435/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2436///
2437/// direct-declarator:
2438/// '(' declarator ')'
2439/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002440/// direct-declarator '(' parameter-type-list ')'
2441/// direct-declarator '(' identifier-list[opt] ')'
2442/// [GNU] direct-declarator '(' parameter-forward-declarations
2443/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002444///
2445void Parser::ParseParenDeclarator(Declarator &D) {
2446 SourceLocation StartLoc = ConsumeParen();
2447 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002448
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002449 // Eat any attributes before we look at whether this is a grouping or function
2450 // declarator paren. If this is a grouping paren, the attribute applies to
2451 // the type being built up, for example:
2452 // int (__attribute__(()) *x)(long y)
2453 // If this ends up not being a grouping paren, the attribute applies to the
2454 // first argument, for example:
2455 // int (__attribute__(()) int x)
2456 // In either case, we need to eat any attributes to be able to determine what
2457 // sort of paren this is.
2458 //
2459 AttributeList *AttrList = 0;
2460 bool RequiresArg = false;
2461 if (Tok.is(tok::kw___attribute)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002462 AttrList = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +00002463
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002464 // We require that the argument list (if this is a non-grouping paren) be
2465 // present even if the attribute list was empty.
2466 RequiresArg = true;
2467 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002468 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002469 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2470 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2471 Tok.is(tok::kw___ptr64)) {
2472 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2473 }
Mike Stump11289f42009-09-09 15:08:12 +00002474
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002475 // If we haven't past the identifier yet (or where the identifier would be
2476 // stored, if this is an abstract declarator), then this is probably just
2477 // grouping parens. However, if this could be an abstract-declarator, then
2478 // this could also be the start of function arguments (consider 'void()').
2479 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002480
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002481 if (!D.mayOmitIdentifier()) {
2482 // If this can't be an abstract-declarator, this *must* be a grouping
2483 // paren, because we haven't seen the identifier yet.
2484 isGrouping = true;
2485 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002486 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002487 isDeclarationSpecifier()) { // 'int(int)' is a function.
2488 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2489 // considered to be a type, not a K&R identifier-list.
2490 isGrouping = false;
2491 } else {
2492 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2493 isGrouping = true;
2494 }
Mike Stump11289f42009-09-09 15:08:12 +00002495
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002496 // If this is a grouping paren, handle:
2497 // direct-declarator: '(' declarator ')'
2498 // direct-declarator: '(' attributes declarator ')'
2499 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002500 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002501 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002502 if (AttrList)
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002503 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002504
Sebastian Redlbd150f42008-11-21 19:14:01 +00002505 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002506 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002507 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002508
2509 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002510 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002511 return;
2512 }
Mike Stump11289f42009-09-09 15:08:12 +00002513
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002514 // Okay, if this wasn't a grouping paren, it must be the start of a function
2515 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002516 // identifier (and remember where it would have been), then call into
2517 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002518 D.SetIdentifier(0, Tok.getLocation());
2519
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002520 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002521}
2522
2523/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2524/// declarator D up to a paren, which indicates that we are parsing function
2525/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002526///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002527/// If AttrList is non-null, then the caller parsed those arguments immediately
2528/// after the open paren - they should be considered to be the first argument of
2529/// a parameter. If RequiresArg is true, then the first argument of the
2530/// function is required to be present and required to not be an identifier
2531/// list.
2532///
Chris Lattneracd58a32006-08-06 17:24:14 +00002533/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002534/// parameter-type-list: [C99 6.7.5]
2535/// parameter-list
2536/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002537/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002538///
2539/// parameter-list: [C99 6.7.5]
2540/// parameter-declaration
2541/// parameter-list ',' parameter-declaration
2542///
2543/// parameter-declaration: [C99 6.7.5]
2544/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002545/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002546/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002547/// declaration-specifiers abstract-declarator[opt]
2548/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002549/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002550/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002551///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002552/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002553/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002554///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002555void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2556 AttributeList *AttrList,
2557 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002558 // lparen is already consumed!
2559 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002560
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002561 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002562 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002563 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002564 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002565 delete AttrList;
2566 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002567
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002568 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2569 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002570
2571 // cv-qualifier-seq[opt].
2572 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002573 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002574 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002575 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002576 llvm::SmallVector<TypeTy*, 2> Exceptions;
2577 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002578 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002579 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002580 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002581 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002582
2583 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002584 if (Tok.is(tok::kw_throw)) {
2585 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002586 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002587 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002588 hasAnyExceptionSpec);
2589 assert(Exceptions.size() == ExceptionRanges.size() &&
2590 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002591 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002592 }
2593
Chris Lattner371ed4e2008-04-06 06:57:35 +00002594 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002595 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002596 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002597 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002598 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002599 /*arglist*/ 0, 0,
2600 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002601 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002602 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002603 Exceptions.data(),
2604 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002605 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002606 LParenLoc, RParenLoc, D),
2607 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002608 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002609 }
2610
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002611 // Alternatively, this parameter list may be an identifier list form for a
2612 // K&R-style function: void foo(a,b,c)
Steve Naroffb0486722009-01-28 19:16:40 +00002613 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff3b6a4bd2009-01-30 14:23:32 +00002614 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002615 // K&R identifier lists can't have typedefs as identifiers, per
2616 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002617 if (RequiresArg) {
2618 Diag(Tok, diag::err_argument_required_after_attribute);
2619 delete AttrList;
2620 }
Steve Naroffb0486722009-01-28 19:16:40 +00002621 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2622 // normal declarators, not for abstract-declarators.
2623 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002624 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002625 }
Mike Stump11289f42009-09-09 15:08:12 +00002626
Chris Lattner371ed4e2008-04-06 06:57:35 +00002627 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00002628
Chris Lattner371ed4e2008-04-06 06:57:35 +00002629 // Build up an array of information about the parsed arguments.
2630 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002631
2632 // Enter function-declaration scope, limiting any declarators to the
2633 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00002634 ParseScope PrototypeScope(this,
2635 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00002636
Chris Lattner371ed4e2008-04-06 06:57:35 +00002637 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002638 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00002639 while (1) {
2640 if (Tok.is(tok::ellipsis)) {
2641 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002642 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002643 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00002644 }
Mike Stump11289f42009-09-09 15:08:12 +00002645
Chris Lattner371ed4e2008-04-06 06:57:35 +00002646 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002647
Chris Lattner371ed4e2008-04-06 06:57:35 +00002648 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00002649 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002650 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002651
2652 // If the caller parsed attributes for the first argument, add them now.
2653 if (AttrList) {
2654 DS.AddAttributes(AttrList);
2655 AttrList = 0; // Only apply the attributes to the first parameter.
2656 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002657 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002658
Chris Lattner371ed4e2008-04-06 06:57:35 +00002659 // Parse the declarator. This is "PrototypeContext", because we must
2660 // accept either 'declarator' or 'abstract-declarator' here.
2661 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2662 ParseDeclarator(ParmDecl);
2663
2664 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002665 if (Tok.is(tok::kw___attribute)) {
2666 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002667 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002668 ParmDecl.AddAttributes(AttrList, Loc);
2669 }
Mike Stump11289f42009-09-09 15:08:12 +00002670
Chris Lattner371ed4e2008-04-06 06:57:35 +00002671 // Remember this parsed parameter in ParamInfo.
2672 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00002673
Douglas Gregor4d87df52008-12-16 21:30:33 +00002674 // DefArgToks is used when the parsing of default arguments needs
2675 // to be delayed.
2676 CachedTokens *DefArgToks = 0;
2677
Chris Lattner371ed4e2008-04-06 06:57:35 +00002678 // If no parameter was specified, verify that *something* was specified,
2679 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002680 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2681 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00002682 // Completely missing, emit error.
2683 Diag(DSStart, diag::err_missing_param);
2684 } else {
2685 // Otherwise, we have something. Add it and let semantic analysis try
2686 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00002687
Chris Lattner371ed4e2008-04-06 06:57:35 +00002688 // Inform the actions module about the parameter declarator, so it gets
2689 // added to the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002690 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002691
2692 // Parse the default argument, if any. We parse the default
2693 // arguments in all dialects; the semantic analysis in
2694 // ActOnParamDefaultArgument will reject the default argument in
2695 // C.
2696 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00002697 SourceLocation EqualLoc = Tok.getLocation();
2698
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002699 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00002700 if (D.getContext() == Declarator::MemberContext) {
2701 // If we're inside a class definition, cache the tokens
2702 // corresponding to the default argument. We'll actually parse
2703 // them when we see the end of the class definition.
2704 // FIXME: Templates will require something similar.
2705 // FIXME: Can we use a smart pointer for Toks?
2706 DefArgToks = new CachedTokens;
2707
Mike Stump11289f42009-09-09 15:08:12 +00002708 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002709 tok::semi, false)) {
2710 delete DefArgToks;
2711 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00002712 Actions.ActOnParamDefaultArgumentError(Param);
2713 } else
Mike Stump11289f42009-09-09 15:08:12 +00002714 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00002715 (*DefArgToks)[1].getLocation());
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002716 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00002717 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00002718 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002719
Douglas Gregor4d87df52008-12-16 21:30:33 +00002720 OwningExprResult DefArgResult(ParseAssignmentExpression());
2721 if (DefArgResult.isInvalid()) {
2722 Actions.ActOnParamDefaultArgumentError(Param);
2723 SkipUntil(tok::comma, tok::r_paren, true, true);
2724 } else {
2725 // Inform the actions module about the default argument
2726 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002727 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00002728 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002729 }
2730 }
Mike Stump11289f42009-09-09 15:08:12 +00002731
2732 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2733 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002734 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00002735 }
2736
2737 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002738 if (Tok.isNot(tok::comma)) {
2739 if (Tok.is(tok::ellipsis)) {
2740 IsVariadic = true;
2741 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2742
2743 if (!getLang().CPlusPlus) {
2744 // We have ellipsis without a preceding ',', which is ill-formed
2745 // in C. Complain and provide the fix.
2746 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2747 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2748 }
2749 }
2750
2751 break;
2752 }
Mike Stump11289f42009-09-09 15:08:12 +00002753
Chris Lattner371ed4e2008-04-06 06:57:35 +00002754 // Consume the comma.
2755 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00002756 }
Mike Stump11289f42009-09-09 15:08:12 +00002757
Chris Lattner371ed4e2008-04-06 06:57:35 +00002758 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002759 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00002760
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002761 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002762 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2763 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002764
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002765 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002766 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002767 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002768 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002769 llvm::SmallVector<TypeTy*, 2> Exceptions;
2770 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002771
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002772 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002773 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00002774 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002775 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002776 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002777
2778 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002779 if (Tok.is(tok::kw_throw)) {
2780 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002781 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002782 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002783 hasAnyExceptionSpec);
2784 assert(Exceptions.size() == ExceptionRanges.size() &&
2785 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002786 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002787 }
2788
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002789 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002790 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002791 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002792 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002793 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002794 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002795 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002796 Exceptions.data(),
2797 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002798 Exceptions.size(),
2799 LParenLoc, RParenLoc, D),
2800 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002801}
Chris Lattneracd58a32006-08-06 17:24:14 +00002802
Chris Lattner6c940e62008-04-06 06:34:08 +00002803/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2804/// we found a K&R-style identifier list instead of a type argument list. The
2805/// current token is known to be the first identifier in the list.
2806///
2807/// identifier-list: [C99 6.7.5]
2808/// identifier
2809/// identifier-list ',' identifier
2810///
2811void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2812 Declarator &D) {
2813 // Build up an array of information about the parsed arguments.
2814 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2815 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00002816
Chris Lattner6c940e62008-04-06 06:34:08 +00002817 // If there was no identifier specified for the declarator, either we are in
2818 // an abstract-declarator, or we are in a parameter declarator which was found
2819 // to be abstract. In abstract-declarators, identifier lists are not valid:
2820 // diagnose this.
2821 if (!D.getIdentifier())
2822 Diag(Tok, diag::ext_ident_list_in_param);
2823
2824 // Tok is known to be the first identifier in the list. Remember this
2825 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00002826 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00002827 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner83f095c2009-03-28 19:18:32 +00002828 Tok.getLocation(),
2829 DeclPtrTy()));
Mike Stump11289f42009-09-09 15:08:12 +00002830
Chris Lattner9186f552008-04-06 06:39:19 +00002831 ConsumeToken(); // eat the first identifier.
Mike Stump11289f42009-09-09 15:08:12 +00002832
Chris Lattner6c940e62008-04-06 06:34:08 +00002833 while (Tok.is(tok::comma)) {
2834 // Eat the comma.
2835 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002836
Chris Lattner9186f552008-04-06 06:39:19 +00002837 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00002838 if (Tok.isNot(tok::identifier)) {
2839 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00002840 SkipUntil(tok::r_paren);
2841 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00002842 }
Chris Lattner67b450c2008-04-06 06:47:48 +00002843
Chris Lattner6c940e62008-04-06 06:34:08 +00002844 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00002845
2846 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00002847 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerebad6a22008-11-19 07:37:42 +00002848 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00002849
Chris Lattner6c940e62008-04-06 06:34:08 +00002850 // Verify that the argument identifier has not already been mentioned.
2851 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002852 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00002853 } else {
2854 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00002855 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00002856 Tok.getLocation(),
2857 DeclPtrTy()));
Chris Lattner9186f552008-04-06 06:39:19 +00002858 }
Mike Stump11289f42009-09-09 15:08:12 +00002859
Chris Lattner6c940e62008-04-06 06:34:08 +00002860 // Eat the identifier.
2861 ConsumeToken();
2862 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002863
2864 // If we have the closing ')', eat it and we're done.
2865 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2866
Chris Lattner9186f552008-04-06 06:39:19 +00002867 // Remember that we parsed a function type, and remember the attributes. This
2868 // function type is always a K&R style function type, which is not varargs and
2869 // has no prototype.
2870 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002871 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00002872 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002873 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002874 /*exception*/false,
2875 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002876 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002877 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00002878}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002879
Chris Lattnere8074e62006-08-06 18:30:15 +00002880/// [C90] direct-declarator '[' constant-expression[opt] ']'
2881/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2882/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2883/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2884/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2885void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00002886 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00002887
Chris Lattner84a11622008-12-18 07:27:21 +00002888 // C array syntax has many features, but by-far the most common is [] and [4].
2889 // This code does a fast path to handle some of the most obvious cases.
2890 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002891 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002892 //FIXME: Use these
2893 CXX0XAttributeList Attr;
2894 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
2895 Attr = ParseCXX0XAttributes();
2896 }
2897
Chris Lattner84a11622008-12-18 07:27:21 +00002898 // Remember that we parsed the empty array type.
2899 OwningExprResult NumElements(Actions);
Douglas Gregor04318252009-07-06 15:59:29 +00002900 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2901 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002902 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002903 return;
2904 } else if (Tok.getKind() == tok::numeric_constant &&
2905 GetLookAheadToken(1).is(tok::r_square)) {
2906 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00002907 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00002908 ConsumeToken();
2909
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002910 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002911 //FIXME: Use these
2912 CXX0XAttributeList Attr;
2913 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2914 Attr = ParseCXX0XAttributes();
2915 }
Chris Lattner84a11622008-12-18 07:27:21 +00002916
2917 // If there was an error parsing the assignment-expression, recover.
2918 if (ExprRes.isInvalid())
2919 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00002920
Chris Lattner84a11622008-12-18 07:27:21 +00002921 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00002922 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2923 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002924 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002925 return;
2926 }
Mike Stump11289f42009-09-09 15:08:12 +00002927
Chris Lattnere8074e62006-08-06 18:30:15 +00002928 // If valid, this location is the position where we read the 'static' keyword.
2929 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00002930 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002931 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002932
Chris Lattnere8074e62006-08-06 18:30:15 +00002933 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002934 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00002935 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00002936 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00002937
Chris Lattnere8074e62006-08-06 18:30:15 +00002938 // If we haven't already read 'static', check to see if there is one after the
2939 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002940 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002941 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002942
Chris Lattnere8074e62006-08-06 18:30:15 +00002943 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00002944 bool isStar = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +00002945 OwningExprResult NumElements(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00002946
Chris Lattner521ff2b2008-04-06 05:26:30 +00002947 // Handle the case where we have '[*]' as the array size. However, a leading
2948 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2949 // the the token after the star is a ']'. Since stars in arrays are
2950 // infrequent, use of lookahead is not costly here.
2951 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00002952 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00002953
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002954 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00002955 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002956 StaticLoc = SourceLocation(); // Drop the static.
2957 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00002958 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00002959 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00002960 // Note, in C89, this production uses the constant-expr production instead
2961 // of assignment-expr. The only difference is that assignment-expr allows
2962 // things like '=' and '*='. Sema rejects these in C89 mode because they
2963 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00002964
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00002965 // Parse the constant-expression or assignment-expression now (depending
2966 // on dialect).
2967 if (getLang().CPlusPlus)
2968 NumElements = ParseConstantExpression();
2969 else
2970 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00002971 }
Mike Stump11289f42009-09-09 15:08:12 +00002972
Chris Lattner62591722006-08-12 18:40:58 +00002973 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002974 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00002975 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00002976 // If the expression was invalid, skip it.
2977 SkipUntil(tok::r_square);
2978 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00002979 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002980
2981 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2982
Alexis Hunt96d5c762009-11-21 08:43:09 +00002983 //FIXME: Use these
2984 CXX0XAttributeList Attr;
2985 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2986 Attr = ParseCXX0XAttributes();
2987 }
2988
Chris Lattner84a11622008-12-18 07:27:21 +00002989 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00002990 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2991 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00002992 NumElements.release(),
2993 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002994 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00002995}
2996
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002997/// [GNU] typeof-specifier:
2998/// typeof ( expressions )
2999/// typeof ( type-name )
3000/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00003001///
3002void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00003003 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003004 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003005 SourceLocation StartLoc = ConsumeToken();
3006
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003007 bool isCastExpr;
3008 TypeTy *CastTy;
3009 SourceRange CastRange;
3010 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3011 isCastExpr,
3012 CastTy,
3013 CastRange);
3014
3015 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003016 // FIXME: Not accurate, the range gets one token more than it should.
3017 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003018 else
3019 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003020
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003021 if (isCastExpr) {
3022 if (!CastTy) {
3023 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003024 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003025 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003026
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003027 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003028 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003029 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3030 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003031 DiagID, CastTy))
3032 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003033 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003034 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003035
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003036 // If we get here, the operand to the typeof was an expresion.
3037 if (Operand.isInvalid()) {
3038 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003039 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003040 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003041
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003042 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003043 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003044 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3045 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003046 DiagID, Operand.release()))
3047 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003048}