blob: f429ac991d83958967b5692a834a05eff990712a [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)) {
817 Actions.CodeCompleteOrdinaryName(CurScope);
818 ConsumeToken();
819 }
820
Chris Lattner81c018d2008-03-13 06:29:04 +0000821 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000823 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000825 unsigned DiagID = 0;
826
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000828
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000830 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000831 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 // If this is not a declaration specifier token, we're done reading decl
833 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000834 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000836
Chris Lattner5e02c472009-01-05 00:07:25 +0000837 case tok::coloncolon: // ::foo::bar
838 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000839 if (TryAnnotateCXXScopeToken(true))
Chris Lattner5e02c472009-01-05 00:07:25 +0000840 continue;
841 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000842
843 case tok::annot_cxxscope: {
844 if (DS.hasTypeSpecifier())
845 goto DoneWithDeclSpec;
846
John McCallaa87d332009-12-12 11:40:51 +0000847 CXXScopeSpec SS;
848 SS.setScopeRep(Tok.getAnnotationValue());
849 SS.setRange(Tok.getAnnotationRange());
850
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000851 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000852 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000853 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000854 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000855 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000856 // We have a qualified template-id, e.g., N::A<int>
John McCallaa87d332009-12-12 11:40:51 +0000857 DS.getTypeSpecScope() = SS;
858 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +0000859 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000860 "ParseOptionalCXXScopeSpecifier not working");
861 AnnotateTemplateIdTokenAsType(&SS);
862 continue;
863 }
864
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000865 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +0000866 DS.getTypeSpecScope() = SS;
867 ConsumeToken(); // The C++ scope.
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000868 if (Tok.getAnnotationValue())
869 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
870 PrevSpec, DiagID,
871 Tok.getAnnotationValue());
872 else
873 DS.SetTypeSpecError();
874 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
875 ConsumeToken(); // The typename
876 }
877
Douglas Gregor9135c722009-03-25 15:40:00 +0000878 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000879 goto DoneWithDeclSpec;
880
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000881 // If the next token is the name of the class type that the C++ scope
882 // denotes, followed by a '(', then this is a constructor declaration.
883 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000884 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000885 CurScope, &SS) &&
886 GetLookAheadToken(2).is(tok::l_paren))
887 goto DoneWithDeclSpec;
888
Douglas Gregorb696ea32009-02-04 17:00:24 +0000889 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
890 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000891
Chris Lattnerf4382f52009-04-14 22:17:06 +0000892 // If the referenced identifier is not a type, then this declspec is
893 // erroneous: We already checked about that it has no type specifier, and
894 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000895 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000896 if (TypeRep == 0) {
897 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000898 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000899 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000900 }
Mike Stump1eb44332009-09-09 15:08:12 +0000901
John McCallaa87d332009-12-12 11:40:51 +0000902 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000903 ConsumeToken(); // The C++ scope.
904
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000905 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000906 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000907 if (isInvalid)
908 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000910 DS.SetRangeEnd(Tok.getLocation());
911 ConsumeToken(); // The typename.
912
913 continue;
914 }
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Chris Lattner80d0c892009-01-21 19:48:37 +0000916 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000917 if (Tok.getAnnotationValue())
918 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000919 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +0000920 else
921 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000922 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
923 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Chris Lattner80d0c892009-01-21 19:48:37 +0000925 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
926 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
927 // Objective-C interface. If we don't have Objective-C or a '<', this is
928 // just a normal reference to a typedef name.
929 if (!Tok.is(tok::less) || !getLang().ObjC1)
930 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000932 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000933 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000934 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
935 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
936 LAngleLoc, EndProtoLoc);
937 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
938 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Chris Lattner80d0c892009-01-21 19:48:37 +0000940 DS.SetRangeEnd(EndProtoLoc);
941 continue;
942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Chris Lattner3bd934a2008-07-26 01:18:38 +0000944 // typedef-name
945 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000946 // In C++, check to see if this is a scope specifier like foo::bar::, if
947 // so handle it as such. This is important for ctor parsing.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000948 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner837acd02009-01-21 19:19:26 +0000949 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Chris Lattner3bd934a2008-07-26 01:18:38 +0000951 // This identifier can only be a typedef name if we haven't already seen
952 // a type-specifier. Without this check we misparse:
953 // typedef int X; struct Y { short X; }; as 'short int'.
954 if (DS.hasTypeSpecifier())
955 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Chris Lattner3bd934a2008-07-26 01:18:38 +0000957 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +0000958 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +0000959 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000960
Chris Lattnerc199ab32009-04-12 20:42:31 +0000961 // If this is not a typedef name, don't parse it as part of the declspec,
962 // it must be an implicit int or an error.
963 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000964 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000965 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000966 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000967
Douglas Gregorb48fe382008-10-31 09:07:45 +0000968 // C++: If the identifier is actually the name of the class type
969 // being defined and the next token is a '(', then this is a
970 // constructor declaration. We're done with the decl-specifiers
971 // and will treat this token as an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000972 if (getLang().CPlusPlus &&
973 (CurScope->isClassScope() ||
974 (CurScope->isTemplateParamScope() &&
Douglas Gregordec06662009-08-21 18:42:58 +0000975 CurScope->getParent()->isClassScope())) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000976 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000977 NextToken().getKind() == tok::l_paren)
978 goto DoneWithDeclSpec;
979
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000980 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000981 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000982 if (isInvalid)
983 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Chris Lattner3bd934a2008-07-26 01:18:38 +0000985 DS.SetRangeEnd(Tok.getLocation());
986 ConsumeToken(); // The identifier
987
988 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
989 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
990 // Objective-C interface. If we don't have Objective-C or a '<', this is
991 // just a normal reference to a typedef name.
992 if (!Tok.is(tok::less) || !getLang().ObjC1)
993 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000995 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000996 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000997 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
998 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
999 LAngleLoc, EndProtoLoc);
1000 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1001 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Chris Lattner3bd934a2008-07-26 01:18:38 +00001003 DS.SetRangeEnd(EndProtoLoc);
1004
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001005 // Need to support trailing type qualifiers (e.g. "id<p> const").
1006 // If a type specifier follows, it will be diagnosed elsewhere.
1007 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001008 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001009
1010 // type-name
1011 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001012 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001013 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001014 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001015 // This template-id does not refer to a type name, so we're
1016 // done with the type-specifiers.
1017 goto DoneWithDeclSpec;
1018 }
1019
1020 // Turn the template-id annotation token into a type annotation
1021 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001022 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001023 continue;
1024 }
1025
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 // GNU attributes support.
1027 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001028 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001030
1031 // Microsoft declspec support.
1032 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001033 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001034 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Steve Naroff239f0732008-12-25 14:16:32 +00001036 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001037 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001038 // FIXME: Add handling here!
1039 break;
1040
1041 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001042 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001043 case tok::kw___cdecl:
1044 case tok::kw___stdcall:
1045 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001046 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1047 continue;
1048
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 // storage-class-specifier
1050 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001051 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1052 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 break;
1054 case tok::kw_extern:
1055 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001056 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001057 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1058 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001060 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001061 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001062 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001063 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 case tok::kw_static:
1065 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001066 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001067 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1068 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 break;
1070 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001071 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001072 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1073 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001074 else
John McCallfec54012009-08-03 20:12:06 +00001075 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1076 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 break;
1078 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001079 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1080 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001082 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001083 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1084 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001085 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001087 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 // function-specifier
1091 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001092 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001094 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001095 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001096 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001097 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001098 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001099 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001100
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001101 // friend
1102 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001103 if (DSContext == DSC_class)
1104 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1105 else {
1106 PrevSpec = ""; // not actually used by the diagnostic
1107 DiagID = diag::err_friend_invalid_in_context;
1108 isInvalid = true;
1109 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001110 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Sebastian Redl2ac67232009-11-05 15:47:02 +00001112 // constexpr
1113 case tok::kw_constexpr:
1114 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1115 break;
1116
Chris Lattner80d0c892009-01-21 19:48:37 +00001117 // type-specifier
1118 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001119 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1120 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001121 break;
1122 case tok::kw_long:
1123 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001124 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1125 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001126 else
John McCallfec54012009-08-03 20:12:06 +00001127 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1128 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001129 break;
1130 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001131 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1132 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001133 break;
1134 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001135 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1136 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001137 break;
1138 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001139 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1140 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001141 break;
1142 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001143 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1144 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001145 break;
1146 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001147 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1148 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001149 break;
1150 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001151 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1152 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001153 break;
1154 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001155 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1156 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001157 break;
1158 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001159 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1160 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001161 break;
1162 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001163 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1164 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001165 break;
1166 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001167 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1168 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001169 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001170 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001171 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1172 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001173 break;
1174 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001175 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1176 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001177 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001178 case tok::kw_bool:
1179 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001180 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1181 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001182 break;
1183 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001184 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1185 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001186 break;
1187 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001188 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1189 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001190 break;
1191 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001192 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1193 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001194 break;
1195
1196 // class-specifier:
1197 case tok::kw_class:
1198 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001199 case tok::kw_union: {
1200 tok::TokenKind Kind = Tok.getKind();
1201 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001202 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001203 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001204 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001205
1206 // enum-specifier:
1207 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001208 ConsumeToken();
1209 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001210 continue;
1211
1212 // cv-qualifier:
1213 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001214 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1215 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001216 break;
1217 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001218 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1219 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001220 break;
1221 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001222 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1223 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001224 break;
1225
Douglas Gregord57959a2009-03-27 23:10:48 +00001226 // C++ typename-specifier:
1227 case tok::kw_typename:
1228 if (TryAnnotateTypeOrScopeToken())
1229 continue;
1230 break;
1231
Chris Lattner80d0c892009-01-21 19:48:37 +00001232 // GNU typeof support.
1233 case tok::kw_typeof:
1234 ParseTypeofSpecifier(DS);
1235 continue;
1236
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001237 case tok::kw_decltype:
1238 ParseDecltypeSpecifier(DS);
1239 continue;
1240
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001241 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001242 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001243 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1244 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001245 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001246 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Chris Lattnerbce61352008-07-26 00:20:22 +00001248 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001249 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001250 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001251 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1252 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1253 LAngleLoc, EndProtoLoc);
1254 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1255 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001256 DS.SetRangeEnd(EndProtoLoc);
1257
Chris Lattner1ab3b962008-11-18 07:48:38 +00001258 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001259 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001260 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001261 // Need to support trailing type qualifiers (e.g. "id<p> const").
1262 // If a type specifier follows, it will be diagnosed elsewhere.
1263 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001264 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001265 }
John McCallfec54012009-08-03 20:12:06 +00001266 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 if (isInvalid) {
1268 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001269 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001270 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001272 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 ConsumeToken();
1274 }
1275}
Douglas Gregoradcac882008-12-01 23:54:00 +00001276
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001277/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001278/// primarily follow the C++ grammar with additions for C99 and GNU,
1279/// which together subsume the C grammar. Note that the C++
1280/// type-specifier also includes the C type-qualifier (for const,
1281/// volatile, and C99 restrict). Returns true if a type-specifier was
1282/// found (and parsed), false otherwise.
1283///
1284/// type-specifier: [C++ 7.1.5]
1285/// simple-type-specifier
1286/// class-specifier
1287/// enum-specifier
1288/// elaborated-type-specifier [TODO]
1289/// cv-qualifier
1290///
1291/// cv-qualifier: [C++ 7.1.5.1]
1292/// 'const'
1293/// 'volatile'
1294/// [C99] 'restrict'
1295///
1296/// simple-type-specifier: [ C++ 7.1.5.2]
1297/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1298/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1299/// 'char'
1300/// 'wchar_t'
1301/// 'bool'
1302/// 'short'
1303/// 'int'
1304/// 'long'
1305/// 'signed'
1306/// 'unsigned'
1307/// 'float'
1308/// 'double'
1309/// 'void'
1310/// [C99] '_Bool'
1311/// [C99] '_Complex'
1312/// [C99] '_Imaginary' // Removed in TC2?
1313/// [GNU] '_Decimal32'
1314/// [GNU] '_Decimal64'
1315/// [GNU] '_Decimal128'
1316/// [GNU] typeof-specifier
1317/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1318/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001319/// [C++0x] 'decltype' ( expression )
John McCallfec54012009-08-03 20:12:06 +00001320bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001321 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001322 unsigned &DiagID,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001323 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001324 SourceLocation Loc = Tok.getLocation();
1325
1326 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001327 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001328 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001329 // Annotate typenames and C++ scope specifiers. If we get one, just
1330 // recurse to handle whatever we get.
1331 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001332 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1333 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001334 // Otherwise, not a type specifier.
1335 return false;
1336 case tok::coloncolon: // ::foo::bar
1337 if (NextToken().is(tok::kw_new) || // ::new
1338 NextToken().is(tok::kw_delete)) // ::delete
1339 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Chris Lattner166a8fc2009-01-04 23:41:41 +00001341 // Annotate typenames and C++ scope specifiers. If we get one, just
1342 // recurse to handle whatever we get.
1343 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001344 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1345 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001346 // Otherwise, not a type specifier.
1347 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001348
Douglas Gregor12e083c2008-11-07 15:42:26 +00001349 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001350 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001351 if (Tok.getAnnotationValue())
1352 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001353 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001354 else
1355 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001356 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1357 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Douglas Gregor12e083c2008-11-07 15:42:26 +00001359 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1360 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1361 // Objective-C interface. If we don't have Objective-C or a '<', this is
1362 // just a normal reference to a typedef name.
1363 if (!Tok.is(tok::less) || !getLang().ObjC1)
1364 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001366 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001367 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001368 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1369 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1370 LAngleLoc, EndProtoLoc);
1371 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1372 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Douglas Gregor12e083c2008-11-07 15:42:26 +00001374 DS.SetRangeEnd(EndProtoLoc);
1375 return true;
1376 }
1377
1378 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001379 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001380 break;
1381 case tok::kw_long:
1382 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001383 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1384 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001385 else
John McCallfec54012009-08-03 20:12:06 +00001386 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1387 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001388 break;
1389 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001390 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001391 break;
1392 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001393 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1394 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001395 break;
1396 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001397 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1398 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001399 break;
1400 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001401 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1402 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001403 break;
1404 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001405 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001406 break;
1407 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001408 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001409 break;
1410 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001411 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001412 break;
1413 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001414 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001415 break;
1416 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001417 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001418 break;
1419 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001420 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001421 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001422 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001423 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001424 break;
1425 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001426 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001427 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001428 case tok::kw_bool:
1429 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001430 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001431 break;
1432 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001433 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1434 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001435 break;
1436 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001437 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1438 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001439 break;
1440 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001441 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1442 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001443 break;
1444
1445 // class-specifier:
1446 case tok::kw_class:
1447 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001448 case tok::kw_union: {
1449 tok::TokenKind Kind = Tok.getKind();
1450 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001451 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001452 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001453 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001454
1455 // enum-specifier:
1456 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001457 ConsumeToken();
1458 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001459 return true;
1460
1461 // cv-qualifier:
1462 case tok::kw_const:
1463 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001464 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001465 break;
1466 case tok::kw_volatile:
1467 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001468 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001469 break;
1470 case tok::kw_restrict:
1471 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001472 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001473 break;
1474
1475 // GNU typeof support.
1476 case tok::kw_typeof:
1477 ParseTypeofSpecifier(DS);
1478 return true;
1479
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001480 // C++0x decltype support.
1481 case tok::kw_decltype:
1482 ParseDecltypeSpecifier(DS);
1483 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001485 // C++0x auto support.
1486 case tok::kw_auto:
1487 if (!getLang().CPlusPlus0x)
1488 return false;
1489
John McCallfec54012009-08-03 20:12:06 +00001490 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001491 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001492 case tok::kw___ptr64:
1493 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001494 case tok::kw___cdecl:
1495 case tok::kw___stdcall:
1496 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001497 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001498 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001499
Douglas Gregor12e083c2008-11-07 15:42:26 +00001500 default:
1501 // Not a type-specifier; do nothing.
1502 return false;
1503 }
1504
1505 // If the specifier combination wasn't legal, issue a diagnostic.
1506 if (isInvalid) {
1507 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001508 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001509 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001510 }
1511 DS.SetRangeEnd(Tok.getLocation());
1512 ConsumeToken(); // whatever we parsed above.
1513 return true;
1514}
Reid Spencer5f016e22007-07-11 17:01:13 +00001515
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001516/// ParseStructDeclaration - Parse a struct declaration without the terminating
1517/// semicolon.
1518///
Reid Spencer5f016e22007-07-11 17:01:13 +00001519/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001520/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001521/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001522/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001523/// struct-declarator-list:
1524/// struct-declarator
1525/// struct-declarator-list ',' struct-declarator
1526/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1527/// struct-declarator:
1528/// declarator
1529/// [GNU] declarator attributes[opt]
1530/// declarator[opt] ':' constant-expression
1531/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1532///
Chris Lattnere1359422008-04-10 06:46:29 +00001533void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001534ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001535 if (Tok.is(tok::kw___extension__)) {
1536 // __extension__ silences extension warnings in the subexpression.
1537 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001538 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001539 return ParseStructDeclaration(DS, Fields);
1540 }
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Steve Naroff28a7ca82007-08-20 22:28:22 +00001542 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001543 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001544 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001546 // If there are no declarators, this is a free-standing declaration
1547 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001548 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001549 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001550 return;
1551 }
1552
1553 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001554 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001555 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001556 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001557 FieldDeclarator DeclaratorInfo(DS);
1558
1559 // Attributes are only allowed here on successive declarators.
1560 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1561 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001562 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001563 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1564 }
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Steve Naroff28a7ca82007-08-20 22:28:22 +00001566 /// struct-declarator: declarator
1567 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001568 if (Tok.isNot(tok::colon)) {
1569 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1570 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001571 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001572 }
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Chris Lattner04d66662007-10-09 17:33:22 +00001574 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001575 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001576 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001577 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001578 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001579 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001580 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001581 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001582
Steve Naroff28a7ca82007-08-20 22:28:22 +00001583 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001584 if (Tok.is(tok::kw___attribute)) {
1585 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001586 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001587 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1588 }
1589
John McCallbdd563e2009-11-03 02:38:08 +00001590 // We're done with this declarator; invoke the callback.
John McCall54abf7d2009-11-04 02:18:39 +00001591 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1592 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001593
Steve Naroff28a7ca82007-08-20 22:28:22 +00001594 // If we don't have a comma, it is either the end of the list (a ';')
1595 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001596 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001597 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001598
Steve Naroff28a7ca82007-08-20 22:28:22 +00001599 // Consume the comma.
1600 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001601
John McCallbdd563e2009-11-03 02:38:08 +00001602 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001603 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001604}
1605
1606/// ParseStructUnionBody
1607/// struct-contents:
1608/// struct-declaration-list
1609/// [EXT] empty
1610/// [GNU] "struct-declaration-list" without terminatoring ';'
1611/// struct-declaration-list:
1612/// struct-declaration
1613/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001614/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001615///
Reid Spencer5f016e22007-07-11 17:01:13 +00001616void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001617 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001618 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1619 PP.getSourceManager(),
1620 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001621
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001623
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001624 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001625 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1626
Reid Spencer5f016e22007-07-11 17:01:13 +00001627 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1628 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001629 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001630 Diag(Tok, diag::ext_empty_struct_union_enum)
1631 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001632
Chris Lattnerb28317a2009-03-28 19:18:32 +00001633 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001634
Reid Spencer5f016e22007-07-11 17:01:13 +00001635 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001636 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001637 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Reid Spencer5f016e22007-07-11 17:01:13 +00001639 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001640 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001641 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001642 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001643 ConsumeToken();
1644 continue;
1645 }
Chris Lattnere1359422008-04-10 06:46:29 +00001646
1647 // Parse all the comma separated declarators.
1648 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001649
John McCallbdd563e2009-11-03 02:38:08 +00001650 if (!Tok.is(tok::at)) {
1651 struct CFieldCallback : FieldCallback {
1652 Parser &P;
1653 DeclPtrTy TagDecl;
1654 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1655
1656 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1657 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1658 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1659
1660 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001661 // Install the declarator into the current TagDecl.
John McCall4ba39712009-11-03 21:13:47 +00001662 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1663 FD.D.getDeclSpec().getSourceRange().getBegin(),
1664 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001665 FieldDecls.push_back(Field);
1666 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001667 }
John McCallbdd563e2009-11-03 02:38:08 +00001668 } Callback(*this, TagDecl, FieldDecls);
1669
1670 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001671 } else { // Handle @defs
1672 ConsumeToken();
1673 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1674 Diag(Tok, diag::err_unexpected_at);
1675 SkipUntil(tok::semi, true, true);
1676 continue;
1677 }
1678 ConsumeToken();
1679 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1680 if (!Tok.is(tok::identifier)) {
1681 Diag(Tok, diag::err_expected_ident);
1682 SkipUntil(tok::semi, true, true);
1683 continue;
1684 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001685 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001686 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001687 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001688 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1689 ConsumeToken();
1690 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001691 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001692
Chris Lattner04d66662007-10-09 17:33:22 +00001693 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001694 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001695 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001696 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001697 break;
1698 } else {
1699 Diag(Tok, diag::err_expected_semi_decl_list);
1700 // Skip to end of block or statement
1701 SkipUntil(tok::r_brace, true, true);
1702 }
1703 }
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Steve Naroff60fccee2007-10-29 21:38:07 +00001705 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Reid Spencer5f016e22007-07-11 17:01:13 +00001707 AttributeList *AttrList = 0;
1708 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001709 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001710 AttrList = ParseGNUAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001711
1712 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001713 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001714 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001715 AttrList);
1716 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001717 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001718}
1719
1720
1721/// ParseEnumSpecifier
1722/// enum-specifier: [C99 6.7.2.2]
1723/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001724///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001725/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1726/// '}' attributes[opt]
1727/// 'enum' identifier
1728/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001729///
1730/// [C++] elaborated-type-specifier:
1731/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1732///
Chris Lattner4c97d762009-04-12 21:49:30 +00001733void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1734 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001735 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001736 if (Tok.is(tok::code_completion)) {
1737 // Code completion for an enum name.
1738 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1739 ConsumeToken();
1740 }
1741
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001742 AttributeList *Attr = 0;
1743 // If attributes exist after tag, parse them.
1744 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001745 Attr = ParseGNUAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001746
1747 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001748 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001749 if (Tok.isNot(tok::identifier)) {
1750 Diag(Tok, diag::err_expected_ident);
1751 if (Tok.isNot(tok::l_brace)) {
1752 // Has no name and is not a definition.
1753 // Skip the rest of this declarator, up until the comma or semicolon.
1754 SkipUntil(tok::comma, true);
1755 return;
1756 }
1757 }
1758 }
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001760 // Must have either 'enum name' or 'enum {...}'.
1761 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1762 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001764 // Skip the rest of this declarator, up until the comma or semicolon.
1765 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001766 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001767 }
Mike Stump1eb44332009-09-09 15:08:12 +00001768
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001769 // If an identifier is present, consume and remember it.
1770 IdentifierInfo *Name = 0;
1771 SourceLocation NameLoc;
1772 if (Tok.is(tok::identifier)) {
1773 Name = Tok.getIdentifierInfo();
1774 NameLoc = ConsumeToken();
1775 }
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001777 // There are three options here. If we have 'enum foo;', then this is a
1778 // forward declaration. If we have 'enum foo {...' then this is a
1779 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1780 //
1781 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1782 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1783 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1784 //
John McCall0f434ec2009-07-31 02:45:11 +00001785 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001786 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001787 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001788 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001789 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001790 else
John McCall0f434ec2009-07-31 02:45:11 +00001791 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001792 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001793 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00001794 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001795 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001796 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001797 Owned, IsDependent);
1798 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Chris Lattner04d66662007-10-09 17:33:22 +00001800 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001801 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001802
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 // TODO: semantic analysis on the declspec for enums.
1804 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001805 unsigned DiagID;
1806 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001807 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001808 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001809}
1810
1811/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1812/// enumerator-list:
1813/// enumerator
1814/// enumerator-list ',' enumerator
1815/// enumerator:
1816/// enumeration-constant
1817/// enumeration-constant '=' constant-expression
1818/// enumeration-constant:
1819/// identifier
1820///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001821void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001822 // Enter the scope of the enum body and start the definition.
1823 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001824 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001825
Reid Spencer5f016e22007-07-11 17:01:13 +00001826 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Chris Lattner7946dd32007-08-27 17:24:30 +00001828 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001829 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001830 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Chris Lattnerb28317a2009-03-28 19:18:32 +00001832 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001833
Chris Lattnerb28317a2009-03-28 19:18:32 +00001834 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Reid Spencer5f016e22007-07-11 17:01:13 +00001836 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001837 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001838 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1839 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001840
Reid Spencer5f016e22007-07-11 17:01:13 +00001841 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001842 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001843 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001844 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001845 AssignedVal = ParseConstantExpression();
1846 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001847 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001848 }
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Reid Spencer5f016e22007-07-11 17:01:13 +00001850 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001851 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1852 LastEnumConstDecl,
1853 IdentLoc, Ident,
1854 EqualLoc,
1855 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001856 EnumConstantDecls.push_back(EnumConstDecl);
1857 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Chris Lattner04d66662007-10-09 17:33:22 +00001859 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001860 break;
1861 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001862
1863 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001864 !(getLang().C99 || getLang().CPlusPlus0x))
1865 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1866 << getLang().CPlusPlus
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001867 << CodeModificationHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001868 }
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Reid Spencer5f016e22007-07-11 17:01:13 +00001870 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001871 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001872
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001873 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001875 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001876 Attr = ParseGNUAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001877
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001878 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1879 EnumConstantDecls.data(), EnumConstantDecls.size(),
1880 CurScope, Attr);
Mike Stump1eb44332009-09-09 15:08:12 +00001881
Douglas Gregor72de6672009-01-08 20:45:30 +00001882 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001883 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001884}
1885
1886/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001887/// start of a type-qualifier-list.
1888bool Parser::isTypeQualifier() const {
1889 switch (Tok.getKind()) {
1890 default: return false;
1891 // type-qualifier
1892 case tok::kw_const:
1893 case tok::kw_volatile:
1894 case tok::kw_restrict:
1895 return true;
1896 }
1897}
1898
1899/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001900/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001901bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001902 switch (Tok.getKind()) {
1903 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Chris Lattner166a8fc2009-01-04 23:41:41 +00001905 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001906 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001907 // Annotate typenames and C++ scope specifiers. If we get one, just
1908 // recurse to handle whatever we get.
1909 if (TryAnnotateTypeOrScopeToken())
1910 return isTypeSpecifierQualifier();
1911 // Otherwise, not a type specifier.
1912 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001913
Chris Lattner166a8fc2009-01-04 23:41:41 +00001914 case tok::coloncolon: // ::foo::bar
1915 if (NextToken().is(tok::kw_new) || // ::new
1916 NextToken().is(tok::kw_delete)) // ::delete
1917 return false;
1918
1919 // Annotate typenames and C++ scope specifiers. If we get one, just
1920 // recurse to handle whatever we get.
1921 if (TryAnnotateTypeOrScopeToken())
1922 return isTypeSpecifierQualifier();
1923 // Otherwise, not a type specifier.
1924 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001925
Reid Spencer5f016e22007-07-11 17:01:13 +00001926 // GNU attributes support.
1927 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001928 // GNU typeof support.
1929 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Reid Spencer5f016e22007-07-11 17:01:13 +00001931 // type-specifiers
1932 case tok::kw_short:
1933 case tok::kw_long:
1934 case tok::kw_signed:
1935 case tok::kw_unsigned:
1936 case tok::kw__Complex:
1937 case tok::kw__Imaginary:
1938 case tok::kw_void:
1939 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001940 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001941 case tok::kw_char16_t:
1942 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001943 case tok::kw_int:
1944 case tok::kw_float:
1945 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001946 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001947 case tok::kw__Bool:
1948 case tok::kw__Decimal32:
1949 case tok::kw__Decimal64:
1950 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Chris Lattner99dc9142008-04-13 18:59:07 +00001952 // struct-or-union-specifier (C99) or class-specifier (C++)
1953 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001954 case tok::kw_struct:
1955 case tok::kw_union:
1956 // enum-specifier
1957 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00001958
Reid Spencer5f016e22007-07-11 17:01:13 +00001959 // type-qualifier
1960 case tok::kw_const:
1961 case tok::kw_volatile:
1962 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001963
1964 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001965 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001966 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001967
Chris Lattner7c186be2008-10-20 00:25:30 +00001968 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1969 case tok::less:
1970 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00001971
Steve Naroff239f0732008-12-25 14:16:32 +00001972 case tok::kw___cdecl:
1973 case tok::kw___stdcall:
1974 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001975 case tok::kw___w64:
1976 case tok::kw___ptr64:
1977 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001978 }
1979}
1980
1981/// isDeclarationSpecifier() - Return true if the current token is part of a
1982/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001983bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001984 switch (Tok.getKind()) {
1985 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001986
Chris Lattner166a8fc2009-01-04 23:41:41 +00001987 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001988 // Unfortunate hack to support "Class.factoryMethod" notation.
1989 if (getLang().ObjC1 && NextToken().is(tok::period))
1990 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001991 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001992
Douglas Gregord57959a2009-03-27 23:10:48 +00001993 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001994 // Annotate typenames and C++ scope specifiers. If we get one, just
1995 // recurse to handle whatever we get.
1996 if (TryAnnotateTypeOrScopeToken())
1997 return isDeclarationSpecifier();
1998 // Otherwise, not a declaration specifier.
1999 return false;
2000 case tok::coloncolon: // ::foo::bar
2001 if (NextToken().is(tok::kw_new) || // ::new
2002 NextToken().is(tok::kw_delete)) // ::delete
2003 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Chris Lattner166a8fc2009-01-04 23:41:41 +00002005 // Annotate typenames and C++ scope specifiers. If we get one, just
2006 // recurse to handle whatever we get.
2007 if (TryAnnotateTypeOrScopeToken())
2008 return isDeclarationSpecifier();
2009 // Otherwise, not a declaration specifier.
2010 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Reid Spencer5f016e22007-07-11 17:01:13 +00002012 // storage-class-specifier
2013 case tok::kw_typedef:
2014 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002015 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002016 case tok::kw_static:
2017 case tok::kw_auto:
2018 case tok::kw_register:
2019 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002020
Reid Spencer5f016e22007-07-11 17:01:13 +00002021 // type-specifiers
2022 case tok::kw_short:
2023 case tok::kw_long:
2024 case tok::kw_signed:
2025 case tok::kw_unsigned:
2026 case tok::kw__Complex:
2027 case tok::kw__Imaginary:
2028 case tok::kw_void:
2029 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002030 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002031 case tok::kw_char16_t:
2032 case tok::kw_char32_t:
2033
Reid Spencer5f016e22007-07-11 17:01:13 +00002034 case tok::kw_int:
2035 case tok::kw_float:
2036 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002037 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002038 case tok::kw__Bool:
2039 case tok::kw__Decimal32:
2040 case tok::kw__Decimal64:
2041 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00002042
Chris Lattner99dc9142008-04-13 18:59:07 +00002043 // struct-or-union-specifier (C99) or class-specifier (C++)
2044 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002045 case tok::kw_struct:
2046 case tok::kw_union:
2047 // enum-specifier
2048 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002049
Reid Spencer5f016e22007-07-11 17:01:13 +00002050 // type-qualifier
2051 case tok::kw_const:
2052 case tok::kw_volatile:
2053 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002054
Reid Spencer5f016e22007-07-11 17:01:13 +00002055 // function-specifier
2056 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002057 case tok::kw_virtual:
2058 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002059
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002060 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002061 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002062
Chris Lattner1ef08762007-08-09 17:01:07 +00002063 // GNU typeof support.
2064 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Chris Lattner1ef08762007-08-09 17:01:07 +00002066 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002067 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002068 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Chris Lattnerf3948c42008-07-26 03:38:44 +00002070 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2071 case tok::less:
2072 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Steve Naroff47f52092009-01-06 19:34:12 +00002074 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002075 case tok::kw___cdecl:
2076 case tok::kw___stdcall:
2077 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002078 case tok::kw___w64:
2079 case tok::kw___ptr64:
2080 case tok::kw___forceinline:
2081 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002082 }
2083}
2084
2085
2086/// ParseTypeQualifierListOpt
2087/// type-qualifier-list: [C99 6.7.5]
2088/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002089/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002090/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002091/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Sean Huntbbd37c62009-11-21 08:43:09 +00002092/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2093/// if CXX0XAttributesAllowed = true
Reid Spencer5f016e22007-07-11 17:01:13 +00002094///
Sean Huntbbd37c62009-11-21 08:43:09 +00002095void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2096 bool CXX0XAttributesAllowed) {
2097 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2098 SourceLocation Loc = Tok.getLocation();
2099 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2100 if (CXX0XAttributesAllowed)
2101 DS.AddAttributes(Attr.AttrList);
2102 else
2103 Diag(Loc, diag::err_attributes_not_allowed);
2104 }
2105
Reid Spencer5f016e22007-07-11 17:01:13 +00002106 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002107 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002108 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002109 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002110 SourceLocation Loc = Tok.getLocation();
2111
2112 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002113 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002114 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2115 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002116 break;
2117 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002118 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2119 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002120 break;
2121 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002122 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2123 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002124 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002125 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002126 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002127 case tok::kw___cdecl:
2128 case tok::kw___stdcall:
2129 case tok::kw___fastcall:
Sean Huntbbd37c62009-11-21 08:43:09 +00002130 if (GNUAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002131 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2132 continue;
2133 }
2134 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002135 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002136 if (GNUAttributesAllowed) {
2137 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002138 continue; // do *not* consume the next token!
2139 }
2140 // otherwise, FALL THROUGH!
2141 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002142 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002143 // If this is not a type-qualifier token, we're done reading type
2144 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002145 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002146 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002147 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002148
Reid Spencer5f016e22007-07-11 17:01:13 +00002149 // If the specifier combination wasn't legal, issue a diagnostic.
2150 if (isInvalid) {
2151 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002152 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002153 }
2154 ConsumeToken();
2155 }
2156}
2157
2158
2159/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2160///
2161void Parser::ParseDeclarator(Declarator &D) {
2162 /// This implements the 'declarator' production in the C grammar, then checks
2163 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002164 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002165}
2166
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002167/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2168/// is parsed by the function passed to it. Pass null, and the direct-declarator
2169/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002170/// ptr-operator production.
2171///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002172/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2173/// [C] pointer[opt] direct-declarator
2174/// [C++] direct-declarator
2175/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002176///
2177/// pointer: [C99 6.7.5]
2178/// '*' type-qualifier-list[opt]
2179/// '*' type-qualifier-list[opt] pointer
2180///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002181/// ptr-operator:
2182/// '*' cv-qualifier-seq[opt]
2183/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002184/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002185/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002186/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002187/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002188void Parser::ParseDeclaratorInternal(Declarator &D,
2189 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002190 if (Diags.hasAllExtensionsSilenced())
2191 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002192 // C++ member pointers start with a '::' or a nested-name.
2193 // Member pointers get special handling, since there's no place for the
2194 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002195 if (getLang().CPlusPlus &&
2196 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2197 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002198 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002199 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002200 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002201 // The scope spec really belongs to the direct-declarator.
2202 D.getCXXScopeSpec() = SS;
2203 if (DirectDeclParser)
2204 (this->*DirectDeclParser)(D);
2205 return;
2206 }
2207
2208 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002209 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002210 DeclSpec DS;
2211 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002212 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002213
2214 // Recurse to parse whatever is left.
2215 ParseDeclaratorInternal(D, DirectDeclParser);
2216
2217 // Sema will have to catch (syntactically invalid) pointers into global
2218 // scope. It has to catch pointers into namespace scope anyway.
2219 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002220 Loc, DS.TakeAttributes()),
2221 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002222 return;
2223 }
2224 }
2225
2226 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002227 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002228 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002229 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002230 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002231 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002232 if (DirectDeclParser)
2233 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002234 return;
2235 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002236
Sebastian Redl05532f22009-03-15 22:02:01 +00002237 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2238 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002239 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002240 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002241
Chris Lattner9af55002009-03-27 04:18:06 +00002242 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002243 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002244 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002245
Reid Spencer5f016e22007-07-11 17:01:13 +00002246 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002247 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002248
Reid Spencer5f016e22007-07-11 17:01:13 +00002249 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002250 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002251 if (Kind == tok::star)
2252 // Remember that we parsed a pointer type, and remember the type-quals.
2253 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002254 DS.TakeAttributes()),
2255 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002256 else
2257 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002258 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002259 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002260 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002261 } else {
2262 // Is a reference
2263 DeclSpec DS;
2264
Sebastian Redl743de1f2009-03-23 00:00:23 +00002265 // Complain about rvalue references in C++03, but then go on and build
2266 // the declarator.
2267 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2268 Diag(Loc, diag::err_rvalue_reference);
2269
Reid Spencer5f016e22007-07-11 17:01:13 +00002270 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2271 // cv-qualifiers are introduced through the use of a typedef or of a
2272 // template type argument, in which case the cv-qualifiers are ignored.
2273 //
2274 // [GNU] Retricted references are allowed.
2275 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002276 // [C++0x] Attributes on references are not allowed.
2277 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002278 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002279
2280 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2281 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2282 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002283 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002284 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2285 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002286 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002287 }
2288
2289 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002290 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002291
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002292 if (D.getNumTypeObjects() > 0) {
2293 // C++ [dcl.ref]p4: There shall be no references to references.
2294 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2295 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002296 if (const IdentifierInfo *II = D.getIdentifier())
2297 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2298 << II;
2299 else
2300 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2301 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002302
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002303 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002304 // can go ahead and build the (technically ill-formed)
2305 // declarator: reference collapsing will take care of it.
2306 }
2307 }
2308
Reid Spencer5f016e22007-07-11 17:01:13 +00002309 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002310 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002311 DS.TakeAttributes(),
2312 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002313 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002314 }
2315}
2316
2317/// ParseDirectDeclarator
2318/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002319/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002320/// '(' declarator ')'
2321/// [GNU] '(' attributes declarator ')'
2322/// [C90] direct-declarator '[' constant-expression[opt] ']'
2323/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2324/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2325/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2326/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2327/// direct-declarator '(' parameter-type-list ')'
2328/// direct-declarator '(' identifier-list[opt] ')'
2329/// [GNU] direct-declarator '(' parameter-forward-declarations
2330/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002331/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2332/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002333/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002334///
2335/// declarator-id: [C++ 8]
2336/// id-expression
2337/// '::'[opt] nested-name-specifier[opt] type-name
2338///
2339/// id-expression: [C++ 5.1]
2340/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002341/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002342///
2343/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002344/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002345/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002346/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002347/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002348/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002349///
Reid Spencer5f016e22007-07-11 17:01:13 +00002350void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002351 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002352
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002353 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2354 // ParseDeclaratorInternal might already have parsed the scope.
2355 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2356 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2357 true);
2358 if (afterCXXScope) {
John McCalle7e278b2009-12-11 20:04:54 +00002359 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2360 // Change the declaration context for name lookup, until this function
2361 // is exited (and the declarator has been parsed).
2362 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002363 }
2364
2365 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2366 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2367 // We found something that indicates the start of an unqualified-id.
2368 // Parse that unqualified-id.
2369 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2370 /*EnteringContext=*/true,
2371 /*AllowDestructorName=*/true,
Zhongxing Xua3ddec22009-12-28 06:49:22 +00002372 /*AllowConstructorName=*/!D.getDeclSpec().hasTypeSpecifier(),
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002373 /*ObjectType=*/0,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002374 D.getName())) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002375 D.SetIdentifier(0, Tok.getLocation());
2376 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002377 } else {
2378 // Parsed the unqualified-id; update range information and move along.
2379 if (D.getSourceRange().getBegin().isInvalid())
2380 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2381 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002382 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002383 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002384 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002385 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002386 assert(!getLang().CPlusPlus &&
2387 "There's a C++-specific check for tok::identifier above");
2388 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2389 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2390 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002391 goto PastIdentifier;
2392 }
2393
2394 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002395 // direct-declarator: '(' declarator ')'
2396 // direct-declarator: '(' attributes declarator ')'
2397 // Example: 'char (*X)' or 'int (*XX)(void)'
2398 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002399 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002400 // This could be something simple like "int" (in which case the declarator
2401 // portion is empty), if an abstract-declarator is allowed.
2402 D.SetIdentifier(0, Tok.getLocation());
2403 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002404 if (D.getContext() == Declarator::MemberContext)
2405 Diag(Tok, diag::err_expected_member_name_or_semi)
2406 << D.getDeclSpec().getSourceRange();
2407 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002408 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002409 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002410 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002411 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002412 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002413 }
Mike Stump1eb44332009-09-09 15:08:12 +00002414
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002415 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002416 assert(D.isPastIdentifier() &&
2417 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002418
Sean Huntbbd37c62009-11-21 08:43:09 +00002419 // Don't parse attributes unless we have an identifier.
2420 if (D.getIdentifier() && getLang().CPlusPlus
2421 && isCXX0XAttributeSpecifier(true)) {
2422 SourceLocation AttrEndLoc;
2423 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2424 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2425 }
2426
Reid Spencer5f016e22007-07-11 17:01:13 +00002427 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002428 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002429 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2430 // In such a case, check if we actually have a function declarator; if it
2431 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002432 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2433 // When not in file scope, warn for ambiguous function declarators, just
2434 // in case the author intended it as a variable definition.
2435 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2436 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2437 break;
2438 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002439 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002440 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002441 ParseBracketDeclarator(D);
2442 } else {
2443 break;
2444 }
2445 }
2446}
2447
Chris Lattneref4715c2008-04-06 05:45:57 +00002448/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2449/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002450/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002451/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2452///
2453/// direct-declarator:
2454/// '(' declarator ')'
2455/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002456/// direct-declarator '(' parameter-type-list ')'
2457/// direct-declarator '(' identifier-list[opt] ')'
2458/// [GNU] direct-declarator '(' parameter-forward-declarations
2459/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002460///
2461void Parser::ParseParenDeclarator(Declarator &D) {
2462 SourceLocation StartLoc = ConsumeParen();
2463 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002464
Chris Lattner7399ee02008-10-20 02:05:46 +00002465 // Eat any attributes before we look at whether this is a grouping or function
2466 // declarator paren. If this is a grouping paren, the attribute applies to
2467 // the type being built up, for example:
2468 // int (__attribute__(()) *x)(long y)
2469 // If this ends up not being a grouping paren, the attribute applies to the
2470 // first argument, for example:
2471 // int (__attribute__(()) int x)
2472 // In either case, we need to eat any attributes to be able to determine what
2473 // sort of paren this is.
2474 //
2475 AttributeList *AttrList = 0;
2476 bool RequiresArg = false;
2477 if (Tok.is(tok::kw___attribute)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002478 AttrList = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00002479
Chris Lattner7399ee02008-10-20 02:05:46 +00002480 // We require that the argument list (if this is a non-grouping paren) be
2481 // present even if the attribute list was empty.
2482 RequiresArg = true;
2483 }
Steve Naroff239f0732008-12-25 14:16:32 +00002484 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002485 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2486 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2487 Tok.is(tok::kw___ptr64)) {
2488 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2489 }
Mike Stump1eb44332009-09-09 15:08:12 +00002490
Chris Lattneref4715c2008-04-06 05:45:57 +00002491 // If we haven't past the identifier yet (or where the identifier would be
2492 // stored, if this is an abstract declarator), then this is probably just
2493 // grouping parens. However, if this could be an abstract-declarator, then
2494 // this could also be the start of function arguments (consider 'void()').
2495 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002496
Chris Lattneref4715c2008-04-06 05:45:57 +00002497 if (!D.mayOmitIdentifier()) {
2498 // If this can't be an abstract-declarator, this *must* be a grouping
2499 // paren, because we haven't seen the identifier yet.
2500 isGrouping = true;
2501 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002502 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002503 isDeclarationSpecifier()) { // 'int(int)' is a function.
2504 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2505 // considered to be a type, not a K&R identifier-list.
2506 isGrouping = false;
2507 } else {
2508 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2509 isGrouping = true;
2510 }
Mike Stump1eb44332009-09-09 15:08:12 +00002511
Chris Lattneref4715c2008-04-06 05:45:57 +00002512 // If this is a grouping paren, handle:
2513 // direct-declarator: '(' declarator ')'
2514 // direct-declarator: '(' attributes declarator ')'
2515 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002516 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002517 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002518 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002519 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002520
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002521 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002522 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002523 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002524
2525 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002526 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002527 return;
2528 }
Mike Stump1eb44332009-09-09 15:08:12 +00002529
Chris Lattneref4715c2008-04-06 05:45:57 +00002530 // Okay, if this wasn't a grouping paren, it must be the start of a function
2531 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002532 // identifier (and remember where it would have been), then call into
2533 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002534 D.SetIdentifier(0, Tok.getLocation());
2535
Chris Lattner7399ee02008-10-20 02:05:46 +00002536 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002537}
2538
2539/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2540/// declarator D up to a paren, which indicates that we are parsing function
2541/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002542///
Chris Lattner7399ee02008-10-20 02:05:46 +00002543/// If AttrList is non-null, then the caller parsed those arguments immediately
2544/// after the open paren - they should be considered to be the first argument of
2545/// a parameter. If RequiresArg is true, then the first argument of the
2546/// function is required to be present and required to not be an identifier
2547/// list.
2548///
Reid Spencer5f016e22007-07-11 17:01:13 +00002549/// This method also handles this portion of the grammar:
2550/// parameter-type-list: [C99 6.7.5]
2551/// parameter-list
2552/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002553/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002554///
2555/// parameter-list: [C99 6.7.5]
2556/// parameter-declaration
2557/// parameter-list ',' parameter-declaration
2558///
2559/// parameter-declaration: [C99 6.7.5]
2560/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002561/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002562/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002563/// declaration-specifiers abstract-declarator[opt]
2564/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002565/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002566/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2567///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002568/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002569/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002570///
Chris Lattner7399ee02008-10-20 02:05:46 +00002571void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2572 AttributeList *AttrList,
2573 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002574 // lparen is already consumed!
2575 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002576
Chris Lattner7399ee02008-10-20 02:05:46 +00002577 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002578 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002579 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002580 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002581 delete AttrList;
2582 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002583
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002584 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2585 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002586
2587 // cv-qualifier-seq[opt].
2588 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002589 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002590 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002591 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002592 llvm::SmallVector<TypeTy*, 2> Exceptions;
2593 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002594 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002595 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002596 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002597 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002598
2599 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002600 if (Tok.is(tok::kw_throw)) {
2601 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002602 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002603 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002604 hasAnyExceptionSpec);
2605 assert(Exceptions.size() == ExceptionRanges.size() &&
2606 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002607 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002608 }
2609
Chris Lattnerf97409f2008-04-06 06:57:35 +00002610 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002611 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002612 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002613 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002614 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002615 /*arglist*/ 0, 0,
2616 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002617 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002618 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002619 Exceptions.data(),
2620 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002621 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002622 LParenLoc, RParenLoc, D),
2623 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002624 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002625 }
2626
Chris Lattner7399ee02008-10-20 02:05:46 +00002627 // Alternatively, this parameter list may be an identifier list form for a
2628 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002629 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002630 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002631 // K&R identifier lists can't have typedefs as identifiers, per
2632 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002633 if (RequiresArg) {
2634 Diag(Tok, diag::err_argument_required_after_attribute);
2635 delete AttrList;
2636 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002637 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2638 // normal declarators, not for abstract-declarators.
2639 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002640 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002641 }
Mike Stump1eb44332009-09-09 15:08:12 +00002642
Chris Lattnerf97409f2008-04-06 06:57:35 +00002643 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002644
Chris Lattnerf97409f2008-04-06 06:57:35 +00002645 // Build up an array of information about the parsed arguments.
2646 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002647
2648 // Enter function-declaration scope, limiting any declarators to the
2649 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002650 ParseScope PrototypeScope(this,
2651 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002652
Chris Lattnerf97409f2008-04-06 06:57:35 +00002653 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002654 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002655 while (1) {
2656 if (Tok.is(tok::ellipsis)) {
2657 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002658 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002659 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002660 }
Mike Stump1eb44332009-09-09 15:08:12 +00002661
Chris Lattnerf97409f2008-04-06 06:57:35 +00002662 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002663
Chris Lattnerf97409f2008-04-06 06:57:35 +00002664 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00002665 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002666 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002667
2668 // If the caller parsed attributes for the first argument, add them now.
2669 if (AttrList) {
2670 DS.AddAttributes(AttrList);
2671 AttrList = 0; // Only apply the attributes to the first parameter.
2672 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002673 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002674
Chris Lattnerf97409f2008-04-06 06:57:35 +00002675 // Parse the declarator. This is "PrototypeContext", because we must
2676 // accept either 'declarator' or 'abstract-declarator' here.
2677 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2678 ParseDeclarator(ParmDecl);
2679
2680 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002681 if (Tok.is(tok::kw___attribute)) {
2682 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00002683 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002684 ParmDecl.AddAttributes(AttrList, Loc);
2685 }
Mike Stump1eb44332009-09-09 15:08:12 +00002686
Chris Lattnerf97409f2008-04-06 06:57:35 +00002687 // Remember this parsed parameter in ParamInfo.
2688 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002689
Douglas Gregor72b505b2008-12-16 21:30:33 +00002690 // DefArgToks is used when the parsing of default arguments needs
2691 // to be delayed.
2692 CachedTokens *DefArgToks = 0;
2693
Chris Lattnerf97409f2008-04-06 06:57:35 +00002694 // If no parameter was specified, verify that *something* was specified,
2695 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002696 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2697 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002698 // Completely missing, emit error.
2699 Diag(DSStart, diag::err_missing_param);
2700 } else {
2701 // Otherwise, we have something. Add it and let semantic analysis try
2702 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00002703
Chris Lattnerf97409f2008-04-06 06:57:35 +00002704 // Inform the actions module about the parameter declarator, so it gets
2705 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002706 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002707
2708 // Parse the default argument, if any. We parse the default
2709 // arguments in all dialects; the semantic analysis in
2710 // ActOnParamDefaultArgument will reject the default argument in
2711 // C.
2712 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002713 SourceLocation EqualLoc = Tok.getLocation();
2714
Chris Lattner04421082008-04-08 04:40:51 +00002715 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002716 if (D.getContext() == Declarator::MemberContext) {
2717 // If we're inside a class definition, cache the tokens
2718 // corresponding to the default argument. We'll actually parse
2719 // them when we see the end of the class definition.
2720 // FIXME: Templates will require something similar.
2721 // FIXME: Can we use a smart pointer for Toks?
2722 DefArgToks = new CachedTokens;
2723
Mike Stump1eb44332009-09-09 15:08:12 +00002724 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002725 tok::semi, false)) {
2726 delete DefArgToks;
2727 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002728 Actions.ActOnParamDefaultArgumentError(Param);
2729 } else
Mike Stump1eb44332009-09-09 15:08:12 +00002730 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00002731 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002732 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002733 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002734 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002735
Douglas Gregor72b505b2008-12-16 21:30:33 +00002736 OwningExprResult DefArgResult(ParseAssignmentExpression());
2737 if (DefArgResult.isInvalid()) {
2738 Actions.ActOnParamDefaultArgumentError(Param);
2739 SkipUntil(tok::comma, tok::r_paren, true, true);
2740 } else {
2741 // Inform the actions module about the default argument
2742 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002743 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002744 }
Chris Lattner04421082008-04-08 04:40:51 +00002745 }
2746 }
Mike Stump1eb44332009-09-09 15:08:12 +00002747
2748 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2749 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002750 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002751 }
2752
2753 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00002754 if (Tok.isNot(tok::comma)) {
2755 if (Tok.is(tok::ellipsis)) {
2756 IsVariadic = true;
2757 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2758
2759 if (!getLang().CPlusPlus) {
2760 // We have ellipsis without a preceding ',', which is ill-formed
2761 // in C. Complain and provide the fix.
2762 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2763 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2764 }
2765 }
2766
2767 break;
2768 }
Mike Stump1eb44332009-09-09 15:08:12 +00002769
Chris Lattnerf97409f2008-04-06 06:57:35 +00002770 // Consume the comma.
2771 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002772 }
Mike Stump1eb44332009-09-09 15:08:12 +00002773
Chris Lattnerf97409f2008-04-06 06:57:35 +00002774 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002775 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00002776
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002777 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002778 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2779 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002780
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002781 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002782 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002783 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002784 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002785 llvm::SmallVector<TypeTy*, 2> Exceptions;
2786 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00002787
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002788 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002789 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002790 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002791 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002792 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002793
2794 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002795 if (Tok.is(tok::kw_throw)) {
2796 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002797 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002798 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002799 hasAnyExceptionSpec);
2800 assert(Exceptions.size() == ExceptionRanges.size() &&
2801 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002802 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002803 }
2804
Reid Spencer5f016e22007-07-11 17:01:13 +00002805 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002806 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002807 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002808 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002809 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002810 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002811 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002812 Exceptions.data(),
2813 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002814 Exceptions.size(),
2815 LParenLoc, RParenLoc, D),
2816 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002817}
2818
Chris Lattner66d28652008-04-06 06:34:08 +00002819/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2820/// we found a K&R-style identifier list instead of a type argument list. The
2821/// current token is known to be the first identifier in the list.
2822///
2823/// identifier-list: [C99 6.7.5]
2824/// identifier
2825/// identifier-list ',' identifier
2826///
2827void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2828 Declarator &D) {
2829 // Build up an array of information about the parsed arguments.
2830 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2831 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00002832
Chris Lattner66d28652008-04-06 06:34:08 +00002833 // If there was no identifier specified for the declarator, either we are in
2834 // an abstract-declarator, or we are in a parameter declarator which was found
2835 // to be abstract. In abstract-declarators, identifier lists are not valid:
2836 // diagnose this.
2837 if (!D.getIdentifier())
2838 Diag(Tok, diag::ext_ident_list_in_param);
2839
2840 // Tok is known to be the first identifier in the list. Remember this
2841 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002842 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002843 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002844 Tok.getLocation(),
2845 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00002846
Chris Lattner50c64772008-04-06 06:39:19 +00002847 ConsumeToken(); // eat the first identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00002848
Chris Lattner66d28652008-04-06 06:34:08 +00002849 while (Tok.is(tok::comma)) {
2850 // Eat the comma.
2851 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002852
Chris Lattner50c64772008-04-06 06:39:19 +00002853 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002854 if (Tok.isNot(tok::identifier)) {
2855 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002856 SkipUntil(tok::r_paren);
2857 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002858 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002859
Chris Lattner66d28652008-04-06 06:34:08 +00002860 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002861
2862 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002863 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002864 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00002865
Chris Lattner66d28652008-04-06 06:34:08 +00002866 // Verify that the argument identifier has not already been mentioned.
2867 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002868 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002869 } else {
2870 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002871 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002872 Tok.getLocation(),
2873 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002874 }
Mike Stump1eb44332009-09-09 15:08:12 +00002875
Chris Lattner66d28652008-04-06 06:34:08 +00002876 // Eat the identifier.
2877 ConsumeToken();
2878 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002879
2880 // If we have the closing ')', eat it and we're done.
2881 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2882
Chris Lattner50c64772008-04-06 06:39:19 +00002883 // Remember that we parsed a function type, and remember the attributes. This
2884 // function type is always a K&R style function type, which is not varargs and
2885 // has no prototype.
2886 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002887 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002888 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002889 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00002890 /*exception*/false,
2891 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002892 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002893 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002894}
Chris Lattneref4715c2008-04-06 05:45:57 +00002895
Reid Spencer5f016e22007-07-11 17:01:13 +00002896/// [C90] direct-declarator '[' constant-expression[opt] ']'
2897/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2898/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2899/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2900/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2901void Parser::ParseBracketDeclarator(Declarator &D) {
2902 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00002903
Chris Lattner378c7e42008-12-18 07:27:21 +00002904 // C array syntax has many features, but by-far the most common is [] and [4].
2905 // This code does a fast path to handle some of the most obvious cases.
2906 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002907 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002908 //FIXME: Use these
2909 CXX0XAttributeList Attr;
2910 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
2911 Attr = ParseCXX0XAttributes();
2912 }
2913
Chris Lattner378c7e42008-12-18 07:27:21 +00002914 // Remember that we parsed the empty array type.
2915 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002916 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2917 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002918 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002919 return;
2920 } else if (Tok.getKind() == tok::numeric_constant &&
2921 GetLookAheadToken(1).is(tok::r_square)) {
2922 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002923 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002924 ConsumeToken();
2925
Sebastian Redlab197ba2009-02-09 18:23:29 +00002926 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002927 //FIXME: Use these
2928 CXX0XAttributeList Attr;
2929 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2930 Attr = ParseCXX0XAttributes();
2931 }
Chris Lattner378c7e42008-12-18 07:27:21 +00002932
2933 // If there was an error parsing the assignment-expression, recover.
2934 if (ExprRes.isInvalid())
2935 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00002936
Chris Lattner378c7e42008-12-18 07:27:21 +00002937 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002938 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2939 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002940 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002941 return;
2942 }
Mike Stump1eb44332009-09-09 15:08:12 +00002943
Reid Spencer5f016e22007-07-11 17:01:13 +00002944 // If valid, this location is the position where we read the 'static' keyword.
2945 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002946 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002947 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002948
Reid Spencer5f016e22007-07-11 17:01:13 +00002949 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002950 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002951 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002952 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00002953
Reid Spencer5f016e22007-07-11 17:01:13 +00002954 // If we haven't already read 'static', check to see if there is one after the
2955 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002956 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002957 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002958
Reid Spencer5f016e22007-07-11 17:01:13 +00002959 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2960 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002961 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00002962
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002963 // Handle the case where we have '[*]' as the array size. However, a leading
2964 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2965 // the the token after the star is a ']'. Since stars in arrays are
2966 // infrequent, use of lookahead is not costly here.
2967 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002968 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002969
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002970 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002971 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002972 StaticLoc = SourceLocation(); // Drop the static.
2973 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002974 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002975 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002976 // Note, in C89, this production uses the constant-expr production instead
2977 // of assignment-expr. The only difference is that assignment-expr allows
2978 // things like '=' and '*='. Sema rejects these in C89 mode because they
2979 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00002980
Douglas Gregore0762c92009-06-19 23:52:42 +00002981 // Parse the constant-expression or assignment-expression now (depending
2982 // on dialect).
2983 if (getLang().CPlusPlus)
2984 NumElements = ParseConstantExpression();
2985 else
2986 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00002987 }
Mike Stump1eb44332009-09-09 15:08:12 +00002988
Reid Spencer5f016e22007-07-11 17:01:13 +00002989 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002990 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002991 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002992 // If the expression was invalid, skip it.
2993 SkipUntil(tok::r_square);
2994 return;
2995 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002996
2997 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2998
Sean Huntbbd37c62009-11-21 08:43:09 +00002999 //FIXME: Use these
3000 CXX0XAttributeList Attr;
3001 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3002 Attr = ParseCXX0XAttributes();
3003 }
3004
Chris Lattner378c7e42008-12-18 07:27:21 +00003005 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003006 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3007 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003008 NumElements.release(),
3009 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003010 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003011}
3012
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003013/// [GNU] typeof-specifier:
3014/// typeof ( expressions )
3015/// typeof ( type-name )
3016/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003017///
3018void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003019 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003020 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003021 SourceLocation StartLoc = ConsumeToken();
3022
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003023 bool isCastExpr;
3024 TypeTy *CastTy;
3025 SourceRange CastRange;
3026 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3027 isCastExpr,
3028 CastTy,
3029 CastRange);
3030
3031 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003032 // FIXME: Not accurate, the range gets one token more than it should.
3033 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003034 else
3035 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003036
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003037 if (isCastExpr) {
3038 if (!CastTy) {
3039 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003040 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003041 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003042
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003043 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003044 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003045 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3046 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003047 DiagID, CastTy))
3048 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003049 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003050 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003051
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003052 // If we get here, the operand to the typeof was an expresion.
3053 if (Operand.isInvalid()) {
3054 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003055 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003056 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003057
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003058 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003059 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003060 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3061 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003062 DiagID, Operand.release()))
3063 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003064}