blob: bc6dda8ed79d9b2e39ce798d5cc3a1becd3f2f0b [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);
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
362 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000363 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000365 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall54abf7d2009-11-04 02:18:39 +0000366 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000367 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
John McCalld8ac0572009-11-03 19:26:08 +0000370 DeclGroupPtrTy DG = ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false,
371 &DeclEnd);
372 return DG;
373}
Mike Stump1eb44332009-09-09 15:08:12 +0000374
John McCalld8ac0572009-11-03 19:26:08 +0000375/// ParseDeclGroup - Having concluded that this is either a function
376/// definition or a group of object declarations, actually parse the
377/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000378Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
379 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000380 bool AllowFunctionDefinitions,
381 SourceLocation *DeclEnd) {
382 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000383 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000384 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000385
John McCalld8ac0572009-11-03 19:26:08 +0000386 // Bail out if the first declarator didn't seem well-formed.
387 if (!D.hasName() && !D.mayOmitIdentifier()) {
388 // Skip until ; or }.
389 SkipUntil(tok::r_brace, true, true);
390 if (Tok.is(tok::semi))
391 ConsumeToken();
392 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
John McCalld8ac0572009-11-03 19:26:08 +0000395 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
396 if (isDeclarationAfterDeclarator()) {
397 // Fall though. We have to check this first, though, because
398 // __attribute__ might be the start of a function definition in
399 // (extended) K&R C.
400 } else if (isStartOfFunctionDefinition()) {
401 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
402 Diag(Tok, diag::err_function_declared_typedef);
403
404 // Recover by treating the 'typedef' as spurious.
405 DS.ClearStorageClassSpecs();
406 }
407
408 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
409 return Actions.ConvertDeclToDeclGroup(TheDecl);
410 } else {
411 Diag(Tok, diag::err_expected_fn_body);
412 SkipUntil(tok::semi);
413 return DeclGroupPtrTy();
414 }
415 }
416
417 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
418 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000419 D.complete(FirstDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000420 if (FirstDecl.get())
421 DeclsInGroup.push_back(FirstDecl);
422
423 // If we don't have a comma, it is either the end of the list (a ';') or an
424 // error, bail out.
425 while (Tok.is(tok::comma)) {
426 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000427 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000428
429 // Parse the next declarator.
430 D.clear();
431
432 // Accept attributes in an init-declarator. In the first declarator in a
433 // declaration, these would be part of the declspec. In subsequent
434 // declarators, they become part of the declarator itself, so that they
435 // don't apply to declarators after *this* one. Examples:
436 // short __attribute__((common)) var; -> declspec
437 // short var __attribute__((common)); -> declarator
438 // short x, __attribute__((common)) var; -> declarator
439 if (Tok.is(tok::kw___attribute)) {
440 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000441 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000442 D.AddAttributes(AttrList, Loc);
443 }
444
445 ParseDeclarator(D);
446
447 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000448 D.complete(ThisDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000449 if (ThisDecl.get())
450 DeclsInGroup.push_back(ThisDecl);
451 }
452
453 if (DeclEnd)
454 *DeclEnd = Tok.getLocation();
455
456 if (Context != Declarator::ForContext &&
457 ExpectAndConsume(tok::semi,
458 Context == Declarator::FileContext
459 ? diag::err_invalid_token_after_toplevel_declarator
460 : diag::err_expected_semi_declaration)) {
461 SkipUntil(tok::r_brace, true, true);
462 if (Tok.is(tok::semi))
463 ConsumeToken();
464 }
465
466 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
467 DeclsInGroup.data(),
468 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000469}
470
Douglas Gregor1426e532009-05-12 21:31:51 +0000471/// \brief Parse 'declaration' after parsing 'declaration-specifiers
472/// declarator'. This method parses the remainder of the declaration
473/// (including any attributes or initializer, among other things) and
474/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000475///
Reid Spencer5f016e22007-07-11 17:01:13 +0000476/// init-declarator: [C99 6.7]
477/// declarator
478/// declarator '=' initializer
479/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
480/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000481/// [C++] declarator initializer[opt]
482///
483/// [C++] initializer:
484/// [C++] '=' initializer-clause
485/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000486/// [C++0x] '=' 'default' [TODO]
487/// [C++0x] '=' 'delete'
488///
489/// According to the standard grammar, =default and =delete are function
490/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000491///
Douglas Gregore542c862009-06-23 23:11:28 +0000492Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
493 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000494 // If a simple-asm-expr is present, parse it.
495 if (Tok.is(tok::kw_asm)) {
496 SourceLocation Loc;
497 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
498 if (AsmLabel.isInvalid()) {
499 SkipUntil(tok::semi, true, true);
500 return DeclPtrTy();
501 }
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Douglas Gregor1426e532009-05-12 21:31:51 +0000503 D.setAsmLabel(AsmLabel.release());
504 D.SetRangeEnd(Loc);
505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Douglas Gregor1426e532009-05-12 21:31:51 +0000507 // If attributes are present, parse them.
508 if (Tok.is(tok::kw___attribute)) {
509 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000510 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000511 D.AddAttributes(AttrList, Loc);
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Douglas Gregor1426e532009-05-12 21:31:51 +0000514 // Inform the current actions module that we just parsed this declarator.
Douglas Gregord5a423b2009-09-25 18:43:00 +0000515 DeclPtrTy ThisDecl;
516 switch (TemplateInfo.Kind) {
517 case ParsedTemplateInfo::NonTemplate:
518 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
519 break;
520
521 case ParsedTemplateInfo::Template:
522 case ParsedTemplateInfo::ExplicitSpecialization:
523 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregore542c862009-06-23 23:11:28 +0000524 Action::MultiTemplateParamsArg(Actions,
525 TemplateInfo.TemplateParams->data(),
526 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000527 D);
528 break;
529
530 case ParsedTemplateInfo::ExplicitInstantiation: {
531 Action::DeclResult ThisRes
532 = Actions.ActOnExplicitInstantiation(CurScope,
533 TemplateInfo.ExternLoc,
534 TemplateInfo.TemplateLoc,
535 D);
536 if (ThisRes.isInvalid()) {
537 SkipUntil(tok::semi, true, true);
538 return DeclPtrTy();
539 }
540
541 ThisDecl = ThisRes.get();
542 break;
543 }
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Douglas Gregor1426e532009-05-12 21:31:51 +0000546 // Parse declarator '=' initializer.
547 if (Tok.is(tok::equal)) {
548 ConsumeToken();
549 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
550 SourceLocation DelLoc = ConsumeToken();
551 Actions.SetDeclDeleted(ThisDecl, DelLoc);
552 } else {
John McCall731ad842009-12-19 09:28:58 +0000553 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
554 EnterScope(0);
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000555 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000556 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000557
Douglas Gregor1426e532009-05-12 21:31:51 +0000558 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000559
John McCall731ad842009-12-19 09:28:58 +0000560 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000561 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000562 ExitScope();
563 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000564
Douglas Gregor1426e532009-05-12 21:31:51 +0000565 if (Init.isInvalid()) {
566 SkipUntil(tok::semi, true, true);
567 return DeclPtrTy();
568 }
Anders Carlsson9abf2ae2009-08-16 05:13:48 +0000569 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000570 }
571 } else if (Tok.is(tok::l_paren)) {
572 // Parse C++ direct initializer: '(' expression-list ')'
573 SourceLocation LParenLoc = ConsumeParen();
574 ExprVector Exprs(Actions);
575 CommaLocsTy CommaLocs;
576
Douglas Gregorb4debae2009-12-22 17:47:17 +0000577 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
578 EnterScope(0);
579 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
580 }
581
Douglas Gregor1426e532009-05-12 21:31:51 +0000582 if (ParseExpressionList(Exprs, CommaLocs)) {
583 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000584
585 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
586 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
587 ExitScope();
588 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000589 } else {
590 // Match the ')'.
591 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
592
593 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
594 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000595
596 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
597 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
598 ExitScope();
599 }
600
Douglas Gregor1426e532009-05-12 21:31:51 +0000601 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
602 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000603 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000604 }
605 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000606 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000607 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
608 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000609 }
610
611 return ThisDecl;
612}
613
Reid Spencer5f016e22007-07-11 17:01:13 +0000614/// ParseSpecifierQualifierList
615/// specifier-qualifier-list:
616/// type-specifier specifier-qualifier-list[opt]
617/// type-qualifier specifier-qualifier-list[opt]
618/// [GNU] attributes specifier-qualifier-list[opt]
619///
620void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
621 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
622 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 // Validate declspec for type-name.
626 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000627 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
628 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 // Issue diagnostic and remove storage class if present.
632 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
633 if (DS.getStorageClassSpecLoc().isValid())
634 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
635 else
636 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
637 DS.ClearStorageClassSpecs();
638 }
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 // Issue diagnostic and remove function specfier if present.
641 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000642 if (DS.isInlineSpecified())
643 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
644 if (DS.isVirtualSpecified())
645 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
646 if (DS.isExplicitSpecified())
647 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 DS.ClearFunctionSpecs();
649 }
650}
651
Chris Lattnerc199ab32009-04-12 20:42:31 +0000652/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
653/// specified token is valid after the identifier in a declarator which
654/// immediately follows the declspec. For example, these things are valid:
655///
656/// int x [ 4]; // direct-declarator
657/// int x ( int y); // direct-declarator
658/// int(int x ) // direct-declarator
659/// int x ; // simple-declaration
660/// int x = 17; // init-declarator-list
661/// int x , y; // init-declarator-list
662/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000663/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000664/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000665///
666/// This is not, because 'x' does not immediately follow the declspec (though
667/// ')' happens to be valid anyway).
668/// int (x)
669///
670static bool isValidAfterIdentifierInDeclarator(const Token &T) {
671 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
672 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000673 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000674}
675
Chris Lattnere40c2952009-04-14 21:34:55 +0000676
677/// ParseImplicitInt - This method is called when we have an non-typename
678/// identifier in a declspec (which normally terminates the decl spec) when
679/// the declspec has no type specifier. In this case, the declspec is either
680/// malformed or is "implicit int" (in K&R and C89).
681///
682/// This method handles diagnosing this prettily and returns false if the
683/// declspec is done being processed. If it recovers and thinks there may be
684/// other pieces of declspec after it, it returns true.
685///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000686bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000687 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000688 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000689 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Chris Lattnere40c2952009-04-14 21:34:55 +0000691 SourceLocation Loc = Tok.getLocation();
692 // If we see an identifier that is not a type name, we normally would
693 // parse it as the identifer being declared. However, when a typename
694 // is typo'd or the definition is not included, this will incorrectly
695 // parse the typename as the identifier name and fall over misparsing
696 // later parts of the diagnostic.
697 //
698 // As such, we try to do some look-ahead in cases where this would
699 // otherwise be an "implicit-int" case to see if this is invalid. For
700 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
701 // an identifier with implicit int, we'd get a parse error because the
702 // next token is obviously invalid for a type. Parse these as a case
703 // with an invalid type specifier.
704 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Chris Lattnere40c2952009-04-14 21:34:55 +0000706 // Since we know that this either implicit int (which is rare) or an
707 // error, we'd do lookahead to try to do better recovery.
708 if (isValidAfterIdentifierInDeclarator(NextToken())) {
709 // If this token is valid for implicit int, e.g. "static x = 4", then
710 // we just avoid eating the identifier, so it will be parsed as the
711 // identifier in the declarator.
712 return false;
713 }
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattnere40c2952009-04-14 21:34:55 +0000715 // Otherwise, if we don't consume this token, we are going to emit an
716 // error anyway. Try to recover from various common problems. Check
717 // to see if this was a reference to a tag name without a tag specified.
718 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000719 //
720 // C++ doesn't need this, and isTagName doesn't take SS.
721 if (SS == 0) {
722 const char *TagName = 0;
723 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Chris Lattnere40c2952009-04-14 21:34:55 +0000725 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
726 default: break;
727 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
728 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
729 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
730 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Chris Lattnerf4382f52009-04-14 22:17:06 +0000733 if (TagName) {
734 Diag(Loc, diag::err_use_of_tag_name_without_tag)
735 << Tok.getIdentifierInfo() << TagName
736 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Chris Lattnerf4382f52009-04-14 22:17:06 +0000738 // Parse this as a tag as if the missing tag were present.
739 if (TagKind == tok::kw_enum)
740 ParseEnumSpecifier(Loc, DS, AS);
741 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000742 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000743 return true;
744 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000745 }
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Douglas Gregora786fdb2009-10-13 23:27:22 +0000747 // This is almost certainly an invalid type name. Let the action emit a
748 // diagnostic and attempt to recover.
749 Action::TypeTy *T = 0;
750 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
751 CurScope, SS, T)) {
752 // The action emitted a diagnostic, so we don't have to.
753 if (T) {
754 // The action has suggested that the type T could be used. Set that as
755 // the type in the declaration specifiers, consume the would-be type
756 // name token, and we're done.
757 const char *PrevSpec;
758 unsigned DiagID;
759 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
760 false);
761 DS.SetRangeEnd(Tok.getLocation());
762 ConsumeToken();
763
764 // There may be other declaration specifiers after this.
765 return true;
766 }
767
768 // Fall through; the action had no suggestion for us.
769 } else {
770 // The action did not emit a diagnostic, so emit one now.
771 SourceRange R;
772 if (SS) R = SS->getRange();
773 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Douglas Gregora786fdb2009-10-13 23:27:22 +0000776 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000777 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000778 unsigned DiagID;
779 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000780 DS.SetRangeEnd(Tok.getLocation());
781 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattnere40c2952009-04-14 21:34:55 +0000783 // TODO: Could inject an invalid typedef decl in an enclosing scope to
784 // avoid rippling error messages on subsequent uses of the same type,
785 // could be useful if #include was forgotten.
786 return false;
787}
788
Reid Spencer5f016e22007-07-11 17:01:13 +0000789/// ParseDeclarationSpecifiers
790/// declaration-specifiers: [C99 6.7]
791/// storage-class-specifier declaration-specifiers[opt]
792/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000793/// [C99] function-specifier declaration-specifiers[opt]
794/// [GNU] attributes declaration-specifiers[opt]
795///
796/// storage-class-specifier: [C99 6.7.1]
797/// 'typedef'
798/// 'extern'
799/// 'static'
800/// 'auto'
801/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000802/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000803/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000804/// function-specifier: [C99 6.7.4]
805/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000806/// [C++] 'virtual'
807/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000808/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000809/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000810
Reid Spencer5f016e22007-07-11 17:01:13 +0000811///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000812void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000813 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000814 AccessSpecifier AS,
815 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000816 if (Tok.is(tok::code_completion)) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000817 Action::CodeCompletionContext CCC = Action::CCC_Namespace;
818 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
819 CCC = DSContext == DSC_class? Action::CCC_MemberTemplate
820 : Action::CCC_Template;
821 else if (DSContext == DSC_class)
822 CCC = Action::CCC_Class;
823
824 Actions.CodeCompleteOrdinaryName(CurScope, CCC);
Douglas Gregor791215b2009-09-21 20:51:25 +0000825 ConsumeToken();
826 }
827
Chris Lattner81c018d2008-03-13 06:29:04 +0000828 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000830 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000831 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000832 unsigned DiagID = 0;
833
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000835
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000837 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000838 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 // If this is not a declaration specifier token, we're done reading decl
840 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000841 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Chris Lattner5e02c472009-01-05 00:07:25 +0000844 case tok::coloncolon: // ::foo::bar
845 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000846 if (TryAnnotateCXXScopeToken(true))
Chris Lattner5e02c472009-01-05 00:07:25 +0000847 continue;
848 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000849
850 case tok::annot_cxxscope: {
851 if (DS.hasTypeSpecifier())
852 goto DoneWithDeclSpec;
853
John McCallaa87d332009-12-12 11:40:51 +0000854 CXXScopeSpec SS;
855 SS.setScopeRep(Tok.getAnnotationValue());
856 SS.setRange(Tok.getAnnotationRange());
857
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000858 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000859 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000860 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000861 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000862 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000863 // We have a qualified template-id, e.g., N::A<int>
John McCallaa87d332009-12-12 11:40:51 +0000864 DS.getTypeSpecScope() = SS;
865 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +0000866 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000867 "ParseOptionalCXXScopeSpecifier not working");
868 AnnotateTemplateIdTokenAsType(&SS);
869 continue;
870 }
871
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000872 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +0000873 DS.getTypeSpecScope() = SS;
874 ConsumeToken(); // The C++ scope.
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000875 if (Tok.getAnnotationValue())
876 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
877 PrevSpec, DiagID,
878 Tok.getAnnotationValue());
879 else
880 DS.SetTypeSpecError();
881 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
882 ConsumeToken(); // The typename
883 }
884
Douglas Gregor9135c722009-03-25 15:40:00 +0000885 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000886 goto DoneWithDeclSpec;
887
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000888 // If the next token is the name of the class type that the C++ scope
889 // denotes, followed by a '(', then this is a constructor declaration.
890 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000891 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000892 CurScope, &SS) &&
893 GetLookAheadToken(2).is(tok::l_paren))
894 goto DoneWithDeclSpec;
895
Douglas Gregorb696ea32009-02-04 17:00:24 +0000896 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
897 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000898
Chris Lattnerf4382f52009-04-14 22:17:06 +0000899 // If the referenced identifier is not a type, then this declspec is
900 // erroneous: We already checked about that it has no type specifier, and
901 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000902 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000903 if (TypeRep == 0) {
904 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000905 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000906 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
John McCallaa87d332009-12-12 11:40:51 +0000909 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000910 ConsumeToken(); // The C++ scope.
911
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000912 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000913 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000914 if (isInvalid)
915 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000917 DS.SetRangeEnd(Tok.getLocation());
918 ConsumeToken(); // The typename.
919
920 continue;
921 }
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Chris Lattner80d0c892009-01-21 19:48:37 +0000923 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000924 if (Tok.getAnnotationValue())
925 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000926 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +0000927 else
928 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000929 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
930 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Chris Lattner80d0c892009-01-21 19:48:37 +0000932 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
933 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
934 // Objective-C interface. If we don't have Objective-C or a '<', this is
935 // just a normal reference to a typedef name.
936 if (!Tok.is(tok::less) || !getLang().ObjC1)
937 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000939 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000940 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000941 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
942 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
943 LAngleLoc, EndProtoLoc);
944 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
945 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Chris Lattner80d0c892009-01-21 19:48:37 +0000947 DS.SetRangeEnd(EndProtoLoc);
948 continue;
949 }
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Chris Lattner3bd934a2008-07-26 01:18:38 +0000951 // typedef-name
952 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000953 // In C++, check to see if this is a scope specifier like foo::bar::, if
954 // so handle it as such. This is important for ctor parsing.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000955 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner837acd02009-01-21 19:19:26 +0000956 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Chris Lattner3bd934a2008-07-26 01:18:38 +0000958 // This identifier can only be a typedef name if we haven't already seen
959 // a type-specifier. Without this check we misparse:
960 // typedef int X; struct Y { short X; }; as 'short int'.
961 if (DS.hasTypeSpecifier())
962 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Chris Lattner3bd934a2008-07-26 01:18:38 +0000964 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +0000965 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +0000966 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000967
Chris Lattnerc199ab32009-04-12 20:42:31 +0000968 // If this is not a typedef name, don't parse it as part of the declspec,
969 // it must be an implicit int or an error.
970 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000971 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000972 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000973 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000974
Douglas Gregorb48fe382008-10-31 09:07:45 +0000975 // C++: If the identifier is actually the name of the class type
976 // being defined and the next token is a '(', then this is a
977 // constructor declaration. We're done with the decl-specifiers
978 // and will treat this token as an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000979 if (getLang().CPlusPlus &&
980 (CurScope->isClassScope() ||
981 (CurScope->isTemplateParamScope() &&
Douglas Gregordec06662009-08-21 18:42:58 +0000982 CurScope->getParent()->isClassScope())) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000983 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000984 NextToken().getKind() == tok::l_paren)
985 goto DoneWithDeclSpec;
986
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000987 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000988 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000989 if (isInvalid)
990 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Chris Lattner3bd934a2008-07-26 01:18:38 +0000992 DS.SetRangeEnd(Tok.getLocation());
993 ConsumeToken(); // The identifier
994
995 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
996 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
997 // Objective-C interface. If we don't have Objective-C or a '<', this is
998 // just a normal reference to a typedef name.
999 if (!Tok.is(tok::less) || !getLang().ObjC1)
1000 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001002 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001003 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001004 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1005 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1006 LAngleLoc, EndProtoLoc);
1007 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1008 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Chris Lattner3bd934a2008-07-26 01:18:38 +00001010 DS.SetRangeEnd(EndProtoLoc);
1011
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001012 // Need to support trailing type qualifiers (e.g. "id<p> const").
1013 // If a type specifier follows, it will be diagnosed elsewhere.
1014 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001015 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001016
1017 // type-name
1018 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001019 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001020 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001021 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001022 // This template-id does not refer to a type name, so we're
1023 // done with the type-specifiers.
1024 goto DoneWithDeclSpec;
1025 }
1026
1027 // Turn the template-id annotation token into a type annotation
1028 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001029 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001030 continue;
1031 }
1032
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 // GNU attributes support.
1034 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001035 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001036 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001037
1038 // Microsoft declspec support.
1039 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001040 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001041 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Steve Naroff239f0732008-12-25 14:16:32 +00001043 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001044 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001045 // FIXME: Add handling here!
1046 break;
1047
1048 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001049 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001050 case tok::kw___cdecl:
1051 case tok::kw___stdcall:
1052 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001053 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1054 continue;
1055
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 // storage-class-specifier
1057 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001058 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1059 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 break;
1061 case tok::kw_extern:
1062 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001063 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001064 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1065 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001067 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001068 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001069 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001070 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 case tok::kw_static:
1072 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001073 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001074 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1075 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 break;
1077 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001078 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001079 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1080 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001081 else
John McCallfec54012009-08-03 20:12:06 +00001082 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1083 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001084 break;
1085 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001086 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1087 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001089 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001090 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1091 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001092 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001094 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001095 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001096
Reid Spencer5f016e22007-07-11 17:01:13 +00001097 // function-specifier
1098 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001099 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001101 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001102 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001103 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001104 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001105 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001106 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001107
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001108 // friend
1109 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001110 if (DSContext == DSC_class)
1111 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1112 else {
1113 PrevSpec = ""; // not actually used by the diagnostic
1114 DiagID = diag::err_friend_invalid_in_context;
1115 isInvalid = true;
1116 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001117 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Sebastian Redl2ac67232009-11-05 15:47:02 +00001119 // constexpr
1120 case tok::kw_constexpr:
1121 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1122 break;
1123
Chris Lattner80d0c892009-01-21 19:48:37 +00001124 // type-specifier
1125 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001126 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1127 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001128 break;
1129 case tok::kw_long:
1130 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001131 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1132 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001133 else
John McCallfec54012009-08-03 20:12:06 +00001134 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1135 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001136 break;
1137 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001138 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1139 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001140 break;
1141 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001142 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1143 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001144 break;
1145 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001146 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1147 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001148 break;
1149 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001150 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1151 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001152 break;
1153 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001154 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1155 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001156 break;
1157 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001158 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1159 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001160 break;
1161 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001162 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1163 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001164 break;
1165 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001166 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1167 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001168 break;
1169 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001170 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1171 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001172 break;
1173 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001174 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1175 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001176 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001177 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001178 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1179 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001180 break;
1181 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001182 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1183 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001184 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001185 case tok::kw_bool:
1186 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001187 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1188 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001189 break;
1190 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001191 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1192 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001193 break;
1194 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001195 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1196 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001197 break;
1198 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001199 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1200 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001201 break;
1202
1203 // class-specifier:
1204 case tok::kw_class:
1205 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001206 case tok::kw_union: {
1207 tok::TokenKind Kind = Tok.getKind();
1208 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001209 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001210 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001211 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001212
1213 // enum-specifier:
1214 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001215 ConsumeToken();
1216 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001217 continue;
1218
1219 // cv-qualifier:
1220 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001221 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1222 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001223 break;
1224 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001225 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1226 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001227 break;
1228 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001229 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1230 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001231 break;
1232
Douglas Gregord57959a2009-03-27 23:10:48 +00001233 // C++ typename-specifier:
1234 case tok::kw_typename:
1235 if (TryAnnotateTypeOrScopeToken())
1236 continue;
1237 break;
1238
Chris Lattner80d0c892009-01-21 19:48:37 +00001239 // GNU typeof support.
1240 case tok::kw_typeof:
1241 ParseTypeofSpecifier(DS);
1242 continue;
1243
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001244 case tok::kw_decltype:
1245 ParseDecltypeSpecifier(DS);
1246 continue;
1247
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001248 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001249 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001250 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1251 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001252 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001253 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Chris Lattnerbce61352008-07-26 00:20:22 +00001255 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001256 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001257 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001258 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1259 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1260 LAngleLoc, EndProtoLoc);
1261 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1262 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001263 DS.SetRangeEnd(EndProtoLoc);
1264
Chris Lattner1ab3b962008-11-18 07:48:38 +00001265 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001266 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001267 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001268 // Need to support trailing type qualifiers (e.g. "id<p> const").
1269 // If a type specifier follows, it will be diagnosed elsewhere.
1270 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001271 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 }
John McCallfec54012009-08-03 20:12:06 +00001273 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 if (isInvalid) {
1275 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001276 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001277 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001279 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 ConsumeToken();
1281 }
1282}
Douglas Gregoradcac882008-12-01 23:54:00 +00001283
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001284/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001285/// primarily follow the C++ grammar with additions for C99 and GNU,
1286/// which together subsume the C grammar. Note that the C++
1287/// type-specifier also includes the C type-qualifier (for const,
1288/// volatile, and C99 restrict). Returns true if a type-specifier was
1289/// found (and parsed), false otherwise.
1290///
1291/// type-specifier: [C++ 7.1.5]
1292/// simple-type-specifier
1293/// class-specifier
1294/// enum-specifier
1295/// elaborated-type-specifier [TODO]
1296/// cv-qualifier
1297///
1298/// cv-qualifier: [C++ 7.1.5.1]
1299/// 'const'
1300/// 'volatile'
1301/// [C99] 'restrict'
1302///
1303/// simple-type-specifier: [ C++ 7.1.5.2]
1304/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1305/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1306/// 'char'
1307/// 'wchar_t'
1308/// 'bool'
1309/// 'short'
1310/// 'int'
1311/// 'long'
1312/// 'signed'
1313/// 'unsigned'
1314/// 'float'
1315/// 'double'
1316/// 'void'
1317/// [C99] '_Bool'
1318/// [C99] '_Complex'
1319/// [C99] '_Imaginary' // Removed in TC2?
1320/// [GNU] '_Decimal32'
1321/// [GNU] '_Decimal64'
1322/// [GNU] '_Decimal128'
1323/// [GNU] typeof-specifier
1324/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1325/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001326/// [C++0x] 'decltype' ( expression )
John McCallfec54012009-08-03 20:12:06 +00001327bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001328 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001329 unsigned &DiagID,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001330 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001331 SourceLocation Loc = Tok.getLocation();
1332
1333 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001334 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001335 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001336 // Annotate typenames and C++ scope specifiers. If we get one, just
1337 // recurse to handle whatever we get.
1338 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001339 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1340 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001341 // Otherwise, not a type specifier.
1342 return false;
1343 case tok::coloncolon: // ::foo::bar
1344 if (NextToken().is(tok::kw_new) || // ::new
1345 NextToken().is(tok::kw_delete)) // ::delete
1346 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Chris Lattner166a8fc2009-01-04 23:41:41 +00001348 // Annotate typenames and C++ scope specifiers. If we get one, just
1349 // recurse to handle whatever we get.
1350 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001351 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1352 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001353 // Otherwise, not a type specifier.
1354 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Douglas Gregor12e083c2008-11-07 15:42:26 +00001356 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001357 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001358 if (Tok.getAnnotationValue())
1359 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001360 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001361 else
1362 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001363 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1364 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Douglas Gregor12e083c2008-11-07 15:42:26 +00001366 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1367 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1368 // Objective-C interface. If we don't have Objective-C or a '<', this is
1369 // just a normal reference to a typedef name.
1370 if (!Tok.is(tok::less) || !getLang().ObjC1)
1371 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001373 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001374 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001375 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1376 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1377 LAngleLoc, EndProtoLoc);
1378 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1379 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Douglas Gregor12e083c2008-11-07 15:42:26 +00001381 DS.SetRangeEnd(EndProtoLoc);
1382 return true;
1383 }
1384
1385 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001386 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001387 break;
1388 case tok::kw_long:
1389 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001390 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1391 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001392 else
John McCallfec54012009-08-03 20:12:06 +00001393 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1394 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001395 break;
1396 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001397 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001398 break;
1399 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001400 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1401 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001402 break;
1403 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001404 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1405 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001406 break;
1407 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001408 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1409 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001410 break;
1411 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001412 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001413 break;
1414 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001415 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001416 break;
1417 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001418 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001419 break;
1420 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001421 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001422 break;
1423 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001424 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001425 break;
1426 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001427 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001428 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001429 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001430 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001431 break;
1432 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001433 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001434 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001435 case tok::kw_bool:
1436 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001437 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001438 break;
1439 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001440 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1441 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001442 break;
1443 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001444 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1445 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001446 break;
1447 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001448 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1449 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001450 break;
1451
1452 // class-specifier:
1453 case tok::kw_class:
1454 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001455 case tok::kw_union: {
1456 tok::TokenKind Kind = Tok.getKind();
1457 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001458 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001459 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001460 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001461
1462 // enum-specifier:
1463 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001464 ConsumeToken();
1465 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001466 return true;
1467
1468 // cv-qualifier:
1469 case tok::kw_const:
1470 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001471 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001472 break;
1473 case tok::kw_volatile:
1474 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001475 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001476 break;
1477 case tok::kw_restrict:
1478 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001479 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001480 break;
1481
1482 // GNU typeof support.
1483 case tok::kw_typeof:
1484 ParseTypeofSpecifier(DS);
1485 return true;
1486
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001487 // C++0x decltype support.
1488 case tok::kw_decltype:
1489 ParseDecltypeSpecifier(DS);
1490 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001492 // C++0x auto support.
1493 case tok::kw_auto:
1494 if (!getLang().CPlusPlus0x)
1495 return false;
1496
John McCallfec54012009-08-03 20:12:06 +00001497 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001498 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001499 case tok::kw___ptr64:
1500 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001501 case tok::kw___cdecl:
1502 case tok::kw___stdcall:
1503 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001504 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001505 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001506
Douglas Gregor12e083c2008-11-07 15:42:26 +00001507 default:
1508 // Not a type-specifier; do nothing.
1509 return false;
1510 }
1511
1512 // If the specifier combination wasn't legal, issue a diagnostic.
1513 if (isInvalid) {
1514 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001515 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001516 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001517 }
1518 DS.SetRangeEnd(Tok.getLocation());
1519 ConsumeToken(); // whatever we parsed above.
1520 return true;
1521}
Reid Spencer5f016e22007-07-11 17:01:13 +00001522
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001523/// ParseStructDeclaration - Parse a struct declaration without the terminating
1524/// semicolon.
1525///
Reid Spencer5f016e22007-07-11 17:01:13 +00001526/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001527/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001528/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001529/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001530/// struct-declarator-list:
1531/// struct-declarator
1532/// struct-declarator-list ',' struct-declarator
1533/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1534/// struct-declarator:
1535/// declarator
1536/// [GNU] declarator attributes[opt]
1537/// declarator[opt] ':' constant-expression
1538/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1539///
Chris Lattnere1359422008-04-10 06:46:29 +00001540void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001541ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001542 if (Tok.is(tok::kw___extension__)) {
1543 // __extension__ silences extension warnings in the subexpression.
1544 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001545 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001546 return ParseStructDeclaration(DS, Fields);
1547 }
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Steve Naroff28a7ca82007-08-20 22:28:22 +00001549 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001550 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001551 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001553 // If there are no declarators, this is a free-standing declaration
1554 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001555 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001556 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001557 return;
1558 }
1559
1560 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001561 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001562 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001563 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001564 FieldDeclarator DeclaratorInfo(DS);
1565
1566 // Attributes are only allowed here on successive declarators.
1567 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1568 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001569 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001570 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1571 }
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Steve Naroff28a7ca82007-08-20 22:28:22 +00001573 /// struct-declarator: declarator
1574 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001575 if (Tok.isNot(tok::colon)) {
1576 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1577 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001578 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001579 }
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Chris Lattner04d66662007-10-09 17:33:22 +00001581 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001582 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001583 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001584 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001585 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001586 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001587 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001588 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001589
Steve Naroff28a7ca82007-08-20 22:28:22 +00001590 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001591 if (Tok.is(tok::kw___attribute)) {
1592 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001593 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001594 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1595 }
1596
John McCallbdd563e2009-11-03 02:38:08 +00001597 // We're done with this declarator; invoke the callback.
John McCall54abf7d2009-11-04 02:18:39 +00001598 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1599 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001600
Steve Naroff28a7ca82007-08-20 22:28:22 +00001601 // If we don't have a comma, it is either the end of the list (a ';')
1602 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001603 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001604 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001605
Steve Naroff28a7ca82007-08-20 22:28:22 +00001606 // Consume the comma.
1607 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001608
John McCallbdd563e2009-11-03 02:38:08 +00001609 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001610 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001611}
1612
1613/// ParseStructUnionBody
1614/// struct-contents:
1615/// struct-declaration-list
1616/// [EXT] empty
1617/// [GNU] "struct-declaration-list" without terminatoring ';'
1618/// struct-declaration-list:
1619/// struct-declaration
1620/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001621/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001622///
Reid Spencer5f016e22007-07-11 17:01:13 +00001623void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001624 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001625 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1626 PP.getSourceManager(),
1627 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001628
Reid Spencer5f016e22007-07-11 17:01:13 +00001629 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001631 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001632 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1633
Reid Spencer5f016e22007-07-11 17:01:13 +00001634 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1635 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001636 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001637 Diag(Tok, diag::ext_empty_struct_union_enum)
1638 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001639
Chris Lattnerb28317a2009-03-28 19:18:32 +00001640 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001641
Reid Spencer5f016e22007-07-11 17:01:13 +00001642 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001643 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001644 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Reid Spencer5f016e22007-07-11 17:01:13 +00001646 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001647 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001648 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001649 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001650 ConsumeToken();
1651 continue;
1652 }
Chris Lattnere1359422008-04-10 06:46:29 +00001653
1654 // Parse all the comma separated declarators.
1655 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001656
John McCallbdd563e2009-11-03 02:38:08 +00001657 if (!Tok.is(tok::at)) {
1658 struct CFieldCallback : FieldCallback {
1659 Parser &P;
1660 DeclPtrTy TagDecl;
1661 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1662
1663 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1664 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1665 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1666
1667 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001668 // Install the declarator into the current TagDecl.
John McCall4ba39712009-11-03 21:13:47 +00001669 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1670 FD.D.getDeclSpec().getSourceRange().getBegin(),
1671 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001672 FieldDecls.push_back(Field);
1673 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001674 }
John McCallbdd563e2009-11-03 02:38:08 +00001675 } Callback(*this, TagDecl, FieldDecls);
1676
1677 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001678 } else { // Handle @defs
1679 ConsumeToken();
1680 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1681 Diag(Tok, diag::err_unexpected_at);
1682 SkipUntil(tok::semi, true, true);
1683 continue;
1684 }
1685 ConsumeToken();
1686 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1687 if (!Tok.is(tok::identifier)) {
1688 Diag(Tok, diag::err_expected_ident);
1689 SkipUntil(tok::semi, true, true);
1690 continue;
1691 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001692 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001693 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001694 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001695 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1696 ConsumeToken();
1697 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001698 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001699
Chris Lattner04d66662007-10-09 17:33:22 +00001700 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001701 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001702 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001703 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 break;
1705 } else {
1706 Diag(Tok, diag::err_expected_semi_decl_list);
1707 // Skip to end of block or statement
1708 SkipUntil(tok::r_brace, true, true);
1709 }
1710 }
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Steve Naroff60fccee2007-10-29 21:38:07 +00001712 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Reid Spencer5f016e22007-07-11 17:01:13 +00001714 AttributeList *AttrList = 0;
1715 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001716 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001717 AttrList = ParseGNUAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001718
1719 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001720 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001721 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001722 AttrList);
1723 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001724 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001725}
1726
1727
1728/// ParseEnumSpecifier
1729/// enum-specifier: [C99 6.7.2.2]
1730/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001731///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001732/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1733/// '}' attributes[opt]
1734/// 'enum' identifier
1735/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001736///
1737/// [C++] elaborated-type-specifier:
1738/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1739///
Chris Lattner4c97d762009-04-12 21:49:30 +00001740void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1741 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001742 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001743 if (Tok.is(tok::code_completion)) {
1744 // Code completion for an enum name.
1745 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1746 ConsumeToken();
1747 }
1748
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001749 AttributeList *Attr = 0;
1750 // If attributes exist after tag, parse them.
1751 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001752 Attr = ParseGNUAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001753
1754 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001755 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001756 if (Tok.isNot(tok::identifier)) {
1757 Diag(Tok, diag::err_expected_ident);
1758 if (Tok.isNot(tok::l_brace)) {
1759 // Has no name and is not a definition.
1760 // Skip the rest of this declarator, up until the comma or semicolon.
1761 SkipUntil(tok::comma, true);
1762 return;
1763 }
1764 }
1765 }
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001767 // Must have either 'enum name' or 'enum {...}'.
1768 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1769 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001771 // Skip the rest of this declarator, up until the comma or semicolon.
1772 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001773 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001774 }
Mike Stump1eb44332009-09-09 15:08:12 +00001775
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001776 // If an identifier is present, consume and remember it.
1777 IdentifierInfo *Name = 0;
1778 SourceLocation NameLoc;
1779 if (Tok.is(tok::identifier)) {
1780 Name = Tok.getIdentifierInfo();
1781 NameLoc = ConsumeToken();
1782 }
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001784 // There are three options here. If we have 'enum foo;', then this is a
1785 // forward declaration. If we have 'enum foo {...' then this is a
1786 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1787 //
1788 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1789 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1790 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1791 //
John McCall0f434ec2009-07-31 02:45:11 +00001792 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001793 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001794 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001795 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001796 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001797 else
John McCall0f434ec2009-07-31 02:45:11 +00001798 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001799 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001800 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00001801 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001802 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001803 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001804 Owned, IsDependent);
1805 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Chris Lattner04d66662007-10-09 17:33:22 +00001807 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001808 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Reid Spencer5f016e22007-07-11 17:01:13 +00001810 // TODO: semantic analysis on the declspec for enums.
1811 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001812 unsigned DiagID;
1813 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001814 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001815 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001816}
1817
1818/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1819/// enumerator-list:
1820/// enumerator
1821/// enumerator-list ',' enumerator
1822/// enumerator:
1823/// enumeration-constant
1824/// enumeration-constant '=' constant-expression
1825/// enumeration-constant:
1826/// identifier
1827///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001828void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001829 // Enter the scope of the enum body and start the definition.
1830 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001831 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001832
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Chris Lattner7946dd32007-08-27 17:24:30 +00001835 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001836 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001837 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Chris Lattnerb28317a2009-03-28 19:18:32 +00001839 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001840
Chris Lattnerb28317a2009-03-28 19:18:32 +00001841 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Reid Spencer5f016e22007-07-11 17:01:13 +00001843 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001844 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001845 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1846 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Reid Spencer5f016e22007-07-11 17:01:13 +00001848 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001849 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001850 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001851 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001852 AssignedVal = ParseConstantExpression();
1853 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001854 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001855 }
Mike Stump1eb44332009-09-09 15:08:12 +00001856
Reid Spencer5f016e22007-07-11 17:01:13 +00001857 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001858 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1859 LastEnumConstDecl,
1860 IdentLoc, Ident,
1861 EqualLoc,
1862 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001863 EnumConstantDecls.push_back(EnumConstDecl);
1864 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Chris Lattner04d66662007-10-09 17:33:22 +00001866 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001867 break;
1868 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001869
1870 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001871 !(getLang().C99 || getLang().CPlusPlus0x))
1872 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1873 << getLang().CPlusPlus
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001874 << CodeModificationHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001875 }
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Reid Spencer5f016e22007-07-11 17:01:13 +00001877 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001878 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001879
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001880 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001881 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001882 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001883 Attr = ParseGNUAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001884
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001885 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1886 EnumConstantDecls.data(), EnumConstantDecls.size(),
1887 CurScope, Attr);
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Douglas Gregor72de6672009-01-08 20:45:30 +00001889 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001890 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001891}
1892
1893/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001894/// start of a type-qualifier-list.
1895bool Parser::isTypeQualifier() const {
1896 switch (Tok.getKind()) {
1897 default: return false;
1898 // type-qualifier
1899 case tok::kw_const:
1900 case tok::kw_volatile:
1901 case tok::kw_restrict:
1902 return true;
1903 }
1904}
1905
1906/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001907/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001908bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001909 switch (Tok.getKind()) {
1910 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Chris Lattner166a8fc2009-01-04 23:41:41 +00001912 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001913 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001914 // Annotate typenames and C++ scope specifiers. If we get one, just
1915 // recurse to handle whatever we get.
1916 if (TryAnnotateTypeOrScopeToken())
1917 return isTypeSpecifierQualifier();
1918 // Otherwise, not a type specifier.
1919 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001920
Chris Lattner166a8fc2009-01-04 23:41:41 +00001921 case tok::coloncolon: // ::foo::bar
1922 if (NextToken().is(tok::kw_new) || // ::new
1923 NextToken().is(tok::kw_delete)) // ::delete
1924 return false;
1925
1926 // Annotate typenames and C++ scope specifiers. If we get one, just
1927 // recurse to handle whatever we get.
1928 if (TryAnnotateTypeOrScopeToken())
1929 return isTypeSpecifierQualifier();
1930 // Otherwise, not a type specifier.
1931 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 // GNU attributes support.
1934 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001935 // GNU typeof support.
1936 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Reid Spencer5f016e22007-07-11 17:01:13 +00001938 // type-specifiers
1939 case tok::kw_short:
1940 case tok::kw_long:
1941 case tok::kw_signed:
1942 case tok::kw_unsigned:
1943 case tok::kw__Complex:
1944 case tok::kw__Imaginary:
1945 case tok::kw_void:
1946 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001947 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001948 case tok::kw_char16_t:
1949 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001950 case tok::kw_int:
1951 case tok::kw_float:
1952 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001953 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001954 case tok::kw__Bool:
1955 case tok::kw__Decimal32:
1956 case tok::kw__Decimal64:
1957 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00001958
Chris Lattner99dc9142008-04-13 18:59:07 +00001959 // struct-or-union-specifier (C99) or class-specifier (C++)
1960 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001961 case tok::kw_struct:
1962 case tok::kw_union:
1963 // enum-specifier
1964 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Reid Spencer5f016e22007-07-11 17:01:13 +00001966 // type-qualifier
1967 case tok::kw_const:
1968 case tok::kw_volatile:
1969 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001970
1971 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001972 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001973 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Chris Lattner7c186be2008-10-20 00:25:30 +00001975 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1976 case tok::less:
1977 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Steve Naroff239f0732008-12-25 14:16:32 +00001979 case tok::kw___cdecl:
1980 case tok::kw___stdcall:
1981 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001982 case tok::kw___w64:
1983 case tok::kw___ptr64:
1984 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001985 }
1986}
1987
1988/// isDeclarationSpecifier() - Return true if the current token is part of a
1989/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001990bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001991 switch (Tok.getKind()) {
1992 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Chris Lattner166a8fc2009-01-04 23:41:41 +00001994 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001995 // Unfortunate hack to support "Class.factoryMethod" notation.
1996 if (getLang().ObjC1 && NextToken().is(tok::period))
1997 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001998 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001999
Douglas Gregord57959a2009-03-27 23:10:48 +00002000 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002001 // Annotate typenames and C++ scope specifiers. If we get one, just
2002 // recurse to handle whatever we get.
2003 if (TryAnnotateTypeOrScopeToken())
2004 return isDeclarationSpecifier();
2005 // Otherwise, not a declaration specifier.
2006 return false;
2007 case tok::coloncolon: // ::foo::bar
2008 if (NextToken().is(tok::kw_new) || // ::new
2009 NextToken().is(tok::kw_delete)) // ::delete
2010 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Chris Lattner166a8fc2009-01-04 23:41:41 +00002012 // Annotate typenames and C++ scope specifiers. If we get one, just
2013 // recurse to handle whatever we get.
2014 if (TryAnnotateTypeOrScopeToken())
2015 return isDeclarationSpecifier();
2016 // Otherwise, not a declaration specifier.
2017 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Reid Spencer5f016e22007-07-11 17:01:13 +00002019 // storage-class-specifier
2020 case tok::kw_typedef:
2021 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002022 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002023 case tok::kw_static:
2024 case tok::kw_auto:
2025 case tok::kw_register:
2026 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002027
Reid Spencer5f016e22007-07-11 17:01:13 +00002028 // type-specifiers
2029 case tok::kw_short:
2030 case tok::kw_long:
2031 case tok::kw_signed:
2032 case tok::kw_unsigned:
2033 case tok::kw__Complex:
2034 case tok::kw__Imaginary:
2035 case tok::kw_void:
2036 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002037 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002038 case tok::kw_char16_t:
2039 case tok::kw_char32_t:
2040
Reid Spencer5f016e22007-07-11 17:01:13 +00002041 case tok::kw_int:
2042 case tok::kw_float:
2043 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002044 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002045 case tok::kw__Bool:
2046 case tok::kw__Decimal32:
2047 case tok::kw__Decimal64:
2048 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00002049
Chris Lattner99dc9142008-04-13 18:59:07 +00002050 // struct-or-union-specifier (C99) or class-specifier (C++)
2051 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002052 case tok::kw_struct:
2053 case tok::kw_union:
2054 // enum-specifier
2055 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002056
Reid Spencer5f016e22007-07-11 17:01:13 +00002057 // type-qualifier
2058 case tok::kw_const:
2059 case tok::kw_volatile:
2060 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002061
Reid Spencer5f016e22007-07-11 17:01:13 +00002062 // function-specifier
2063 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002064 case tok::kw_virtual:
2065 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002066
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002067 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002068 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002069
Chris Lattner1ef08762007-08-09 17:01:07 +00002070 // GNU typeof support.
2071 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Chris Lattner1ef08762007-08-09 17:01:07 +00002073 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002074 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002075 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002076
Chris Lattnerf3948c42008-07-26 03:38:44 +00002077 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2078 case tok::less:
2079 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002080
Steve Naroff47f52092009-01-06 19:34:12 +00002081 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002082 case tok::kw___cdecl:
2083 case tok::kw___stdcall:
2084 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002085 case tok::kw___w64:
2086 case tok::kw___ptr64:
2087 case tok::kw___forceinline:
2088 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002089 }
2090}
2091
2092
2093/// ParseTypeQualifierListOpt
2094/// type-qualifier-list: [C99 6.7.5]
2095/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002096/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002097/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002098/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Sean Huntbbd37c62009-11-21 08:43:09 +00002099/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2100/// if CXX0XAttributesAllowed = true
Reid Spencer5f016e22007-07-11 17:01:13 +00002101///
Sean Huntbbd37c62009-11-21 08:43:09 +00002102void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2103 bool CXX0XAttributesAllowed) {
2104 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2105 SourceLocation Loc = Tok.getLocation();
2106 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2107 if (CXX0XAttributesAllowed)
2108 DS.AddAttributes(Attr.AttrList);
2109 else
2110 Diag(Loc, diag::err_attributes_not_allowed);
2111 }
2112
Reid Spencer5f016e22007-07-11 17:01:13 +00002113 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002114 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002115 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002116 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002117 SourceLocation Loc = Tok.getLocation();
2118
2119 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002120 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002121 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2122 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002123 break;
2124 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002125 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2126 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002127 break;
2128 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002129 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2130 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002131 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002132 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002133 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002134 case tok::kw___cdecl:
2135 case tok::kw___stdcall:
2136 case tok::kw___fastcall:
Sean Huntbbd37c62009-11-21 08:43:09 +00002137 if (GNUAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002138 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2139 continue;
2140 }
2141 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002142 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002143 if (GNUAttributesAllowed) {
2144 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002145 continue; // do *not* consume the next token!
2146 }
2147 // otherwise, FALL THROUGH!
2148 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002149 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002150 // If this is not a type-qualifier token, we're done reading type
2151 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002152 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002153 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002154 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002155
Reid Spencer5f016e22007-07-11 17:01:13 +00002156 // If the specifier combination wasn't legal, issue a diagnostic.
2157 if (isInvalid) {
2158 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002159 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002160 }
2161 ConsumeToken();
2162 }
2163}
2164
2165
2166/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2167///
2168void Parser::ParseDeclarator(Declarator &D) {
2169 /// This implements the 'declarator' production in the C grammar, then checks
2170 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002171 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002172}
2173
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002174/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2175/// is parsed by the function passed to it. Pass null, and the direct-declarator
2176/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002177/// ptr-operator production.
2178///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002179/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2180/// [C] pointer[opt] direct-declarator
2181/// [C++] direct-declarator
2182/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002183///
2184/// pointer: [C99 6.7.5]
2185/// '*' type-qualifier-list[opt]
2186/// '*' type-qualifier-list[opt] pointer
2187///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002188/// ptr-operator:
2189/// '*' cv-qualifier-seq[opt]
2190/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002191/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002192/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002193/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002194/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002195void Parser::ParseDeclaratorInternal(Declarator &D,
2196 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002197 if (Diags.hasAllExtensionsSilenced())
2198 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002199 // C++ member pointers start with a '::' or a nested-name.
2200 // Member pointers get special handling, since there's no place for the
2201 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002202 if (getLang().CPlusPlus &&
2203 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2204 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002205 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002206 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002207 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002208 // The scope spec really belongs to the direct-declarator.
2209 D.getCXXScopeSpec() = SS;
2210 if (DirectDeclParser)
2211 (this->*DirectDeclParser)(D);
2212 return;
2213 }
2214
2215 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002216 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002217 DeclSpec DS;
2218 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002219 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002220
2221 // Recurse to parse whatever is left.
2222 ParseDeclaratorInternal(D, DirectDeclParser);
2223
2224 // Sema will have to catch (syntactically invalid) pointers into global
2225 // scope. It has to catch pointers into namespace scope anyway.
2226 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002227 Loc, DS.TakeAttributes()),
2228 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002229 return;
2230 }
2231 }
2232
2233 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002234 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002235 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002236 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002237 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002238 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002239 if (DirectDeclParser)
2240 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002241 return;
2242 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002243
Sebastian Redl05532f22009-03-15 22:02:01 +00002244 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2245 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002246 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002247 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002248
Chris Lattner9af55002009-03-27 04:18:06 +00002249 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002250 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002251 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002252
Reid Spencer5f016e22007-07-11 17:01:13 +00002253 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002254 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002255
Reid Spencer5f016e22007-07-11 17:01:13 +00002256 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002257 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002258 if (Kind == tok::star)
2259 // Remember that we parsed a pointer type, and remember the type-quals.
2260 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002261 DS.TakeAttributes()),
2262 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002263 else
2264 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002265 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002266 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002267 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002268 } else {
2269 // Is a reference
2270 DeclSpec DS;
2271
Sebastian Redl743de1f2009-03-23 00:00:23 +00002272 // Complain about rvalue references in C++03, but then go on and build
2273 // the declarator.
2274 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2275 Diag(Loc, diag::err_rvalue_reference);
2276
Reid Spencer5f016e22007-07-11 17:01:13 +00002277 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2278 // cv-qualifiers are introduced through the use of a typedef or of a
2279 // template type argument, in which case the cv-qualifiers are ignored.
2280 //
2281 // [GNU] Retricted references are allowed.
2282 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002283 // [C++0x] Attributes on references are not allowed.
2284 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002285 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002286
2287 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2288 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2289 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002290 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002291 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2292 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002293 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002294 }
2295
2296 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002297 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002298
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002299 if (D.getNumTypeObjects() > 0) {
2300 // C++ [dcl.ref]p4: There shall be no references to references.
2301 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2302 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002303 if (const IdentifierInfo *II = D.getIdentifier())
2304 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2305 << II;
2306 else
2307 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2308 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002309
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002310 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002311 // can go ahead and build the (technically ill-formed)
2312 // declarator: reference collapsing will take care of it.
2313 }
2314 }
2315
Reid Spencer5f016e22007-07-11 17:01:13 +00002316 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002317 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002318 DS.TakeAttributes(),
2319 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002320 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002321 }
2322}
2323
2324/// ParseDirectDeclarator
2325/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002326/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002327/// '(' declarator ')'
2328/// [GNU] '(' attributes declarator ')'
2329/// [C90] direct-declarator '[' constant-expression[opt] ']'
2330/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2331/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2332/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2333/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2334/// direct-declarator '(' parameter-type-list ')'
2335/// direct-declarator '(' identifier-list[opt] ')'
2336/// [GNU] direct-declarator '(' parameter-forward-declarations
2337/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002338/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2339/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002340/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002341///
2342/// declarator-id: [C++ 8]
2343/// id-expression
2344/// '::'[opt] nested-name-specifier[opt] type-name
2345///
2346/// id-expression: [C++ 5.1]
2347/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002348/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002349///
2350/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002351/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002352/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002353/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002354/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002355/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002356///
Reid Spencer5f016e22007-07-11 17:01:13 +00002357void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002358 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002359
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002360 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2361 // ParseDeclaratorInternal might already have parsed the scope.
2362 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2363 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2364 true);
2365 if (afterCXXScope) {
John McCalle7e278b2009-12-11 20:04:54 +00002366 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2367 // Change the declaration context for name lookup, until this function
2368 // is exited (and the declarator has been parsed).
2369 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002370 }
2371
2372 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2373 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2374 // We found something that indicates the start of an unqualified-id.
2375 // Parse that unqualified-id.
2376 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2377 /*EnteringContext=*/true,
2378 /*AllowDestructorName=*/true,
Zhongxing Xua3ddec22009-12-28 06:49:22 +00002379 /*AllowConstructorName=*/!D.getDeclSpec().hasTypeSpecifier(),
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002380 /*ObjectType=*/0,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002381 D.getName())) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002382 D.SetIdentifier(0, Tok.getLocation());
2383 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002384 } else {
2385 // Parsed the unqualified-id; update range information and move along.
2386 if (D.getSourceRange().getBegin().isInvalid())
2387 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2388 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002389 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002390 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002391 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002392 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002393 assert(!getLang().CPlusPlus &&
2394 "There's a C++-specific check for tok::identifier above");
2395 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2396 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2397 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002398 goto PastIdentifier;
2399 }
2400
2401 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002402 // direct-declarator: '(' declarator ')'
2403 // direct-declarator: '(' attributes declarator ')'
2404 // Example: 'char (*X)' or 'int (*XX)(void)'
2405 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002406 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002407 // This could be something simple like "int" (in which case the declarator
2408 // portion is empty), if an abstract-declarator is allowed.
2409 D.SetIdentifier(0, Tok.getLocation());
2410 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002411 if (D.getContext() == Declarator::MemberContext)
2412 Diag(Tok, diag::err_expected_member_name_or_semi)
2413 << D.getDeclSpec().getSourceRange();
2414 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002415 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002416 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002417 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002418 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002419 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002420 }
Mike Stump1eb44332009-09-09 15:08:12 +00002421
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002422 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002423 assert(D.isPastIdentifier() &&
2424 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002425
Sean Huntbbd37c62009-11-21 08:43:09 +00002426 // Don't parse attributes unless we have an identifier.
2427 if (D.getIdentifier() && getLang().CPlusPlus
2428 && isCXX0XAttributeSpecifier(true)) {
2429 SourceLocation AttrEndLoc;
2430 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2431 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2432 }
2433
Reid Spencer5f016e22007-07-11 17:01:13 +00002434 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002435 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002436 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2437 // In such a case, check if we actually have a function declarator; if it
2438 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002439 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2440 // When not in file scope, warn for ambiguous function declarators, just
2441 // in case the author intended it as a variable definition.
2442 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2443 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2444 break;
2445 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002446 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002447 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002448 ParseBracketDeclarator(D);
2449 } else {
2450 break;
2451 }
2452 }
2453}
2454
Chris Lattneref4715c2008-04-06 05:45:57 +00002455/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2456/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002457/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002458/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2459///
2460/// direct-declarator:
2461/// '(' declarator ')'
2462/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002463/// direct-declarator '(' parameter-type-list ')'
2464/// direct-declarator '(' identifier-list[opt] ')'
2465/// [GNU] direct-declarator '(' parameter-forward-declarations
2466/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002467///
2468void Parser::ParseParenDeclarator(Declarator &D) {
2469 SourceLocation StartLoc = ConsumeParen();
2470 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002471
Chris Lattner7399ee02008-10-20 02:05:46 +00002472 // Eat any attributes before we look at whether this is a grouping or function
2473 // declarator paren. If this is a grouping paren, the attribute applies to
2474 // the type being built up, for example:
2475 // int (__attribute__(()) *x)(long y)
2476 // If this ends up not being a grouping paren, the attribute applies to the
2477 // first argument, for example:
2478 // int (__attribute__(()) int x)
2479 // In either case, we need to eat any attributes to be able to determine what
2480 // sort of paren this is.
2481 //
2482 AttributeList *AttrList = 0;
2483 bool RequiresArg = false;
2484 if (Tok.is(tok::kw___attribute)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002485 AttrList = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00002486
Chris Lattner7399ee02008-10-20 02:05:46 +00002487 // We require that the argument list (if this is a non-grouping paren) be
2488 // present even if the attribute list was empty.
2489 RequiresArg = true;
2490 }
Steve Naroff239f0732008-12-25 14:16:32 +00002491 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002492 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2493 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2494 Tok.is(tok::kw___ptr64)) {
2495 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2496 }
Mike Stump1eb44332009-09-09 15:08:12 +00002497
Chris Lattneref4715c2008-04-06 05:45:57 +00002498 // If we haven't past the identifier yet (or where the identifier would be
2499 // stored, if this is an abstract declarator), then this is probably just
2500 // grouping parens. However, if this could be an abstract-declarator, then
2501 // this could also be the start of function arguments (consider 'void()').
2502 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002503
Chris Lattneref4715c2008-04-06 05:45:57 +00002504 if (!D.mayOmitIdentifier()) {
2505 // If this can't be an abstract-declarator, this *must* be a grouping
2506 // paren, because we haven't seen the identifier yet.
2507 isGrouping = true;
2508 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002509 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002510 isDeclarationSpecifier()) { // 'int(int)' is a function.
2511 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2512 // considered to be a type, not a K&R identifier-list.
2513 isGrouping = false;
2514 } else {
2515 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2516 isGrouping = true;
2517 }
Mike Stump1eb44332009-09-09 15:08:12 +00002518
Chris Lattneref4715c2008-04-06 05:45:57 +00002519 // If this is a grouping paren, handle:
2520 // direct-declarator: '(' declarator ')'
2521 // direct-declarator: '(' attributes declarator ')'
2522 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002523 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002524 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002525 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002526 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002527
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002528 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002529 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002530 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002531
2532 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002533 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002534 return;
2535 }
Mike Stump1eb44332009-09-09 15:08:12 +00002536
Chris Lattneref4715c2008-04-06 05:45:57 +00002537 // Okay, if this wasn't a grouping paren, it must be the start of a function
2538 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002539 // identifier (and remember where it would have been), then call into
2540 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002541 D.SetIdentifier(0, Tok.getLocation());
2542
Chris Lattner7399ee02008-10-20 02:05:46 +00002543 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002544}
2545
2546/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2547/// declarator D up to a paren, which indicates that we are parsing function
2548/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002549///
Chris Lattner7399ee02008-10-20 02:05:46 +00002550/// If AttrList is non-null, then the caller parsed those arguments immediately
2551/// after the open paren - they should be considered to be the first argument of
2552/// a parameter. If RequiresArg is true, then the first argument of the
2553/// function is required to be present and required to not be an identifier
2554/// list.
2555///
Reid Spencer5f016e22007-07-11 17:01:13 +00002556/// This method also handles this portion of the grammar:
2557/// parameter-type-list: [C99 6.7.5]
2558/// parameter-list
2559/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002560/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002561///
2562/// parameter-list: [C99 6.7.5]
2563/// parameter-declaration
2564/// parameter-list ',' parameter-declaration
2565///
2566/// parameter-declaration: [C99 6.7.5]
2567/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002568/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002569/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002570/// declaration-specifiers abstract-declarator[opt]
2571/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002572/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002573/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2574///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002575/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002576/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002577///
Chris Lattner7399ee02008-10-20 02:05:46 +00002578void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2579 AttributeList *AttrList,
2580 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002581 // lparen is already consumed!
2582 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002583
Chris Lattner7399ee02008-10-20 02:05:46 +00002584 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002585 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002586 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002587 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002588 delete AttrList;
2589 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002590
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002591 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2592 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002593
2594 // cv-qualifier-seq[opt].
2595 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002596 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002597 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002598 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002599 llvm::SmallVector<TypeTy*, 2> Exceptions;
2600 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002601 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002602 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002603 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002604 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002605
2606 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002607 if (Tok.is(tok::kw_throw)) {
2608 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002609 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002610 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002611 hasAnyExceptionSpec);
2612 assert(Exceptions.size() == ExceptionRanges.size() &&
2613 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002614 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002615 }
2616
Chris Lattnerf97409f2008-04-06 06:57:35 +00002617 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002618 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002619 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002620 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002621 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002622 /*arglist*/ 0, 0,
2623 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002624 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002625 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002626 Exceptions.data(),
2627 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002628 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002629 LParenLoc, RParenLoc, D),
2630 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002631 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002632 }
2633
Chris Lattner7399ee02008-10-20 02:05:46 +00002634 // Alternatively, this parameter list may be an identifier list form for a
2635 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002636 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002637 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002638 // K&R identifier lists can't have typedefs as identifiers, per
2639 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002640 if (RequiresArg) {
2641 Diag(Tok, diag::err_argument_required_after_attribute);
2642 delete AttrList;
2643 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002644 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2645 // normal declarators, not for abstract-declarators.
2646 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002647 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002648 }
Mike Stump1eb44332009-09-09 15:08:12 +00002649
Chris Lattnerf97409f2008-04-06 06:57:35 +00002650 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002651
Chris Lattnerf97409f2008-04-06 06:57:35 +00002652 // Build up an array of information about the parsed arguments.
2653 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002654
2655 // Enter function-declaration scope, limiting any declarators to the
2656 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002657 ParseScope PrototypeScope(this,
2658 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002659
Chris Lattnerf97409f2008-04-06 06:57:35 +00002660 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002661 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002662 while (1) {
2663 if (Tok.is(tok::ellipsis)) {
2664 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002665 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002666 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002667 }
Mike Stump1eb44332009-09-09 15:08:12 +00002668
Chris Lattnerf97409f2008-04-06 06:57:35 +00002669 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002670
Chris Lattnerf97409f2008-04-06 06:57:35 +00002671 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00002672 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002673 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002674
2675 // If the caller parsed attributes for the first argument, add them now.
2676 if (AttrList) {
2677 DS.AddAttributes(AttrList);
2678 AttrList = 0; // Only apply the attributes to the first parameter.
2679 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002680 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002681
Chris Lattnerf97409f2008-04-06 06:57:35 +00002682 // Parse the declarator. This is "PrototypeContext", because we must
2683 // accept either 'declarator' or 'abstract-declarator' here.
2684 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2685 ParseDeclarator(ParmDecl);
2686
2687 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002688 if (Tok.is(tok::kw___attribute)) {
2689 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00002690 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002691 ParmDecl.AddAttributes(AttrList, Loc);
2692 }
Mike Stump1eb44332009-09-09 15:08:12 +00002693
Chris Lattnerf97409f2008-04-06 06:57:35 +00002694 // Remember this parsed parameter in ParamInfo.
2695 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Douglas Gregor72b505b2008-12-16 21:30:33 +00002697 // DefArgToks is used when the parsing of default arguments needs
2698 // to be delayed.
2699 CachedTokens *DefArgToks = 0;
2700
Chris Lattnerf97409f2008-04-06 06:57:35 +00002701 // If no parameter was specified, verify that *something* was specified,
2702 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002703 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2704 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002705 // Completely missing, emit error.
2706 Diag(DSStart, diag::err_missing_param);
2707 } else {
2708 // Otherwise, we have something. Add it and let semantic analysis try
2709 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00002710
Chris Lattnerf97409f2008-04-06 06:57:35 +00002711 // Inform the actions module about the parameter declarator, so it gets
2712 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002713 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002714
2715 // Parse the default argument, if any. We parse the default
2716 // arguments in all dialects; the semantic analysis in
2717 // ActOnParamDefaultArgument will reject the default argument in
2718 // C.
2719 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002720 SourceLocation EqualLoc = Tok.getLocation();
2721
Chris Lattner04421082008-04-08 04:40:51 +00002722 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002723 if (D.getContext() == Declarator::MemberContext) {
2724 // If we're inside a class definition, cache the tokens
2725 // corresponding to the default argument. We'll actually parse
2726 // them when we see the end of the class definition.
2727 // FIXME: Templates will require something similar.
2728 // FIXME: Can we use a smart pointer for Toks?
2729 DefArgToks = new CachedTokens;
2730
Mike Stump1eb44332009-09-09 15:08:12 +00002731 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002732 tok::semi, false)) {
2733 delete DefArgToks;
2734 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002735 Actions.ActOnParamDefaultArgumentError(Param);
2736 } else
Mike Stump1eb44332009-09-09 15:08:12 +00002737 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00002738 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002739 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002740 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002741 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002742
Douglas Gregor72b505b2008-12-16 21:30:33 +00002743 OwningExprResult DefArgResult(ParseAssignmentExpression());
2744 if (DefArgResult.isInvalid()) {
2745 Actions.ActOnParamDefaultArgumentError(Param);
2746 SkipUntil(tok::comma, tok::r_paren, true, true);
2747 } else {
2748 // Inform the actions module about the default argument
2749 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002750 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002751 }
Chris Lattner04421082008-04-08 04:40:51 +00002752 }
2753 }
Mike Stump1eb44332009-09-09 15:08:12 +00002754
2755 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2756 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002757 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002758 }
2759
2760 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00002761 if (Tok.isNot(tok::comma)) {
2762 if (Tok.is(tok::ellipsis)) {
2763 IsVariadic = true;
2764 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2765
2766 if (!getLang().CPlusPlus) {
2767 // We have ellipsis without a preceding ',', which is ill-formed
2768 // in C. Complain and provide the fix.
2769 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2770 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2771 }
2772 }
2773
2774 break;
2775 }
Mike Stump1eb44332009-09-09 15:08:12 +00002776
Chris Lattnerf97409f2008-04-06 06:57:35 +00002777 // Consume the comma.
2778 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002779 }
Mike Stump1eb44332009-09-09 15:08:12 +00002780
Chris Lattnerf97409f2008-04-06 06:57:35 +00002781 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002782 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00002783
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002784 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002785 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2786 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002787
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002788 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002789 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002790 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002791 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002792 llvm::SmallVector<TypeTy*, 2> Exceptions;
2793 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00002794
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002795 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002796 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002797 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002798 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002799 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002800
2801 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002802 if (Tok.is(tok::kw_throw)) {
2803 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002804 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002805 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002806 hasAnyExceptionSpec);
2807 assert(Exceptions.size() == ExceptionRanges.size() &&
2808 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002809 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002810 }
2811
Reid Spencer5f016e22007-07-11 17:01:13 +00002812 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002813 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002814 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002815 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002816 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002817 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002818 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002819 Exceptions.data(),
2820 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002821 Exceptions.size(),
2822 LParenLoc, RParenLoc, D),
2823 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002824}
2825
Chris Lattner66d28652008-04-06 06:34:08 +00002826/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2827/// we found a K&R-style identifier list instead of a type argument list. The
2828/// current token is known to be the first identifier in the list.
2829///
2830/// identifier-list: [C99 6.7.5]
2831/// identifier
2832/// identifier-list ',' identifier
2833///
2834void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2835 Declarator &D) {
2836 // Build up an array of information about the parsed arguments.
2837 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2838 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00002839
Chris Lattner66d28652008-04-06 06:34:08 +00002840 // If there was no identifier specified for the declarator, either we are in
2841 // an abstract-declarator, or we are in a parameter declarator which was found
2842 // to be abstract. In abstract-declarators, identifier lists are not valid:
2843 // diagnose this.
2844 if (!D.getIdentifier())
2845 Diag(Tok, diag::ext_ident_list_in_param);
2846
2847 // Tok is known to be the first identifier in the list. Remember this
2848 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002849 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002850 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002851 Tok.getLocation(),
2852 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00002853
Chris Lattner50c64772008-04-06 06:39:19 +00002854 ConsumeToken(); // eat the first identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00002855
Chris Lattner66d28652008-04-06 06:34:08 +00002856 while (Tok.is(tok::comma)) {
2857 // Eat the comma.
2858 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002859
Chris Lattner50c64772008-04-06 06:39:19 +00002860 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002861 if (Tok.isNot(tok::identifier)) {
2862 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002863 SkipUntil(tok::r_paren);
2864 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002865 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002866
Chris Lattner66d28652008-04-06 06:34:08 +00002867 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002868
2869 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002870 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002871 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00002872
Chris Lattner66d28652008-04-06 06:34:08 +00002873 // Verify that the argument identifier has not already been mentioned.
2874 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002875 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002876 } else {
2877 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002878 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002879 Tok.getLocation(),
2880 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002881 }
Mike Stump1eb44332009-09-09 15:08:12 +00002882
Chris Lattner66d28652008-04-06 06:34:08 +00002883 // Eat the identifier.
2884 ConsumeToken();
2885 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002886
2887 // If we have the closing ')', eat it and we're done.
2888 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2889
Chris Lattner50c64772008-04-06 06:39:19 +00002890 // Remember that we parsed a function type, and remember the attributes. This
2891 // function type is always a K&R style function type, which is not varargs and
2892 // has no prototype.
2893 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002894 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002895 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002896 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00002897 /*exception*/false,
2898 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002899 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002900 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002901}
Chris Lattneref4715c2008-04-06 05:45:57 +00002902
Reid Spencer5f016e22007-07-11 17:01:13 +00002903/// [C90] direct-declarator '[' constant-expression[opt] ']'
2904/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2905/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2906/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2907/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2908void Parser::ParseBracketDeclarator(Declarator &D) {
2909 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00002910
Chris Lattner378c7e42008-12-18 07:27:21 +00002911 // C array syntax has many features, but by-far the most common is [] and [4].
2912 // This code does a fast path to handle some of the most obvious cases.
2913 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002914 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002915 //FIXME: Use these
2916 CXX0XAttributeList Attr;
2917 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
2918 Attr = ParseCXX0XAttributes();
2919 }
2920
Chris Lattner378c7e42008-12-18 07:27:21 +00002921 // Remember that we parsed the empty array type.
2922 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002923 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2924 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002925 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002926 return;
2927 } else if (Tok.getKind() == tok::numeric_constant &&
2928 GetLookAheadToken(1).is(tok::r_square)) {
2929 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002930 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002931 ConsumeToken();
2932
Sebastian Redlab197ba2009-02-09 18:23:29 +00002933 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002934 //FIXME: Use these
2935 CXX0XAttributeList Attr;
2936 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2937 Attr = ParseCXX0XAttributes();
2938 }
Chris Lattner378c7e42008-12-18 07:27:21 +00002939
2940 // If there was an error parsing the assignment-expression, recover.
2941 if (ExprRes.isInvalid())
2942 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00002943
Chris Lattner378c7e42008-12-18 07:27:21 +00002944 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002945 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2946 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002947 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002948 return;
2949 }
Mike Stump1eb44332009-09-09 15:08:12 +00002950
Reid Spencer5f016e22007-07-11 17:01:13 +00002951 // If valid, this location is the position where we read the 'static' keyword.
2952 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002953 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002954 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002955
Reid Spencer5f016e22007-07-11 17:01:13 +00002956 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002957 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002958 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002959 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00002960
Reid Spencer5f016e22007-07-11 17:01:13 +00002961 // If we haven't already read 'static', check to see if there is one after the
2962 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002963 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002964 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002965
Reid Spencer5f016e22007-07-11 17:01:13 +00002966 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2967 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002968 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00002969
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002970 // Handle the case where we have '[*]' as the array size. However, a leading
2971 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2972 // the the token after the star is a ']'. Since stars in arrays are
2973 // infrequent, use of lookahead is not costly here.
2974 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002975 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002976
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002977 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002978 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002979 StaticLoc = SourceLocation(); // Drop the static.
2980 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002981 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002982 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002983 // Note, in C89, this production uses the constant-expr production instead
2984 // of assignment-expr. The only difference is that assignment-expr allows
2985 // things like '=' and '*='. Sema rejects these in C89 mode because they
2986 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00002987
Douglas Gregore0762c92009-06-19 23:52:42 +00002988 // Parse the constant-expression or assignment-expression now (depending
2989 // on dialect).
2990 if (getLang().CPlusPlus)
2991 NumElements = ParseConstantExpression();
2992 else
2993 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00002994 }
Mike Stump1eb44332009-09-09 15:08:12 +00002995
Reid Spencer5f016e22007-07-11 17:01:13 +00002996 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002997 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002998 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002999 // If the expression was invalid, skip it.
3000 SkipUntil(tok::r_square);
3001 return;
3002 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003003
3004 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3005
Sean Huntbbd37c62009-11-21 08:43:09 +00003006 //FIXME: Use these
3007 CXX0XAttributeList Attr;
3008 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3009 Attr = ParseCXX0XAttributes();
3010 }
3011
Chris Lattner378c7e42008-12-18 07:27:21 +00003012 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003013 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3014 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003015 NumElements.release(),
3016 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003017 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003018}
3019
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003020/// [GNU] typeof-specifier:
3021/// typeof ( expressions )
3022/// typeof ( type-name )
3023/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003024///
3025void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003026 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003027 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003028 SourceLocation StartLoc = ConsumeToken();
3029
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003030 bool isCastExpr;
3031 TypeTy *CastTy;
3032 SourceRange CastRange;
3033 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3034 isCastExpr,
3035 CastTy,
3036 CastRange);
3037
3038 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003039 // FIXME: Not accurate, the range gets one token more than it should.
3040 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003041 else
3042 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003043
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003044 if (isCastExpr) {
3045 if (!CastTy) {
3046 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003047 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003048 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003049
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003050 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003051 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003052 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3053 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003054 DiagID, CastTy))
3055 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003056 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003057 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003058
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003059 // If we get here, the operand to the typeof was an expresion.
3060 if (Operand.isInvalid()) {
3061 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003062 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003063 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003064
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003065 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003066 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003067 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3068 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003069 DiagID, Operand.release()))
3070 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003071}