blob: 5dd78f7b547ce4271f9335f5d57907bc64cace79 [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 {
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000553 if (getLang().CPlusPlus)
554 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
555
Douglas Gregor23996282009-05-12 21:31:51 +0000556 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +0000557
558 if (getLang().CPlusPlus)
559 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
560
Douglas Gregor23996282009-05-12 21:31:51 +0000561 if (Init.isInvalid()) {
562 SkipUntil(tok::semi, true, true);
563 return DeclPtrTy();
564 }
Anders Carlsson250aada2009-08-16 05:13:48 +0000565 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor23996282009-05-12 21:31:51 +0000566 }
567 } else if (Tok.is(tok::l_paren)) {
568 // Parse C++ direct initializer: '(' expression-list ')'
569 SourceLocation LParenLoc = ConsumeParen();
570 ExprVector Exprs(Actions);
571 CommaLocsTy CommaLocs;
572
573 if (ParseExpressionList(Exprs, CommaLocs)) {
574 SkipUntil(tok::r_paren);
575 } else {
576 // Match the ')'.
577 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
578
579 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
580 "Unexpected number of commas!");
581 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
582 move_arg(Exprs),
Jay Foad7d0479f2009-05-21 09:52:38 +0000583 CommaLocs.data(), RParenLoc);
Douglas Gregor23996282009-05-12 21:31:51 +0000584 }
585 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000586 bool TypeContainsUndeducedAuto =
Anders Carlssonae019932009-07-11 00:34:39 +0000587 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
588 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor23996282009-05-12 21:31:51 +0000589 }
590
591 return ThisDecl;
592}
593
Chris Lattner1890ac82006-08-13 01:16:23 +0000594/// ParseSpecifierQualifierList
595/// specifier-qualifier-list:
596/// type-specifier specifier-qualifier-list[opt]
597/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000598/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +0000599///
600void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
601 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
602 /// parse declaration-specifiers and complain about extra stuff.
Chris Lattner1890ac82006-08-13 01:16:23 +0000603 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000604
Chris Lattner1890ac82006-08-13 01:16:23 +0000605 // Validate declspec for type-name.
606 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnera723ba92009-04-14 21:16:09 +0000607 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
608 !DS.getAttributes())
Chris Lattner1890ac82006-08-13 01:16:23 +0000609 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump11289f42009-09-09 15:08:12 +0000610
Chris Lattner1b22eed2006-11-28 05:12:07 +0000611 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000612 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +0000613 if (DS.getStorageClassSpecLoc().isValid())
614 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
615 else
616 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +0000617 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000618 }
Mike Stump11289f42009-09-09 15:08:12 +0000619
Chris Lattner1b22eed2006-11-28 05:12:07 +0000620 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +0000621 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +0000622 if (DS.isInlineSpecified())
623 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
624 if (DS.isVirtualSpecified())
625 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
626 if (DS.isExplicitSpecified())
627 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +0000628 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +0000629 }
630}
Chris Lattner53361ac2006-08-10 05:19:57 +0000631
Chris Lattner6cc055a2009-04-12 20:42:31 +0000632/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
633/// specified token is valid after the identifier in a declarator which
634/// immediately follows the declspec. For example, these things are valid:
635///
636/// int x [ 4]; // direct-declarator
637/// int x ( int y); // direct-declarator
638/// int(int x ) // direct-declarator
639/// int x ; // simple-declaration
640/// int x = 17; // init-declarator-list
641/// int x , y; // init-declarator-list
642/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +0000643/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +0000644/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +0000645///
646/// This is not, because 'x' does not immediately follow the declspec (though
647/// ')' happens to be valid anyway).
648/// int (x)
649///
650static bool isValidAfterIdentifierInDeclarator(const Token &T) {
651 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
652 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +0000653 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +0000654}
655
Chris Lattner20a0c612009-04-14 21:34:55 +0000656
657/// ParseImplicitInt - This method is called when we have an non-typename
658/// identifier in a declspec (which normally terminates the decl spec) when
659/// the declspec has no type specifier. In this case, the declspec is either
660/// malformed or is "implicit int" (in K&R and C89).
661///
662/// This method handles diagnosing this prettily and returns false if the
663/// declspec is done being processed. If it recovers and thinks there may be
664/// other pieces of declspec after it, it returns true.
665///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000666bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000667 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner20a0c612009-04-14 21:34:55 +0000668 AccessSpecifier AS) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000669 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000670
Chris Lattner20a0c612009-04-14 21:34:55 +0000671 SourceLocation Loc = Tok.getLocation();
672 // If we see an identifier that is not a type name, we normally would
673 // parse it as the identifer being declared. However, when a typename
674 // is typo'd or the definition is not included, this will incorrectly
675 // parse the typename as the identifier name and fall over misparsing
676 // later parts of the diagnostic.
677 //
678 // As such, we try to do some look-ahead in cases where this would
679 // otherwise be an "implicit-int" case to see if this is invalid. For
680 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
681 // an identifier with implicit int, we'd get a parse error because the
682 // next token is obviously invalid for a type. Parse these as a case
683 // with an invalid type specifier.
684 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +0000685
Chris Lattner20a0c612009-04-14 21:34:55 +0000686 // Since we know that this either implicit int (which is rare) or an
687 // error, we'd do lookahead to try to do better recovery.
688 if (isValidAfterIdentifierInDeclarator(NextToken())) {
689 // If this token is valid for implicit int, e.g. "static x = 4", then
690 // we just avoid eating the identifier, so it will be parsed as the
691 // identifier in the declarator.
692 return false;
693 }
Mike Stump11289f42009-09-09 15:08:12 +0000694
Chris Lattner20a0c612009-04-14 21:34:55 +0000695 // Otherwise, if we don't consume this token, we are going to emit an
696 // error anyway. Try to recover from various common problems. Check
697 // to see if this was a reference to a tag name without a tag specified.
698 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000699 //
700 // C++ doesn't need this, and isTagName doesn't take SS.
701 if (SS == 0) {
702 const char *TagName = 0;
703 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +0000704
Chris Lattner20a0c612009-04-14 21:34:55 +0000705 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
706 default: break;
707 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
708 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
709 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
710 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
711 }
Mike Stump11289f42009-09-09 15:08:12 +0000712
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000713 if (TagName) {
714 Diag(Loc, diag::err_use_of_tag_name_without_tag)
715 << Tok.getIdentifierInfo() << TagName
716 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump11289f42009-09-09 15:08:12 +0000717
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000718 // Parse this as a tag as if the missing tag were present.
719 if (TagKind == tok::kw_enum)
720 ParseEnumSpecifier(Loc, DS, AS);
721 else
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000722 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000723 return true;
724 }
Chris Lattner20a0c612009-04-14 21:34:55 +0000725 }
Mike Stump11289f42009-09-09 15:08:12 +0000726
Douglas Gregor15e56022009-10-13 23:27:22 +0000727 // This is almost certainly an invalid type name. Let the action emit a
728 // diagnostic and attempt to recover.
729 Action::TypeTy *T = 0;
730 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
731 CurScope, SS, T)) {
732 // The action emitted a diagnostic, so we don't have to.
733 if (T) {
734 // The action has suggested that the type T could be used. Set that as
735 // the type in the declaration specifiers, consume the would-be type
736 // name token, and we're done.
737 const char *PrevSpec;
738 unsigned DiagID;
739 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
740 false);
741 DS.SetRangeEnd(Tok.getLocation());
742 ConsumeToken();
743
744 // There may be other declaration specifiers after this.
745 return true;
746 }
747
748 // Fall through; the action had no suggestion for us.
749 } else {
750 // The action did not emit a diagnostic, so emit one now.
751 SourceRange R;
752 if (SS) R = SS->getRange();
753 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
754 }
Mike Stump11289f42009-09-09 15:08:12 +0000755
Douglas Gregor15e56022009-10-13 23:27:22 +0000756 // Mark this as an error.
Chris Lattner20a0c612009-04-14 21:34:55 +0000757 const char *PrevSpec;
John McCall49bfce42009-08-03 20:12:06 +0000758 unsigned DiagID;
759 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattner20a0c612009-04-14 21:34:55 +0000760 DS.SetRangeEnd(Tok.getLocation());
761 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000762
Chris Lattner20a0c612009-04-14 21:34:55 +0000763 // TODO: Could inject an invalid typedef decl in an enclosing scope to
764 // avoid rippling error messages on subsequent uses of the same type,
765 // could be useful if #include was forgotten.
766 return false;
767}
768
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000769/// ParseDeclarationSpecifiers
770/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +0000771/// storage-class-specifier declaration-specifiers[opt]
772/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +0000773/// [C99] function-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +0000774/// [GNU] attributes declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000775///
Chris Lattnerf63f89a2006-08-05 03:28:50 +0000776/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000777/// 'typedef'
778/// 'extern'
779/// 'static'
780/// 'auto'
781/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +0000782/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +0000783/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000784/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +0000785/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +0000786/// [C++] 'virtual'
787/// [C++] 'explicit'
Anders Carlssoncd8db412009-05-06 04:46:28 +0000788/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +0000789/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +0000790
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000791///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +0000792void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000793 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +0000794 AccessSpecifier AS,
795 DeclSpecContext DSContext) {
Douglas Gregor9d64c5e2009-09-21 20:51:25 +0000796 if (Tok.is(tok::code_completion)) {
797 Actions.CodeCompleteOrdinaryName(CurScope);
798 ConsumeToken();
799 }
800
Chris Lattner2e232092008-03-13 06:29:04 +0000801 DS.SetRangeStart(Tok.getLocation());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000802 while (1) {
John McCall49bfce42009-08-03 20:12:06 +0000803 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000804 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +0000805 unsigned DiagID = 0;
806
Chris Lattner4d8f8732006-11-28 05:05:08 +0000807 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +0000808
Chris Lattnerc0acd3d2006-07-31 05:13:43 +0000809 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +0000810 default:
Chris Lattner0974b232008-07-26 00:20:22 +0000811 DoneWithDeclSpec:
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000812 // If this is not a declaration specifier token, we're done reading decl
813 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +0000814 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +0000815 return;
Mike Stump11289f42009-09-09 15:08:12 +0000816
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000817 case tok::coloncolon: // ::foo::bar
818 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregore861bac2009-08-25 22:51:20 +0000819 if (TryAnnotateCXXScopeToken(true))
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000820 continue;
821 goto DoneWithDeclSpec;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000822
823 case tok::annot_cxxscope: {
824 if (DS.hasTypeSpecifier())
825 goto DoneWithDeclSpec;
826
John McCall9dab4e62009-12-12 11:40:51 +0000827 CXXScopeSpec SS;
828 SS.setScopeRep(Tok.getAnnotationValue());
829 SS.setRange(Tok.getAnnotationRange());
830
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000831 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +0000832 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +0000833 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000834 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +0000835 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +0000836 // We have a qualified template-id, e.g., N::A<int>
John McCall9dab4e62009-12-12 11:40:51 +0000837 DS.getTypeSpecScope() = SS;
838 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +0000839 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +0000840 "ParseOptionalCXXScopeSpecifier not working");
841 AnnotateTemplateIdTokenAsType(&SS);
842 continue;
843 }
844
Douglas Gregorc5790df2009-09-28 07:26:33 +0000845 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +0000846 DS.getTypeSpecScope() = SS;
847 ConsumeToken(); // The C++ scope.
Douglas Gregorc5790df2009-09-28 07:26:33 +0000848 if (Tok.getAnnotationValue())
849 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
850 PrevSpec, DiagID,
851 Tok.getAnnotationValue());
852 else
853 DS.SetTypeSpecError();
854 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
855 ConsumeToken(); // The typename
856 }
857
Douglas Gregor167fa622009-03-25 15:40:00 +0000858 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000859 goto DoneWithDeclSpec;
860
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000861 // If the next token is the name of the class type that the C++ scope
862 // denotes, followed by a '(', then this is a constructor declaration.
863 // We're done with the decl-specifiers.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000864 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000865 CurScope, &SS) &&
866 GetLookAheadToken(2).is(tok::l_paren))
867 goto DoneWithDeclSpec;
868
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000869 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
870 Next.getLocation(), CurScope, &SS);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000871
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000872 // If the referenced identifier is not a type, then this declspec is
873 // erroneous: We already checked about that it has no type specifier, and
874 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +0000875 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000876 if (TypeRep == 0) {
877 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000878 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000879 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +0000880 }
Mike Stump11289f42009-09-09 15:08:12 +0000881
John McCall9dab4e62009-12-12 11:40:51 +0000882 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000883 ConsumeToken(); // The C++ scope.
884
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000885 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000886 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000887 if (isInvalid)
888 break;
Mike Stump11289f42009-09-09 15:08:12 +0000889
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +0000890 DS.SetRangeEnd(Tok.getLocation());
891 ConsumeToken(); // The typename.
892
893 continue;
894 }
Mike Stump11289f42009-09-09 15:08:12 +0000895
Chris Lattnere387d9e2009-01-21 19:48:37 +0000896 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000897 if (Tok.getAnnotationValue())
898 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000899 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +0000900 else
901 DS.SetTypeSpecError();
Chris Lattnere387d9e2009-01-21 19:48:37 +0000902 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
903 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +0000904
Chris Lattnere387d9e2009-01-21 19:48:37 +0000905 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
906 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
907 // Objective-C interface. If we don't have Objective-C or a '<', this is
908 // just a normal reference to a typedef name.
909 if (!Tok.is(tok::less) || !getLang().ObjC1)
910 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000911
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000912 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000913 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000914 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
915 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
916 LAngleLoc, EndProtoLoc);
917 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
918 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000919
Chris Lattnere387d9e2009-01-21 19:48:37 +0000920 DS.SetRangeEnd(EndProtoLoc);
921 continue;
922 }
Mike Stump11289f42009-09-09 15:08:12 +0000923
Chris Lattner16fac4f2008-07-26 01:18:38 +0000924 // typedef-name
925 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +0000926 // In C++, check to see if this is a scope specifier like foo::bar::, if
927 // so handle it as such. This is important for ctor parsing.
Douglas Gregore861bac2009-08-25 22:51:20 +0000928 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner78ecd4f2009-01-21 19:19:26 +0000929 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000930
Chris Lattner16fac4f2008-07-26 01:18:38 +0000931 // This identifier can only be a typedef name if we haven't already seen
932 // a type-specifier. Without this check we misparse:
933 // typedef int X; struct Y { short X; }; as 'short int'.
934 if (DS.hasTypeSpecifier())
935 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +0000936
Chris Lattner16fac4f2008-07-26 01:18:38 +0000937 // It has to be available as a typedef too!
Mike Stump11289f42009-09-09 15:08:12 +0000938 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregor8a6be5e2009-02-04 17:00:24 +0000939 Tok.getLocation(), CurScope);
Douglas Gregor8bf42052009-02-09 18:46:07 +0000940
Chris Lattner6cc055a2009-04-12 20:42:31 +0000941 // If this is not a typedef name, don't parse it as part of the declspec,
942 // it must be an implicit int or an error.
943 if (TypeRep == 0) {
Douglas Gregor1b57ff32009-05-12 23:25:50 +0000944 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000945 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +0000946 }
Douglas Gregor8bf42052009-02-09 18:46:07 +0000947
Douglas Gregor61956c42008-10-31 09:07:45 +0000948 // C++: If the identifier is actually the name of the class type
949 // being defined and the next token is a '(', then this is a
950 // constructor declaration. We're done with the decl-specifiers
951 // and will treat this token as an identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000952 if (getLang().CPlusPlus &&
953 (CurScope->isClassScope() ||
954 (CurScope->isTemplateParamScope() &&
Douglas Gregor5ed5ae42009-08-21 18:42:58 +0000955 CurScope->getParent()->isClassScope())) &&
Mike Stump11289f42009-09-09 15:08:12 +0000956 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor61956c42008-10-31 09:07:45 +0000957 NextToken().getKind() == tok::l_paren)
958 goto DoneWithDeclSpec;
959
Douglas Gregor9817f4a2009-02-09 15:09:02 +0000960 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +0000961 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +0000962 if (isInvalid)
963 break;
Mike Stump11289f42009-09-09 15:08:12 +0000964
Chris Lattner16fac4f2008-07-26 01:18:38 +0000965 DS.SetRangeEnd(Tok.getLocation());
966 ConsumeToken(); // The identifier
967
968 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
969 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
970 // Objective-C interface. If we don't have Objective-C or a '<', this is
971 // just a normal reference to a typedef name.
972 if (!Tok.is(tok::less) || !getLang().ObjC1)
973 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000974
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000975 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000976 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000977 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
978 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
979 LAngleLoc, EndProtoLoc);
980 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
981 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000982
Chris Lattner16fac4f2008-07-26 01:18:38 +0000983 DS.SetRangeEnd(EndProtoLoc);
984
Steve Naroffcd5e7822008-09-22 10:28:57 +0000985 // Need to support trailing type qualifiers (e.g. "id<p> const").
986 // If a type specifier follows, it will be diagnosed elsewhere.
987 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +0000988 }
Douglas Gregor7f741122009-02-25 19:37:18 +0000989
990 // type-name
991 case tok::annot_template_id: {
Mike Stump11289f42009-09-09 15:08:12 +0000992 TemplateIdAnnotation *TemplateId
Douglas Gregor7f741122009-02-25 19:37:18 +0000993 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorb67535d2009-03-31 00:43:58 +0000994 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +0000995 // This template-id does not refer to a type name, so we're
996 // done with the type-specifiers.
997 goto DoneWithDeclSpec;
998 }
999
1000 // Turn the template-id annotation token into a type annotation
1001 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001002 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00001003 continue;
1004 }
1005
Chris Lattnere37e2332006-08-15 04:50:22 +00001006 // GNU attributes support.
1007 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00001008 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnerb95cca02006-10-17 03:01:08 +00001009 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001010
1011 // Microsoft declspec support.
1012 case tok::kw___declspec:
Eli Friedman06de2b52009-06-08 07:21:15 +00001013 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00001014 continue;
Mike Stump11289f42009-09-09 15:08:12 +00001015
Steve Naroff44ac7772008-12-25 14:16:32 +00001016 // Microsoft single token adornments.
Steve Narofff9c29d42008-12-25 14:41:26 +00001017 case tok::kw___forceinline:
Eli Friedman53339e02009-06-08 23:27:34 +00001018 // FIXME: Add handling here!
1019 break;
1020
1021 case tok::kw___ptr64:
Steve Narofff9c29d42008-12-25 14:41:26 +00001022 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001023 case tok::kw___cdecl:
1024 case tok::kw___stdcall:
1025 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001026 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1027 continue;
1028
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001029 // storage-class-specifier
1030 case tok::kw_typedef:
John McCall49bfce42009-08-03 20:12:06 +00001031 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1032 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001033 break;
1034 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00001035 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001036 Diag(Tok, diag::ext_thread_before) << "extern";
John McCall49bfce42009-08-03 20:12:06 +00001037 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1038 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001039 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00001040 case tok::kw___private_extern__:
Chris Lattner371ed4e2008-04-06 06:57:35 +00001041 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCall49bfce42009-08-03 20:12:06 +00001042 PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00001043 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001044 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00001045 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00001046 Diag(Tok, diag::ext_thread_before) << "static";
John McCall49bfce42009-08-03 20:12:06 +00001047 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1048 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001049 break;
1050 case tok::kw_auto:
Anders Carlsson082acde2009-06-26 18:41:36 +00001051 if (getLang().CPlusPlus0x)
John McCall49bfce42009-08-03 20:12:06 +00001052 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1053 DiagID);
Anders Carlsson082acde2009-06-26 18:41:36 +00001054 else
John McCall49bfce42009-08-03 20:12:06 +00001055 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1056 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001057 break;
1058 case tok::kw_register:
John McCall49bfce42009-08-03 20:12:06 +00001059 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1060 DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001061 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001062 case tok::kw_mutable:
John McCall49bfce42009-08-03 20:12:06 +00001063 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1064 DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00001065 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001066 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00001067 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00001068 break;
Mike Stump11289f42009-09-09 15:08:12 +00001069
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001070 // function-specifier
1071 case tok::kw_inline:
John McCall49bfce42009-08-03 20:12:06 +00001072 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001073 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001074 case tok::kw_virtual:
John McCall49bfce42009-08-03 20:12:06 +00001075 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001076 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00001077 case tok::kw_explicit:
John McCall49bfce42009-08-03 20:12:06 +00001078 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00001079 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001080
Anders Carlssoncd8db412009-05-06 04:46:28 +00001081 // friend
1082 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00001083 if (DSContext == DSC_class)
1084 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1085 else {
1086 PrevSpec = ""; // not actually used by the diagnostic
1087 DiagID = diag::err_friend_invalid_in_context;
1088 isInvalid = true;
1089 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00001090 break;
Mike Stump11289f42009-09-09 15:08:12 +00001091
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00001092 // constexpr
1093 case tok::kw_constexpr:
1094 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1095 break;
1096
Chris Lattnere387d9e2009-01-21 19:48:37 +00001097 // type-specifier
1098 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001099 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1100 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001101 break;
1102 case tok::kw_long:
1103 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001104 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1105 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001106 else
John McCall49bfce42009-08-03 20:12:06 +00001107 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1108 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001109 break;
1110 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001111 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1112 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001113 break;
1114 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001115 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1116 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001117 break;
1118 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001119 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1120 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001121 break;
1122 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001123 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1124 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001125 break;
1126 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001127 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1128 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001129 break;
1130 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001131 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1132 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001133 break;
1134 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001135 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1136 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001137 break;
1138 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001139 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1140 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001141 break;
1142 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001143 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1144 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001145 break;
1146 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001147 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1148 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001149 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001150 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001151 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1152 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001153 break;
1154 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001155 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1156 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001157 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00001158 case tok::kw_bool:
1159 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001160 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1161 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001162 break;
1163 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001164 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1165 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001166 break;
1167 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001168 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1169 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001170 break;
1171 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001172 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1173 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001174 break;
1175
1176 // class-specifier:
1177 case tok::kw_class:
1178 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001179 case tok::kw_union: {
1180 tok::TokenKind Kind = Tok.getKind();
1181 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001182 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001183 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001184 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00001185
1186 // enum-specifier:
1187 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001188 ConsumeToken();
1189 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattnere387d9e2009-01-21 19:48:37 +00001190 continue;
1191
1192 // cv-qualifier:
1193 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00001194 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1195 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001196 break;
1197 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00001198 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1199 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001200 break;
1201 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00001202 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1203 getLang());
Chris Lattnere387d9e2009-01-21 19:48:37 +00001204 break;
1205
Douglas Gregor333489b2009-03-27 23:10:48 +00001206 // C++ typename-specifier:
1207 case tok::kw_typename:
1208 if (TryAnnotateTypeOrScopeToken())
1209 continue;
1210 break;
1211
Chris Lattnere387d9e2009-01-21 19:48:37 +00001212 // GNU typeof support.
1213 case tok::kw_typeof:
1214 ParseTypeofSpecifier(DS);
1215 continue;
1216
Anders Carlsson74948d02009-06-24 17:47:40 +00001217 case tok::kw_decltype:
1218 ParseDecltypeSpecifier(DS);
1219 continue;
1220
Steve Naroffcfdf6162008-06-05 00:02:44 +00001221 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00001222 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00001223 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1224 // but we support it.
Chris Lattner16fac4f2008-07-26 01:18:38 +00001225 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00001226 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00001227
Chris Lattner0974b232008-07-26 00:20:22 +00001228 {
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001229 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001230 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001231 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1232 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1233 LAngleLoc, EndProtoLoc);
1234 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1235 ProtocolLocs.data(), LAngleLoc);
Chris Lattner16fac4f2008-07-26 01:18:38 +00001236 DS.SetRangeEnd(EndProtoLoc);
1237
Chris Lattner6d29c102008-11-18 07:48:38 +00001238 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner3a4e4312009-04-03 18:38:42 +00001239 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner6d29c102008-11-18 07:48:38 +00001240 << SourceRange(Loc, EndProtoLoc);
Steve Naroffcd5e7822008-09-22 10:28:57 +00001241 // Need to support trailing type qualifiers (e.g. "id<p> const").
1242 // If a type specifier follows, it will be diagnosed elsewhere.
1243 continue;
Steve Naroffcfdf6162008-06-05 00:02:44 +00001244 }
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001245 }
John McCall49bfce42009-08-03 20:12:06 +00001246 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001247 if (isInvalid) {
1248 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00001249 assert(DiagID);
Chris Lattner6d29c102008-11-18 07:48:38 +00001250 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001251 }
Chris Lattner2e232092008-03-13 06:29:04 +00001252 DS.SetRangeEnd(Tok.getLocation());
Chris Lattnerb9093cd2006-08-04 04:39:53 +00001253 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001254 }
1255}
Douglas Gregoreb31f392008-12-01 23:54:00 +00001256
Chris Lattnera448d752009-01-06 06:59:53 +00001257/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor450c75a2008-11-07 15:42:26 +00001258/// primarily follow the C++ grammar with additions for C99 and GNU,
1259/// which together subsume the C grammar. Note that the C++
1260/// type-specifier also includes the C type-qualifier (for const,
1261/// volatile, and C99 restrict). Returns true if a type-specifier was
1262/// found (and parsed), false otherwise.
1263///
1264/// type-specifier: [C++ 7.1.5]
1265/// simple-type-specifier
1266/// class-specifier
1267/// enum-specifier
1268/// elaborated-type-specifier [TODO]
1269/// cv-qualifier
1270///
1271/// cv-qualifier: [C++ 7.1.5.1]
1272/// 'const'
1273/// 'volatile'
1274/// [C99] 'restrict'
1275///
1276/// simple-type-specifier: [ C++ 7.1.5.2]
1277/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1278/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1279/// 'char'
1280/// 'wchar_t'
1281/// 'bool'
1282/// 'short'
1283/// 'int'
1284/// 'long'
1285/// 'signed'
1286/// 'unsigned'
1287/// 'float'
1288/// 'double'
1289/// 'void'
1290/// [C99] '_Bool'
1291/// [C99] '_Complex'
1292/// [C99] '_Imaginary' // Removed in TC2?
1293/// [GNU] '_Decimal32'
1294/// [GNU] '_Decimal64'
1295/// [GNU] '_Decimal128'
1296/// [GNU] typeof-specifier
1297/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1298/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson74948d02009-06-24 17:47:40 +00001299/// [C++0x] 'decltype' ( expression )
John McCall49bfce42009-08-03 20:12:06 +00001300bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattnera448d752009-01-06 06:59:53 +00001301 const char *&PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001302 unsigned &DiagID,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001303 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor450c75a2008-11-07 15:42:26 +00001304 SourceLocation Loc = Tok.getLocation();
1305
1306 switch (Tok.getKind()) {
Chris Lattner020bab92009-01-04 23:41:41 +00001307 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001308 case tok::kw_typename: // typename foo::bar
Chris Lattner020bab92009-01-04 23:41:41 +00001309 // Annotate typenames and C++ scope specifiers. If we get one, just
1310 // recurse to handle whatever we get.
1311 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001312 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1313 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001314 // Otherwise, not a type specifier.
1315 return false;
1316 case tok::coloncolon: // ::foo::bar
1317 if (NextToken().is(tok::kw_new) || // ::new
1318 NextToken().is(tok::kw_delete)) // ::delete
1319 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001320
Chris Lattner020bab92009-01-04 23:41:41 +00001321 // Annotate typenames and C++ scope specifiers. If we get one, just
1322 // recurse to handle whatever we get.
1323 if (TryAnnotateTypeOrScopeToken())
John McCall49bfce42009-08-03 20:12:06 +00001324 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1325 TemplateInfo);
Chris Lattner020bab92009-01-04 23:41:41 +00001326 // Otherwise, not a type specifier.
1327 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001328
Douglas Gregor450c75a2008-11-07 15:42:26 +00001329 // simple-type-specifier:
Chris Lattnera8a3f732009-01-06 05:06:21 +00001330 case tok::annot_typename: {
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001331 if (Tok.getAnnotationValue())
1332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001333 DiagID, Tok.getAnnotationValue());
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00001334 else
1335 DS.SetTypeSpecError();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001336 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1337 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00001338
Douglas Gregor450c75a2008-11-07 15:42:26 +00001339 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1340 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1341 // Objective-C interface. If we don't have Objective-C or a '<', this is
1342 // just a normal reference to a typedef name.
1343 if (!Tok.is(tok::less) || !getLang().ObjC1)
1344 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001345
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001346 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +00001347 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001348 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1349 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1350 LAngleLoc, EndProtoLoc);
1351 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1352 ProtocolLocs.data(), LAngleLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001353
Douglas Gregor450c75a2008-11-07 15:42:26 +00001354 DS.SetRangeEnd(EndProtoLoc);
1355 return true;
1356 }
1357
1358 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00001359 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001360 break;
1361 case tok::kw_long:
1362 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00001363 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1364 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001365 else
John McCall49bfce42009-08-03 20:12:06 +00001366 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1367 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001368 break;
1369 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00001370 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001371 break;
1372 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00001373 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1374 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001375 break;
1376 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00001377 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1378 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001379 break;
1380 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00001381 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1382 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001383 break;
1384 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00001385 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001386 break;
1387 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00001388 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001389 break;
1390 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00001391 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001392 break;
1393 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00001394 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001395 break;
1396 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00001397 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001398 break;
1399 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00001400 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001401 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001402 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00001403 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001404 break;
1405 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00001406 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001407 break;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001408 case tok::kw_bool:
1409 case tok::kw__Bool:
John McCall49bfce42009-08-03 20:12:06 +00001410 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001411 break;
1412 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00001413 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1414 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001415 break;
1416 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00001417 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1418 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001419 break;
1420 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00001421 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1422 DiagID);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001423 break;
1424
1425 // class-specifier:
1426 case tok::kw_class:
1427 case tok::kw_struct:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001428 case tok::kw_union: {
1429 tok::TokenKind Kind = Tok.getKind();
1430 ConsumeToken();
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001431 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001432 return true;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001433 }
Douglas Gregor450c75a2008-11-07 15:42:26 +00001434
1435 // enum-specifier:
1436 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001437 ConsumeToken();
1438 ParseEnumSpecifier(Loc, DS);
Douglas Gregor450c75a2008-11-07 15:42:26 +00001439 return true;
1440
1441 // cv-qualifier:
1442 case tok::kw_const:
1443 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001444 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001445 break;
1446 case tok::kw_volatile:
1447 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, 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_restrict:
1451 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00001452 DiagID, getLang());
Douglas Gregor450c75a2008-11-07 15:42:26 +00001453 break;
1454
1455 // GNU typeof support.
1456 case tok::kw_typeof:
1457 ParseTypeofSpecifier(DS);
1458 return true;
1459
Anders Carlsson74948d02009-06-24 17:47:40 +00001460 // C++0x decltype support.
1461 case tok::kw_decltype:
1462 ParseDecltypeSpecifier(DS);
1463 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001464
Anders Carlssonbae27372009-06-26 23:44:14 +00001465 // C++0x auto support.
1466 case tok::kw_auto:
1467 if (!getLang().CPlusPlus0x)
1468 return false;
1469
John McCall49bfce42009-08-03 20:12:06 +00001470 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlssonbae27372009-06-26 23:44:14 +00001471 break;
Eli Friedman53339e02009-06-08 23:27:34 +00001472 case tok::kw___ptr64:
1473 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00001474 case tok::kw___cdecl:
1475 case tok::kw___stdcall:
1476 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001477 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner78ecd4f2009-01-21 19:19:26 +00001478 return true;
Steve Naroff44ac7772008-12-25 14:16:32 +00001479
Douglas Gregor450c75a2008-11-07 15:42:26 +00001480 default:
1481 // Not a type-specifier; do nothing.
1482 return false;
1483 }
1484
1485 // If the specifier combination wasn't legal, issue a diagnostic.
1486 if (isInvalid) {
1487 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00001488 // Pick between error or extwarn.
Chris Lattner6d29c102008-11-18 07:48:38 +00001489 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor450c75a2008-11-07 15:42:26 +00001490 }
1491 DS.SetRangeEnd(Tok.getLocation());
1492 ConsumeToken(); // whatever we parsed above.
1493 return true;
1494}
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00001495
Chris Lattner70ae4912007-10-29 04:42:53 +00001496/// ParseStructDeclaration - Parse a struct declaration without the terminating
1497/// semicolon.
1498///
Chris Lattner90a26b02007-01-23 04:38:16 +00001499/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00001500/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00001501/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00001502/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00001503/// struct-declarator-list:
1504/// struct-declarator
1505/// struct-declarator-list ',' struct-declarator
1506/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1507/// struct-declarator:
1508/// declarator
1509/// [GNU] declarator attributes[opt]
1510/// declarator[opt] ':' constant-expression
1511/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1512///
Chris Lattnera12405b2008-04-10 06:46:29 +00001513void Parser::
John McCallcfefb6d2009-11-03 02:38:08 +00001514ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001515 if (Tok.is(tok::kw___extension__)) {
1516 // __extension__ silences extension warnings in the subexpression.
1517 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00001518 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00001519 return ParseStructDeclaration(DS, Fields);
1520 }
Mike Stump11289f42009-09-09 15:08:12 +00001521
Steve Naroff97170802007-08-20 22:28:22 +00001522 // Parse the common specifier-qualifiers-list piece.
Chris Lattner32295d32008-04-10 06:15:14 +00001523 SourceLocation DSStart = Tok.getLocation();
Steve Naroff97170802007-08-20 22:28:22 +00001524 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00001525
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001526 // If there are no declarators, this is a free-standing declaration
1527 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00001528 if (Tok.is(tok::semi)) {
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00001529 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff97170802007-08-20 22:28:22 +00001530 return;
1531 }
1532
1533 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00001534 bool FirstDeclarator = true;
Steve Naroff97170802007-08-20 22:28:22 +00001535 while (1) {
John McCall28a6aea2009-11-04 02:18:39 +00001536 ParsingDeclRAIIObject PD(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00001537 FieldDeclarator DeclaratorInfo(DS);
1538
1539 // Attributes are only allowed here on successive declarators.
1540 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1541 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001542 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallcfefb6d2009-11-03 02:38:08 +00001543 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1544 }
Mike Stump11289f42009-09-09 15:08:12 +00001545
Steve Naroff97170802007-08-20 22:28:22 +00001546 /// struct-declarator: declarator
1547 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001548 if (Tok.isNot(tok::colon)) {
1549 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1550 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00001551 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00001552 }
Mike Stump11289f42009-09-09 15:08:12 +00001553
Chris Lattner76c72282007-10-09 17:33:22 +00001554 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00001555 ConsumeToken();
Sebastian Redl59b5e512008-12-11 21:36:32 +00001556 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001557 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00001558 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00001559 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001560 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00001561 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001562
Steve Naroff97170802007-08-20 22:28:22 +00001563 // If attributes exist after the declarator, parse them.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001564 if (Tok.is(tok::kw___attribute)) {
1565 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001566 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001567 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1568 }
1569
John McCallcfefb6d2009-11-03 02:38:08 +00001570 // We're done with this declarator; invoke the callback.
John McCall28a6aea2009-11-04 02:18:39 +00001571 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1572 PD.complete(D);
John McCallcfefb6d2009-11-03 02:38:08 +00001573
Steve Naroff97170802007-08-20 22:28:22 +00001574 // If we don't have a comma, it is either the end of the list (a ';')
1575 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00001576 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00001577 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001578
Steve Naroff97170802007-08-20 22:28:22 +00001579 // Consume the comma.
1580 ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00001581
John McCallcfefb6d2009-11-03 02:38:08 +00001582 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00001583 }
Steve Naroff97170802007-08-20 22:28:22 +00001584}
1585
1586/// ParseStructUnionBody
1587/// struct-contents:
1588/// struct-declaration-list
1589/// [EXT] empty
1590/// [GNU] "struct-declaration-list" without terminatoring ';'
1591/// struct-declaration-list:
1592/// struct-declaration
1593/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00001594/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00001595///
Chris Lattner1300fb92007-01-23 23:42:53 +00001596void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattner83f095c2009-03-28 19:18:32 +00001597 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattnereae6cb62009-03-05 08:00:35 +00001598 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1599 PP.getSourceManager(),
1600 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00001601
Chris Lattner90a26b02007-01-23 04:38:16 +00001602 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001603
Douglas Gregor658b9552009-01-09 22:42:13 +00001604 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001605 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1606
Chris Lattner7b9ace62007-01-23 20:11:08 +00001607 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1608 // C++.
Douglas Gregor556877c2008-04-13 21:30:24 +00001609 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001610 Diag(Tok, diag::ext_empty_struct_union_enum)
1611 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Chris Lattner7b9ace62007-01-23 20:11:08 +00001612
Chris Lattner83f095c2009-03-28 19:18:32 +00001613 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001614
Chris Lattner7b9ace62007-01-23 20:11:08 +00001615 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00001616 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001617 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00001618
Chris Lattner736ed5d2007-06-09 05:59:07 +00001619 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00001620 if (Tok.is(tok::semi)) {
Douglas Gregore3e01a22009-04-01 22:41:11 +00001621 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001622 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Chris Lattner36e46a22007-06-09 05:49:55 +00001623 ConsumeToken();
1624 continue;
1625 }
Chris Lattnera12405b2008-04-10 06:46:29 +00001626
1627 // Parse all the comma separated declarators.
1628 DeclSpec DS;
Mike Stump11289f42009-09-09 15:08:12 +00001629
John McCallcfefb6d2009-11-03 02:38:08 +00001630 if (!Tok.is(tok::at)) {
1631 struct CFieldCallback : FieldCallback {
1632 Parser &P;
1633 DeclPtrTy TagDecl;
1634 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1635
1636 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1637 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1638 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1639
1640 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001641 // Install the declarator into the current TagDecl.
John McCall5e6253b2009-11-03 21:13:47 +00001642 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1643 FD.D.getDeclSpec().getSourceRange().getBegin(),
1644 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00001645 FieldDecls.push_back(Field);
1646 return Field;
Douglas Gregor66a985d2009-08-26 14:27:30 +00001647 }
John McCallcfefb6d2009-11-03 02:38:08 +00001648 } Callback(*this, TagDecl, FieldDecls);
1649
1650 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00001651 } else { // Handle @defs
1652 ConsumeToken();
1653 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1654 Diag(Tok, diag::err_unexpected_at);
1655 SkipUntil(tok::semi, true, true);
1656 continue;
1657 }
1658 ConsumeToken();
1659 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1660 if (!Tok.is(tok::identifier)) {
1661 Diag(Tok, diag::err_expected_ident);
1662 SkipUntil(tok::semi, true, true);
1663 continue;
1664 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001665 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump11289f42009-09-09 15:08:12 +00001666 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00001667 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00001668 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1669 ConsumeToken();
1670 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00001671 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00001672
Chris Lattner76c72282007-10-09 17:33:22 +00001673 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00001674 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00001675 } else if (Tok.is(tok::r_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001676 Diag(Tok, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00001677 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00001678 } else {
1679 Diag(Tok, diag::err_expected_semi_decl_list);
1680 // Skip to end of block or statement
1681 SkipUntil(tok::r_brace, true, true);
1682 }
1683 }
Mike Stump11289f42009-09-09 15:08:12 +00001684
Steve Naroff33a1e802007-10-29 21:38:07 +00001685 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001686
Steve Naroffb8371e12007-06-09 03:39:29 +00001687 AttributeList *AttrList = 0;
Chris Lattner90a26b02007-01-23 04:38:16 +00001688 // If attributes exist after struct contents, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001689 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +00001690 AttrList = ParseGNUAttributes();
Daniel Dunbar15619c72008-10-03 02:03:53 +00001691
1692 Actions.ActOnFields(CurScope,
Jay Foad7d0479f2009-05-21 09:52:38 +00001693 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001694 LBraceLoc, RBraceLoc,
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001695 AttrList);
1696 StructScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001697 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Chris Lattner90a26b02007-01-23 04:38:16 +00001698}
1699
1700
Chris Lattner3b561a32006-08-13 00:12:11 +00001701/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00001702/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00001703/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001704///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00001705/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1706/// '}' attributes[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00001707/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00001708/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001709///
1710/// [C++] elaborated-type-specifier:
1711/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1712///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00001713void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1714 AccessSpecifier AS) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00001715 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00001716 if (Tok.is(tok::code_completion)) {
1717 // Code completion for an enum name.
1718 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1719 ConsumeToken();
1720 }
1721
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001722 AttributeList *Attr = 0;
1723 // If attributes exist after tag, parse them.
1724 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +00001725 Attr = ParseGNUAttributes();
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001726
1727 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00001728 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001729 if (Tok.isNot(tok::identifier)) {
1730 Diag(Tok, diag::err_expected_ident);
1731 if (Tok.isNot(tok::l_brace)) {
1732 // Has no name and is not a definition.
1733 // Skip the rest of this declarator, up until the comma or semicolon.
1734 SkipUntil(tok::comma, true);
1735 return;
1736 }
1737 }
1738 }
Mike Stump11289f42009-09-09 15:08:12 +00001739
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001740 // Must have either 'enum name' or 'enum {...}'.
1741 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1742 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00001743
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001744 // Skip the rest of this declarator, up until the comma or semicolon.
1745 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00001746 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001747 }
Mike Stump11289f42009-09-09 15:08:12 +00001748
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001749 // If an identifier is present, consume and remember it.
1750 IdentifierInfo *Name = 0;
1751 SourceLocation NameLoc;
1752 if (Tok.is(tok::identifier)) {
1753 Name = Tok.getIdentifierInfo();
1754 NameLoc = ConsumeToken();
1755 }
Mike Stump11289f42009-09-09 15:08:12 +00001756
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001757 // There are three options here. If we have 'enum foo;', then this is a
1758 // forward declaration. If we have 'enum foo {...' then this is a
1759 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1760 //
1761 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1762 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1763 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1764 //
John McCall9bb74a52009-07-31 02:45:11 +00001765 Action::TagUseKind TUK;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001766 if (Tok.is(tok::l_brace))
John McCall9bb74a52009-07-31 02:45:11 +00001767 TUK = Action::TUK_Definition;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001768 else if (Tok.is(tok::semi))
John McCall9bb74a52009-07-31 02:45:11 +00001769 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00001770 else
John McCall9bb74a52009-07-31 02:45:11 +00001771 TUK = Action::TUK_Reference;
Douglas Gregord6ab8742009-05-28 23:31:59 +00001772 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00001773 bool IsDependent = false;
John McCall9bb74a52009-07-31 02:45:11 +00001774 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001775 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregor27bdf00f2009-07-23 16:36:45 +00001776 Action::MultiTemplateParamsArg(Actions),
John McCall7f41d982009-09-11 04:59:25 +00001777 Owned, IsDependent);
1778 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump11289f42009-09-09 15:08:12 +00001779
Chris Lattner76c72282007-10-09 17:33:22 +00001780 if (Tok.is(tok::l_brace))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001781 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001782
Chris Lattner3b561a32006-08-13 00:12:11 +00001783 // TODO: semantic analysis on the declspec for enums.
Chris Lattnerda72c822006-08-13 22:16:42 +00001784 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00001785 unsigned DiagID;
1786 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregord6ab8742009-05-28 23:31:59 +00001787 TagDecl.getAs<void>(), Owned))
John McCall49bfce42009-08-03 20:12:06 +00001788 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00001789}
1790
Chris Lattnerc1915e22007-01-25 07:29:02 +00001791/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1792/// enumerator-list:
1793/// enumerator
1794/// enumerator-list ',' enumerator
1795/// enumerator:
1796/// enumeration-constant
1797/// enumeration-constant '=' constant-expression
1798/// enumeration-constant:
1799/// identifier
1800///
Chris Lattner83f095c2009-03-28 19:18:32 +00001801void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00001802 // Enter the scope of the enum body and start the definition.
1803 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001804 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00001805
Chris Lattnerc1915e22007-01-25 07:29:02 +00001806 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump11289f42009-09-09 15:08:12 +00001807
Chris Lattner37256fb2007-08-27 17:24:30 +00001808 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner76c72282007-10-09 17:33:22 +00001809 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner6d29c102008-11-18 07:48:38 +00001810 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump11289f42009-09-09 15:08:12 +00001811
Chris Lattner83f095c2009-03-28 19:18:32 +00001812 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001813
Chris Lattner83f095c2009-03-28 19:18:32 +00001814 DeclPtrTy LastEnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001815
Chris Lattnerc1915e22007-01-25 07:29:02 +00001816 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00001817 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001818 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1819 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001820
Chris Lattnerc1915e22007-01-25 07:29:02 +00001821 SourceLocation EqualLoc;
Sebastian Redlc13f2682008-12-09 20:22:58 +00001822 OwningExprResult AssignedVal(Actions);
Chris Lattner76c72282007-10-09 17:33:22 +00001823 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00001824 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001825 AssignedVal = ParseConstantExpression();
1826 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00001827 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001828 }
Mike Stump11289f42009-09-09 15:08:12 +00001829
Chris Lattnerc1915e22007-01-25 07:29:02 +00001830 // Install the enumerator constant into EnumDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +00001831 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1832 LastEnumConstDecl,
1833 IdentLoc, Ident,
1834 EqualLoc,
1835 AssignedVal.release());
Chris Lattner4ef40012007-06-11 01:28:17 +00001836 EnumConstantDecls.push_back(EnumConstDecl);
1837 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00001838
Chris Lattner76c72282007-10-09 17:33:22 +00001839 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00001840 break;
1841 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001842
1843 if (Tok.isNot(tok::identifier) &&
Douglas Gregore3e01a22009-04-01 22:41:11 +00001844 !(getLang().C99 || getLang().CPlusPlus0x))
1845 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1846 << getLang().CPlusPlus
Chris Lattner3c7b86f2009-12-06 17:36:05 +00001847 << CodeModificationHint::CreateRemoval(CommaLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001848 }
Mike Stump11289f42009-09-09 15:08:12 +00001849
Chris Lattnerc1915e22007-01-25 07:29:02 +00001850 // Eat the }.
Mike Stump6814d1c2009-05-16 07:06:02 +00001851 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001852
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001853 AttributeList *Attr = 0;
Chris Lattnerc1915e22007-01-25 07:29:02 +00001854 // If attributes exist after the identifier list, parse them.
Chris Lattner76c72282007-10-09 17:33:22 +00001855 if (Tok.is(tok::kw___attribute))
Alexis Hunt96d5c762009-11-21 08:43:09 +00001856 Attr = ParseGNUAttributes(); // FIXME: where do they do?
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001857
Edward O'Callaghanc69169d2009-08-08 14:36:57 +00001858 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1859 EnumConstantDecls.data(), EnumConstantDecls.size(),
1860 CurScope, Attr);
Mike Stump11289f42009-09-09 15:08:12 +00001861
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001862 EnumScope.Exit();
Argyrios Kyrtzidis23e1f1d2009-07-14 03:17:52 +00001863 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Chris Lattnerc1915e22007-01-25 07:29:02 +00001864}
Chris Lattner3b561a32006-08-13 00:12:11 +00001865
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001866/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00001867/// start of a type-qualifier-list.
1868bool Parser::isTypeQualifier() const {
1869 switch (Tok.getKind()) {
1870 default: return false;
1871 // type-qualifier
1872 case tok::kw_const:
1873 case tok::kw_volatile:
1874 case tok::kw_restrict:
1875 return true;
1876 }
1877}
1878
1879/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001880/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001881bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001882 switch (Tok.getKind()) {
1883 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001884
Chris Lattner020bab92009-01-04 23:41:41 +00001885 case tok::identifier: // foo::bar
Douglas Gregor333489b2009-03-27 23:10:48 +00001886 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001887 // Annotate typenames and C++ scope specifiers. If we get one, just
1888 // recurse to handle whatever we get.
1889 if (TryAnnotateTypeOrScopeToken())
1890 return isTypeSpecifierQualifier();
1891 // Otherwise, not a type specifier.
1892 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001893
Chris Lattner020bab92009-01-04 23:41:41 +00001894 case tok::coloncolon: // ::foo::bar
1895 if (NextToken().is(tok::kw_new) || // ::new
1896 NextToken().is(tok::kw_delete)) // ::delete
1897 return false;
1898
1899 // Annotate typenames and C++ scope specifiers. If we get one, just
1900 // recurse to handle whatever we get.
1901 if (TryAnnotateTypeOrScopeToken())
1902 return isTypeSpecifierQualifier();
1903 // Otherwise, not a type specifier.
1904 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001905
Chris Lattnere37e2332006-08-15 04:50:22 +00001906 // GNU attributes support.
1907 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00001908 // GNU typeof support.
1909 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00001910
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001911 // type-specifiers
1912 case tok::kw_short:
1913 case tok::kw_long:
1914 case tok::kw_signed:
1915 case tok::kw_unsigned:
1916 case tok::kw__Complex:
1917 case tok::kw__Imaginary:
1918 case tok::kw_void:
1919 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00001920 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00001921 case tok::kw_char16_t:
1922 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001923 case tok::kw_int:
1924 case tok::kw_float:
1925 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00001926 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001927 case tok::kw__Bool:
1928 case tok::kw__Decimal32:
1929 case tok::kw__Decimal64:
1930 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00001931
Chris Lattner861a2262008-04-13 18:59:07 +00001932 // struct-or-union-specifier (C99) or class-specifier (C++)
1933 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001934 case tok::kw_struct:
1935 case tok::kw_union:
1936 // enum-specifier
1937 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00001938
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001939 // type-qualifier
1940 case tok::kw_const:
1941 case tok::kw_volatile:
1942 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001943
1944 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00001945 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001946 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001947
Chris Lattner409bf7d2008-10-20 00:25:30 +00001948 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1949 case tok::less:
1950 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00001951
Steve Naroff44ac7772008-12-25 14:16:32 +00001952 case tok::kw___cdecl:
1953 case tok::kw___stdcall:
1954 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00001955 case tok::kw___w64:
1956 case tok::kw___ptr64:
1957 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00001958 }
1959}
1960
Chris Lattneracd58a32006-08-06 17:24:14 +00001961/// isDeclarationSpecifier() - Return true if the current token is part of a
1962/// declaration specifier.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00001963bool Parser::isDeclarationSpecifier() {
Chris Lattneracd58a32006-08-06 17:24:14 +00001964 switch (Tok.getKind()) {
1965 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00001966
Chris Lattner020bab92009-01-04 23:41:41 +00001967 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00001968 // Unfortunate hack to support "Class.factoryMethod" notation.
1969 if (getLang().ObjC1 && NextToken().is(tok::period))
1970 return false;
Douglas Gregor333489b2009-03-27 23:10:48 +00001971 // Fall through
Steve Naroff9527bbf2009-03-09 21:12:44 +00001972
Douglas Gregor333489b2009-03-27 23:10:48 +00001973 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00001974 // Annotate typenames and C++ scope specifiers. If we get one, just
1975 // recurse to handle whatever we get.
1976 if (TryAnnotateTypeOrScopeToken())
1977 return isDeclarationSpecifier();
1978 // Otherwise, not a declaration specifier.
1979 return false;
1980 case tok::coloncolon: // ::foo::bar
1981 if (NextToken().is(tok::kw_new) || // ::new
1982 NextToken().is(tok::kw_delete)) // ::delete
1983 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001984
Chris Lattner020bab92009-01-04 23:41:41 +00001985 // Annotate typenames and C++ scope specifiers. If we get one, just
1986 // recurse to handle whatever we get.
1987 if (TryAnnotateTypeOrScopeToken())
1988 return isDeclarationSpecifier();
1989 // Otherwise, not a declaration specifier.
1990 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001991
Chris Lattneracd58a32006-08-06 17:24:14 +00001992 // storage-class-specifier
1993 case tok::kw_typedef:
1994 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00001995 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00001996 case tok::kw_static:
1997 case tok::kw_auto:
1998 case tok::kw_register:
1999 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00002000
Chris Lattneracd58a32006-08-06 17:24:14 +00002001 // type-specifiers
2002 case tok::kw_short:
2003 case tok::kw_long:
2004 case tok::kw_signed:
2005 case tok::kw_unsigned:
2006 case tok::kw__Complex:
2007 case tok::kw__Imaginary:
2008 case tok::kw_void:
2009 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00002010 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002011 case tok::kw_char16_t:
2012 case tok::kw_char32_t:
2013
Chris Lattneracd58a32006-08-06 17:24:14 +00002014 case tok::kw_int:
2015 case tok::kw_float:
2016 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00002017 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00002018 case tok::kw__Bool:
2019 case tok::kw__Decimal32:
2020 case tok::kw__Decimal64:
2021 case tok::kw__Decimal128:
Mike Stump11289f42009-09-09 15:08:12 +00002022
Chris Lattner861a2262008-04-13 18:59:07 +00002023 // struct-or-union-specifier (C99) or class-specifier (C++)
2024 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00002025 case tok::kw_struct:
2026 case tok::kw_union:
2027 // enum-specifier
2028 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00002029
Chris Lattneracd58a32006-08-06 17:24:14 +00002030 // type-qualifier
2031 case tok::kw_const:
2032 case tok::kw_volatile:
2033 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00002034
Chris Lattneracd58a32006-08-06 17:24:14 +00002035 // function-specifier
2036 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00002037 case tok::kw_virtual:
2038 case tok::kw_explicit:
Chris Lattner7b20dc72007-08-09 16:40:21 +00002039
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002040 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00002041 case tok::annot_typename:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002042
Chris Lattner599e47e2007-08-09 17:01:07 +00002043 // GNU typeof support.
2044 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00002045
Chris Lattner599e47e2007-08-09 17:01:07 +00002046 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00002047 case tok::kw___attribute:
Chris Lattneracd58a32006-08-06 17:24:14 +00002048 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002049
Chris Lattner8b2ec162008-07-26 03:38:44 +00002050 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2051 case tok::less:
2052 return getLang().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00002053
Steve Narofff192fab2009-01-06 19:34:12 +00002054 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00002055 case tok::kw___cdecl:
2056 case tok::kw___stdcall:
2057 case tok::kw___fastcall:
Eli Friedman53339e02009-06-08 23:27:34 +00002058 case tok::kw___w64:
2059 case tok::kw___ptr64:
2060 case tok::kw___forceinline:
2061 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00002062 }
2063}
2064
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002065
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002066/// ParseTypeQualifierListOpt
2067/// type-qualifier-list: [C99 6.7.5]
2068/// type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002069/// [GNU] attributes [ only if AttributesAllowed=true ]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002070/// type-qualifier-list type-qualifier
Chris Lattnercf0bab22008-12-18 07:02:59 +00002071/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Alexis Hunt96d5c762009-11-21 08:43:09 +00002072/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2073/// if CXX0XAttributesAllowed = true
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002074///
Alexis Hunt96d5c762009-11-21 08:43:09 +00002075void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2076 bool CXX0XAttributesAllowed) {
2077 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2078 SourceLocation Loc = Tok.getLocation();
2079 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2080 if (CXX0XAttributesAllowed)
2081 DS.AddAttributes(Attr.AttrList);
2082 else
2083 Diag(Loc, diag::err_attributes_not_allowed);
2084 }
2085
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002086 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002087 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002088 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002089 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00002090 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002091
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002092 switch (Tok.getKind()) {
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002093 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002094 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2095 getLang());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002096 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002097 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002098 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, 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_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002102 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2103 getLang());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002104 break;
Eli Friedman53339e02009-06-08 23:27:34 +00002105 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00002106 case tok::kw___ptr64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002107 case tok::kw___cdecl:
2108 case tok::kw___stdcall:
2109 case tok::kw___fastcall:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002110 if (GNUAttributesAllowed) {
Eli Friedman53339e02009-06-08 23:27:34 +00002111 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2112 continue;
2113 }
2114 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00002115 case tok::kw___attribute:
Alexis Hunt96d5c762009-11-21 08:43:09 +00002116 if (GNUAttributesAllowed) {
2117 DS.AddAttributes(ParseGNUAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00002118 continue; // do *not* consume the next token!
2119 }
2120 // otherwise, FALL THROUGH!
2121 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00002122 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00002123 // If this is not a type-qualifier token, we're done reading type
2124 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002125 DS.Finish(Diags, PP);
Chris Lattnercf0bab22008-12-18 07:02:59 +00002126 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002127 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002128
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002129 // If the specifier combination wasn't legal, issue a diagnostic.
2130 if (isInvalid) {
2131 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00002132 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00002133 }
2134 ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002135 }
2136}
2137
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002138
2139/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2140///
2141void Parser::ParseDeclarator(Declarator &D) {
2142 /// This implements the 'declarator' production in the C grammar, then checks
2143 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002144 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002145}
2146
Sebastian Redlbd150f42008-11-21 19:14:01 +00002147/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2148/// is parsed by the function passed to it. Pass null, and the direct-declarator
2149/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002150/// ptr-operator production.
2151///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002152/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2153/// [C] pointer[opt] direct-declarator
2154/// [C++] direct-declarator
2155/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00002156///
2157/// pointer: [C99 6.7.5]
2158/// '*' type-qualifier-list[opt]
2159/// '*' type-qualifier-list[opt] pointer
2160///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002161/// ptr-operator:
2162/// '*' cv-qualifier-seq[opt]
2163/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00002164/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002165/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00002166/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002167/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00002168void Parser::ParseDeclaratorInternal(Declarator &D,
2169 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00002170 if (Diags.hasAllExtensionsSilenced())
2171 D.setExtension();
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002172 // C++ member pointers start with a '::' or a nested-name.
2173 // Member pointers get special handling, since there's no place for the
2174 // scope spec in the generic path below.
Chris Lattner803802d2009-03-24 17:04:48 +00002175 if (getLang().CPlusPlus &&
2176 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2177 Tok.is(tok::annot_cxxscope))) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002178 CXXScopeSpec SS;
Douglas Gregorb7bfe792009-09-02 22:59:36 +00002179 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump11289f42009-09-09 15:08:12 +00002180 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002181 // The scope spec really belongs to the direct-declarator.
2182 D.getCXXScopeSpec() = SS;
2183 if (DirectDeclParser)
2184 (this->*DirectDeclParser)(D);
2185 return;
2186 }
2187
2188 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002189 D.SetRangeEnd(Loc);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002190 DeclSpec DS;
2191 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002192 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002193
2194 // Recurse to parse whatever is left.
2195 ParseDeclaratorInternal(D, DirectDeclParser);
2196
2197 // Sema will have to catch (syntactically invalid) pointers into global
2198 // scope. It has to catch pointers into namespace scope anyway.
2199 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002200 Loc, DS.TakeAttributes()),
2201 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002202 return;
2203 }
2204 }
2205
2206 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00002207 // Not a pointer, C++ reference, or block.
Chris Lattner9eac9312009-03-27 04:18:06 +00002208 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattner803802d2009-03-24 17:04:48 +00002209 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl3b27be62009-03-23 00:00:23 +00002210 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9eac9312009-03-27 04:18:06 +00002211 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00002212 if (DirectDeclParser)
2213 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002214 return;
2215 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002216
Sebastian Redled0f3b02009-03-15 22:02:01 +00002217 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2218 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00002219 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002220 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00002221
Chris Lattner9eac9312009-03-27 04:18:06 +00002222 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00002223 // Is a pointer.
Bill Wendling3708c182007-05-27 10:15:43 +00002224 DeclSpec DS;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002225
Bill Wendling3708c182007-05-27 10:15:43 +00002226 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002227 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00002228
Bill Wendling3708c182007-05-27 10:15:43 +00002229 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002230 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00002231 if (Kind == tok::star)
2232 // Remember that we parsed a pointer type, and remember the type-quals.
2233 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002234 DS.TakeAttributes()),
2235 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00002236 else
2237 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00002238 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump3214d122009-04-21 00:51:43 +00002239 Loc, DS.TakeAttributes()),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002240 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002241 } else {
2242 // Is a reference
Bill Wendling93efb222007-06-02 23:28:54 +00002243 DeclSpec DS;
2244
Sebastian Redl3b27be62009-03-23 00:00:23 +00002245 // Complain about rvalue references in C++03, but then go on and build
2246 // the declarator.
2247 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2248 Diag(Loc, diag::err_rvalue_reference);
2249
Bill Wendling93efb222007-06-02 23:28:54 +00002250 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2251 // cv-qualifiers are introduced through the use of a typedef or of a
2252 // template type argument, in which case the cv-qualifiers are ignored.
2253 //
2254 // [GNU] Retricted references are allowed.
2255 // [GNU] Attributes on references are allowed.
Alexis Hunt96d5c762009-11-21 08:43:09 +00002256 // [C++0x] Attributes on references are not allowed.
2257 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002258 D.ExtendWithDeclSpec(DS);
Bill Wendling93efb222007-06-02 23:28:54 +00002259
2260 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2261 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2262 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002263 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00002264 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2265 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00002266 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00002267 }
Bill Wendling3708c182007-05-27 10:15:43 +00002268
2269 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00002270 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00002271
Douglas Gregor66583c52008-11-03 15:51:28 +00002272 if (D.getNumTypeObjects() > 0) {
2273 // C++ [dcl.ref]p4: There shall be no references to references.
2274 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2275 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002276 if (const IdentifierInfo *II = D.getIdentifier())
2277 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2278 << II;
2279 else
2280 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2281 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00002282
Sebastian Redlbd150f42008-11-21 19:14:01 +00002283 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00002284 // can go ahead and build the (technically ill-formed)
2285 // declarator: reference collapsing will take care of it.
2286 }
2287 }
2288
Bill Wendling3708c182007-05-27 10:15:43 +00002289 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00002290 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00002291 DS.TakeAttributes(),
2292 Kind == tok::amp),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002293 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00002294 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00002295}
2296
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002297/// ParseDirectDeclarator
2298/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00002299/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002300/// '(' declarator ')'
2301/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00002302/// [C90] direct-declarator '[' constant-expression[opt] ']'
2303/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2304/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2305/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2306/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002307/// direct-declarator '(' parameter-type-list ')'
2308/// direct-declarator '(' identifier-list[opt] ')'
2309/// [GNU] direct-declarator '(' parameter-forward-declarations
2310/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002311/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2312/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00002313/// [C++] declarator-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002314///
2315/// declarator-id: [C++ 8]
2316/// id-expression
2317/// '::'[opt] nested-name-specifier[opt] type-name
2318///
2319/// id-expression: [C++ 5.1]
2320/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002321/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00002322///
2323/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00002324/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002325/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00002326/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00002327/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00002328/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00002329///
Chris Lattneracd58a32006-08-06 17:24:14 +00002330void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002331 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002332
Douglas Gregor7861a802009-11-03 01:35:08 +00002333 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2334 // ParseDeclaratorInternal might already have parsed the scope.
2335 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2336 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2337 true);
2338 if (afterCXXScope) {
John McCall2b058ef2009-12-11 20:04:54 +00002339 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2340 // Change the declaration context for name lookup, until this function
2341 // is exited (and the declarator has been parsed).
2342 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor7861a802009-11-03 01:35:08 +00002343 }
2344
2345 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2346 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2347 // We found something that indicates the start of an unqualified-id.
2348 // Parse that unqualified-id.
2349 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2350 /*EnteringContext=*/true,
2351 /*AllowDestructorName=*/true,
2352 /*AllowConstructorName=*/!D.getDeclSpec().hasTypeSpecifier(),
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002353 /*ObjectType=*/0,
Douglas Gregor7861a802009-11-03 01:35:08 +00002354 D.getName())) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002355 D.SetIdentifier(0, Tok.getLocation());
2356 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00002357 } else {
2358 // Parsed the unqualified-id; update range information and move along.
2359 if (D.getSourceRange().getBegin().isInvalid())
2360 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2361 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00002362 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002363 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002364 }
Douglas Gregor7861a802009-11-03 01:35:08 +00002365 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002366 assert(!getLang().CPlusPlus &&
2367 "There's a C++-specific check for tok::identifier above");
2368 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2369 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2370 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00002371 goto PastIdentifier;
2372 }
2373
2374 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002375 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00002376 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00002377 // Example: 'char (*X)' or 'int (*XX)(void)'
2378 ParseParenDeclarator(D);
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002379 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00002380 // This could be something simple like "int" (in which case the declarator
2381 // portion is empty), if an abstract-declarator is allowed.
2382 D.SetIdentifier(0, Tok.getLocation());
2383 } else {
Douglas Gregord9f92e22009-03-06 23:28:18 +00002384 if (D.getContext() == Declarator::MemberContext)
2385 Diag(Tok, diag::err_expected_member_name_or_semi)
2386 << D.getDeclSpec().getSourceRange();
2387 else if (getLang().CPlusPlus)
Douglas Gregor30d60cb2009-11-03 19:44:04 +00002388 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002389 else
Chris Lattner6d29c102008-11-18 07:48:38 +00002390 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00002391 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00002392 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00002393 }
Mike Stump11289f42009-09-09 15:08:12 +00002394
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00002395 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00002396 assert(D.isPastIdentifier() &&
2397 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00002398
Alexis Hunt96d5c762009-11-21 08:43:09 +00002399 // Don't parse attributes unless we have an identifier.
2400 if (D.getIdentifier() && getLang().CPlusPlus
2401 && isCXX0XAttributeSpecifier(true)) {
2402 SourceLocation AttrEndLoc;
2403 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2404 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2405 }
2406
Chris Lattneracd58a32006-08-06 17:24:14 +00002407 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00002408 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002409 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2410 // In such a case, check if we actually have a function declarator; if it
2411 // is not, the declarator has been fully parsed.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002412 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2413 // When not in file scope, warn for ambiguous function declarators, just
2414 // in case the author intended it as a variable definition.
2415 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2416 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2417 break;
2418 }
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002419 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner76c72282007-10-09 17:33:22 +00002420 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00002421 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00002422 } else {
2423 break;
2424 }
2425 }
2426}
2427
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002428/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2429/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00002430/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002431/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2432///
2433/// direct-declarator:
2434/// '(' declarator ')'
2435/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002436/// direct-declarator '(' parameter-type-list ')'
2437/// direct-declarator '(' identifier-list[opt] ')'
2438/// [GNU] direct-declarator '(' parameter-forward-declarations
2439/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002440///
2441void Parser::ParseParenDeclarator(Declarator &D) {
2442 SourceLocation StartLoc = ConsumeParen();
2443 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002444
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002445 // Eat any attributes before we look at whether this is a grouping or function
2446 // declarator paren. If this is a grouping paren, the attribute applies to
2447 // the type being built up, for example:
2448 // int (__attribute__(()) *x)(long y)
2449 // If this ends up not being a grouping paren, the attribute applies to the
2450 // first argument, for example:
2451 // int (__attribute__(()) int x)
2452 // In either case, we need to eat any attributes to be able to determine what
2453 // sort of paren this is.
2454 //
2455 AttributeList *AttrList = 0;
2456 bool RequiresArg = false;
2457 if (Tok.is(tok::kw___attribute)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +00002458 AttrList = ParseGNUAttributes();
Mike Stump11289f42009-09-09 15:08:12 +00002459
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002460 // We require that the argument list (if this is a non-grouping paren) be
2461 // present even if the attribute list was empty.
2462 RequiresArg = true;
2463 }
Steve Naroff44ac7772008-12-25 14:16:32 +00002464 // Eat any Microsoft extensions.
Eli Friedman53339e02009-06-08 23:27:34 +00002465 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2466 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2467 Tok.is(tok::kw___ptr64)) {
2468 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2469 }
Mike Stump11289f42009-09-09 15:08:12 +00002470
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002471 // If we haven't past the identifier yet (or where the identifier would be
2472 // stored, if this is an abstract declarator), then this is probably just
2473 // grouping parens. However, if this could be an abstract-declarator, then
2474 // this could also be the start of function arguments (consider 'void()').
2475 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00002476
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002477 if (!D.mayOmitIdentifier()) {
2478 // If this can't be an abstract-declarator, this *must* be a grouping
2479 // paren, because we haven't seen the identifier yet.
2480 isGrouping = true;
2481 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise8addf52008-10-06 00:07:55 +00002482 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002483 isDeclarationSpecifier()) { // 'int(int)' is a function.
2484 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2485 // considered to be a type, not a K&R identifier-list.
2486 isGrouping = false;
2487 } else {
2488 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2489 isGrouping = true;
2490 }
Mike Stump11289f42009-09-09 15:08:12 +00002491
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002492 // If this is a grouping paren, handle:
2493 // direct-declarator: '(' declarator ')'
2494 // direct-declarator: '(' attributes declarator ')'
2495 if (isGrouping) {
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002496 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002497 D.setGroupingParens(true);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002498 if (AttrList)
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002499 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00002500
Sebastian Redlbd150f42008-11-21 19:14:01 +00002501 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002502 // Match the ')'.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002503 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00002504
2505 D.setGroupingParens(hadGroupingParens);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002506 D.SetRangeEnd(Loc);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002507 return;
2508 }
Mike Stump11289f42009-09-09 15:08:12 +00002509
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002510 // Okay, if this wasn't a grouping paren, it must be the start of a function
2511 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002512 // identifier (and remember where it would have been), then call into
2513 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002514 D.SetIdentifier(0, Tok.getLocation());
2515
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002516 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002517}
2518
2519/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2520/// declarator D up to a paren, which indicates that we are parsing function
2521/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00002522///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002523/// If AttrList is non-null, then the caller parsed those arguments immediately
2524/// after the open paren - they should be considered to be the first argument of
2525/// a parameter. If RequiresArg is true, then the first argument of the
2526/// function is required to be present and required to not be an identifier
2527/// list.
2528///
Chris Lattneracd58a32006-08-06 17:24:14 +00002529/// This method also handles this portion of the grammar:
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002530/// parameter-type-list: [C99 6.7.5]
2531/// parameter-list
2532/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002533/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002534///
2535/// parameter-list: [C99 6.7.5]
2536/// parameter-declaration
2537/// parameter-list ',' parameter-declaration
2538///
2539/// parameter-declaration: [C99 6.7.5]
2540/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002541/// [C++] declaration-specifiers declarator '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002542/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00002543/// declaration-specifiers abstract-declarator[opt]
2544/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00002545/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00002546/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002547///
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002548/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redlf769df52009-03-24 22:27:57 +00002549/// and "exception-specification[opt]".
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002550///
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002551void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2552 AttributeList *AttrList,
2553 bool RequiresArg) {
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002554 // lparen is already consumed!
2555 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump11289f42009-09-09 15:08:12 +00002556
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002557 // This parameter list may be empty.
Chris Lattner76c72282007-10-09 17:33:22 +00002558 if (Tok.is(tok::r_paren)) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002559 if (RequiresArg) {
Chris Lattner6d29c102008-11-18 07:48:38 +00002560 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002561 delete AttrList;
2562 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002563
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002564 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2565 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002566
2567 // cv-qualifier-seq[opt].
2568 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002569 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002570 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002571 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002572 llvm::SmallVector<TypeTy*, 2> Exceptions;
2573 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002574 if (getLang().CPlusPlus) {
Chris Lattnercf0bab22008-12-18 07:02:59 +00002575 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002576 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002577 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002578
2579 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002580 if (Tok.is(tok::kw_throw)) {
2581 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002582 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002583 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002584 hasAnyExceptionSpec);
2585 assert(Exceptions.size() == ExceptionRanges.size() &&
2586 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002587 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002588 }
2589
Chris Lattner371ed4e2008-04-06 06:57:35 +00002590 // Remember that we parsed a function type, and remember the attributes.
Chris Lattneracd58a32006-08-06 17:24:14 +00002591 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002592 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattner371ed4e2008-04-06 06:57:35 +00002593 /*variadic*/ false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002594 SourceLocation(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002595 /*arglist*/ 0, 0,
2596 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002597 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002598 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002599 Exceptions.data(),
2600 ExceptionRanges.data(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002601 Exceptions.size(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002602 LParenLoc, RParenLoc, D),
2603 EndLoc);
Chris Lattner371ed4e2008-04-06 06:57:35 +00002604 return;
Sebastian Redld6434562009-05-29 18:02:33 +00002605 }
2606
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002607 // Alternatively, this parameter list may be an identifier list form for a
2608 // K&R-style function: void foo(a,b,c)
Steve Naroffb0486722009-01-28 19:16:40 +00002609 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Naroff3b6a4bd2009-01-30 14:23:32 +00002610 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002611 // K&R identifier lists can't have typedefs as identifiers, per
2612 // C99 6.7.5.3p11.
Steve Naroffb0486722009-01-28 19:16:40 +00002613 if (RequiresArg) {
2614 Diag(Tok, diag::err_argument_required_after_attribute);
2615 delete AttrList;
2616 }
Steve Naroffb0486722009-01-28 19:16:40 +00002617 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2618 // normal declarators, not for abstract-declarators.
2619 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002620 }
Chris Lattner371ed4e2008-04-06 06:57:35 +00002621 }
Mike Stump11289f42009-09-09 15:08:12 +00002622
Chris Lattner371ed4e2008-04-06 06:57:35 +00002623 // Finally, a normal, non-empty parameter type list.
Mike Stump11289f42009-09-09 15:08:12 +00002624
Chris Lattner371ed4e2008-04-06 06:57:35 +00002625 // Build up an array of information about the parsed arguments.
2626 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002627
2628 // Enter function-declaration scope, limiting any declarators to the
2629 // function prototype scope, including parameter declarators.
Chris Lattnerbd61a952009-03-05 00:00:31 +00002630 ParseScope PrototypeScope(this,
2631 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00002632
Chris Lattner371ed4e2008-04-06 06:57:35 +00002633 bool IsVariadic = false;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002634 SourceLocation EllipsisLoc;
Chris Lattner371ed4e2008-04-06 06:57:35 +00002635 while (1) {
2636 if (Tok.is(tok::ellipsis)) {
2637 IsVariadic = true;
Douglas Gregor94349fd2009-02-18 07:07:28 +00002638 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002639 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00002640 }
Mike Stump11289f42009-09-09 15:08:12 +00002641
Chris Lattner371ed4e2008-04-06 06:57:35 +00002642 SourceLocation DSStart = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002643
Chris Lattner371ed4e2008-04-06 06:57:35 +00002644 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00002645 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002646 DeclSpec DS;
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00002647
2648 // If the caller parsed attributes for the first argument, add them now.
2649 if (AttrList) {
2650 DS.AddAttributes(AttrList);
2651 AttrList = 0; // Only apply the attributes to the first parameter.
2652 }
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002653 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002654
Chris Lattner371ed4e2008-04-06 06:57:35 +00002655 // Parse the declarator. This is "PrototypeContext", because we must
2656 // accept either 'declarator' or 'abstract-declarator' here.
2657 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2658 ParseDeclarator(ParmDecl);
2659
2660 // Parse GNU attributes, if present.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002661 if (Tok.is(tok::kw___attribute)) {
2662 SourceLocation Loc;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002663 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002664 ParmDecl.AddAttributes(AttrList, Loc);
2665 }
Mike Stump11289f42009-09-09 15:08:12 +00002666
Chris Lattner371ed4e2008-04-06 06:57:35 +00002667 // Remember this parsed parameter in ParamInfo.
2668 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00002669
Douglas Gregor4d87df52008-12-16 21:30:33 +00002670 // DefArgToks is used when the parsing of default arguments needs
2671 // to be delayed.
2672 CachedTokens *DefArgToks = 0;
2673
Chris Lattner371ed4e2008-04-06 06:57:35 +00002674 // If no parameter was specified, verify that *something* was specified,
2675 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00002676 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2677 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00002678 // Completely missing, emit error.
2679 Diag(DSStart, diag::err_missing_param);
2680 } else {
2681 // Otherwise, we have something. Add it and let semantic analysis try
2682 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00002683
Chris Lattner371ed4e2008-04-06 06:57:35 +00002684 // Inform the actions module about the parameter declarator, so it gets
2685 // added to the current scope.
Chris Lattner83f095c2009-03-28 19:18:32 +00002686 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002687
2688 // Parse the default argument, if any. We parse the default
2689 // arguments in all dialects; the semantic analysis in
2690 // ActOnParamDefaultArgument will reject the default argument in
2691 // C.
2692 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00002693 SourceLocation EqualLoc = Tok.getLocation();
2694
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002695 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00002696 if (D.getContext() == Declarator::MemberContext) {
2697 // If we're inside a class definition, cache the tokens
2698 // corresponding to the default argument. We'll actually parse
2699 // them when we see the end of the class definition.
2700 // FIXME: Templates will require something similar.
2701 // FIXME: Can we use a smart pointer for Toks?
2702 DefArgToks = new CachedTokens;
2703
Mike Stump11289f42009-09-09 15:08:12 +00002704 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002705 tok::semi, false)) {
2706 delete DefArgToks;
2707 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00002708 Actions.ActOnParamDefaultArgumentError(Param);
2709 } else
Mike Stump11289f42009-09-09 15:08:12 +00002710 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00002711 (*DefArgToks)[1].getLocation());
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002712 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00002713 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00002714 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002715
Douglas Gregor4d87df52008-12-16 21:30:33 +00002716 OwningExprResult DefArgResult(ParseAssignmentExpression());
2717 if (DefArgResult.isInvalid()) {
2718 Actions.ActOnParamDefaultArgumentError(Param);
2719 SkipUntil(tok::comma, tok::r_paren, true, true);
2720 } else {
2721 // Inform the actions module about the default argument
2722 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redl6d4256c2009-03-15 17:47:39 +00002723 move(DefArgResult));
Douglas Gregor4d87df52008-12-16 21:30:33 +00002724 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002725 }
2726 }
Mike Stump11289f42009-09-09 15:08:12 +00002727
2728 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2729 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00002730 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00002731 }
2732
2733 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00002734 if (Tok.isNot(tok::comma)) {
2735 if (Tok.is(tok::ellipsis)) {
2736 IsVariadic = true;
2737 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2738
2739 if (!getLang().CPlusPlus) {
2740 // We have ellipsis without a preceding ',', which is ill-formed
2741 // in C. Complain and provide the fix.
2742 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2743 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2744 }
2745 }
2746
2747 break;
2748 }
Mike Stump11289f42009-09-09 15:08:12 +00002749
Chris Lattner371ed4e2008-04-06 06:57:35 +00002750 // Consume the comma.
2751 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00002752 }
Mike Stump11289f42009-09-09 15:08:12 +00002753
Chris Lattner371ed4e2008-04-06 06:57:35 +00002754 // Leave prototype scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00002755 PrototypeScope.Exit();
Mike Stump11289f42009-09-09 15:08:12 +00002756
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002757 // If we have the closing ')', eat it.
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002758 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2759 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002760
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002761 DeclSpec DS;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002762 bool hasExceptionSpec = false;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002763 SourceLocation ThrowLoc;
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002764 bool hasAnyExceptionSpec = false;
Sebastian Redld6434562009-05-29 18:02:33 +00002765 llvm::SmallVector<TypeTy*, 2> Exceptions;
2766 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002767
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002768 if (getLang().CPlusPlus) {
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002769 // Parse cv-qualifier-seq[opt].
Chris Lattnercf0bab22008-12-18 07:02:59 +00002770 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002771 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002772 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor2afd0be2008-11-25 03:22:00 +00002773
2774 // Parse exception-specification[opt].
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002775 if (Tok.is(tok::kw_throw)) {
2776 hasExceptionSpec = true;
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002777 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002778 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redld6434562009-05-29 18:02:33 +00002779 hasAnyExceptionSpec);
2780 assert(Exceptions.size() == ExceptionRanges.size() &&
2781 "Produced different number of exception types and ranges.");
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002782 }
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002783 }
2784
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00002785 // Remember that we parsed a function type, and remember the attributes.
Chris Lattner371ed4e2008-04-06 06:57:35 +00002786 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002787 EllipsisLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00002788 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00002789 DS.getTypeQualifiers(),
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002790 hasExceptionSpec, ThrowLoc,
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002791 hasAnyExceptionSpec,
Sebastian Redld6434562009-05-29 18:02:33 +00002792 Exceptions.data(),
2793 ExceptionRanges.data(),
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002794 Exceptions.size(),
2795 LParenLoc, RParenLoc, D),
2796 EndLoc);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002797}
Chris Lattneracd58a32006-08-06 17:24:14 +00002798
Chris Lattner6c940e62008-04-06 06:34:08 +00002799/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2800/// we found a K&R-style identifier list instead of a type argument list. The
2801/// current token is known to be the first identifier in the list.
2802///
2803/// identifier-list: [C99 6.7.5]
2804/// identifier
2805/// identifier-list ',' identifier
2806///
2807void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2808 Declarator &D) {
2809 // Build up an array of information about the parsed arguments.
2810 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2811 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump11289f42009-09-09 15:08:12 +00002812
Chris Lattner6c940e62008-04-06 06:34:08 +00002813 // If there was no identifier specified for the declarator, either we are in
2814 // an abstract-declarator, or we are in a parameter declarator which was found
2815 // to be abstract. In abstract-declarators, identifier lists are not valid:
2816 // diagnose this.
2817 if (!D.getIdentifier())
2818 Diag(Tok, diag::ext_ident_list_in_param);
2819
2820 // Tok is known to be the first identifier in the list. Remember this
2821 // identifier in ParamInfo.
Chris Lattner285a3e42008-04-06 06:50:56 +00002822 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner6c940e62008-04-06 06:34:08 +00002823 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattner83f095c2009-03-28 19:18:32 +00002824 Tok.getLocation(),
2825 DeclPtrTy()));
Mike Stump11289f42009-09-09 15:08:12 +00002826
Chris Lattner9186f552008-04-06 06:39:19 +00002827 ConsumeToken(); // eat the first identifier.
Mike Stump11289f42009-09-09 15:08:12 +00002828
Chris Lattner6c940e62008-04-06 06:34:08 +00002829 while (Tok.is(tok::comma)) {
2830 // Eat the comma.
2831 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002832
Chris Lattner9186f552008-04-06 06:39:19 +00002833 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner6c940e62008-04-06 06:34:08 +00002834 if (Tok.isNot(tok::identifier)) {
2835 Diag(Tok, diag::err_expected_ident);
Chris Lattner9186f552008-04-06 06:39:19 +00002836 SkipUntil(tok::r_paren);
2837 return;
Chris Lattner6c940e62008-04-06 06:34:08 +00002838 }
Chris Lattner67b450c2008-04-06 06:47:48 +00002839
Chris Lattner6c940e62008-04-06 06:34:08 +00002840 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattner67b450c2008-04-06 06:47:48 +00002841
2842 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregor8a6be5e2009-02-04 17:00:24 +00002843 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerebad6a22008-11-19 07:37:42 +00002844 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump11289f42009-09-09 15:08:12 +00002845
Chris Lattner6c940e62008-04-06 06:34:08 +00002846 // Verify that the argument identifier has not already been mentioned.
2847 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00002848 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner9186f552008-04-06 06:39:19 +00002849 } else {
2850 // Remember this identifier in ParamInfo.
Chris Lattner6c940e62008-04-06 06:34:08 +00002851 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattner83f095c2009-03-28 19:18:32 +00002852 Tok.getLocation(),
2853 DeclPtrTy()));
Chris Lattner9186f552008-04-06 06:39:19 +00002854 }
Mike Stump11289f42009-09-09 15:08:12 +00002855
Chris Lattner6c940e62008-04-06 06:34:08 +00002856 // Eat the identifier.
2857 ConsumeToken();
2858 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002859
2860 // If we have the closing ')', eat it and we're done.
2861 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2862
Chris Lattner9186f552008-04-06 06:39:19 +00002863 // Remember that we parsed a function type, and remember the attributes. This
2864 // function type is always a K&R style function type, which is not varargs and
2865 // has no prototype.
2866 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor94349fd2009-02-18 07:07:28 +00002867 SourceLocation(),
Chris Lattner9186f552008-04-06 06:39:19 +00002868 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl2b9cacb2009-04-29 17:30:04 +00002869 /*TypeQuals*/0,
Sebastian Redlfb3f1792009-05-31 11:47:27 +00002870 /*exception*/false,
2871 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis20cf1912009-08-19 23:14:54 +00002872 LParenLoc, RLoc, D),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002873 RLoc);
Chris Lattner6c940e62008-04-06 06:34:08 +00002874}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00002875
Chris Lattnere8074e62006-08-06 18:30:15 +00002876/// [C90] direct-declarator '[' constant-expression[opt] ']'
2877/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2878/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2879/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2880/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2881void Parser::ParseBracketDeclarator(Declarator &D) {
Chris Lattner04132372006-10-16 06:12:55 +00002882 SourceLocation StartLoc = ConsumeBracket();
Mike Stump11289f42009-09-09 15:08:12 +00002883
Chris Lattner84a11622008-12-18 07:27:21 +00002884 // C array syntax has many features, but by-far the most common is [] and [4].
2885 // This code does a fast path to handle some of the most obvious cases.
2886 if (Tok.getKind() == tok::r_square) {
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002887 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002888 //FIXME: Use these
2889 CXX0XAttributeList Attr;
2890 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
2891 Attr = ParseCXX0XAttributes();
2892 }
2893
Chris Lattner84a11622008-12-18 07:27:21 +00002894 // Remember that we parsed the empty array type.
2895 OwningExprResult NumElements(Actions);
Douglas Gregor04318252009-07-06 15:59:29 +00002896 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2897 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002898 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002899 return;
2900 } else if (Tok.getKind() == tok::numeric_constant &&
2901 GetLookAheadToken(1).is(tok::r_square)) {
2902 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlffbcf962009-01-18 18:53:16 +00002903 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner84a11622008-12-18 07:27:21 +00002904 ConsumeToken();
2905
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002906 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Alexis Hunt96d5c762009-11-21 08:43:09 +00002907 //FIXME: Use these
2908 CXX0XAttributeList Attr;
2909 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2910 Attr = ParseCXX0XAttributes();
2911 }
Chris Lattner84a11622008-12-18 07:27:21 +00002912
2913 // If there was an error parsing the assignment-expression, recover.
2914 if (ExprRes.isInvalid())
2915 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump11289f42009-09-09 15:08:12 +00002916
Chris Lattner84a11622008-12-18 07:27:21 +00002917 // Remember that we parsed a array type, and remember its features.
Douglas Gregor04318252009-07-06 15:59:29 +00002918 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2919 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002920 EndLoc);
Chris Lattner84a11622008-12-18 07:27:21 +00002921 return;
2922 }
Mike Stump11289f42009-09-09 15:08:12 +00002923
Chris Lattnere8074e62006-08-06 18:30:15 +00002924 // If valid, this location is the position where we read the 'static' keyword.
2925 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00002926 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002927 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002928
Chris Lattnere8074e62006-08-06 18:30:15 +00002929 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002930 // Type qualifiers in an array subscript are a C99 feature.
Chris Lattnere8074e62006-08-06 18:30:15 +00002931 DeclSpec DS;
Chris Lattnercf0bab22008-12-18 07:02:59 +00002932 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00002933
Chris Lattnere8074e62006-08-06 18:30:15 +00002934 // If we haven't already read 'static', check to see if there is one after the
2935 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00002936 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00002937 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002938
Chris Lattnere8074e62006-08-06 18:30:15 +00002939 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00002940 bool isStar = false;
Sebastian Redlc13f2682008-12-09 20:22:58 +00002941 OwningExprResult NumElements(Actions);
Mike Stump11289f42009-09-09 15:08:12 +00002942
Chris Lattner521ff2b2008-04-06 05:26:30 +00002943 // Handle the case where we have '[*]' as the array size. However, a leading
2944 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2945 // the the token after the star is a ']'. Since stars in arrays are
2946 // infrequent, use of lookahead is not costly here.
2947 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00002948 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00002949
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002950 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00002951 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00002952 StaticLoc = SourceLocation(); // Drop the static.
2953 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00002954 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00002955 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00002956 // Note, in C89, this production uses the constant-expr production instead
2957 // of assignment-expr. The only difference is that assignment-expr allows
2958 // things like '=' and '*='. Sema rejects these in C89 mode because they
2959 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00002960
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00002961 // Parse the constant-expression or assignment-expression now (depending
2962 // on dialect).
2963 if (getLang().CPlusPlus)
2964 NumElements = ParseConstantExpression();
2965 else
2966 NumElements = ParseAssignmentExpression();
Chris Lattner62591722006-08-12 18:40:58 +00002967 }
Mike Stump11289f42009-09-09 15:08:12 +00002968
Chris Lattner62591722006-08-12 18:40:58 +00002969 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002970 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00002971 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00002972 // If the expression was invalid, skip it.
2973 SkipUntil(tok::r_square);
2974 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00002975 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002976
2977 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2978
Alexis Hunt96d5c762009-11-21 08:43:09 +00002979 //FIXME: Use these
2980 CXX0XAttributeList Attr;
2981 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2982 Attr = ParseCXX0XAttributes();
2983 }
2984
Chris Lattner84a11622008-12-18 07:27:21 +00002985 // Remember that we parsed a array type, and remember its features.
Chris Lattnercbc426d2006-12-02 06:43:02 +00002986 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2987 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00002988 NumElements.release(),
2989 StartLoc, EndLoc),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002990 EndLoc);
Chris Lattnere8074e62006-08-06 18:30:15 +00002991}
2992
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00002993/// [GNU] typeof-specifier:
2994/// typeof ( expressions )
2995/// typeof ( type-name )
2996/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00002997///
2998void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00002999 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003000 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00003001 SourceLocation StartLoc = ConsumeToken();
3002
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003003 bool isCastExpr;
3004 TypeTy *CastTy;
3005 SourceRange CastRange;
3006 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3007 isCastExpr,
3008 CastTy,
3009 CastRange);
3010
3011 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003012 // FIXME: Not accurate, the range gets one token more than it should.
3013 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003014 else
3015 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00003016
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003017 if (isCastExpr) {
3018 if (!CastTy) {
3019 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003020 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00003021 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003022
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003023 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003024 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003025 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3026 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003027 DiagID, CastTy))
3028 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00003029 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003030 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003031
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003032 // If we get here, the operand to the typeof was an expresion.
3033 if (Operand.isInvalid()) {
3034 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00003035 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00003036 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00003037
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003038 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00003039 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00003040 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3041 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00003042 DiagID, Operand.release()))
3043 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00003044}