blob: 5a5f5092db7fde3d02de448f91951d4c89a93e30 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000018#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redlef65f062009-05-29 18:02:33 +000031Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000035
Reid Spencer5f016e22007-07-11 17:01:13 +000036 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000039 if (Range)
40 *Range = DeclaratorInfo.getSourceRange();
41
Chris Lattnereaaebc72009-04-25 08:06:05 +000042 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000043 return true;
44
45 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000046}
47
Sean Huntbbd37c62009-11-21 08:43:09 +000048/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +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
63/// attrib-name
64/// attrib-name '(' identifier ')'
65/// attrib-name '(' identifier ',' nonempty-expr-list ')'
66/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
67///
68/// [GNU] attrib-name:
69/// identifier
70/// typespec
71/// typequal
72/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000073///
Reid Spencer5f016e22007-07-11 17:01:13 +000074/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-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
Reid Spencer5f016e22007-07-11 17:01:13 +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.
83
Sean Huntbbd37c62009-11-21 08:43:09 +000084AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
85 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087 AttributeList *CurrAttr = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner04d66662007-10-09 17:33:22 +000089 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner04d66662007-10-09 17:33:22 +0000101 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
102 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000103
104 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +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 Stump1eb44332009-09-09 15:08:12 +0000112
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000114 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattner04d66662007-10-09 17:33:22 +0000117 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
119 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000120
121 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 // __attribute__(( mode(byte) ))
123 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000124 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000126 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 ConsumeToken();
128 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000129 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 // now parse the non-empty comma separated list of expressions
133 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000134 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000135 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 ArgExprsOk = false;
137 SkipUntil(tok::r_paren);
138 break;
139 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000140 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 }
Chris Lattner04d66662007-10-09 17:33:22 +0000142 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 break;
144 ConsumeToken(); // Eat the comma, move to the next argument
145 }
Chris Lattner04d66662007-10-09 17:33:22 +0000146 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000148 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
149 AttrNameLoc, ParmName, ParmLoc,
150 ArgExprs.take(), ArgExprs.size(),
151 CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
153 }
154 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000155 switch (Tok.getKind()) {
156 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // __attribute__(( nonnull() ))
159 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000160 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000162 break;
163 case tok::kw_char:
164 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000165 case tok::kw_char16_t:
166 case tok::kw_char32_t:
Nate Begeman6f3d8382009-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();
Sean Huntbbd37c62009-11-21 08:43:09 +0000180 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Nate Begeman6f3d8382009-06-26 06:32:41 +0000181 0, SourceLocation(), 0, 0, CurrAttr);
182 if (Tok.is(tok::r_paren))
183 ConsumeParen();
184 break;
185 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000187 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 // now parse the list of expressions
191 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000192 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000193 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 ArgExprsOk = false;
195 SkipUntil(tok::r_paren);
196 break;
197 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000198 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 }
Chris Lattner04d66662007-10-09 17:33:22 +0000200 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 break;
202 ConsumeToken(); // Eat the comma, move to the next argument
203 }
204 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000205 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000207 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Sean Huntbbd37c62009-11-21 08:43:09 +0000208 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
209 ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 CurrAttr);
211 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000212 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 }
214 }
215 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000216 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 0, SourceLocation(), 0, 0, CurrAttr);
218 }
219 }
220 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000222 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 }
229 return CurrAttr;
230}
231
Eli Friedmana23b4852009-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 Friedman290eeb02009-06-08 23:27:34 +0000241AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000242 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000243
Steve Narofff59e17e2008-12-24 20:59:21 +0000244 ConsumeToken();
Eli Friedmana23b4852009-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 Friedman290eeb02009-06-08 23:27:34 +0000250 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-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();
Sean Huntbbd37c62009-11-21 08:43:09 +0000260 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedmana23b4852009-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 {
Sean Huntbbd37c62009-11-21 08:43:09 +0000267 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
268 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000269 }
270 }
271 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
272 SkipUntil(tok::r_paren, false);
Eli Friedman290eeb02009-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;
Sean Huntbbd37c62009-11-21 08:43:09 +0000287 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman290eeb02009-06-08 23:27:34 +0000288 SourceLocation(), 0, 0, CurrAttr, true);
289 }
290 return CurrAttr;
Steve Narofff59e17e2008-12-24 20:59:21 +0000291}
292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293/// ParseDeclaration - Parse a full 'declaration', which consists of
294/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000295/// 'Context' should be a Declarator::TheContext value. This returns the
296/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000297///
298/// declaration: [C99 6.7]
299/// block-declaration ->
300/// simple-declaration
301/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000302/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000303/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000304/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000305/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000306/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000307/// others... [FIXME]
308///
Chris Lattner97144fc2009-04-02 04:16:50 +0000309Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000310 SourceLocation &DeclEnd,
311 CXX0XAttributeList Attr) {
Chris Lattner682bf922009-03-29 16:50:03 +0000312 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000313 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000314 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000315 case tok::kw_export:
Sean Huntbbd37c62009-11-21 08:43:09 +0000316 if (Attr.HasAttr)
317 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
318 << Attr.Range;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000319 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000320 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000321 case tok::kw_namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +0000322 if (Attr.HasAttr)
323 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
324 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000325 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000326 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000327 case tok::kw_using:
Sean Huntbbd37c62009-11-21 08:43:09 +0000328 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner682bf922009-03-29 16:50:03 +0000329 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000330 case tok::kw_static_assert:
Sean Huntbbd37c62009-11-21 08:43:09 +0000331 if (Attr.HasAttr)
332 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
333 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000334 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000335 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000336 default:
Sean Huntbbd37c62009-11-21 08:43:09 +0000337 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000338 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000339
Chris Lattner682bf922009-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 Lattner8f08cb72007-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 Lattnercd147752009-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,
Sean Huntbbd37c62009-11-21 08:43:09 +0000353 SourceLocation &DeclEnd,
354 AttributeList *Attr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000355 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000356 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000357 if (Attr)
358 DS.AddAttributes(Attr);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000359 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
360 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
363 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000364 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000366 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall54abf7d2009-11-04 02:18:39 +0000367 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000368 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 }
Mike Stump1eb44332009-09-09 15:08:12 +0000370
John McCalld8ac0572009-11-03 19:26:08 +0000371 DeclGroupPtrTy DG = ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false,
372 &DeclEnd);
373 return DG;
374}
Mike Stump1eb44332009-09-09 15:08:12 +0000375
John McCalld8ac0572009-11-03 19:26:08 +0000376/// ParseDeclGroup - Having concluded that this is either a function
377/// definition or a group of object declarations, actually parse the
378/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000379Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
380 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000381 bool AllowFunctionDefinitions,
382 SourceLocation *DeclEnd) {
383 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000384 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000385 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000386
John McCalld8ac0572009-11-03 19:26:08 +0000387 // Bail out if the first declarator didn't seem well-formed.
388 if (!D.hasName() && !D.mayOmitIdentifier()) {
389 // Skip until ; or }.
390 SkipUntil(tok::r_brace, true, true);
391 if (Tok.is(tok::semi))
392 ConsumeToken();
393 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
John McCalld8ac0572009-11-03 19:26:08 +0000396 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
397 if (isDeclarationAfterDeclarator()) {
398 // Fall though. We have to check this first, though, because
399 // __attribute__ might be the start of a function definition in
400 // (extended) K&R C.
401 } else if (isStartOfFunctionDefinition()) {
402 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
403 Diag(Tok, diag::err_function_declared_typedef);
404
405 // Recover by treating the 'typedef' as spurious.
406 DS.ClearStorageClassSpecs();
407 }
408
409 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
410 return Actions.ConvertDeclToDeclGroup(TheDecl);
411 } else {
412 Diag(Tok, diag::err_expected_fn_body);
413 SkipUntil(tok::semi);
414 return DeclGroupPtrTy();
415 }
416 }
417
418 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
419 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000420 D.complete(FirstDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000421 if (FirstDecl.get())
422 DeclsInGroup.push_back(FirstDecl);
423
424 // If we don't have a comma, it is either the end of the list (a ';') or an
425 // error, bail out.
426 while (Tok.is(tok::comma)) {
427 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000428 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000429
430 // Parse the next declarator.
431 D.clear();
432
433 // Accept attributes in an init-declarator. In the first declarator in a
434 // declaration, these would be part of the declspec. In subsequent
435 // declarators, they become part of the declarator itself, so that they
436 // don't apply to declarators after *this* one. Examples:
437 // short __attribute__((common)) var; -> declspec
438 // short var __attribute__((common)); -> declarator
439 // short x, __attribute__((common)) var; -> declarator
440 if (Tok.is(tok::kw___attribute)) {
441 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000442 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000443 D.AddAttributes(AttrList, Loc);
444 }
445
446 ParseDeclarator(D);
447
448 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000449 D.complete(ThisDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000450 if (ThisDecl.get())
451 DeclsInGroup.push_back(ThisDecl);
452 }
453
454 if (DeclEnd)
455 *DeclEnd = Tok.getLocation();
456
457 if (Context != Declarator::ForContext &&
458 ExpectAndConsume(tok::semi,
459 Context == Declarator::FileContext
460 ? diag::err_invalid_token_after_toplevel_declarator
461 : diag::err_expected_semi_declaration)) {
462 SkipUntil(tok::r_brace, true, true);
463 if (Tok.is(tok::semi))
464 ConsumeToken();
465 }
466
467 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
468 DeclsInGroup.data(),
469 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000470}
471
Douglas Gregor1426e532009-05-12 21:31:51 +0000472/// \brief Parse 'declaration' after parsing 'declaration-specifiers
473/// declarator'. This method parses the remainder of the declaration
474/// (including any attributes or initializer, among other things) and
475/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000476///
Reid Spencer5f016e22007-07-11 17:01:13 +0000477/// init-declarator: [C99 6.7]
478/// declarator
479/// declarator '=' initializer
480/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
481/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000482/// [C++] declarator initializer[opt]
483///
484/// [C++] initializer:
485/// [C++] '=' initializer-clause
486/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000487/// [C++0x] '=' 'default' [TODO]
488/// [C++0x] '=' 'delete'
489///
490/// According to the standard grammar, =default and =delete are function
491/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000492///
Douglas Gregore542c862009-06-23 23:11:28 +0000493Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
494 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000495 // If a simple-asm-expr is present, parse it.
496 if (Tok.is(tok::kw_asm)) {
497 SourceLocation Loc;
498 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
499 if (AsmLabel.isInvalid()) {
500 SkipUntil(tok::semi, true, true);
501 return DeclPtrTy();
502 }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Douglas Gregor1426e532009-05-12 21:31:51 +0000504 D.setAsmLabel(AsmLabel.release());
505 D.SetRangeEnd(Loc);
506 }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Douglas Gregor1426e532009-05-12 21:31:51 +0000508 // If attributes are present, parse them.
509 if (Tok.is(tok::kw___attribute)) {
510 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000511 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000512 D.AddAttributes(AttrList, Loc);
513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Douglas Gregor1426e532009-05-12 21:31:51 +0000515 // Inform the current actions module that we just parsed this declarator.
Douglas Gregord5a423b2009-09-25 18:43:00 +0000516 DeclPtrTy ThisDecl;
517 switch (TemplateInfo.Kind) {
518 case ParsedTemplateInfo::NonTemplate:
519 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
520 break;
521
522 case ParsedTemplateInfo::Template:
523 case ParsedTemplateInfo::ExplicitSpecialization:
524 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregore542c862009-06-23 23:11:28 +0000525 Action::MultiTemplateParamsArg(Actions,
526 TemplateInfo.TemplateParams->data(),
527 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000528 D);
529 break;
530
531 case ParsedTemplateInfo::ExplicitInstantiation: {
532 Action::DeclResult ThisRes
533 = Actions.ActOnExplicitInstantiation(CurScope,
534 TemplateInfo.ExternLoc,
535 TemplateInfo.TemplateLoc,
536 D);
537 if (ThisRes.isInvalid()) {
538 SkipUntil(tok::semi, true, true);
539 return DeclPtrTy();
540 }
541
542 ThisDecl = ThisRes.get();
543 break;
544 }
545 }
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Douglas Gregor1426e532009-05-12 21:31:51 +0000547 // Parse declarator '=' initializer.
548 if (Tok.is(tok::equal)) {
549 ConsumeToken();
550 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
551 SourceLocation DelLoc = ConsumeToken();
552 Actions.SetDeclDeleted(ThisDecl, DelLoc);
553 } else {
John McCall731ad842009-12-19 09:28:58 +0000554 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
555 EnterScope(0);
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000556 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000557 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000558
Douglas Gregor1426e532009-05-12 21:31:51 +0000559 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000560
John McCall731ad842009-12-19 09:28:58 +0000561 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000562 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000563 ExitScope();
564 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000565
Douglas Gregor1426e532009-05-12 21:31:51 +0000566 if (Init.isInvalid()) {
567 SkipUntil(tok::semi, true, true);
568 return DeclPtrTy();
569 }
Anders Carlsson9abf2ae2009-08-16 05:13:48 +0000570 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000571 }
572 } else if (Tok.is(tok::l_paren)) {
573 // Parse C++ direct initializer: '(' expression-list ')'
574 SourceLocation LParenLoc = ConsumeParen();
575 ExprVector Exprs(Actions);
576 CommaLocsTy CommaLocs;
577
Douglas Gregorb4debae2009-12-22 17:47:17 +0000578 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
579 EnterScope(0);
580 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
581 }
582
Douglas Gregor1426e532009-05-12 21:31:51 +0000583 if (ParseExpressionList(Exprs, CommaLocs)) {
584 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000585
586 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
587 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
588 ExitScope();
589 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000590 } else {
591 // Match the ')'.
592 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
593
594 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
595 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000596
597 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
598 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
599 ExitScope();
600 }
601
Douglas Gregor1426e532009-05-12 21:31:51 +0000602 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
603 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000604 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000605 }
606 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000607 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000608 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
609 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000610 }
611
612 return ThisDecl;
613}
614
Reid Spencer5f016e22007-07-11 17:01:13 +0000615/// ParseSpecifierQualifierList
616/// specifier-qualifier-list:
617/// type-specifier specifier-qualifier-list[opt]
618/// type-qualifier specifier-qualifier-list[opt]
619/// [GNU] attributes specifier-qualifier-list[opt]
620///
621void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
622 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
623 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000625
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 // Validate declspec for type-name.
627 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000628 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
629 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 // Issue diagnostic and remove storage class if present.
633 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
634 if (DS.getStorageClassSpecLoc().isValid())
635 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
636 else
637 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
638 DS.ClearStorageClassSpecs();
639 }
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 // Issue diagnostic and remove function specfier if present.
642 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000643 if (DS.isInlineSpecified())
644 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
645 if (DS.isVirtualSpecified())
646 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
647 if (DS.isExplicitSpecified())
648 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 DS.ClearFunctionSpecs();
650 }
651}
652
Chris Lattnerc199ab32009-04-12 20:42:31 +0000653/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
654/// specified token is valid after the identifier in a declarator which
655/// immediately follows the declspec. For example, these things are valid:
656///
657/// int x [ 4]; // direct-declarator
658/// int x ( int y); // direct-declarator
659/// int(int x ) // direct-declarator
660/// int x ; // simple-declaration
661/// int x = 17; // init-declarator-list
662/// int x , y; // init-declarator-list
663/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000664/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000665/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000666///
667/// This is not, because 'x' does not immediately follow the declspec (though
668/// ')' happens to be valid anyway).
669/// int (x)
670///
671static bool isValidAfterIdentifierInDeclarator(const Token &T) {
672 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
673 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000674 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000675}
676
Chris Lattnere40c2952009-04-14 21:34:55 +0000677
678/// ParseImplicitInt - This method is called when we have an non-typename
679/// identifier in a declspec (which normally terminates the decl spec) when
680/// the declspec has no type specifier. In this case, the declspec is either
681/// malformed or is "implicit int" (in K&R and C89).
682///
683/// This method handles diagnosing this prettily and returns false if the
684/// declspec is done being processed. If it recovers and thinks there may be
685/// other pieces of declspec after it, it returns true.
686///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000687bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000688 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000689 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000690 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Chris Lattnere40c2952009-04-14 21:34:55 +0000692 SourceLocation Loc = Tok.getLocation();
693 // If we see an identifier that is not a type name, we normally would
694 // parse it as the identifer being declared. However, when a typename
695 // is typo'd or the definition is not included, this will incorrectly
696 // parse the typename as the identifier name and fall over misparsing
697 // later parts of the diagnostic.
698 //
699 // As such, we try to do some look-ahead in cases where this would
700 // otherwise be an "implicit-int" case to see if this is invalid. For
701 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
702 // an identifier with implicit int, we'd get a parse error because the
703 // next token is obviously invalid for a type. Parse these as a case
704 // with an invalid type specifier.
705 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Chris Lattnere40c2952009-04-14 21:34:55 +0000707 // Since we know that this either implicit int (which is rare) or an
708 // error, we'd do lookahead to try to do better recovery.
709 if (isValidAfterIdentifierInDeclarator(NextToken())) {
710 // If this token is valid for implicit int, e.g. "static x = 4", then
711 // we just avoid eating the identifier, so it will be parsed as the
712 // identifier in the declarator.
713 return false;
714 }
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Chris Lattnere40c2952009-04-14 21:34:55 +0000716 // Otherwise, if we don't consume this token, we are going to emit an
717 // error anyway. Try to recover from various common problems. Check
718 // to see if this was a reference to a tag name without a tag specified.
719 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000720 //
721 // C++ doesn't need this, and isTagName doesn't take SS.
722 if (SS == 0) {
723 const char *TagName = 0;
724 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Chris Lattnere40c2952009-04-14 21:34:55 +0000726 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
727 default: break;
728 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
729 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
730 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
731 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
732 }
Mike Stump1eb44332009-09-09 15:08:12 +0000733
Chris Lattnerf4382f52009-04-14 22:17:06 +0000734 if (TagName) {
735 Diag(Loc, diag::err_use_of_tag_name_without_tag)
736 << Tok.getIdentifierInfo() << TagName
737 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Chris Lattnerf4382f52009-04-14 22:17:06 +0000739 // Parse this as a tag as if the missing tag were present.
740 if (TagKind == tok::kw_enum)
741 ParseEnumSpecifier(Loc, DS, AS);
742 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000743 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000744 return true;
745 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000746 }
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Douglas Gregora786fdb2009-10-13 23:27:22 +0000748 // This is almost certainly an invalid type name. Let the action emit a
749 // diagnostic and attempt to recover.
750 Action::TypeTy *T = 0;
751 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
752 CurScope, SS, T)) {
753 // The action emitted a diagnostic, so we don't have to.
754 if (T) {
755 // The action has suggested that the type T could be used. Set that as
756 // the type in the declaration specifiers, consume the would-be type
757 // name token, and we're done.
758 const char *PrevSpec;
759 unsigned DiagID;
760 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
761 false);
762 DS.SetRangeEnd(Tok.getLocation());
763 ConsumeToken();
764
765 // There may be other declaration specifiers after this.
766 return true;
767 }
768
769 // Fall through; the action had no suggestion for us.
770 } else {
771 // The action did not emit a diagnostic, so emit one now.
772 SourceRange R;
773 if (SS) R = SS->getRange();
774 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
775 }
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Douglas Gregora786fdb2009-10-13 23:27:22 +0000777 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000778 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000779 unsigned DiagID;
780 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000781 DS.SetRangeEnd(Tok.getLocation());
782 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000783
Chris Lattnere40c2952009-04-14 21:34:55 +0000784 // TODO: Could inject an invalid typedef decl in an enclosing scope to
785 // avoid rippling error messages on subsequent uses of the same type,
786 // could be useful if #include was forgotten.
787 return false;
788}
789
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000790/// \brief Determine the declaration specifier context from the declarator
791/// context.
792///
793/// \param Context the declarator context, which is one of the
794/// Declarator::TheContext enumerator values.
795Parser::DeclSpecContext
796Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
797 if (Context == Declarator::MemberContext)
798 return DSC_class;
799 if (Context == Declarator::FileContext)
800 return DSC_top_level;
801 return DSC_normal;
802}
803
Reid Spencer5f016e22007-07-11 17:01:13 +0000804/// ParseDeclarationSpecifiers
805/// declaration-specifiers: [C99 6.7]
806/// storage-class-specifier declaration-specifiers[opt]
807/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000808/// [C99] function-specifier declaration-specifiers[opt]
809/// [GNU] attributes declaration-specifiers[opt]
810///
811/// storage-class-specifier: [C99 6.7.1]
812/// 'typedef'
813/// 'extern'
814/// 'static'
815/// 'auto'
816/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000817/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000818/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000819/// function-specifier: [C99 6.7.4]
820/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000821/// [C++] 'virtual'
822/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000823/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000824/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000825
Reid Spencer5f016e22007-07-11 17:01:13 +0000826///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000827void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000828 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000829 AccessSpecifier AS,
830 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000831 if (Tok.is(tok::code_completion)) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000832 Action::CodeCompletionContext CCC = Action::CCC_Namespace;
833 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
834 CCC = DSContext == DSC_class? Action::CCC_MemberTemplate
835 : Action::CCC_Template;
836 else if (DSContext == DSC_class)
837 CCC = Action::CCC_Class;
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000838 else if (ObjCImpDecl)
839 CCC = Action::CCC_ObjCImplementation;
840
Douglas Gregor01dfea02010-01-10 23:08:15 +0000841 Actions.CodeCompleteOrdinaryName(CurScope, CCC);
Douglas Gregor791215b2009-09-21 20:51:25 +0000842 ConsumeToken();
843 }
844
Chris Lattner81c018d2008-03-13 06:29:04 +0000845 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000847 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000849 unsigned DiagID = 0;
850
Reid Spencer5f016e22007-07-11 17:01:13 +0000851 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000852
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000854 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000855 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 // If this is not a declaration specifier token, we're done reading decl
857 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000858 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Chris Lattner5e02c472009-01-05 00:07:25 +0000861 case tok::coloncolon: // ::foo::bar
862 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000863 if (TryAnnotateCXXScopeToken(true))
Chris Lattner5e02c472009-01-05 00:07:25 +0000864 continue;
865 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000866
867 case tok::annot_cxxscope: {
868 if (DS.hasTypeSpecifier())
869 goto DoneWithDeclSpec;
870
John McCallaa87d332009-12-12 11:40:51 +0000871 CXXScopeSpec SS;
872 SS.setScopeRep(Tok.getAnnotationValue());
873 SS.setRange(Tok.getAnnotationRange());
874
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000875 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000876 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000877 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000878 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000879 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000880 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000881
882 // C++ [class.qual]p2:
883 // In a lookup in which the constructor is an acceptable lookup
884 // result and the nested-name-specifier nominates a class C:
885 //
886 // - if the name specified after the
887 // nested-name-specifier, when looked up in C, is the
888 // injected-class-name of C (Clause 9), or
889 //
890 // - if the name specified after the nested-name-specifier
891 // is the same as the identifier or the
892 // simple-template-id's template-name in the last
893 // component of the nested-name-specifier,
894 //
895 // the name is instead considered to name the constructor of
896 // class C.
897 //
898 // Thus, if the template-name is actually the constructor
899 // name, then the code is ill-formed; this interpretation is
900 // reinforced by the NAD status of core issue 635.
901 TemplateIdAnnotation *TemplateId
902 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
903 if (DSContext == DSC_top_level && TemplateId->Name &&
904 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
905 if (isConstructorDeclarator()) {
906 // The user meant this to be an out-of-line constructor
907 // definition, but template arguments are not allowed
908 // there. Just allow this as a constructor; we'll
909 // complain about it later.
910 goto DoneWithDeclSpec;
911 }
912
913 // The user meant this to name a type, but it actually names
914 // a constructor with some extraneous template
915 // arguments. Complain, then parse it as a type as the user
916 // intended.
917 Diag(TemplateId->TemplateNameLoc,
918 diag::err_out_of_line_template_id_names_constructor)
919 << TemplateId->Name;
920 }
921
John McCallaa87d332009-12-12 11:40:51 +0000922 DS.getTypeSpecScope() = SS;
923 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +0000924 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000925 "ParseOptionalCXXScopeSpecifier not working");
926 AnnotateTemplateIdTokenAsType(&SS);
927 continue;
928 }
929
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000930 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +0000931 DS.getTypeSpecScope() = SS;
932 ConsumeToken(); // The C++ scope.
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000933 if (Tok.getAnnotationValue())
934 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
935 PrevSpec, DiagID,
936 Tok.getAnnotationValue());
937 else
938 DS.SetTypeSpecError();
939 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
940 ConsumeToken(); // The typename
941 }
942
Douglas Gregor9135c722009-03-25 15:40:00 +0000943 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000944 goto DoneWithDeclSpec;
945
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000946 // If we're in a context where the identifier could be a class name,
947 // check whether this is a constructor declaration.
948 if (DSContext == DSC_top_level &&
949 Actions.isCurrentClassName(*Next.getIdentifierInfo(), CurScope,
950 &SS)) {
951 if (isConstructorDeclarator())
952 goto DoneWithDeclSpec;
953
954 // As noted in C++ [class.qual]p2 (cited above), when the name
955 // of the class is qualified in a context where it could name
956 // a constructor, its a constructor name. However, we've
957 // looked at the declarator, and the user probably meant this
958 // to be a type. Complain that it isn't supposed to be treated
959 // as a type, then proceed to parse it as a type.
960 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
961 << Next.getIdentifierInfo();
962 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000963
Douglas Gregorb696ea32009-02-04 17:00:24 +0000964 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
965 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000966
Chris Lattnerf4382f52009-04-14 22:17:06 +0000967 // If the referenced identifier is not a type, then this declspec is
968 // erroneous: We already checked about that it has no type specifier, and
969 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000970 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000971 if (TypeRep == 0) {
972 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000973 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000974 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000975 }
Mike Stump1eb44332009-09-09 15:08:12 +0000976
John McCallaa87d332009-12-12 11:40:51 +0000977 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000978 ConsumeToken(); // The C++ scope.
979
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000980 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000981 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000982 if (isInvalid)
983 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000985 DS.SetRangeEnd(Tok.getLocation());
986 ConsumeToken(); // The typename.
987
988 continue;
989 }
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Chris Lattner80d0c892009-01-21 19:48:37 +0000991 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000992 if (Tok.getAnnotationValue())
993 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000994 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +0000995 else
996 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000997 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
998 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Chris Lattner80d0c892009-01-21 19:48:37 +00001000 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1001 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1002 // Objective-C interface. If we don't have Objective-C or a '<', this is
1003 // just a normal reference to a typedef name.
1004 if (!Tok.is(tok::less) || !getLang().ObjC1)
1005 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001007 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001008 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001009 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1010 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1011 LAngleLoc, EndProtoLoc);
1012 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1013 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Chris Lattner80d0c892009-01-21 19:48:37 +00001015 DS.SetRangeEnd(EndProtoLoc);
1016 continue;
1017 }
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Chris Lattner3bd934a2008-07-26 01:18:38 +00001019 // typedef-name
1020 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001021 // In C++, check to see if this is a scope specifier like foo::bar::, if
1022 // so handle it as such. This is important for ctor parsing.
Douglas Gregor495c35d2009-08-25 22:51:20 +00001023 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner837acd02009-01-21 19:19:26 +00001024 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Chris Lattner3bd934a2008-07-26 01:18:38 +00001026 // This identifier can only be a typedef name if we haven't already seen
1027 // a type-specifier. Without this check we misparse:
1028 // typedef int X; struct Y { short X; }; as 'short int'.
1029 if (DS.hasTypeSpecifier())
1030 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Chris Lattner3bd934a2008-07-26 01:18:38 +00001032 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +00001033 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +00001034 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001035
Chris Lattnerc199ab32009-04-12 20:42:31 +00001036 // If this is not a typedef name, don't parse it as part of the declspec,
1037 // it must be an implicit int or an error.
1038 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001039 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001040 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001041 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001042
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001043 // If we're in a context where the identifier could be a class name,
1044 // check whether this is a constructor declaration.
1045 if (getLang().CPlusPlus && DSContext == DSC_class &&
Mike Stump1eb44332009-09-09 15:08:12 +00001046 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001047 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001048 goto DoneWithDeclSpec;
1049
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001050 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001051 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001052 if (isInvalid)
1053 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Chris Lattner3bd934a2008-07-26 01:18:38 +00001055 DS.SetRangeEnd(Tok.getLocation());
1056 ConsumeToken(); // The identifier
1057
1058 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1059 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1060 // Objective-C interface. If we don't have Objective-C or a '<', this is
1061 // just a normal reference to a typedef name.
1062 if (!Tok.is(tok::less) || !getLang().ObjC1)
1063 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001065 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001066 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001067 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1068 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1069 LAngleLoc, EndProtoLoc);
1070 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1071 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Chris Lattner3bd934a2008-07-26 01:18:38 +00001073 DS.SetRangeEnd(EndProtoLoc);
1074
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001075 // Need to support trailing type qualifiers (e.g. "id<p> const").
1076 // If a type specifier follows, it will be diagnosed elsewhere.
1077 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001078 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001079
1080 // type-name
1081 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001082 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001083 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001084 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001085 // This template-id does not refer to a type name, so we're
1086 // done with the type-specifiers.
1087 goto DoneWithDeclSpec;
1088 }
1089
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001090 // If we're in a context where the template-id could be a
1091 // constructor name or specialization, check whether this is a
1092 // constructor declaration.
1093 if (getLang().CPlusPlus && DSContext == DSC_class &&
1094 Actions.isCurrentClassName(*TemplateId->Name, CurScope) &&
1095 isConstructorDeclarator())
1096 goto DoneWithDeclSpec;
1097
Douglas Gregor39a8de12009-02-25 19:37:18 +00001098 // Turn the template-id annotation token into a type annotation
1099 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001100 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001101 continue;
1102 }
1103
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 // GNU attributes support.
1105 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001106 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001107 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001108
1109 // Microsoft declspec support.
1110 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001111 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001112 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Steve Naroff239f0732008-12-25 14:16:32 +00001114 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001115 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001116 // FIXME: Add handling here!
1117 break;
1118
1119 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001120 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001121 case tok::kw___cdecl:
1122 case tok::kw___stdcall:
1123 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001124 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1125 continue;
1126
Reid Spencer5f016e22007-07-11 17:01:13 +00001127 // storage-class-specifier
1128 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001129 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1130 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001131 break;
1132 case tok::kw_extern:
1133 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001134 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001135 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1136 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001137 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001138 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001139 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001140 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001141 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001142 case tok::kw_static:
1143 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001144 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001145 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1146 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 break;
1148 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001149 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001150 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1151 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001152 else
John McCallfec54012009-08-03 20:12:06 +00001153 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1154 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 break;
1156 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001157 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1158 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001160 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001161 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1162 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001163 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001165 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 // function-specifier
1169 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001170 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001172 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001173 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001174 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001175 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001176 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001177 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001178
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001179 // friend
1180 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001181 if (DSContext == DSC_class)
1182 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1183 else {
1184 PrevSpec = ""; // not actually used by the diagnostic
1185 DiagID = diag::err_friend_invalid_in_context;
1186 isInvalid = true;
1187 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001188 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Sebastian Redl2ac67232009-11-05 15:47:02 +00001190 // constexpr
1191 case tok::kw_constexpr:
1192 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1193 break;
1194
Chris Lattner80d0c892009-01-21 19:48:37 +00001195 // type-specifier
1196 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001197 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1198 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001199 break;
1200 case tok::kw_long:
1201 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001202 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1203 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001204 else
John McCallfec54012009-08-03 20:12:06 +00001205 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1206 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001207 break;
1208 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001209 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1210 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001211 break;
1212 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001213 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1214 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001215 break;
1216 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001217 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1218 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001219 break;
1220 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001221 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1222 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001223 break;
1224 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001225 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1226 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001227 break;
1228 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001229 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1230 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001231 break;
1232 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001233 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1234 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001235 break;
1236 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001237 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1238 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001239 break;
1240 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001241 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1242 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001243 break;
1244 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001245 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1246 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001247 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001248 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001249 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1250 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001251 break;
1252 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001253 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1254 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001255 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001256 case tok::kw_bool:
1257 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001258 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1259 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001260 break;
1261 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001262 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1263 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001264 break;
1265 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001266 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1267 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001268 break;
1269 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001270 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1271 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001272 break;
1273
1274 // class-specifier:
1275 case tok::kw_class:
1276 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001277 case tok::kw_union: {
1278 tok::TokenKind Kind = Tok.getKind();
1279 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001280 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001281 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001282 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001283
1284 // enum-specifier:
1285 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001286 ConsumeToken();
1287 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001288 continue;
1289
1290 // cv-qualifier:
1291 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001292 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1293 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001294 break;
1295 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001296 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1297 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001298 break;
1299 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001300 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1301 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001302 break;
1303
Douglas Gregord57959a2009-03-27 23:10:48 +00001304 // C++ typename-specifier:
1305 case tok::kw_typename:
1306 if (TryAnnotateTypeOrScopeToken())
1307 continue;
1308 break;
1309
Chris Lattner80d0c892009-01-21 19:48:37 +00001310 // GNU typeof support.
1311 case tok::kw_typeof:
1312 ParseTypeofSpecifier(DS);
1313 continue;
1314
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001315 case tok::kw_decltype:
1316 ParseDecltypeSpecifier(DS);
1317 continue;
1318
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001319 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001320 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001321 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1322 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001323 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001324 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Chris Lattnerbce61352008-07-26 00:20:22 +00001326 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001327 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001328 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001329 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1330 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1331 LAngleLoc, EndProtoLoc);
1332 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1333 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001334 DS.SetRangeEnd(EndProtoLoc);
1335
Chris Lattner1ab3b962008-11-18 07:48:38 +00001336 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001337 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001338 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001339 // Need to support trailing type qualifiers (e.g. "id<p> const").
1340 // If a type specifier follows, it will be diagnosed elsewhere.
1341 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001342 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 }
John McCallfec54012009-08-03 20:12:06 +00001344 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001345 if (isInvalid) {
1346 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001347 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001348 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001350 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 ConsumeToken();
1352 }
1353}
Douglas Gregoradcac882008-12-01 23:54:00 +00001354
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001355/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001356/// primarily follow the C++ grammar with additions for C99 and GNU,
1357/// which together subsume the C grammar. Note that the C++
1358/// type-specifier also includes the C type-qualifier (for const,
1359/// volatile, and C99 restrict). Returns true if a type-specifier was
1360/// found (and parsed), false otherwise.
1361///
1362/// type-specifier: [C++ 7.1.5]
1363/// simple-type-specifier
1364/// class-specifier
1365/// enum-specifier
1366/// elaborated-type-specifier [TODO]
1367/// cv-qualifier
1368///
1369/// cv-qualifier: [C++ 7.1.5.1]
1370/// 'const'
1371/// 'volatile'
1372/// [C99] 'restrict'
1373///
1374/// simple-type-specifier: [ C++ 7.1.5.2]
1375/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1376/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1377/// 'char'
1378/// 'wchar_t'
1379/// 'bool'
1380/// 'short'
1381/// 'int'
1382/// 'long'
1383/// 'signed'
1384/// 'unsigned'
1385/// 'float'
1386/// 'double'
1387/// 'void'
1388/// [C99] '_Bool'
1389/// [C99] '_Complex'
1390/// [C99] '_Imaginary' // Removed in TC2?
1391/// [GNU] '_Decimal32'
1392/// [GNU] '_Decimal64'
1393/// [GNU] '_Decimal128'
1394/// [GNU] typeof-specifier
1395/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1396/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001397/// [C++0x] 'decltype' ( expression )
John McCallfec54012009-08-03 20:12:06 +00001398bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001399 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001400 unsigned &DiagID,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001401 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001402 SourceLocation Loc = Tok.getLocation();
1403
1404 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001405 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001406 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001407 // Annotate typenames and C++ scope specifiers. If we get one, just
1408 // recurse to handle whatever we get.
1409 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001410 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1411 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001412 // Otherwise, not a type specifier.
1413 return false;
1414 case tok::coloncolon: // ::foo::bar
1415 if (NextToken().is(tok::kw_new) || // ::new
1416 NextToken().is(tok::kw_delete)) // ::delete
1417 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Chris Lattner166a8fc2009-01-04 23:41:41 +00001419 // Annotate typenames and C++ scope specifiers. If we get one, just
1420 // recurse to handle whatever we get.
1421 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001422 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1423 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001424 // Otherwise, not a type specifier.
1425 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001426
Douglas Gregor12e083c2008-11-07 15:42:26 +00001427 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001428 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001429 if (Tok.getAnnotationValue())
1430 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001431 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001432 else
1433 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001434 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1435 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Douglas Gregor12e083c2008-11-07 15:42:26 +00001437 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1438 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1439 // Objective-C interface. If we don't have Objective-C or a '<', this is
1440 // just a normal reference to a typedef name.
1441 if (!Tok.is(tok::less) || !getLang().ObjC1)
1442 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001444 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001445 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001446 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1447 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1448 LAngleLoc, EndProtoLoc);
1449 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1450 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Douglas Gregor12e083c2008-11-07 15:42:26 +00001452 DS.SetRangeEnd(EndProtoLoc);
1453 return true;
1454 }
1455
1456 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001457 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001458 break;
1459 case tok::kw_long:
1460 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001461 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1462 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001463 else
John McCallfec54012009-08-03 20:12:06 +00001464 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1465 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001466 break;
1467 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001468 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001469 break;
1470 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001471 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1472 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001473 break;
1474 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001475 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1476 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001477 break;
1478 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001479 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1480 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001481 break;
1482 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001483 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001484 break;
1485 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001486 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001487 break;
1488 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001489 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001490 break;
1491 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001492 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001493 break;
1494 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001495 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001496 break;
1497 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001498 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001499 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001500 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001501 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001502 break;
1503 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001504 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001505 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001506 case tok::kw_bool:
1507 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001508 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001509 break;
1510 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001511 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1512 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001513 break;
1514 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001515 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1516 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001517 break;
1518 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001519 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1520 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001521 break;
1522
1523 // class-specifier:
1524 case tok::kw_class:
1525 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001526 case tok::kw_union: {
1527 tok::TokenKind Kind = Tok.getKind();
1528 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001529 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001530 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001531 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001532
1533 // enum-specifier:
1534 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001535 ConsumeToken();
1536 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001537 return true;
1538
1539 // cv-qualifier:
1540 case tok::kw_const:
1541 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001542 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001543 break;
1544 case tok::kw_volatile:
1545 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001546 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001547 break;
1548 case tok::kw_restrict:
1549 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001550 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001551 break;
1552
1553 // GNU typeof support.
1554 case tok::kw_typeof:
1555 ParseTypeofSpecifier(DS);
1556 return true;
1557
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001558 // C++0x decltype support.
1559 case tok::kw_decltype:
1560 ParseDecltypeSpecifier(DS);
1561 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001563 // C++0x auto support.
1564 case tok::kw_auto:
1565 if (!getLang().CPlusPlus0x)
1566 return false;
1567
John McCallfec54012009-08-03 20:12:06 +00001568 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001569 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001570 case tok::kw___ptr64:
1571 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001572 case tok::kw___cdecl:
1573 case tok::kw___stdcall:
1574 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001575 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001576 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001577
Douglas Gregor12e083c2008-11-07 15:42:26 +00001578 default:
1579 // Not a type-specifier; do nothing.
1580 return false;
1581 }
1582
1583 // If the specifier combination wasn't legal, issue a diagnostic.
1584 if (isInvalid) {
1585 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001586 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001587 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001588 }
1589 DS.SetRangeEnd(Tok.getLocation());
1590 ConsumeToken(); // whatever we parsed above.
1591 return true;
1592}
Reid Spencer5f016e22007-07-11 17:01:13 +00001593
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001594/// ParseStructDeclaration - Parse a struct declaration without the terminating
1595/// semicolon.
1596///
Reid Spencer5f016e22007-07-11 17:01:13 +00001597/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001598/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001599/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001600/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001601/// struct-declarator-list:
1602/// struct-declarator
1603/// struct-declarator-list ',' struct-declarator
1604/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1605/// struct-declarator:
1606/// declarator
1607/// [GNU] declarator attributes[opt]
1608/// declarator[opt] ':' constant-expression
1609/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1610///
Chris Lattnere1359422008-04-10 06:46:29 +00001611void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001612ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001613 if (Tok.is(tok::kw___extension__)) {
1614 // __extension__ silences extension warnings in the subexpression.
1615 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001616 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001617 return ParseStructDeclaration(DS, Fields);
1618 }
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Steve Naroff28a7ca82007-08-20 22:28:22 +00001620 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001621 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001622 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001624 // If there are no declarators, this is a free-standing declaration
1625 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001626 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001627 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001628 return;
1629 }
1630
1631 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001632 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001633 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001634 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001635 FieldDeclarator DeclaratorInfo(DS);
1636
1637 // Attributes are only allowed here on successive declarators.
1638 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1639 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001640 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001641 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1642 }
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Steve Naroff28a7ca82007-08-20 22:28:22 +00001644 /// struct-declarator: declarator
1645 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001646 if (Tok.isNot(tok::colon)) {
1647 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1648 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001649 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001650 }
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Chris Lattner04d66662007-10-09 17:33:22 +00001652 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001653 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001654 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001655 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001656 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001657 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001658 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001659 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001660
Steve Naroff28a7ca82007-08-20 22:28:22 +00001661 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001662 if (Tok.is(tok::kw___attribute)) {
1663 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001664 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001665 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1666 }
1667
John McCallbdd563e2009-11-03 02:38:08 +00001668 // We're done with this declarator; invoke the callback.
John McCall54abf7d2009-11-04 02:18:39 +00001669 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1670 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001671
Steve Naroff28a7ca82007-08-20 22:28:22 +00001672 // If we don't have a comma, it is either the end of the list (a ';')
1673 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001674 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001675 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001676
Steve Naroff28a7ca82007-08-20 22:28:22 +00001677 // Consume the comma.
1678 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001679
John McCallbdd563e2009-11-03 02:38:08 +00001680 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001681 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001682}
1683
1684/// ParseStructUnionBody
1685/// struct-contents:
1686/// struct-declaration-list
1687/// [EXT] empty
1688/// [GNU] "struct-declaration-list" without terminatoring ';'
1689/// struct-declaration-list:
1690/// struct-declaration
1691/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001692/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001693///
Reid Spencer5f016e22007-07-11 17:01:13 +00001694void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001695 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001696 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1697 PP.getSourceManager(),
1698 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Reid Spencer5f016e22007-07-11 17:01:13 +00001700 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001702 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001703 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1704
Reid Spencer5f016e22007-07-11 17:01:13 +00001705 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1706 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001707 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001708 Diag(Tok, diag::ext_empty_struct_union_enum)
1709 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001710
Chris Lattnerb28317a2009-03-28 19:18:32 +00001711 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001712
Reid Spencer5f016e22007-07-11 17:01:13 +00001713 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001714 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001715 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001716
Reid Spencer5f016e22007-07-11 17:01:13 +00001717 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001718 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001719 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001720 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001721 ConsumeToken();
1722 continue;
1723 }
Chris Lattnere1359422008-04-10 06:46:29 +00001724
1725 // Parse all the comma separated declarators.
1726 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001727
John McCallbdd563e2009-11-03 02:38:08 +00001728 if (!Tok.is(tok::at)) {
1729 struct CFieldCallback : FieldCallback {
1730 Parser &P;
1731 DeclPtrTy TagDecl;
1732 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1733
1734 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1735 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1736 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1737
1738 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001739 // Install the declarator into the current TagDecl.
John McCall4ba39712009-11-03 21:13:47 +00001740 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1741 FD.D.getDeclSpec().getSourceRange().getBegin(),
1742 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001743 FieldDecls.push_back(Field);
1744 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001745 }
John McCallbdd563e2009-11-03 02:38:08 +00001746 } Callback(*this, TagDecl, FieldDecls);
1747
1748 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001749 } else { // Handle @defs
1750 ConsumeToken();
1751 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1752 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001753 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001754 continue;
1755 }
1756 ConsumeToken();
1757 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1758 if (!Tok.is(tok::identifier)) {
1759 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001760 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001761 continue;
1762 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001763 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001764 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001765 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001766 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1767 ConsumeToken();
1768 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001769 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001770
Chris Lattner04d66662007-10-09 17:33:22 +00001771 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001772 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001773 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001774 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001775 break;
1776 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001777 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1778 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001779 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001780 // If we stopped at a ';', eat it.
1781 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001782 }
1783 }
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Steve Naroff60fccee2007-10-29 21:38:07 +00001785 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Reid Spencer5f016e22007-07-11 17:01:13 +00001787 AttributeList *AttrList = 0;
1788 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001789 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001790 AttrList = ParseGNUAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001791
1792 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001793 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001794 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001795 AttrList);
1796 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001797 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001798}
1799
1800
1801/// ParseEnumSpecifier
1802/// enum-specifier: [C99 6.7.2.2]
1803/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001804///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001805/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1806/// '}' attributes[opt]
1807/// 'enum' identifier
1808/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001809///
1810/// [C++] elaborated-type-specifier:
1811/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1812///
Chris Lattner4c97d762009-04-12 21:49:30 +00001813void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1814 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001816 if (Tok.is(tok::code_completion)) {
1817 // Code completion for an enum name.
1818 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1819 ConsumeToken();
1820 }
1821
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001822 AttributeList *Attr = 0;
1823 // If attributes exist after tag, parse them.
1824 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001825 Attr = ParseGNUAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001826
1827 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001828 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001829 if (Tok.isNot(tok::identifier)) {
1830 Diag(Tok, diag::err_expected_ident);
1831 if (Tok.isNot(tok::l_brace)) {
1832 // Has no name and is not a definition.
1833 // Skip the rest of this declarator, up until the comma or semicolon.
1834 SkipUntil(tok::comma, true);
1835 return;
1836 }
1837 }
1838 }
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001840 // Must have either 'enum name' or 'enum {...}'.
1841 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1842 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001843
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001844 // Skip the rest of this declarator, up until the comma or semicolon.
1845 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001846 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001847 }
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001849 // If an identifier is present, consume and remember it.
1850 IdentifierInfo *Name = 0;
1851 SourceLocation NameLoc;
1852 if (Tok.is(tok::identifier)) {
1853 Name = Tok.getIdentifierInfo();
1854 NameLoc = ConsumeToken();
1855 }
Mike Stump1eb44332009-09-09 15:08:12 +00001856
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001857 // There are three options here. If we have 'enum foo;', then this is a
1858 // forward declaration. If we have 'enum foo {...' then this is a
1859 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1860 //
1861 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1862 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1863 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1864 //
John McCall0f434ec2009-07-31 02:45:11 +00001865 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001866 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001867 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001868 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001869 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001870 else
John McCall0f434ec2009-07-31 02:45:11 +00001871 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001872 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001873 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00001874 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001875 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001876 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001877 Owned, IsDependent);
1878 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Chris Lattner04d66662007-10-09 17:33:22 +00001880 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001881 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Douglas Gregorb988f9c2010-01-25 16:33:23 +00001883 // FIXME: The DeclSpec should keep the locations of both the keyword and the
1884 // name (if there is one).
1885 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +00001886 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001887 unsigned DiagID;
Douglas Gregorb988f9c2010-01-25 16:33:23 +00001888 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001889 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001890 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001891}
1892
1893/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1894/// enumerator-list:
1895/// enumerator
1896/// enumerator-list ',' enumerator
1897/// enumerator:
1898/// enumeration-constant
1899/// enumeration-constant '=' constant-expression
1900/// enumeration-constant:
1901/// identifier
1902///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001903void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001904 // Enter the scope of the enum body and start the definition.
1905 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001906 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001907
Reid Spencer5f016e22007-07-11 17:01:13 +00001908 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001909
Chris Lattner7946dd32007-08-27 17:24:30 +00001910 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001911 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001912 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump1eb44332009-09-09 15:08:12 +00001913
Chris Lattnerb28317a2009-03-28 19:18:32 +00001914 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001915
Chris Lattnerb28317a2009-03-28 19:18:32 +00001916 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001917
Reid Spencer5f016e22007-07-11 17:01:13 +00001918 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001919 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001920 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1921 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Reid Spencer5f016e22007-07-11 17:01:13 +00001923 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001924 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001925 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001926 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001927 AssignedVal = ParseConstantExpression();
1928 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001929 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001930 }
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Reid Spencer5f016e22007-07-11 17:01:13 +00001932 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001933 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1934 LastEnumConstDecl,
1935 IdentLoc, Ident,
1936 EqualLoc,
1937 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001938 EnumConstantDecls.push_back(EnumConstDecl);
1939 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Chris Lattner04d66662007-10-09 17:33:22 +00001941 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001942 break;
1943 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001944
1945 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001946 !(getLang().C99 || getLang().CPlusPlus0x))
1947 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1948 << getLang().CPlusPlus
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001949 << CodeModificationHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001950 }
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001953 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001954
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001955 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001956 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001957 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001958 Attr = ParseGNUAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001959
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001960 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1961 EnumConstantDecls.data(), EnumConstantDecls.size(),
1962 CurScope, Attr);
Mike Stump1eb44332009-09-09 15:08:12 +00001963
Douglas Gregor72de6672009-01-08 20:45:30 +00001964 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001965 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001966}
1967
1968/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001969/// start of a type-qualifier-list.
1970bool Parser::isTypeQualifier() const {
1971 switch (Tok.getKind()) {
1972 default: return false;
1973 // type-qualifier
1974 case tok::kw_const:
1975 case tok::kw_volatile:
1976 case tok::kw_restrict:
1977 return true;
1978 }
1979}
1980
1981/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001982/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001983bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001984 switch (Tok.getKind()) {
1985 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001986
Chris Lattner166a8fc2009-01-04 23:41:41 +00001987 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001988 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001989 // Annotate typenames and C++ scope specifiers. If we get one, just
1990 // recurse to handle whatever we get.
1991 if (TryAnnotateTypeOrScopeToken())
1992 return isTypeSpecifierQualifier();
1993 // Otherwise, not a type specifier.
1994 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001995
Chris Lattner166a8fc2009-01-04 23:41:41 +00001996 case tok::coloncolon: // ::foo::bar
1997 if (NextToken().is(tok::kw_new) || // ::new
1998 NextToken().is(tok::kw_delete)) // ::delete
1999 return false;
2000
2001 // Annotate typenames and C++ scope specifiers. If we get one, just
2002 // recurse to handle whatever we get.
2003 if (TryAnnotateTypeOrScopeToken())
2004 return isTypeSpecifierQualifier();
2005 // Otherwise, not a type specifier.
2006 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Reid Spencer5f016e22007-07-11 17:01:13 +00002008 // GNU attributes support.
2009 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002010 // GNU typeof support.
2011 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002012
Reid Spencer5f016e22007-07-11 17:01:13 +00002013 // type-specifiers
2014 case tok::kw_short:
2015 case tok::kw_long:
2016 case tok::kw_signed:
2017 case tok::kw_unsigned:
2018 case tok::kw__Complex:
2019 case tok::kw__Imaginary:
2020 case tok::kw_void:
2021 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002022 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002023 case tok::kw_char16_t:
2024 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002025 case tok::kw_int:
2026 case tok::kw_float:
2027 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002028 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002029 case tok::kw__Bool:
2030 case tok::kw__Decimal32:
2031 case tok::kw__Decimal64:
2032 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00002033
Chris Lattner99dc9142008-04-13 18:59:07 +00002034 // struct-or-union-specifier (C99) or class-specifier (C++)
2035 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002036 case tok::kw_struct:
2037 case tok::kw_union:
2038 // enum-specifier
2039 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002040
Reid Spencer5f016e22007-07-11 17:01:13 +00002041 // type-qualifier
2042 case tok::kw_const:
2043 case tok::kw_volatile:
2044 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002045
2046 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002047 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002048 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002049
Chris Lattner7c186be2008-10-20 00:25:30 +00002050 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2051 case tok::less:
2052 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002053
Steve Naroff239f0732008-12-25 14:16:32 +00002054 case tok::kw___cdecl:
2055 case tok::kw___stdcall:
2056 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002057 case tok::kw___w64:
2058 case tok::kw___ptr64:
2059 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002060 }
2061}
2062
2063/// isDeclarationSpecifier() - Return true if the current token is part of a
2064/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002065bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 switch (Tok.getKind()) {
2067 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002068
Chris Lattner166a8fc2009-01-04 23:41:41 +00002069 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002070 // Unfortunate hack to support "Class.factoryMethod" notation.
2071 if (getLang().ObjC1 && NextToken().is(tok::period))
2072 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00002073 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00002074
Douglas Gregord57959a2009-03-27 23:10:48 +00002075 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002076 // Annotate typenames and C++ scope specifiers. If we get one, just
2077 // recurse to handle whatever we get.
2078 if (TryAnnotateTypeOrScopeToken())
2079 return isDeclarationSpecifier();
2080 // Otherwise, not a declaration specifier.
2081 return false;
2082 case tok::coloncolon: // ::foo::bar
2083 if (NextToken().is(tok::kw_new) || // ::new
2084 NextToken().is(tok::kw_delete)) // ::delete
2085 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002086
Chris Lattner166a8fc2009-01-04 23:41:41 +00002087 // Annotate typenames and C++ scope specifiers. If we get one, just
2088 // recurse to handle whatever we get.
2089 if (TryAnnotateTypeOrScopeToken())
2090 return isDeclarationSpecifier();
2091 // Otherwise, not a declaration specifier.
2092 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002093
Reid Spencer5f016e22007-07-11 17:01:13 +00002094 // storage-class-specifier
2095 case tok::kw_typedef:
2096 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002097 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002098 case tok::kw_static:
2099 case tok::kw_auto:
2100 case tok::kw_register:
2101 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Reid Spencer5f016e22007-07-11 17:01:13 +00002103 // type-specifiers
2104 case tok::kw_short:
2105 case tok::kw_long:
2106 case tok::kw_signed:
2107 case tok::kw_unsigned:
2108 case tok::kw__Complex:
2109 case tok::kw__Imaginary:
2110 case tok::kw_void:
2111 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002112 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002113 case tok::kw_char16_t:
2114 case tok::kw_char32_t:
2115
Reid Spencer5f016e22007-07-11 17:01:13 +00002116 case tok::kw_int:
2117 case tok::kw_float:
2118 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002119 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002120 case tok::kw__Bool:
2121 case tok::kw__Decimal32:
2122 case tok::kw__Decimal64:
2123 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00002124
Chris Lattner99dc9142008-04-13 18:59:07 +00002125 // struct-or-union-specifier (C99) or class-specifier (C++)
2126 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002127 case tok::kw_struct:
2128 case tok::kw_union:
2129 // enum-specifier
2130 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002131
Reid Spencer5f016e22007-07-11 17:01:13 +00002132 // type-qualifier
2133 case tok::kw_const:
2134 case tok::kw_volatile:
2135 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002136
Reid Spencer5f016e22007-07-11 17:01:13 +00002137 // function-specifier
2138 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002139 case tok::kw_virtual:
2140 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002141
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002142 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002143 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002144
Chris Lattner1ef08762007-08-09 17:01:07 +00002145 // GNU typeof support.
2146 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002147
Chris Lattner1ef08762007-08-09 17:01:07 +00002148 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002149 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002150 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002151
Chris Lattnerf3948c42008-07-26 03:38:44 +00002152 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2153 case tok::less:
2154 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002155
Steve Naroff47f52092009-01-06 19:34:12 +00002156 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002157 case tok::kw___cdecl:
2158 case tok::kw___stdcall:
2159 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002160 case tok::kw___w64:
2161 case tok::kw___ptr64:
2162 case tok::kw___forceinline:
2163 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002164 }
2165}
2166
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002167bool Parser::isConstructorDeclarator() {
2168 TentativeParsingAction TPA(*this);
2169
2170 // Parse the C++ scope specifier.
2171 CXXScopeSpec SS;
2172 ParseOptionalCXXScopeSpecifier(SS, 0, true);
2173
2174 // Parse the constructor name.
2175 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2176 // We already know that we have a constructor name; just consume
2177 // the token.
2178 ConsumeToken();
2179 } else {
2180 TPA.Revert();
2181 return false;
2182 }
2183
2184 // Current class name must be followed by a left parentheses.
2185 if (Tok.isNot(tok::l_paren)) {
2186 TPA.Revert();
2187 return false;
2188 }
2189 ConsumeParen();
2190
2191 // A right parentheses or ellipsis signals that we have a constructor.
2192 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2193 TPA.Revert();
2194 return true;
2195 }
2196
2197 // If we need to, enter the specified scope.
2198 DeclaratorScopeObj DeclScopeObj(*this, SS);
2199 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(CurScope, SS))
2200 DeclScopeObj.EnterDeclaratorScope();
2201
2202 // Check whether the next token(s) are part of a declaration
2203 // specifier, in which case we have the start of a parameter and,
2204 // therefore, we know that this is a constructor.
2205 bool IsConstructor = isDeclarationSpecifier();
2206 TPA.Revert();
2207 return IsConstructor;
2208}
Reid Spencer5f016e22007-07-11 17:01:13 +00002209
2210/// ParseTypeQualifierListOpt
2211/// type-qualifier-list: [C99 6.7.5]
2212/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002213/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002214/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002215/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Sean Huntbbd37c62009-11-21 08:43:09 +00002216/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2217/// if CXX0XAttributesAllowed = true
Reid Spencer5f016e22007-07-11 17:01:13 +00002218///
Sean Huntbbd37c62009-11-21 08:43:09 +00002219void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2220 bool CXX0XAttributesAllowed) {
2221 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2222 SourceLocation Loc = Tok.getLocation();
2223 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2224 if (CXX0XAttributesAllowed)
2225 DS.AddAttributes(Attr.AttrList);
2226 else
2227 Diag(Loc, diag::err_attributes_not_allowed);
2228 }
2229
Reid Spencer5f016e22007-07-11 17:01:13 +00002230 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002231 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002232 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002233 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002234 SourceLocation Loc = Tok.getLocation();
2235
2236 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002237 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002238 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2239 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002240 break;
2241 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002242 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2243 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002244 break;
2245 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002246 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2247 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002248 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002249 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002250 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002251 case tok::kw___cdecl:
2252 case tok::kw___stdcall:
2253 case tok::kw___fastcall:
Sean Huntbbd37c62009-11-21 08:43:09 +00002254 if (GNUAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002255 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2256 continue;
2257 }
2258 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002259 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002260 if (GNUAttributesAllowed) {
2261 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002262 continue; // do *not* consume the next token!
2263 }
2264 // otherwise, FALL THROUGH!
2265 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002266 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002267 // If this is not a type-qualifier token, we're done reading type
2268 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002269 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002270 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002271 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002272
Reid Spencer5f016e22007-07-11 17:01:13 +00002273 // If the specifier combination wasn't legal, issue a diagnostic.
2274 if (isInvalid) {
2275 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002276 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002277 }
2278 ConsumeToken();
2279 }
2280}
2281
2282
2283/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2284///
2285void Parser::ParseDeclarator(Declarator &D) {
2286 /// This implements the 'declarator' production in the C grammar, then checks
2287 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002288 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002289}
2290
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002291/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2292/// is parsed by the function passed to it. Pass null, and the direct-declarator
2293/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002294/// ptr-operator production.
2295///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002296/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2297/// [C] pointer[opt] direct-declarator
2298/// [C++] direct-declarator
2299/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002300///
2301/// pointer: [C99 6.7.5]
2302/// '*' type-qualifier-list[opt]
2303/// '*' type-qualifier-list[opt] pointer
2304///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002305/// ptr-operator:
2306/// '*' cv-qualifier-seq[opt]
2307/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002308/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002309/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002310/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002311/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002312void Parser::ParseDeclaratorInternal(Declarator &D,
2313 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002314 if (Diags.hasAllExtensionsSilenced())
2315 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002316 // C++ member pointers start with a '::' or a nested-name.
2317 // Member pointers get special handling, since there's no place for the
2318 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002319 if (getLang().CPlusPlus &&
2320 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2321 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002322 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002323 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002324 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002325 // The scope spec really belongs to the direct-declarator.
2326 D.getCXXScopeSpec() = SS;
2327 if (DirectDeclParser)
2328 (this->*DirectDeclParser)(D);
2329 return;
2330 }
2331
2332 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002333 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002334 DeclSpec DS;
2335 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002336 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002337
2338 // Recurse to parse whatever is left.
2339 ParseDeclaratorInternal(D, DirectDeclParser);
2340
2341 // Sema will have to catch (syntactically invalid) pointers into global
2342 // scope. It has to catch pointers into namespace scope anyway.
2343 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002344 Loc, DS.TakeAttributes()),
2345 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002346 return;
2347 }
2348 }
2349
2350 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002351 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002352 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002353 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002354 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002355 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002356 if (DirectDeclParser)
2357 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002358 return;
2359 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002360
Sebastian Redl05532f22009-03-15 22:02:01 +00002361 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2362 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002363 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002364 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002365
Chris Lattner9af55002009-03-27 04:18:06 +00002366 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002367 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002368 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002369
Reid Spencer5f016e22007-07-11 17:01:13 +00002370 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002371 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002372
Reid Spencer5f016e22007-07-11 17:01:13 +00002373 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002374 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002375 if (Kind == tok::star)
2376 // Remember that we parsed a pointer type, and remember the type-quals.
2377 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002378 DS.TakeAttributes()),
2379 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002380 else
2381 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002382 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002383 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002384 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002385 } else {
2386 // Is a reference
2387 DeclSpec DS;
2388
Sebastian Redl743de1f2009-03-23 00:00:23 +00002389 // Complain about rvalue references in C++03, but then go on and build
2390 // the declarator.
2391 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2392 Diag(Loc, diag::err_rvalue_reference);
2393
Reid Spencer5f016e22007-07-11 17:01:13 +00002394 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2395 // cv-qualifiers are introduced through the use of a typedef or of a
2396 // template type argument, in which case the cv-qualifiers are ignored.
2397 //
2398 // [GNU] Retricted references are allowed.
2399 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002400 // [C++0x] Attributes on references are not allowed.
2401 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002402 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002403
2404 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2405 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2406 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002407 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002408 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2409 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002410 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002411 }
2412
2413 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002414 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002415
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002416 if (D.getNumTypeObjects() > 0) {
2417 // C++ [dcl.ref]p4: There shall be no references to references.
2418 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2419 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002420 if (const IdentifierInfo *II = D.getIdentifier())
2421 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2422 << II;
2423 else
2424 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2425 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002426
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002427 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002428 // can go ahead and build the (technically ill-formed)
2429 // declarator: reference collapsing will take care of it.
2430 }
2431 }
2432
Reid Spencer5f016e22007-07-11 17:01:13 +00002433 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002434 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002435 DS.TakeAttributes(),
2436 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002437 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002438 }
2439}
2440
2441/// ParseDirectDeclarator
2442/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002443/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002444/// '(' declarator ')'
2445/// [GNU] '(' attributes declarator ')'
2446/// [C90] direct-declarator '[' constant-expression[opt] ']'
2447/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2448/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2449/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2450/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2451/// direct-declarator '(' parameter-type-list ')'
2452/// direct-declarator '(' identifier-list[opt] ')'
2453/// [GNU] direct-declarator '(' parameter-forward-declarations
2454/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002455/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2456/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002457/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002458///
2459/// declarator-id: [C++ 8]
2460/// id-expression
2461/// '::'[opt] nested-name-specifier[opt] type-name
2462///
2463/// id-expression: [C++ 5.1]
2464/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002465/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002466///
2467/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002468/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002469/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002470/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002471/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002472/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002473///
Reid Spencer5f016e22007-07-11 17:01:13 +00002474void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002475 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002476
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002477 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2478 // ParseDeclaratorInternal might already have parsed the scope.
2479 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2480 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2481 true);
2482 if (afterCXXScope) {
John McCalle7e278b2009-12-11 20:04:54 +00002483 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2484 // Change the declaration context for name lookup, until this function
2485 // is exited (and the declarator has been parsed).
2486 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002487 }
2488
2489 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2490 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2491 // We found something that indicates the start of an unqualified-id.
2492 // Parse that unqualified-id.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002493 bool AllowConstructorName
2494 = ((D.getCXXScopeSpec().isSet() &&
2495 D.getContext() == Declarator::FileContext) ||
2496 (!D.getCXXScopeSpec().isSet() &&
2497 D.getContext() == Declarator::MemberContext)) &&
2498 !D.getDeclSpec().hasTypeSpecifier();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002499 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2500 /*EnteringContext=*/true,
2501 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002502 AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002503 /*ObjectType=*/0,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002504 D.getName())) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002505 D.SetIdentifier(0, Tok.getLocation());
2506 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002507 } else {
2508 // Parsed the unqualified-id; update range information and move along.
2509 if (D.getSourceRange().getBegin().isInvalid())
2510 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2511 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002512 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002513 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002514 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002515 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002516 assert(!getLang().CPlusPlus &&
2517 "There's a C++-specific check for tok::identifier above");
2518 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2519 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2520 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002521 goto PastIdentifier;
2522 }
2523
2524 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002525 // direct-declarator: '(' declarator ')'
2526 // direct-declarator: '(' attributes declarator ')'
2527 // Example: 'char (*X)' or 'int (*XX)(void)'
2528 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002529
2530 // If the declarator was parenthesized, we entered the declarator
2531 // scope when parsing the parenthesized declarator, then exited
2532 // the scope already. Re-enter the scope, if we need to.
2533 if (D.getCXXScopeSpec().isSet()) {
2534 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2535 // Change the declaration context for name lookup, until this function
2536 // is exited (and the declarator has been parsed).
2537 DeclScopeObj.EnterDeclaratorScope();
2538 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002539 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002540 // This could be something simple like "int" (in which case the declarator
2541 // portion is empty), if an abstract-declarator is allowed.
2542 D.SetIdentifier(0, Tok.getLocation());
2543 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002544 if (D.getContext() == Declarator::MemberContext)
2545 Diag(Tok, diag::err_expected_member_name_or_semi)
2546 << D.getDeclSpec().getSourceRange();
2547 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002548 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002549 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002550 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002551 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002552 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002553 }
Mike Stump1eb44332009-09-09 15:08:12 +00002554
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002555 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002556 assert(D.isPastIdentifier() &&
2557 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002558
Sean Huntbbd37c62009-11-21 08:43:09 +00002559 // Don't parse attributes unless we have an identifier.
2560 if (D.getIdentifier() && getLang().CPlusPlus
2561 && isCXX0XAttributeSpecifier(true)) {
2562 SourceLocation AttrEndLoc;
2563 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2564 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2565 }
2566
Reid Spencer5f016e22007-07-11 17:01:13 +00002567 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002568 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002569 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2570 // In such a case, check if we actually have a function declarator; if it
2571 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002572 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2573 // When not in file scope, warn for ambiguous function declarators, just
2574 // in case the author intended it as a variable definition.
2575 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2576 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2577 break;
2578 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002579 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002580 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002581 ParseBracketDeclarator(D);
2582 } else {
2583 break;
2584 }
2585 }
2586}
2587
Chris Lattneref4715c2008-04-06 05:45:57 +00002588/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2589/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002590/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002591/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2592///
2593/// direct-declarator:
2594/// '(' declarator ')'
2595/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002596/// direct-declarator '(' parameter-type-list ')'
2597/// direct-declarator '(' identifier-list[opt] ')'
2598/// [GNU] direct-declarator '(' parameter-forward-declarations
2599/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002600///
2601void Parser::ParseParenDeclarator(Declarator &D) {
2602 SourceLocation StartLoc = ConsumeParen();
2603 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002604
Chris Lattner7399ee02008-10-20 02:05:46 +00002605 // Eat any attributes before we look at whether this is a grouping or function
2606 // declarator paren. If this is a grouping paren, the attribute applies to
2607 // the type being built up, for example:
2608 // int (__attribute__(()) *x)(long y)
2609 // If this ends up not being a grouping paren, the attribute applies to the
2610 // first argument, for example:
2611 // int (__attribute__(()) int x)
2612 // In either case, we need to eat any attributes to be able to determine what
2613 // sort of paren this is.
2614 //
2615 AttributeList *AttrList = 0;
2616 bool RequiresArg = false;
2617 if (Tok.is(tok::kw___attribute)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002618 AttrList = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Chris Lattner7399ee02008-10-20 02:05:46 +00002620 // We require that the argument list (if this is a non-grouping paren) be
2621 // present even if the attribute list was empty.
2622 RequiresArg = true;
2623 }
Steve Naroff239f0732008-12-25 14:16:32 +00002624 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002625 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2626 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2627 Tok.is(tok::kw___ptr64)) {
2628 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2629 }
Mike Stump1eb44332009-09-09 15:08:12 +00002630
Chris Lattneref4715c2008-04-06 05:45:57 +00002631 // If we haven't past the identifier yet (or where the identifier would be
2632 // stored, if this is an abstract declarator), then this is probably just
2633 // grouping parens. However, if this could be an abstract-declarator, then
2634 // this could also be the start of function arguments (consider 'void()').
2635 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002636
Chris Lattneref4715c2008-04-06 05:45:57 +00002637 if (!D.mayOmitIdentifier()) {
2638 // If this can't be an abstract-declarator, this *must* be a grouping
2639 // paren, because we haven't seen the identifier yet.
2640 isGrouping = true;
2641 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002642 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002643 isDeclarationSpecifier()) { // 'int(int)' is a function.
2644 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2645 // considered to be a type, not a K&R identifier-list.
2646 isGrouping = false;
2647 } else {
2648 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2649 isGrouping = true;
2650 }
Mike Stump1eb44332009-09-09 15:08:12 +00002651
Chris Lattneref4715c2008-04-06 05:45:57 +00002652 // If this is a grouping paren, handle:
2653 // direct-declarator: '(' declarator ')'
2654 // direct-declarator: '(' attributes declarator ')'
2655 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002656 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002657 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002658 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002659 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002660
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002661 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002662 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002663 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002664
2665 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002666 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002667 return;
2668 }
Mike Stump1eb44332009-09-09 15:08:12 +00002669
Chris Lattneref4715c2008-04-06 05:45:57 +00002670 // Okay, if this wasn't a grouping paren, it must be the start of a function
2671 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002672 // identifier (and remember where it would have been), then call into
2673 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002674 D.SetIdentifier(0, Tok.getLocation());
2675
Chris Lattner7399ee02008-10-20 02:05:46 +00002676 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002677}
2678
2679/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2680/// declarator D up to a paren, which indicates that we are parsing function
2681/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002682///
Chris Lattner7399ee02008-10-20 02:05:46 +00002683/// If AttrList is non-null, then the caller parsed those arguments immediately
2684/// after the open paren - they should be considered to be the first argument of
2685/// a parameter. If RequiresArg is true, then the first argument of the
2686/// function is required to be present and required to not be an identifier
2687/// list.
2688///
Reid Spencer5f016e22007-07-11 17:01:13 +00002689/// This method also handles this portion of the grammar:
2690/// parameter-type-list: [C99 6.7.5]
2691/// parameter-list
2692/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002693/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002694///
2695/// parameter-list: [C99 6.7.5]
2696/// parameter-declaration
2697/// parameter-list ',' parameter-declaration
2698///
2699/// parameter-declaration: [C99 6.7.5]
2700/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002701/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002702/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002703/// declaration-specifiers abstract-declarator[opt]
2704/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002705/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002706/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2707///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002708/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002709/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002710///
Chris Lattner7399ee02008-10-20 02:05:46 +00002711void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2712 AttributeList *AttrList,
2713 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002714 // lparen is already consumed!
2715 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002716
Chris Lattner7399ee02008-10-20 02:05:46 +00002717 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002718 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002719 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002720 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002721 delete AttrList;
2722 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002723
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002724 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2725 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002726
2727 // cv-qualifier-seq[opt].
2728 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002729 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002730 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002731 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002732 llvm::SmallVector<TypeTy*, 2> Exceptions;
2733 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002734 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002735 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002736 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002737 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002738
2739 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002740 if (Tok.is(tok::kw_throw)) {
2741 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002742 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002743 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002744 hasAnyExceptionSpec);
2745 assert(Exceptions.size() == ExceptionRanges.size() &&
2746 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002747 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002748 }
2749
Chris Lattnerf97409f2008-04-06 06:57:35 +00002750 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002751 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002752 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002753 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002754 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002755 /*arglist*/ 0, 0,
2756 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002757 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002758 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002759 Exceptions.data(),
2760 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002761 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002762 LParenLoc, RParenLoc, D),
2763 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002764 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002765 }
2766
Chris Lattner7399ee02008-10-20 02:05:46 +00002767 // Alternatively, this parameter list may be an identifier list form for a
2768 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002769 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002770 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002771 // K&R identifier lists can't have typedefs as identifiers, per
2772 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002773 if (RequiresArg) {
2774 Diag(Tok, diag::err_argument_required_after_attribute);
2775 delete AttrList;
2776 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002777 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2778 // normal declarators, not for abstract-declarators.
2779 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002780 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002781 }
Mike Stump1eb44332009-09-09 15:08:12 +00002782
Chris Lattnerf97409f2008-04-06 06:57:35 +00002783 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002784
Chris Lattnerf97409f2008-04-06 06:57:35 +00002785 // Build up an array of information about the parsed arguments.
2786 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002787
2788 // Enter function-declaration scope, limiting any declarators to the
2789 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002790 ParseScope PrototypeScope(this,
2791 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002792
Chris Lattnerf97409f2008-04-06 06:57:35 +00002793 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002794 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002795 while (1) {
2796 if (Tok.is(tok::ellipsis)) {
2797 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002798 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002799 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002800 }
Mike Stump1eb44332009-09-09 15:08:12 +00002801
Chris Lattnerf97409f2008-04-06 06:57:35 +00002802 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002803
Chris Lattnerf97409f2008-04-06 06:57:35 +00002804 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00002805 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002806 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002807
2808 // If the caller parsed attributes for the first argument, add them now.
2809 if (AttrList) {
2810 DS.AddAttributes(AttrList);
2811 AttrList = 0; // Only apply the attributes to the first parameter.
2812 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002813 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002814
Chris Lattnerf97409f2008-04-06 06:57:35 +00002815 // Parse the declarator. This is "PrototypeContext", because we must
2816 // accept either 'declarator' or 'abstract-declarator' here.
2817 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2818 ParseDeclarator(ParmDecl);
2819
2820 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002821 if (Tok.is(tok::kw___attribute)) {
2822 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00002823 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002824 ParmDecl.AddAttributes(AttrList, Loc);
2825 }
Mike Stump1eb44332009-09-09 15:08:12 +00002826
Chris Lattnerf97409f2008-04-06 06:57:35 +00002827 // Remember this parsed parameter in ParamInfo.
2828 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002829
Douglas Gregor72b505b2008-12-16 21:30:33 +00002830 // DefArgToks is used when the parsing of default arguments needs
2831 // to be delayed.
2832 CachedTokens *DefArgToks = 0;
2833
Chris Lattnerf97409f2008-04-06 06:57:35 +00002834 // If no parameter was specified, verify that *something* was specified,
2835 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002836 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2837 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002838 // Completely missing, emit error.
2839 Diag(DSStart, diag::err_missing_param);
2840 } else {
2841 // Otherwise, we have something. Add it and let semantic analysis try
2842 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00002843
Chris Lattnerf97409f2008-04-06 06:57:35 +00002844 // Inform the actions module about the parameter declarator, so it gets
2845 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002846 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002847
2848 // Parse the default argument, if any. We parse the default
2849 // arguments in all dialects; the semantic analysis in
2850 // ActOnParamDefaultArgument will reject the default argument in
2851 // C.
2852 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002853 SourceLocation EqualLoc = Tok.getLocation();
2854
Chris Lattner04421082008-04-08 04:40:51 +00002855 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002856 if (D.getContext() == Declarator::MemberContext) {
2857 // If we're inside a class definition, cache the tokens
2858 // corresponding to the default argument. We'll actually parse
2859 // them when we see the end of the class definition.
2860 // FIXME: Templates will require something similar.
2861 // FIXME: Can we use a smart pointer for Toks?
2862 DefArgToks = new CachedTokens;
2863
Mike Stump1eb44332009-09-09 15:08:12 +00002864 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002865 tok::semi, false)) {
2866 delete DefArgToks;
2867 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002868 Actions.ActOnParamDefaultArgumentError(Param);
2869 } else
Mike Stump1eb44332009-09-09 15:08:12 +00002870 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00002871 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002872 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002873 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002874 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002875
Douglas Gregor72b505b2008-12-16 21:30:33 +00002876 OwningExprResult DefArgResult(ParseAssignmentExpression());
2877 if (DefArgResult.isInvalid()) {
2878 Actions.ActOnParamDefaultArgumentError(Param);
2879 SkipUntil(tok::comma, tok::r_paren, true, true);
2880 } else {
2881 // Inform the actions module about the default argument
2882 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002883 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002884 }
Chris Lattner04421082008-04-08 04:40:51 +00002885 }
2886 }
Mike Stump1eb44332009-09-09 15:08:12 +00002887
2888 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2889 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002890 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002891 }
2892
2893 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00002894 if (Tok.isNot(tok::comma)) {
2895 if (Tok.is(tok::ellipsis)) {
2896 IsVariadic = true;
2897 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2898
2899 if (!getLang().CPlusPlus) {
2900 // We have ellipsis without a preceding ',', which is ill-formed
2901 // in C. Complain and provide the fix.
2902 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2903 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2904 }
2905 }
2906
2907 break;
2908 }
Mike Stump1eb44332009-09-09 15:08:12 +00002909
Chris Lattnerf97409f2008-04-06 06:57:35 +00002910 // Consume the comma.
2911 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002912 }
Mike Stump1eb44332009-09-09 15:08:12 +00002913
Chris Lattnerf97409f2008-04-06 06:57:35 +00002914 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002915 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00002916
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002917 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002918 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2919 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002920
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002921 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002922 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002923 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002924 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002925 llvm::SmallVector<TypeTy*, 2> Exceptions;
2926 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00002927
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002928 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002929 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002930 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002931 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002932 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002933
2934 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002935 if (Tok.is(tok::kw_throw)) {
2936 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002937 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002938 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002939 hasAnyExceptionSpec);
2940 assert(Exceptions.size() == ExceptionRanges.size() &&
2941 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002942 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002943 }
2944
Reid Spencer5f016e22007-07-11 17:01:13 +00002945 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002946 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002947 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002948 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002949 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002950 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002951 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002952 Exceptions.data(),
2953 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002954 Exceptions.size(),
2955 LParenLoc, RParenLoc, D),
2956 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002957}
2958
Chris Lattner66d28652008-04-06 06:34:08 +00002959/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2960/// we found a K&R-style identifier list instead of a type argument list. The
2961/// current token is known to be the first identifier in the list.
2962///
2963/// identifier-list: [C99 6.7.5]
2964/// identifier
2965/// identifier-list ',' identifier
2966///
2967void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2968 Declarator &D) {
2969 // Build up an array of information about the parsed arguments.
2970 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2971 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00002972
Chris Lattner66d28652008-04-06 06:34:08 +00002973 // If there was no identifier specified for the declarator, either we are in
2974 // an abstract-declarator, or we are in a parameter declarator which was found
2975 // to be abstract. In abstract-declarators, identifier lists are not valid:
2976 // diagnose this.
2977 if (!D.getIdentifier())
2978 Diag(Tok, diag::ext_ident_list_in_param);
2979
2980 // Tok is known to be the first identifier in the list. Remember this
2981 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002982 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002983 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002984 Tok.getLocation(),
2985 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00002986
Chris Lattner50c64772008-04-06 06:39:19 +00002987 ConsumeToken(); // eat the first identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00002988
Chris Lattner66d28652008-04-06 06:34:08 +00002989 while (Tok.is(tok::comma)) {
2990 // Eat the comma.
2991 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002992
Chris Lattner50c64772008-04-06 06:39:19 +00002993 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002994 if (Tok.isNot(tok::identifier)) {
2995 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002996 SkipUntil(tok::r_paren);
2997 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002998 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002999
Chris Lattner66d28652008-04-06 06:34:08 +00003000 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003001
3002 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00003003 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003004 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003005
Chris Lattner66d28652008-04-06 06:34:08 +00003006 // Verify that the argument identifier has not already been mentioned.
3007 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003008 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003009 } else {
3010 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003011 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003012 Tok.getLocation(),
3013 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00003014 }
Mike Stump1eb44332009-09-09 15:08:12 +00003015
Chris Lattner66d28652008-04-06 06:34:08 +00003016 // Eat the identifier.
3017 ConsumeToken();
3018 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003019
3020 // If we have the closing ')', eat it and we're done.
3021 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3022
Chris Lattner50c64772008-04-06 06:39:19 +00003023 // Remember that we parsed a function type, and remember the attributes. This
3024 // function type is always a K&R style function type, which is not varargs and
3025 // has no prototype.
3026 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003027 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003028 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003029 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003030 /*exception*/false,
3031 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003032 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003033 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003034}
Chris Lattneref4715c2008-04-06 05:45:57 +00003035
Reid Spencer5f016e22007-07-11 17:01:13 +00003036/// [C90] direct-declarator '[' constant-expression[opt] ']'
3037/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3038/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3039/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3040/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3041void Parser::ParseBracketDeclarator(Declarator &D) {
3042 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003043
Chris Lattner378c7e42008-12-18 07:27:21 +00003044 // C array syntax has many features, but by-far the most common is [] and [4].
3045 // This code does a fast path to handle some of the most obvious cases.
3046 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003047 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003048 //FIXME: Use these
3049 CXX0XAttributeList Attr;
3050 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3051 Attr = ParseCXX0XAttributes();
3052 }
3053
Chris Lattner378c7e42008-12-18 07:27:21 +00003054 // Remember that we parsed the empty array type.
3055 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003056 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3057 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003058 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003059 return;
3060 } else if (Tok.getKind() == tok::numeric_constant &&
3061 GetLookAheadToken(1).is(tok::r_square)) {
3062 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00003063 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003064 ConsumeToken();
3065
Sebastian Redlab197ba2009-02-09 18:23:29 +00003066 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003067 //FIXME: Use these
3068 CXX0XAttributeList Attr;
3069 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3070 Attr = ParseCXX0XAttributes();
3071 }
Chris Lattner378c7e42008-12-18 07:27:21 +00003072
3073 // If there was an error parsing the assignment-expression, recover.
3074 if (ExprRes.isInvalid())
3075 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00003076
Chris Lattner378c7e42008-12-18 07:27:21 +00003077 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003078 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3079 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003080 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003081 return;
3082 }
Mike Stump1eb44332009-09-09 15:08:12 +00003083
Reid Spencer5f016e22007-07-11 17:01:13 +00003084 // If valid, this location is the position where we read the 'static' keyword.
3085 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003086 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003087 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003088
Reid Spencer5f016e22007-07-11 17:01:13 +00003089 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003090 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003091 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003092 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003093
Reid Spencer5f016e22007-07-11 17:01:13 +00003094 // If we haven't already read 'static', check to see if there is one after the
3095 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003096 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003097 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003098
Reid Spencer5f016e22007-07-11 17:01:13 +00003099 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3100 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00003101 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00003102
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003103 // Handle the case where we have '[*]' as the array size. However, a leading
3104 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3105 // the the token after the star is a ']'. Since stars in arrays are
3106 // infrequent, use of lookahead is not costly here.
3107 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003108 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003109
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003110 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003111 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003112 StaticLoc = SourceLocation(); // Drop the static.
3113 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003114 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003115 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003116 // Note, in C89, this production uses the constant-expr production instead
3117 // of assignment-expr. The only difference is that assignment-expr allows
3118 // things like '=' and '*='. Sema rejects these in C89 mode because they
3119 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003120
Douglas Gregore0762c92009-06-19 23:52:42 +00003121 // Parse the constant-expression or assignment-expression now (depending
3122 // on dialect).
3123 if (getLang().CPlusPlus)
3124 NumElements = ParseConstantExpression();
3125 else
3126 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003127 }
Mike Stump1eb44332009-09-09 15:08:12 +00003128
Reid Spencer5f016e22007-07-11 17:01:13 +00003129 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003130 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003131 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003132 // If the expression was invalid, skip it.
3133 SkipUntil(tok::r_square);
3134 return;
3135 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003136
3137 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3138
Sean Huntbbd37c62009-11-21 08:43:09 +00003139 //FIXME: Use these
3140 CXX0XAttributeList Attr;
3141 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3142 Attr = ParseCXX0XAttributes();
3143 }
3144
Chris Lattner378c7e42008-12-18 07:27:21 +00003145 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003146 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3147 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003148 NumElements.release(),
3149 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003150 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003151}
3152
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003153/// [GNU] typeof-specifier:
3154/// typeof ( expressions )
3155/// typeof ( type-name )
3156/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003157///
3158void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003159 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003160 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003161 SourceLocation StartLoc = ConsumeToken();
3162
John McCallcfb708c2010-01-13 20:03:27 +00003163 const bool hasParens = Tok.is(tok::l_paren);
3164
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003165 bool isCastExpr;
3166 TypeTy *CastTy;
3167 SourceRange CastRange;
3168 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3169 isCastExpr,
3170 CastTy,
3171 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003172 if (hasParens)
3173 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003174
3175 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003176 // FIXME: Not accurate, the range gets one token more than it should.
3177 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003178 else
3179 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003180
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003181 if (isCastExpr) {
3182 if (!CastTy) {
3183 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003184 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003185 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003186
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003187 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003188 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003189 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3190 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003191 DiagID, CastTy))
3192 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003193 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003194 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003195
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003196 // If we get here, the operand to the typeof was an expresion.
3197 if (Operand.isInvalid()) {
3198 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003199 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003200 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003201
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003202 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003203 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003204 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3205 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003206 DiagID, Operand.release()))
3207 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003208}