blob: 2a6a0494e57f9135f25ae6e1a228f219cc7ac91f [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 {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000553 if (getLang().CPlusPlus)
554 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
555
Douglas Gregor1426e532009-05-12 21:31:51 +0000556 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000557
558 if (getLang().CPlusPlus)
559 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
560
Douglas Gregor1426e532009-05-12 21:31:51 +0000561 if (Init.isInvalid()) {
562 SkipUntil(tok::semi, true, true);
563 return DeclPtrTy();
564 }
Anders Carlsson9abf2ae2009-08-16 05:13:48 +0000565 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000566 }
567 } else if (Tok.is(tok::l_paren)) {
568 // Parse C++ direct initializer: '(' expression-list ')'
569 SourceLocation LParenLoc = ConsumeParen();
570 ExprVector Exprs(Actions);
571 CommaLocsTy CommaLocs;
572
573 if (ParseExpressionList(Exprs, CommaLocs)) {
574 SkipUntil(tok::r_paren);
575 } else {
576 // Match the ')'.
577 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
578
579 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
580 "Unexpected number of commas!");
581 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
582 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000583 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000584 }
585 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000586 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000587 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
588 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000589 }
590
591 return ThisDecl;
592}
593
Reid Spencer5f016e22007-07-11 17:01:13 +0000594/// ParseSpecifierQualifierList
595/// specifier-qualifier-list:
596/// type-specifier specifier-qualifier-list[opt]
597/// type-qualifier specifier-qualifier-list[opt]
598/// [GNU] attributes specifier-qualifier-list[opt]
599///
600void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
601 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
602 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000603 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 // Validate declspec for type-name.
606 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000607 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
608 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Reid Spencer5f016e22007-07-11 17:01:13 +0000611 // Issue diagnostic and remove storage class if present.
612 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
613 if (DS.getStorageClassSpecLoc().isValid())
614 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
615 else
616 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
617 DS.ClearStorageClassSpecs();
618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 // Issue diagnostic and remove function specfier if present.
621 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000622 if (DS.isInlineSpecified())
623 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
624 if (DS.isVirtualSpecified())
625 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
626 if (DS.isExplicitSpecified())
627 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000628 DS.ClearFunctionSpecs();
629 }
630}
631
Chris Lattnerc199ab32009-04-12 20:42:31 +0000632/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
633/// specified token is valid after the identifier in a declarator which
634/// immediately follows the declspec. For example, these things are valid:
635///
636/// int x [ 4]; // direct-declarator
637/// int x ( int y); // direct-declarator
638/// int(int x ) // direct-declarator
639/// int x ; // simple-declaration
640/// int x = 17; // init-declarator-list
641/// int x , y; // init-declarator-list
642/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000643/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000644/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000645///
646/// This is not, because 'x' does not immediately follow the declspec (though
647/// ')' happens to be valid anyway).
648/// int (x)
649///
650static bool isValidAfterIdentifierInDeclarator(const Token &T) {
651 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
652 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000653 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000654}
655
Chris Lattnere40c2952009-04-14 21:34:55 +0000656
657/// ParseImplicitInt - This method is called when we have an non-typename
658/// identifier in a declspec (which normally terminates the decl spec) when
659/// the declspec has no type specifier. In this case, the declspec is either
660/// malformed or is "implicit int" (in K&R and C89).
661///
662/// This method handles diagnosing this prettily and returns false if the
663/// declspec is done being processed. If it recovers and thinks there may be
664/// other pieces of declspec after it, it returns true.
665///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000666bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000667 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000668 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000669 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Chris Lattnere40c2952009-04-14 21:34:55 +0000671 SourceLocation Loc = Tok.getLocation();
672 // If we see an identifier that is not a type name, we normally would
673 // parse it as the identifer being declared. However, when a typename
674 // is typo'd or the definition is not included, this will incorrectly
675 // parse the typename as the identifier name and fall over misparsing
676 // later parts of the diagnostic.
677 //
678 // As such, we try to do some look-ahead in cases where this would
679 // otherwise be an "implicit-int" case to see if this is invalid. For
680 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
681 // an identifier with implicit int, we'd get a parse error because the
682 // next token is obviously invalid for a type. Parse these as a case
683 // with an invalid type specifier.
684 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000685
Chris Lattnere40c2952009-04-14 21:34:55 +0000686 // Since we know that this either implicit int (which is rare) or an
687 // error, we'd do lookahead to try to do better recovery.
688 if (isValidAfterIdentifierInDeclarator(NextToken())) {
689 // If this token is valid for implicit int, e.g. "static x = 4", then
690 // we just avoid eating the identifier, so it will be parsed as the
691 // identifier in the declarator.
692 return false;
693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Chris Lattnere40c2952009-04-14 21:34:55 +0000695 // Otherwise, if we don't consume this token, we are going to emit an
696 // error anyway. Try to recover from various common problems. Check
697 // to see if this was a reference to a tag name without a tag specified.
698 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000699 //
700 // C++ doesn't need this, and isTagName doesn't take SS.
701 if (SS == 0) {
702 const char *TagName = 0;
703 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Chris Lattnere40c2952009-04-14 21:34:55 +0000705 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
706 default: break;
707 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
708 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
709 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
710 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
711 }
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Chris Lattnerf4382f52009-04-14 22:17:06 +0000713 if (TagName) {
714 Diag(Loc, diag::err_use_of_tag_name_without_tag)
715 << Tok.getIdentifierInfo() << TagName
716 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000717
Chris Lattnerf4382f52009-04-14 22:17:06 +0000718 // Parse this as a tag as if the missing tag were present.
719 if (TagKind == tok::kw_enum)
720 ParseEnumSpecifier(Loc, DS, AS);
721 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000722 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000723 return true;
724 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000725 }
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Douglas Gregora786fdb2009-10-13 23:27:22 +0000727 // This is almost certainly an invalid type name. Let the action emit a
728 // diagnostic and attempt to recover.
729 Action::TypeTy *T = 0;
730 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
731 CurScope, SS, T)) {
732 // The action emitted a diagnostic, so we don't have to.
733 if (T) {
734 // The action has suggested that the type T could be used. Set that as
735 // the type in the declaration specifiers, consume the would-be type
736 // name token, and we're done.
737 const char *PrevSpec;
738 unsigned DiagID;
739 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
740 false);
741 DS.SetRangeEnd(Tok.getLocation());
742 ConsumeToken();
743
744 // There may be other declaration specifiers after this.
745 return true;
746 }
747
748 // Fall through; the action had no suggestion for us.
749 } else {
750 // The action did not emit a diagnostic, so emit one now.
751 SourceRange R;
752 if (SS) R = SS->getRange();
753 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
754 }
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Douglas Gregora786fdb2009-10-13 23:27:22 +0000756 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000757 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000758 unsigned DiagID;
759 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000760 DS.SetRangeEnd(Tok.getLocation());
761 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Chris Lattnere40c2952009-04-14 21:34:55 +0000763 // TODO: Could inject an invalid typedef decl in an enclosing scope to
764 // avoid rippling error messages on subsequent uses of the same type,
765 // could be useful if #include was forgotten.
766 return false;
767}
768
Reid Spencer5f016e22007-07-11 17:01:13 +0000769/// ParseDeclarationSpecifiers
770/// declaration-specifiers: [C99 6.7]
771/// storage-class-specifier declaration-specifiers[opt]
772/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000773/// [C99] function-specifier declaration-specifiers[opt]
774/// [GNU] attributes declaration-specifiers[opt]
775///
776/// storage-class-specifier: [C99 6.7.1]
777/// 'typedef'
778/// 'extern'
779/// 'static'
780/// 'auto'
781/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000782/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000783/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000784/// function-specifier: [C99 6.7.4]
785/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000786/// [C++] 'virtual'
787/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000788/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000789/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000790
Reid Spencer5f016e22007-07-11 17:01:13 +0000791///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000792void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000793 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000794 AccessSpecifier AS,
795 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000796 if (Tok.is(tok::code_completion)) {
797 Actions.CodeCompleteOrdinaryName(CurScope);
798 ConsumeToken();
799 }
800
Chris Lattner81c018d2008-03-13 06:29:04 +0000801 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000802 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000803 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000804 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000805 unsigned DiagID = 0;
806
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000808
Reid Spencer5f016e22007-07-11 17:01:13 +0000809 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000810 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000811 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000812 // If this is not a declaration specifier token, we're done reading decl
813 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000814 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Chris Lattner5e02c472009-01-05 00:07:25 +0000817 case tok::coloncolon: // ::foo::bar
818 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000819 if (TryAnnotateCXXScopeToken(true))
Chris Lattner5e02c472009-01-05 00:07:25 +0000820 continue;
821 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000822
823 case tok::annot_cxxscope: {
824 if (DS.hasTypeSpecifier())
825 goto DoneWithDeclSpec;
826
827 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000828 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000829 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000830 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000831 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000832 // We have a qualified template-id, e.g., N::A<int>
833 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000834 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000835 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000836 "ParseOptionalCXXScopeSpecifier not working");
837 AnnotateTemplateIdTokenAsType(&SS);
838 continue;
839 }
840
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000841 if (Next.is(tok::annot_typename)) {
842 // FIXME: is this scope-specifier getting dropped?
843 ConsumeToken(); // the scope-specifier
844 if (Tok.getAnnotationValue())
845 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
846 PrevSpec, DiagID,
847 Tok.getAnnotationValue());
848 else
849 DS.SetTypeSpecError();
850 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
851 ConsumeToken(); // The typename
852 }
853
Douglas Gregor9135c722009-03-25 15:40:00 +0000854 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000855 goto DoneWithDeclSpec;
856
857 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000858 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000859 SS.setRange(Tok.getAnnotationRange());
860
861 // If the next token is the name of the class type that the C++ scope
862 // denotes, followed by a '(', then this is a constructor declaration.
863 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000864 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000865 CurScope, &SS) &&
866 GetLookAheadToken(2).is(tok::l_paren))
867 goto DoneWithDeclSpec;
868
Douglas Gregorb696ea32009-02-04 17:00:24 +0000869 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
870 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000871
Chris Lattnerf4382f52009-04-14 22:17:06 +0000872 // If the referenced identifier is not a type, then this declspec is
873 // erroneous: We already checked about that it has no type specifier, and
874 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000875 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000876 if (TypeRep == 0) {
877 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000878 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000879 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000882 ConsumeToken(); // The C++ scope.
883
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000884 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000885 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000886 if (isInvalid)
887 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000889 DS.SetRangeEnd(Tok.getLocation());
890 ConsumeToken(); // The typename.
891
892 continue;
893 }
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Chris Lattner80d0c892009-01-21 19:48:37 +0000895 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000896 if (Tok.getAnnotationValue())
897 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000898 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +0000899 else
900 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000901 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
902 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Chris Lattner80d0c892009-01-21 19:48:37 +0000904 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
905 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
906 // Objective-C interface. If we don't have Objective-C or a '<', this is
907 // just a normal reference to a typedef name.
908 if (!Tok.is(tok::less) || !getLang().ObjC1)
909 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000911 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000912 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000913 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
914 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
915 LAngleLoc, EndProtoLoc);
916 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
917 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Chris Lattner80d0c892009-01-21 19:48:37 +0000919 DS.SetRangeEnd(EndProtoLoc);
920 continue;
921 }
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Chris Lattner3bd934a2008-07-26 01:18:38 +0000923 // typedef-name
924 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000925 // In C++, check to see if this is a scope specifier like foo::bar::, if
926 // so handle it as such. This is important for ctor parsing.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000927 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner837acd02009-01-21 19:19:26 +0000928 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Chris Lattner3bd934a2008-07-26 01:18:38 +0000930 // This identifier can only be a typedef name if we haven't already seen
931 // a type-specifier. Without this check we misparse:
932 // typedef int X; struct Y { short X; }; as 'short int'.
933 if (DS.hasTypeSpecifier())
934 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Chris Lattner3bd934a2008-07-26 01:18:38 +0000936 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +0000937 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +0000938 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000939
Chris Lattnerc199ab32009-04-12 20:42:31 +0000940 // If this is not a typedef name, don't parse it as part of the declspec,
941 // it must be an implicit int or an error.
942 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000943 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000944 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000945 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000946
Douglas Gregorb48fe382008-10-31 09:07:45 +0000947 // C++: If the identifier is actually the name of the class type
948 // being defined and the next token is a '(', then this is a
949 // constructor declaration. We're done with the decl-specifiers
950 // and will treat this token as an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000951 if (getLang().CPlusPlus &&
952 (CurScope->isClassScope() ||
953 (CurScope->isTemplateParamScope() &&
Douglas Gregordec06662009-08-21 18:42:58 +0000954 CurScope->getParent()->isClassScope())) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000955 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000956 NextToken().getKind() == tok::l_paren)
957 goto DoneWithDeclSpec;
958
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000959 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000960 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000961 if (isInvalid)
962 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Chris Lattner3bd934a2008-07-26 01:18:38 +0000964 DS.SetRangeEnd(Tok.getLocation());
965 ConsumeToken(); // The identifier
966
967 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
968 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
969 // Objective-C interface. If we don't have Objective-C or a '<', this is
970 // just a normal reference to a typedef name.
971 if (!Tok.is(tok::less) || !getLang().ObjC1)
972 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000974 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000975 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000976 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
977 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
978 LAngleLoc, EndProtoLoc);
979 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
980 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Chris Lattner3bd934a2008-07-26 01:18:38 +0000982 DS.SetRangeEnd(EndProtoLoc);
983
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000984 // Need to support trailing type qualifiers (e.g. "id<p> const").
985 // If a type specifier follows, it will be diagnosed elsewhere.
986 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000987 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000988
989 // type-name
990 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +0000991 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000992 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000993 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000994 // This template-id does not refer to a type name, so we're
995 // done with the type-specifiers.
996 goto DoneWithDeclSpec;
997 }
998
999 // Turn the template-id annotation token into a type annotation
1000 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001001 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001002 continue;
1003 }
1004
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 // GNU attributes support.
1006 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001007 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001009
1010 // Microsoft declspec support.
1011 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001012 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001013 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Steve Naroff239f0732008-12-25 14:16:32 +00001015 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001016 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001017 // FIXME: Add handling here!
1018 break;
1019
1020 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001021 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001022 case tok::kw___cdecl:
1023 case tok::kw___stdcall:
1024 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001025 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1026 continue;
1027
Reid Spencer5f016e22007-07-11 17:01:13 +00001028 // storage-class-specifier
1029 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001030 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1031 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001032 break;
1033 case tok::kw_extern:
1034 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001035 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001036 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1037 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001038 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001039 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001040 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001041 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001042 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 case tok::kw_static:
1044 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001045 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001046 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1047 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001048 break;
1049 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001050 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001051 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1052 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001053 else
John McCallfec54012009-08-03 20:12:06 +00001054 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1055 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 break;
1057 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001058 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1059 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001061 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001062 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1063 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001064 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001065 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001066 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001067 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 // function-specifier
1070 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001071 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001073 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001074 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001075 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001076 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001077 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001078 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001079
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001080 // friend
1081 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001082 if (DSContext == DSC_class)
1083 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1084 else {
1085 PrevSpec = ""; // not actually used by the diagnostic
1086 DiagID = diag::err_friend_invalid_in_context;
1087 isInvalid = true;
1088 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001089 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Sebastian Redl2ac67232009-11-05 15:47:02 +00001091 // constexpr
1092 case tok::kw_constexpr:
1093 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1094 break;
1095
Chris Lattner80d0c892009-01-21 19:48:37 +00001096 // type-specifier
1097 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001098 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1099 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001100 break;
1101 case tok::kw_long:
1102 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001103 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1104 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001105 else
John McCallfec54012009-08-03 20:12:06 +00001106 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1107 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001108 break;
1109 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001110 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1111 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001112 break;
1113 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001114 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1115 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001116 break;
1117 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001118 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1119 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001120 break;
1121 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001122 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1123 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001124 break;
1125 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001126 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1127 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001128 break;
1129 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001130 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1131 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001132 break;
1133 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001134 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1135 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001136 break;
1137 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001138 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1139 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001140 break;
1141 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001142 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1143 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001144 break;
1145 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001146 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1147 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001148 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001149 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001150 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1151 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001152 break;
1153 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001154 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1155 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001156 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001157 case tok::kw_bool:
1158 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001159 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1160 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001161 break;
1162 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001163 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1164 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001165 break;
1166 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001167 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1168 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001169 break;
1170 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001171 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1172 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001173 break;
1174
1175 // class-specifier:
1176 case tok::kw_class:
1177 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001178 case tok::kw_union: {
1179 tok::TokenKind Kind = Tok.getKind();
1180 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001181 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001182 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001183 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001184
1185 // enum-specifier:
1186 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001187 ConsumeToken();
1188 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001189 continue;
1190
1191 // cv-qualifier:
1192 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001193 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1194 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001195 break;
1196 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001197 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1198 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001199 break;
1200 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001201 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1202 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001203 break;
1204
Douglas Gregord57959a2009-03-27 23:10:48 +00001205 // C++ typename-specifier:
1206 case tok::kw_typename:
1207 if (TryAnnotateTypeOrScopeToken())
1208 continue;
1209 break;
1210
Chris Lattner80d0c892009-01-21 19:48:37 +00001211 // GNU typeof support.
1212 case tok::kw_typeof:
1213 ParseTypeofSpecifier(DS);
1214 continue;
1215
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001216 case tok::kw_decltype:
1217 ParseDecltypeSpecifier(DS);
1218 continue;
1219
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001220 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001221 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001222 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1223 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001224 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001225 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Chris Lattnerbce61352008-07-26 00:20:22 +00001227 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001228 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001229 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001230 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1231 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1232 LAngleLoc, EndProtoLoc);
1233 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1234 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001235 DS.SetRangeEnd(EndProtoLoc);
1236
Chris Lattner1ab3b962008-11-18 07:48:38 +00001237 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001238 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001239 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001240 // Need to support trailing type qualifiers (e.g. "id<p> const").
1241 // If a type specifier follows, it will be diagnosed elsewhere.
1242 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001243 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 }
John McCallfec54012009-08-03 20:12:06 +00001245 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 if (isInvalid) {
1247 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001248 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001249 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001251 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 ConsumeToken();
1253 }
1254}
Douglas Gregoradcac882008-12-01 23:54:00 +00001255
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001256/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001257/// primarily follow the C++ grammar with additions for C99 and GNU,
1258/// which together subsume the C grammar. Note that the C++
1259/// type-specifier also includes the C type-qualifier (for const,
1260/// volatile, and C99 restrict). Returns true if a type-specifier was
1261/// found (and parsed), false otherwise.
1262///
1263/// type-specifier: [C++ 7.1.5]
1264/// simple-type-specifier
1265/// class-specifier
1266/// enum-specifier
1267/// elaborated-type-specifier [TODO]
1268/// cv-qualifier
1269///
1270/// cv-qualifier: [C++ 7.1.5.1]
1271/// 'const'
1272/// 'volatile'
1273/// [C99] 'restrict'
1274///
1275/// simple-type-specifier: [ C++ 7.1.5.2]
1276/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1277/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1278/// 'char'
1279/// 'wchar_t'
1280/// 'bool'
1281/// 'short'
1282/// 'int'
1283/// 'long'
1284/// 'signed'
1285/// 'unsigned'
1286/// 'float'
1287/// 'double'
1288/// 'void'
1289/// [C99] '_Bool'
1290/// [C99] '_Complex'
1291/// [C99] '_Imaginary' // Removed in TC2?
1292/// [GNU] '_Decimal32'
1293/// [GNU] '_Decimal64'
1294/// [GNU] '_Decimal128'
1295/// [GNU] typeof-specifier
1296/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1297/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001298/// [C++0x] 'decltype' ( expression )
John McCallfec54012009-08-03 20:12:06 +00001299bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001300 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001301 unsigned &DiagID,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001302 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001303 SourceLocation Loc = Tok.getLocation();
1304
1305 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001306 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001307 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001308 // Annotate typenames and C++ scope specifiers. If we get one, just
1309 // recurse to handle whatever we get.
1310 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001311 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1312 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001313 // Otherwise, not a type specifier.
1314 return false;
1315 case tok::coloncolon: // ::foo::bar
1316 if (NextToken().is(tok::kw_new) || // ::new
1317 NextToken().is(tok::kw_delete)) // ::delete
1318 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Chris Lattner166a8fc2009-01-04 23:41:41 +00001320 // Annotate typenames and C++ scope specifiers. If we get one, just
1321 // recurse to handle whatever we get.
1322 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001323 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1324 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001325 // Otherwise, not a type specifier.
1326 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Douglas Gregor12e083c2008-11-07 15:42:26 +00001328 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001329 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001330 if (Tok.getAnnotationValue())
1331 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001332 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001333 else
1334 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001335 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1336 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Douglas Gregor12e083c2008-11-07 15:42:26 +00001338 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1339 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1340 // Objective-C interface. If we don't have Objective-C or a '<', this is
1341 // just a normal reference to a typedef name.
1342 if (!Tok.is(tok::less) || !getLang().ObjC1)
1343 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001345 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001346 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001347 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1348 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1349 LAngleLoc, EndProtoLoc);
1350 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1351 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001352
Douglas Gregor12e083c2008-11-07 15:42:26 +00001353 DS.SetRangeEnd(EndProtoLoc);
1354 return true;
1355 }
1356
1357 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001358 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001359 break;
1360 case tok::kw_long:
1361 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001362 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1363 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001364 else
John McCallfec54012009-08-03 20:12:06 +00001365 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1366 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001367 break;
1368 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001369 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001370 break;
1371 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001372 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1373 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001374 break;
1375 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001376 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1377 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001378 break;
1379 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001380 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1381 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001382 break;
1383 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001384 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001385 break;
1386 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001387 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001388 break;
1389 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001390 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001391 break;
1392 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001393 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001394 break;
1395 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001396 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001397 break;
1398 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001399 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001400 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001401 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001402 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001403 break;
1404 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001405 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001406 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001407 case tok::kw_bool:
1408 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001409 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001410 break;
1411 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001412 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1413 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001414 break;
1415 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001416 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1417 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001418 break;
1419 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001420 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1421 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001422 break;
1423
1424 // class-specifier:
1425 case tok::kw_class:
1426 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001427 case tok::kw_union: {
1428 tok::TokenKind Kind = Tok.getKind();
1429 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001430 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001431 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001432 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001433
1434 // enum-specifier:
1435 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001436 ConsumeToken();
1437 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001438 return true;
1439
1440 // cv-qualifier:
1441 case tok::kw_const:
1442 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001443 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001444 break;
1445 case tok::kw_volatile:
1446 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001447 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001448 break;
1449 case tok::kw_restrict:
1450 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001451 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001452 break;
1453
1454 // GNU typeof support.
1455 case tok::kw_typeof:
1456 ParseTypeofSpecifier(DS);
1457 return true;
1458
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001459 // C++0x decltype support.
1460 case tok::kw_decltype:
1461 ParseDecltypeSpecifier(DS);
1462 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001463
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001464 // C++0x auto support.
1465 case tok::kw_auto:
1466 if (!getLang().CPlusPlus0x)
1467 return false;
1468
John McCallfec54012009-08-03 20:12:06 +00001469 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001470 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001471 case tok::kw___ptr64:
1472 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001473 case tok::kw___cdecl:
1474 case tok::kw___stdcall:
1475 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001476 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001477 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001478
Douglas Gregor12e083c2008-11-07 15:42:26 +00001479 default:
1480 // Not a type-specifier; do nothing.
1481 return false;
1482 }
1483
1484 // If the specifier combination wasn't legal, issue a diagnostic.
1485 if (isInvalid) {
1486 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001487 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001488 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001489 }
1490 DS.SetRangeEnd(Tok.getLocation());
1491 ConsumeToken(); // whatever we parsed above.
1492 return true;
1493}
Reid Spencer5f016e22007-07-11 17:01:13 +00001494
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001495/// ParseStructDeclaration - Parse a struct declaration without the terminating
1496/// semicolon.
1497///
Reid Spencer5f016e22007-07-11 17:01:13 +00001498/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001499/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001500/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001501/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001502/// struct-declarator-list:
1503/// struct-declarator
1504/// struct-declarator-list ',' struct-declarator
1505/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1506/// struct-declarator:
1507/// declarator
1508/// [GNU] declarator attributes[opt]
1509/// declarator[opt] ':' constant-expression
1510/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1511///
Chris Lattnere1359422008-04-10 06:46:29 +00001512void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001513ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001514 if (Tok.is(tok::kw___extension__)) {
1515 // __extension__ silences extension warnings in the subexpression.
1516 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001517 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001518 return ParseStructDeclaration(DS, Fields);
1519 }
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Steve Naroff28a7ca82007-08-20 22:28:22 +00001521 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001522 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001523 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001525 // If there are no declarators, this is a free-standing declaration
1526 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001527 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001528 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001529 return;
1530 }
1531
1532 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001533 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001534 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001535 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001536 FieldDeclarator DeclaratorInfo(DS);
1537
1538 // Attributes are only allowed here on successive declarators.
1539 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1540 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001541 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001542 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1543 }
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Steve Naroff28a7ca82007-08-20 22:28:22 +00001545 /// struct-declarator: declarator
1546 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001547 if (Tok.isNot(tok::colon)) {
1548 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1549 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001550 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001551 }
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Chris Lattner04d66662007-10-09 17:33:22 +00001553 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001554 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001555 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001556 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001557 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001558 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001559 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001560 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001561
Steve Naroff28a7ca82007-08-20 22:28:22 +00001562 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001563 if (Tok.is(tok::kw___attribute)) {
1564 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001565 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001566 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1567 }
1568
John McCallbdd563e2009-11-03 02:38:08 +00001569 // We're done with this declarator; invoke the callback.
John McCall54abf7d2009-11-04 02:18:39 +00001570 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1571 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001572
Steve Naroff28a7ca82007-08-20 22:28:22 +00001573 // If we don't have a comma, it is either the end of the list (a ';')
1574 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001575 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001576 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001577
Steve Naroff28a7ca82007-08-20 22:28:22 +00001578 // Consume the comma.
1579 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001580
John McCallbdd563e2009-11-03 02:38:08 +00001581 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001582 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001583}
1584
1585/// ParseStructUnionBody
1586/// struct-contents:
1587/// struct-declaration-list
1588/// [EXT] empty
1589/// [GNU] "struct-declaration-list" without terminatoring ';'
1590/// struct-declaration-list:
1591/// struct-declaration
1592/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001593/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001594///
Reid Spencer5f016e22007-07-11 17:01:13 +00001595void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001596 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001597 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1598 PP.getSourceManager(),
1599 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Reid Spencer5f016e22007-07-11 17:01:13 +00001601 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001602
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001603 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001604 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1605
Reid Spencer5f016e22007-07-11 17:01:13 +00001606 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1607 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001608 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001609 Diag(Tok, diag::ext_empty_struct_union_enum)
1610 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001611
Chris Lattnerb28317a2009-03-28 19:18:32 +00001612 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001613
Reid Spencer5f016e22007-07-11 17:01:13 +00001614 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001615 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Reid Spencer5f016e22007-07-11 17:01:13 +00001618 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001619 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001620 Diag(Tok, diag::ext_extra_struct_semi)
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001621 << CodeModificationHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 ConsumeToken();
1623 continue;
1624 }
Chris Lattnere1359422008-04-10 06:46:29 +00001625
1626 // Parse all the comma separated declarators.
1627 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001628
John McCallbdd563e2009-11-03 02:38:08 +00001629 if (!Tok.is(tok::at)) {
1630 struct CFieldCallback : FieldCallback {
1631 Parser &P;
1632 DeclPtrTy TagDecl;
1633 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1634
1635 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1636 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1637 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1638
1639 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001640 // Install the declarator into the current TagDecl.
John McCall4ba39712009-11-03 21:13:47 +00001641 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1642 FD.D.getDeclSpec().getSourceRange().getBegin(),
1643 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001644 FieldDecls.push_back(Field);
1645 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001646 }
John McCallbdd563e2009-11-03 02:38:08 +00001647 } Callback(*this, TagDecl, FieldDecls);
1648
1649 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001650 } else { // Handle @defs
1651 ConsumeToken();
1652 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1653 Diag(Tok, diag::err_unexpected_at);
1654 SkipUntil(tok::semi, true, true);
1655 continue;
1656 }
1657 ConsumeToken();
1658 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1659 if (!Tok.is(tok::identifier)) {
1660 Diag(Tok, diag::err_expected_ident);
1661 SkipUntil(tok::semi, true, true);
1662 continue;
1663 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001664 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001665 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001666 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001667 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1668 ConsumeToken();
1669 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001670 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001671
Chris Lattner04d66662007-10-09 17:33:22 +00001672 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001674 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001675 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001676 break;
1677 } else {
1678 Diag(Tok, diag::err_expected_semi_decl_list);
1679 // Skip to end of block or statement
1680 SkipUntil(tok::r_brace, true, true);
1681 }
1682 }
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Steve Naroff60fccee2007-10-29 21:38:07 +00001684 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Reid Spencer5f016e22007-07-11 17:01:13 +00001686 AttributeList *AttrList = 0;
1687 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001688 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001689 AttrList = ParseGNUAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001690
1691 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001692 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001693 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001694 AttrList);
1695 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001696 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001697}
1698
1699
1700/// ParseEnumSpecifier
1701/// enum-specifier: [C99 6.7.2.2]
1702/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001703///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001704/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1705/// '}' attributes[opt]
1706/// 'enum' identifier
1707/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001708///
1709/// [C++] elaborated-type-specifier:
1710/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1711///
Chris Lattner4c97d762009-04-12 21:49:30 +00001712void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1713 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001714 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001715 if (Tok.is(tok::code_completion)) {
1716 // Code completion for an enum name.
1717 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1718 ConsumeToken();
1719 }
1720
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001721 AttributeList *Attr = 0;
1722 // If attributes exist after tag, parse them.
1723 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001724 Attr = ParseGNUAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001725
1726 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001727 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001728 if (Tok.isNot(tok::identifier)) {
1729 Diag(Tok, diag::err_expected_ident);
1730 if (Tok.isNot(tok::l_brace)) {
1731 // Has no name and is not a definition.
1732 // Skip the rest of this declarator, up until the comma or semicolon.
1733 SkipUntil(tok::comma, true);
1734 return;
1735 }
1736 }
1737 }
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001739 // Must have either 'enum name' or 'enum {...}'.
1740 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1741 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001743 // Skip the rest of this declarator, up until the comma or semicolon.
1744 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001745 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001746 }
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001748 // If an identifier is present, consume and remember it.
1749 IdentifierInfo *Name = 0;
1750 SourceLocation NameLoc;
1751 if (Tok.is(tok::identifier)) {
1752 Name = Tok.getIdentifierInfo();
1753 NameLoc = ConsumeToken();
1754 }
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001756 // There are three options here. If we have 'enum foo;', then this is a
1757 // forward declaration. If we have 'enum foo {...' then this is a
1758 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1759 //
1760 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1761 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1762 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1763 //
John McCall0f434ec2009-07-31 02:45:11 +00001764 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001765 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001766 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001767 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001768 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001769 else
John McCall0f434ec2009-07-31 02:45:11 +00001770 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001771 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001772 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00001773 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001774 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001775 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001776 Owned, IsDependent);
1777 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Chris Lattner04d66662007-10-09 17:33:22 +00001779 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001780 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001781
Reid Spencer5f016e22007-07-11 17:01:13 +00001782 // TODO: semantic analysis on the declspec for enums.
1783 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001784 unsigned DiagID;
1785 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001786 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001787 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001788}
1789
1790/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1791/// enumerator-list:
1792/// enumerator
1793/// enumerator-list ',' enumerator
1794/// enumerator:
1795/// enumeration-constant
1796/// enumeration-constant '=' constant-expression
1797/// enumeration-constant:
1798/// identifier
1799///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001800void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001801 // Enter the scope of the enum body and start the definition.
1802 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001803 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001804
Reid Spencer5f016e22007-07-11 17:01:13 +00001805 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Chris Lattner7946dd32007-08-27 17:24:30 +00001807 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001808 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001809 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Chris Lattnerb28317a2009-03-28 19:18:32 +00001811 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001812
Chris Lattnerb28317a2009-03-28 19:18:32 +00001813 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001816 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001817 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1818 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Reid Spencer5f016e22007-07-11 17:01:13 +00001820 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001821 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001822 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001823 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001824 AssignedVal = ParseConstantExpression();
1825 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001826 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001827 }
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Reid Spencer5f016e22007-07-11 17:01:13 +00001829 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001830 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1831 LastEnumConstDecl,
1832 IdentLoc, Ident,
1833 EqualLoc,
1834 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001835 EnumConstantDecls.push_back(EnumConstDecl);
1836 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Chris Lattner04d66662007-10-09 17:33:22 +00001838 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001839 break;
1840 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001841
1842 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001843 !(getLang().C99 || getLang().CPlusPlus0x))
1844 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1845 << getLang().CPlusPlus
Chris Lattner29d9c1a2009-12-06 17:36:05 +00001846 << CodeModificationHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001847 }
Mike Stump1eb44332009-09-09 15:08:12 +00001848
Reid Spencer5f016e22007-07-11 17:01:13 +00001849 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001850 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001851
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001852 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001853 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001854 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001855 Attr = ParseGNUAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001856
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001857 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1858 EnumConstantDecls.data(), EnumConstantDecls.size(),
1859 CurScope, Attr);
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Douglas Gregor72de6672009-01-08 20:45:30 +00001861 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001862 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001863}
1864
1865/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001866/// start of a type-qualifier-list.
1867bool Parser::isTypeQualifier() const {
1868 switch (Tok.getKind()) {
1869 default: return false;
1870 // type-qualifier
1871 case tok::kw_const:
1872 case tok::kw_volatile:
1873 case tok::kw_restrict:
1874 return true;
1875 }
1876}
1877
1878/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001879/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001880bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001881 switch (Tok.getKind()) {
1882 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Chris Lattner166a8fc2009-01-04 23:41:41 +00001884 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001885 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001886 // Annotate typenames and C++ scope specifiers. If we get one, just
1887 // recurse to handle whatever we get.
1888 if (TryAnnotateTypeOrScopeToken())
1889 return isTypeSpecifierQualifier();
1890 // Otherwise, not a type specifier.
1891 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001892
Chris Lattner166a8fc2009-01-04 23:41:41 +00001893 case tok::coloncolon: // ::foo::bar
1894 if (NextToken().is(tok::kw_new) || // ::new
1895 NextToken().is(tok::kw_delete)) // ::delete
1896 return false;
1897
1898 // Annotate typenames and C++ scope specifiers. If we get one, just
1899 // recurse to handle whatever we get.
1900 if (TryAnnotateTypeOrScopeToken())
1901 return isTypeSpecifierQualifier();
1902 // Otherwise, not a type specifier.
1903 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Reid Spencer5f016e22007-07-11 17:01:13 +00001905 // GNU attributes support.
1906 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001907 // GNU typeof support.
1908 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00001909
Reid Spencer5f016e22007-07-11 17:01:13 +00001910 // type-specifiers
1911 case tok::kw_short:
1912 case tok::kw_long:
1913 case tok::kw_signed:
1914 case tok::kw_unsigned:
1915 case tok::kw__Complex:
1916 case tok::kw__Imaginary:
1917 case tok::kw_void:
1918 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001919 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001920 case tok::kw_char16_t:
1921 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001922 case tok::kw_int:
1923 case tok::kw_float:
1924 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001925 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001926 case tok::kw__Bool:
1927 case tok::kw__Decimal32:
1928 case tok::kw__Decimal64:
1929 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Chris Lattner99dc9142008-04-13 18:59:07 +00001931 // struct-or-union-specifier (C99) or class-specifier (C++)
1932 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001933 case tok::kw_struct:
1934 case tok::kw_union:
1935 // enum-specifier
1936 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00001937
Reid Spencer5f016e22007-07-11 17:01:13 +00001938 // type-qualifier
1939 case tok::kw_const:
1940 case tok::kw_volatile:
1941 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001942
1943 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001944 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001945 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001946
Chris Lattner7c186be2008-10-20 00:25:30 +00001947 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1948 case tok::less:
1949 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00001950
Steve Naroff239f0732008-12-25 14:16:32 +00001951 case tok::kw___cdecl:
1952 case tok::kw___stdcall:
1953 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001954 case tok::kw___w64:
1955 case tok::kw___ptr64:
1956 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001957 }
1958}
1959
1960/// isDeclarationSpecifier() - Return true if the current token is part of a
1961/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001962bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001963 switch (Tok.getKind()) {
1964 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Chris Lattner166a8fc2009-01-04 23:41:41 +00001966 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001967 // Unfortunate hack to support "Class.factoryMethod" notation.
1968 if (getLang().ObjC1 && NextToken().is(tok::period))
1969 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001970 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001971
Douglas Gregord57959a2009-03-27 23:10:48 +00001972 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001973 // Annotate typenames and C++ scope specifiers. If we get one, just
1974 // recurse to handle whatever we get.
1975 if (TryAnnotateTypeOrScopeToken())
1976 return isDeclarationSpecifier();
1977 // Otherwise, not a declaration specifier.
1978 return false;
1979 case tok::coloncolon: // ::foo::bar
1980 if (NextToken().is(tok::kw_new) || // ::new
1981 NextToken().is(tok::kw_delete)) // ::delete
1982 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001983
Chris Lattner166a8fc2009-01-04 23:41:41 +00001984 // Annotate typenames and C++ scope specifiers. If we get one, just
1985 // recurse to handle whatever we get.
1986 if (TryAnnotateTypeOrScopeToken())
1987 return isDeclarationSpecifier();
1988 // Otherwise, not a declaration specifier.
1989 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001990
Reid Spencer5f016e22007-07-11 17:01:13 +00001991 // storage-class-specifier
1992 case tok::kw_typedef:
1993 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001994 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001995 case tok::kw_static:
1996 case tok::kw_auto:
1997 case tok::kw_register:
1998 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00001999
Reid Spencer5f016e22007-07-11 17:01:13 +00002000 // type-specifiers
2001 case tok::kw_short:
2002 case tok::kw_long:
2003 case tok::kw_signed:
2004 case tok::kw_unsigned:
2005 case tok::kw__Complex:
2006 case tok::kw__Imaginary:
2007 case tok::kw_void:
2008 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002009 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002010 case tok::kw_char16_t:
2011 case tok::kw_char32_t:
2012
Reid Spencer5f016e22007-07-11 17:01:13 +00002013 case tok::kw_int:
2014 case tok::kw_float:
2015 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002016 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002017 case tok::kw__Bool:
2018 case tok::kw__Decimal32:
2019 case tok::kw__Decimal64:
2020 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Chris Lattner99dc9142008-04-13 18:59:07 +00002022 // struct-or-union-specifier (C99) or class-specifier (C++)
2023 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002024 case tok::kw_struct:
2025 case tok::kw_union:
2026 // enum-specifier
2027 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002028
Reid Spencer5f016e22007-07-11 17:01:13 +00002029 // type-qualifier
2030 case tok::kw_const:
2031 case tok::kw_volatile:
2032 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002033
Reid Spencer5f016e22007-07-11 17:01:13 +00002034 // function-specifier
2035 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002036 case tok::kw_virtual:
2037 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002038
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002039 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002040 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002041
Chris Lattner1ef08762007-08-09 17:01:07 +00002042 // GNU typeof support.
2043 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002044
Chris Lattner1ef08762007-08-09 17:01:07 +00002045 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002046 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002047 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002048
Chris Lattnerf3948c42008-07-26 03:38:44 +00002049 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2050 case tok::less:
2051 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002052
Steve Naroff47f52092009-01-06 19:34:12 +00002053 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002054 case tok::kw___cdecl:
2055 case tok::kw___stdcall:
2056 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002057 case tok::kw___w64:
2058 case tok::kw___ptr64:
2059 case tok::kw___forceinline:
2060 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002061 }
2062}
2063
2064
2065/// ParseTypeQualifierListOpt
2066/// type-qualifier-list: [C99 6.7.5]
2067/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002068/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002069/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002070/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Sean Huntbbd37c62009-11-21 08:43:09 +00002071/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2072/// if CXX0XAttributesAllowed = true
Reid Spencer5f016e22007-07-11 17:01:13 +00002073///
Sean Huntbbd37c62009-11-21 08:43:09 +00002074void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2075 bool CXX0XAttributesAllowed) {
2076 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2077 SourceLocation Loc = Tok.getLocation();
2078 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2079 if (CXX0XAttributesAllowed)
2080 DS.AddAttributes(Attr.AttrList);
2081 else
2082 Diag(Loc, diag::err_attributes_not_allowed);
2083 }
2084
Reid Spencer5f016e22007-07-11 17:01:13 +00002085 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002086 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002087 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002088 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002089 SourceLocation Loc = Tok.getLocation();
2090
2091 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002092 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002093 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2094 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002095 break;
2096 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002097 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2098 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002099 break;
2100 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002101 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2102 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002103 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002104 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002105 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002106 case tok::kw___cdecl:
2107 case tok::kw___stdcall:
2108 case tok::kw___fastcall:
Sean Huntbbd37c62009-11-21 08:43:09 +00002109 if (GNUAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002110 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2111 continue;
2112 }
2113 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002114 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002115 if (GNUAttributesAllowed) {
2116 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002117 continue; // do *not* consume the next token!
2118 }
2119 // otherwise, FALL THROUGH!
2120 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002121 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002122 // If this is not a type-qualifier token, we're done reading type
2123 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002124 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002125 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002126 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002127
Reid Spencer5f016e22007-07-11 17:01:13 +00002128 // If the specifier combination wasn't legal, issue a diagnostic.
2129 if (isInvalid) {
2130 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002131 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002132 }
2133 ConsumeToken();
2134 }
2135}
2136
2137
2138/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2139///
2140void Parser::ParseDeclarator(Declarator &D) {
2141 /// This implements the 'declarator' production in the C grammar, then checks
2142 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002143 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002144}
2145
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002146/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2147/// is parsed by the function passed to it. Pass null, and the direct-declarator
2148/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002149/// ptr-operator production.
2150///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002151/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2152/// [C] pointer[opt] direct-declarator
2153/// [C++] direct-declarator
2154/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002155///
2156/// pointer: [C99 6.7.5]
2157/// '*' type-qualifier-list[opt]
2158/// '*' type-qualifier-list[opt] pointer
2159///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002160/// ptr-operator:
2161/// '*' cv-qualifier-seq[opt]
2162/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002163/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002164/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002165/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002166/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002167void Parser::ParseDeclaratorInternal(Declarator &D,
2168 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002169 if (Diags.hasAllExtensionsSilenced())
2170 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002171 // C++ member pointers start with a '::' or a nested-name.
2172 // Member pointers get special handling, since there's no place for the
2173 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002174 if (getLang().CPlusPlus &&
2175 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2176 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002177 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002178 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002179 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002180 // The scope spec really belongs to the direct-declarator.
2181 D.getCXXScopeSpec() = SS;
2182 if (DirectDeclParser)
2183 (this->*DirectDeclParser)(D);
2184 return;
2185 }
2186
2187 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002188 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002189 DeclSpec DS;
2190 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002191 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002192
2193 // Recurse to parse whatever is left.
2194 ParseDeclaratorInternal(D, DirectDeclParser);
2195
2196 // Sema will have to catch (syntactically invalid) pointers into global
2197 // scope. It has to catch pointers into namespace scope anyway.
2198 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002199 Loc, DS.TakeAttributes()),
2200 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002201 return;
2202 }
2203 }
2204
2205 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002206 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002207 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002208 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002209 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002210 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002211 if (DirectDeclParser)
2212 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002213 return;
2214 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002215
Sebastian Redl05532f22009-03-15 22:02:01 +00002216 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2217 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002218 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002219 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002220
Chris Lattner9af55002009-03-27 04:18:06 +00002221 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002222 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002223 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002224
Reid Spencer5f016e22007-07-11 17:01:13 +00002225 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002226 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002227
Reid Spencer5f016e22007-07-11 17:01:13 +00002228 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002229 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002230 if (Kind == tok::star)
2231 // Remember that we parsed a pointer type, and remember the type-quals.
2232 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002233 DS.TakeAttributes()),
2234 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002235 else
2236 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002237 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002238 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002239 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002240 } else {
2241 // Is a reference
2242 DeclSpec DS;
2243
Sebastian Redl743de1f2009-03-23 00:00:23 +00002244 // Complain about rvalue references in C++03, but then go on and build
2245 // the declarator.
2246 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2247 Diag(Loc, diag::err_rvalue_reference);
2248
Reid Spencer5f016e22007-07-11 17:01:13 +00002249 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2250 // cv-qualifiers are introduced through the use of a typedef or of a
2251 // template type argument, in which case the cv-qualifiers are ignored.
2252 //
2253 // [GNU] Retricted references are allowed.
2254 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002255 // [C++0x] Attributes on references are not allowed.
2256 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002257 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002258
2259 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2260 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2261 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002262 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002263 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2264 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002265 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002266 }
2267
2268 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002269 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002270
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002271 if (D.getNumTypeObjects() > 0) {
2272 // C++ [dcl.ref]p4: There shall be no references to references.
2273 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2274 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002275 if (const IdentifierInfo *II = D.getIdentifier())
2276 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2277 << II;
2278 else
2279 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2280 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002281
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002282 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002283 // can go ahead and build the (technically ill-formed)
2284 // declarator: reference collapsing will take care of it.
2285 }
2286 }
2287
Reid Spencer5f016e22007-07-11 17:01:13 +00002288 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002289 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002290 DS.TakeAttributes(),
2291 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002292 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002293 }
2294}
2295
2296/// ParseDirectDeclarator
2297/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002298/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002299/// '(' declarator ')'
2300/// [GNU] '(' attributes declarator ')'
2301/// [C90] direct-declarator '[' constant-expression[opt] ']'
2302/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2303/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2304/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2305/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2306/// direct-declarator '(' parameter-type-list ')'
2307/// direct-declarator '(' identifier-list[opt] ')'
2308/// [GNU] direct-declarator '(' parameter-forward-declarations
2309/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002310/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2311/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002312/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002313///
2314/// declarator-id: [C++ 8]
2315/// id-expression
2316/// '::'[opt] nested-name-specifier[opt] type-name
2317///
2318/// id-expression: [C++ 5.1]
2319/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002320/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002321///
2322/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002323/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002324/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002325/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002326/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002327/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002328///
Reid Spencer5f016e22007-07-11 17:01:13 +00002329void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002330 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002331
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002332 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2333 // ParseDeclaratorInternal might already have parsed the scope.
2334 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2335 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2336 true);
2337 if (afterCXXScope) {
2338 // Change the declaration context for name lookup, until this function
2339 // is exited (and the declarator has been parsed).
2340 DeclScopeObj.EnterDeclaratorScope();
2341 }
2342
2343 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2344 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2345 // We found something that indicates the start of an unqualified-id.
2346 // Parse that unqualified-id.
2347 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2348 /*EnteringContext=*/true,
2349 /*AllowDestructorName=*/true,
2350 /*AllowConstructorName=*/!D.getDeclSpec().hasTypeSpecifier(),
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002351 /*ObjectType=*/0,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002352 D.getName())) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002353 D.SetIdentifier(0, Tok.getLocation());
2354 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002355 } else {
2356 // Parsed the unqualified-id; update range information and move along.
2357 if (D.getSourceRange().getBegin().isInvalid())
2358 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2359 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002360 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002361 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002362 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002363 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002364 assert(!getLang().CPlusPlus &&
2365 "There's a C++-specific check for tok::identifier above");
2366 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2367 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2368 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002369 goto PastIdentifier;
2370 }
2371
2372 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002373 // direct-declarator: '(' declarator ')'
2374 // direct-declarator: '(' attributes declarator ')'
2375 // Example: 'char (*X)' or 'int (*XX)(void)'
2376 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002377 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002378 // This could be something simple like "int" (in which case the declarator
2379 // portion is empty), if an abstract-declarator is allowed.
2380 D.SetIdentifier(0, Tok.getLocation());
2381 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002382 if (D.getContext() == Declarator::MemberContext)
2383 Diag(Tok, diag::err_expected_member_name_or_semi)
2384 << D.getDeclSpec().getSourceRange();
2385 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002386 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002387 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002388 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002389 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002390 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002391 }
Mike Stump1eb44332009-09-09 15:08:12 +00002392
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002393 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002394 assert(D.isPastIdentifier() &&
2395 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Sean Huntbbd37c62009-11-21 08:43:09 +00002397 // Don't parse attributes unless we have an identifier.
2398 if (D.getIdentifier() && getLang().CPlusPlus
2399 && isCXX0XAttributeSpecifier(true)) {
2400 SourceLocation AttrEndLoc;
2401 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2402 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2403 }
2404
Reid Spencer5f016e22007-07-11 17:01:13 +00002405 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002406 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002407 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2408 // In such a case, check if we actually have a function declarator; if it
2409 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002410 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2411 // When not in file scope, warn for ambiguous function declarators, just
2412 // in case the author intended it as a variable definition.
2413 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2414 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2415 break;
2416 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002417 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002418 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002419 ParseBracketDeclarator(D);
2420 } else {
2421 break;
2422 }
2423 }
2424}
2425
Chris Lattneref4715c2008-04-06 05:45:57 +00002426/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2427/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002428/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002429/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2430///
2431/// direct-declarator:
2432/// '(' declarator ')'
2433/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002434/// direct-declarator '(' parameter-type-list ')'
2435/// direct-declarator '(' identifier-list[opt] ')'
2436/// [GNU] direct-declarator '(' parameter-forward-declarations
2437/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002438///
2439void Parser::ParseParenDeclarator(Declarator &D) {
2440 SourceLocation StartLoc = ConsumeParen();
2441 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002442
Chris Lattner7399ee02008-10-20 02:05:46 +00002443 // Eat any attributes before we look at whether this is a grouping or function
2444 // declarator paren. If this is a grouping paren, the attribute applies to
2445 // the type being built up, for example:
2446 // int (__attribute__(()) *x)(long y)
2447 // If this ends up not being a grouping paren, the attribute applies to the
2448 // first argument, for example:
2449 // int (__attribute__(()) int x)
2450 // In either case, we need to eat any attributes to be able to determine what
2451 // sort of paren this is.
2452 //
2453 AttributeList *AttrList = 0;
2454 bool RequiresArg = false;
2455 if (Tok.is(tok::kw___attribute)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002456 AttrList = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00002457
Chris Lattner7399ee02008-10-20 02:05:46 +00002458 // We require that the argument list (if this is a non-grouping paren) be
2459 // present even if the attribute list was empty.
2460 RequiresArg = true;
2461 }
Steve Naroff239f0732008-12-25 14:16:32 +00002462 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002463 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2464 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2465 Tok.is(tok::kw___ptr64)) {
2466 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2467 }
Mike Stump1eb44332009-09-09 15:08:12 +00002468
Chris Lattneref4715c2008-04-06 05:45:57 +00002469 // If we haven't past the identifier yet (or where the identifier would be
2470 // stored, if this is an abstract declarator), then this is probably just
2471 // grouping parens. However, if this could be an abstract-declarator, then
2472 // this could also be the start of function arguments (consider 'void()').
2473 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002474
Chris Lattneref4715c2008-04-06 05:45:57 +00002475 if (!D.mayOmitIdentifier()) {
2476 // If this can't be an abstract-declarator, this *must* be a grouping
2477 // paren, because we haven't seen the identifier yet.
2478 isGrouping = true;
2479 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002480 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002481 isDeclarationSpecifier()) { // 'int(int)' is a function.
2482 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2483 // considered to be a type, not a K&R identifier-list.
2484 isGrouping = false;
2485 } else {
2486 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2487 isGrouping = true;
2488 }
Mike Stump1eb44332009-09-09 15:08:12 +00002489
Chris Lattneref4715c2008-04-06 05:45:57 +00002490 // If this is a grouping paren, handle:
2491 // direct-declarator: '(' declarator ')'
2492 // direct-declarator: '(' attributes declarator ')'
2493 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002494 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002495 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002496 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002497 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002498
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002499 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002500 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002501 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002502
2503 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002504 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002505 return;
2506 }
Mike Stump1eb44332009-09-09 15:08:12 +00002507
Chris Lattneref4715c2008-04-06 05:45:57 +00002508 // Okay, if this wasn't a grouping paren, it must be the start of a function
2509 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002510 // identifier (and remember where it would have been), then call into
2511 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002512 D.SetIdentifier(0, Tok.getLocation());
2513
Chris Lattner7399ee02008-10-20 02:05:46 +00002514 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002515}
2516
2517/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2518/// declarator D up to a paren, which indicates that we are parsing function
2519/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002520///
Chris Lattner7399ee02008-10-20 02:05:46 +00002521/// If AttrList is non-null, then the caller parsed those arguments immediately
2522/// after the open paren - they should be considered to be the first argument of
2523/// a parameter. If RequiresArg is true, then the first argument of the
2524/// function is required to be present and required to not be an identifier
2525/// list.
2526///
Reid Spencer5f016e22007-07-11 17:01:13 +00002527/// This method also handles this portion of the grammar:
2528/// parameter-type-list: [C99 6.7.5]
2529/// parameter-list
2530/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002531/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002532///
2533/// parameter-list: [C99 6.7.5]
2534/// parameter-declaration
2535/// parameter-list ',' parameter-declaration
2536///
2537/// parameter-declaration: [C99 6.7.5]
2538/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002539/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002540/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002541/// declaration-specifiers abstract-declarator[opt]
2542/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002543/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002544/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2545///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002546/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002547/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002548///
Chris Lattner7399ee02008-10-20 02:05:46 +00002549void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2550 AttributeList *AttrList,
2551 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002552 // lparen is already consumed!
2553 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002554
Chris Lattner7399ee02008-10-20 02:05:46 +00002555 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002556 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002557 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002558 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002559 delete AttrList;
2560 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002561
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002562 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2563 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002564
2565 // cv-qualifier-seq[opt].
2566 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002567 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002568 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002569 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002570 llvm::SmallVector<TypeTy*, 2> Exceptions;
2571 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002572 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002573 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002574 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002575 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002576
2577 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002578 if (Tok.is(tok::kw_throw)) {
2579 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002580 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002581 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002582 hasAnyExceptionSpec);
2583 assert(Exceptions.size() == ExceptionRanges.size() &&
2584 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002585 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002586 }
2587
Chris Lattnerf97409f2008-04-06 06:57:35 +00002588 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002589 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002590 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002591 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002592 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002593 /*arglist*/ 0, 0,
2594 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002595 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002596 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002597 Exceptions.data(),
2598 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002599 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002600 LParenLoc, RParenLoc, D),
2601 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002602 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002603 }
2604
Chris Lattner7399ee02008-10-20 02:05:46 +00002605 // Alternatively, this parameter list may be an identifier list form for a
2606 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002607 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002608 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002609 // K&R identifier lists can't have typedefs as identifiers, per
2610 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002611 if (RequiresArg) {
2612 Diag(Tok, diag::err_argument_required_after_attribute);
2613 delete AttrList;
2614 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002615 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2616 // normal declarators, not for abstract-declarators.
2617 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002618 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002619 }
Mike Stump1eb44332009-09-09 15:08:12 +00002620
Chris Lattnerf97409f2008-04-06 06:57:35 +00002621 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002622
Chris Lattnerf97409f2008-04-06 06:57:35 +00002623 // Build up an array of information about the parsed arguments.
2624 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002625
2626 // Enter function-declaration scope, limiting any declarators to the
2627 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002628 ParseScope PrototypeScope(this,
2629 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002630
Chris Lattnerf97409f2008-04-06 06:57:35 +00002631 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002632 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002633 while (1) {
2634 if (Tok.is(tok::ellipsis)) {
2635 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002636 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002637 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002638 }
Mike Stump1eb44332009-09-09 15:08:12 +00002639
Chris Lattnerf97409f2008-04-06 06:57:35 +00002640 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002641
Chris Lattnerf97409f2008-04-06 06:57:35 +00002642 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00002643 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002644 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002645
2646 // If the caller parsed attributes for the first argument, add them now.
2647 if (AttrList) {
2648 DS.AddAttributes(AttrList);
2649 AttrList = 0; // Only apply the attributes to the first parameter.
2650 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002651 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002652
Chris Lattnerf97409f2008-04-06 06:57:35 +00002653 // Parse the declarator. This is "PrototypeContext", because we must
2654 // accept either 'declarator' or 'abstract-declarator' here.
2655 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2656 ParseDeclarator(ParmDecl);
2657
2658 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002659 if (Tok.is(tok::kw___attribute)) {
2660 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00002661 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002662 ParmDecl.AddAttributes(AttrList, Loc);
2663 }
Mike Stump1eb44332009-09-09 15:08:12 +00002664
Chris Lattnerf97409f2008-04-06 06:57:35 +00002665 // Remember this parsed parameter in ParamInfo.
2666 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002667
Douglas Gregor72b505b2008-12-16 21:30:33 +00002668 // DefArgToks is used when the parsing of default arguments needs
2669 // to be delayed.
2670 CachedTokens *DefArgToks = 0;
2671
Chris Lattnerf97409f2008-04-06 06:57:35 +00002672 // If no parameter was specified, verify that *something* was specified,
2673 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002674 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2675 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002676 // Completely missing, emit error.
2677 Diag(DSStart, diag::err_missing_param);
2678 } else {
2679 // Otherwise, we have something. Add it and let semantic analysis try
2680 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00002681
Chris Lattnerf97409f2008-04-06 06:57:35 +00002682 // Inform the actions module about the parameter declarator, so it gets
2683 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002684 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002685
2686 // Parse the default argument, if any. We parse the default
2687 // arguments in all dialects; the semantic analysis in
2688 // ActOnParamDefaultArgument will reject the default argument in
2689 // C.
2690 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002691 SourceLocation EqualLoc = Tok.getLocation();
2692
Chris Lattner04421082008-04-08 04:40:51 +00002693 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002694 if (D.getContext() == Declarator::MemberContext) {
2695 // If we're inside a class definition, cache the tokens
2696 // corresponding to the default argument. We'll actually parse
2697 // them when we see the end of the class definition.
2698 // FIXME: Templates will require something similar.
2699 // FIXME: Can we use a smart pointer for Toks?
2700 DefArgToks = new CachedTokens;
2701
Mike Stump1eb44332009-09-09 15:08:12 +00002702 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002703 tok::semi, false)) {
2704 delete DefArgToks;
2705 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002706 Actions.ActOnParamDefaultArgumentError(Param);
2707 } else
Mike Stump1eb44332009-09-09 15:08:12 +00002708 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00002709 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002710 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002711 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002712 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002713
Douglas Gregor72b505b2008-12-16 21:30:33 +00002714 OwningExprResult DefArgResult(ParseAssignmentExpression());
2715 if (DefArgResult.isInvalid()) {
2716 Actions.ActOnParamDefaultArgumentError(Param);
2717 SkipUntil(tok::comma, tok::r_paren, true, true);
2718 } else {
2719 // Inform the actions module about the default argument
2720 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002721 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002722 }
Chris Lattner04421082008-04-08 04:40:51 +00002723 }
2724 }
Mike Stump1eb44332009-09-09 15:08:12 +00002725
2726 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2727 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002728 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002729 }
2730
2731 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00002732 if (Tok.isNot(tok::comma)) {
2733 if (Tok.is(tok::ellipsis)) {
2734 IsVariadic = true;
2735 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2736
2737 if (!getLang().CPlusPlus) {
2738 // We have ellipsis without a preceding ',', which is ill-formed
2739 // in C. Complain and provide the fix.
2740 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2741 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2742 }
2743 }
2744
2745 break;
2746 }
Mike Stump1eb44332009-09-09 15:08:12 +00002747
Chris Lattnerf97409f2008-04-06 06:57:35 +00002748 // Consume the comma.
2749 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002750 }
Mike Stump1eb44332009-09-09 15:08:12 +00002751
Chris Lattnerf97409f2008-04-06 06:57:35 +00002752 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002753 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00002754
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002755 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002756 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2757 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002758
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002759 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002760 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002761 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002762 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002763 llvm::SmallVector<TypeTy*, 2> Exceptions;
2764 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00002765
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002766 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002767 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002768 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002769 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002770 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002771
2772 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002773 if (Tok.is(tok::kw_throw)) {
2774 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002775 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002776 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002777 hasAnyExceptionSpec);
2778 assert(Exceptions.size() == ExceptionRanges.size() &&
2779 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002780 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002781 }
2782
Reid Spencer5f016e22007-07-11 17:01:13 +00002783 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002784 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002785 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002786 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002787 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002788 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002789 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002790 Exceptions.data(),
2791 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002792 Exceptions.size(),
2793 LParenLoc, RParenLoc, D),
2794 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002795}
2796
Chris Lattner66d28652008-04-06 06:34:08 +00002797/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2798/// we found a K&R-style identifier list instead of a type argument list. The
2799/// current token is known to be the first identifier in the list.
2800///
2801/// identifier-list: [C99 6.7.5]
2802/// identifier
2803/// identifier-list ',' identifier
2804///
2805void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2806 Declarator &D) {
2807 // Build up an array of information about the parsed arguments.
2808 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2809 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00002810
Chris Lattner66d28652008-04-06 06:34:08 +00002811 // If there was no identifier specified for the declarator, either we are in
2812 // an abstract-declarator, or we are in a parameter declarator which was found
2813 // to be abstract. In abstract-declarators, identifier lists are not valid:
2814 // diagnose this.
2815 if (!D.getIdentifier())
2816 Diag(Tok, diag::ext_ident_list_in_param);
2817
2818 // Tok is known to be the first identifier in the list. Remember this
2819 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002820 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002821 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002822 Tok.getLocation(),
2823 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00002824
Chris Lattner50c64772008-04-06 06:39:19 +00002825 ConsumeToken(); // eat the first identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00002826
Chris Lattner66d28652008-04-06 06:34:08 +00002827 while (Tok.is(tok::comma)) {
2828 // Eat the comma.
2829 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002830
Chris Lattner50c64772008-04-06 06:39:19 +00002831 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002832 if (Tok.isNot(tok::identifier)) {
2833 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002834 SkipUntil(tok::r_paren);
2835 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002836 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002837
Chris Lattner66d28652008-04-06 06:34:08 +00002838 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002839
2840 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002841 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002842 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00002843
Chris Lattner66d28652008-04-06 06:34:08 +00002844 // Verify that the argument identifier has not already been mentioned.
2845 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002846 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002847 } else {
2848 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002849 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002850 Tok.getLocation(),
2851 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002852 }
Mike Stump1eb44332009-09-09 15:08:12 +00002853
Chris Lattner66d28652008-04-06 06:34:08 +00002854 // Eat the identifier.
2855 ConsumeToken();
2856 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002857
2858 // If we have the closing ')', eat it and we're done.
2859 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2860
Chris Lattner50c64772008-04-06 06:39:19 +00002861 // Remember that we parsed a function type, and remember the attributes. This
2862 // function type is always a K&R style function type, which is not varargs and
2863 // has no prototype.
2864 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002865 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002866 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002867 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00002868 /*exception*/false,
2869 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002870 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002871 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002872}
Chris Lattneref4715c2008-04-06 05:45:57 +00002873
Reid Spencer5f016e22007-07-11 17:01:13 +00002874/// [C90] direct-declarator '[' constant-expression[opt] ']'
2875/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2876/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2877/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2878/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2879void Parser::ParseBracketDeclarator(Declarator &D) {
2880 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00002881
Chris Lattner378c7e42008-12-18 07:27:21 +00002882 // C array syntax has many features, but by-far the most common is [] and [4].
2883 // This code does a fast path to handle some of the most obvious cases.
2884 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002885 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002886 //FIXME: Use these
2887 CXX0XAttributeList Attr;
2888 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
2889 Attr = ParseCXX0XAttributes();
2890 }
2891
Chris Lattner378c7e42008-12-18 07:27:21 +00002892 // Remember that we parsed the empty array type.
2893 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002894 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2895 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002896 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002897 return;
2898 } else if (Tok.getKind() == tok::numeric_constant &&
2899 GetLookAheadToken(1).is(tok::r_square)) {
2900 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002901 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002902 ConsumeToken();
2903
Sebastian Redlab197ba2009-02-09 18:23:29 +00002904 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002905 //FIXME: Use these
2906 CXX0XAttributeList Attr;
2907 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2908 Attr = ParseCXX0XAttributes();
2909 }
Chris Lattner378c7e42008-12-18 07:27:21 +00002910
2911 // If there was an error parsing the assignment-expression, recover.
2912 if (ExprRes.isInvalid())
2913 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00002914
Chris Lattner378c7e42008-12-18 07:27:21 +00002915 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002916 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2917 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002918 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002919 return;
2920 }
Mike Stump1eb44332009-09-09 15:08:12 +00002921
Reid Spencer5f016e22007-07-11 17:01:13 +00002922 // If valid, this location is the position where we read the 'static' keyword.
2923 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002924 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002925 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002926
Reid Spencer5f016e22007-07-11 17:01:13 +00002927 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002928 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002929 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002930 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00002931
Reid Spencer5f016e22007-07-11 17:01:13 +00002932 // If we haven't already read 'static', check to see if there is one after the
2933 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002934 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002935 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002936
Reid Spencer5f016e22007-07-11 17:01:13 +00002937 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2938 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002939 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00002940
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002941 // Handle the case where we have '[*]' as the array size. However, a leading
2942 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2943 // the the token after the star is a ']'. Since stars in arrays are
2944 // infrequent, use of lookahead is not costly here.
2945 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002946 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002947
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002948 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002949 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002950 StaticLoc = SourceLocation(); // Drop the static.
2951 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002952 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002953 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002954 // Note, in C89, this production uses the constant-expr production instead
2955 // of assignment-expr. The only difference is that assignment-expr allows
2956 // things like '=' and '*='. Sema rejects these in C89 mode because they
2957 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00002958
Douglas Gregore0762c92009-06-19 23:52:42 +00002959 // Parse the constant-expression or assignment-expression now (depending
2960 // on dialect).
2961 if (getLang().CPlusPlus)
2962 NumElements = ParseConstantExpression();
2963 else
2964 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00002965 }
Mike Stump1eb44332009-09-09 15:08:12 +00002966
Reid Spencer5f016e22007-07-11 17:01:13 +00002967 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002968 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002969 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002970 // If the expression was invalid, skip it.
2971 SkipUntil(tok::r_square);
2972 return;
2973 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002974
2975 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2976
Sean Huntbbd37c62009-11-21 08:43:09 +00002977 //FIXME: Use these
2978 CXX0XAttributeList Attr;
2979 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2980 Attr = ParseCXX0XAttributes();
2981 }
2982
Chris Lattner378c7e42008-12-18 07:27:21 +00002983 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002984 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2985 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002986 NumElements.release(),
2987 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002988 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002989}
2990
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002991/// [GNU] typeof-specifier:
2992/// typeof ( expressions )
2993/// typeof ( type-name )
2994/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002995///
2996void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002997 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002998 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002999 SourceLocation StartLoc = ConsumeToken();
3000
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003001 bool isCastExpr;
3002 TypeTy *CastTy;
3003 SourceRange CastRange;
3004 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3005 isCastExpr,
3006 CastTy,
3007 CastRange);
3008
3009 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003010 // FIXME: Not accurate, the range gets one token more than it should.
3011 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003012 else
3013 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003014
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003015 if (isCastExpr) {
3016 if (!CastTy) {
3017 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003018 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003019 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003020
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003021 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003022 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003023 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3024 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003025 DiagID, CastTy))
3026 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003027 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003028 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003029
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003030 // If we get here, the operand to the typeof was an expresion.
3031 if (Operand.isInvalid()) {
3032 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003033 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003034 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003035
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003036 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003037 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003038 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3039 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003040 DiagID, Operand.release()))
3041 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003042}