blob: b13dc7335670320645845d6ee162d370df6a1509 [file] [log] [blame]
Sean Huntbbd37c62009-11-21 08:43:09 +00001
Reid Spencer5f016e22007-07-11 17:01:13 +00002//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
Chris Lattner0bc735f2007-12-29 19:59:25 +00006// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00008//
9//===----------------------------------------------------------------------===//
10//
11// This file implements the Declaration portions of the Parser interfaces.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000016#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000017#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000018#include "clang/Parse/Template.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000019#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallSet.h"
21using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// C99 6.7: Declarations.
25//===----------------------------------------------------------------------===//
26
27/// ParseTypeName
28/// type-name: [C99 6.7.6]
29/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000030///
31/// Called type-id in C++.
Sebastian Redlef65f062009-05-29 18:02:33 +000032Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 // Parse the common declaration-specifiers piece.
34 DeclSpec DS;
35 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000036
Reid Spencer5f016e22007-07-11 17:01:13 +000037 // Parse the abstract-declarator, if present.
38 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
39 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000040 if (Range)
41 *Range = DeclaratorInfo.getSourceRange();
42
Chris Lattnereaaebc72009-04-25 08:06:05 +000043 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000044 return true;
45
46 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000047}
48
Sean Huntbbd37c62009-11-21 08:43:09 +000049/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000050///
51/// [GNU] attributes:
52/// attribute
53/// attributes attribute
54///
55/// [GNU] attribute:
56/// '__attribute__' '(' '(' attribute-list ')' ')'
57///
58/// [GNU] attribute-list:
59/// attrib
60/// attribute_list ',' attrib
61///
62/// [GNU] attrib:
63/// empty
64/// attrib-name
65/// attrib-name '(' identifier ')'
66/// attrib-name '(' identifier ',' nonempty-expr-list ')'
67/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
68///
69/// [GNU] attrib-name:
70/// identifier
71/// typespec
72/// typequal
73/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000074///
Reid Spencer5f016e22007-07-11 17:01:13 +000075/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000076/// token lookahead. Comment from gcc: "If they start with an identifier
77/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000078/// start with that identifier; otherwise they are an expression list."
79///
80/// At the moment, I am not doing 2 token lookahead. I am also unaware of
81/// any attributes that don't work (based on my limited testing). Most
82/// attributes are very simple in practice. Until we find a bug, I don't see
83/// a pressing need to implement the 2 token lookahead.
84
Sean Huntbbd37c62009-11-21 08:43:09 +000085AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
86 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000087
Reid Spencer5f016e22007-07-11 17:01:13 +000088 AttributeList *CurrAttr = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner04d66662007-10-09 17:33:22 +000090 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000091 ConsumeToken();
92 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
93 "attribute")) {
94 SkipUntil(tok::r_paren, true); // skip until ) or ;
95 return CurrAttr;
96 }
97 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
98 SkipUntil(tok::r_paren, true); // skip until ) or ;
99 return CurrAttr;
100 }
101 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000102 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
103 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000104
105 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
107 ConsumeToken();
108 continue;
109 }
110 // we have an identifier or declaration specifier (const, int, etc.)
111 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
112 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000115 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattner04d66662007-10-09 17:33:22 +0000118 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000119 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
120 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000121
122 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 // __attribute__(( mode(byte) ))
124 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000125 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000127 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 ConsumeToken();
129 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000130 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 // now parse the non-empty comma separated list of expressions
134 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000135 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000136 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 ArgExprsOk = false;
138 SkipUntil(tok::r_paren);
139 break;
140 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000141 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 }
Chris Lattner04d66662007-10-09 17:33:22 +0000143 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000144 break;
145 ConsumeToken(); // Eat the comma, move to the next argument
146 }
Chris Lattner04d66662007-10-09 17:33:22 +0000147 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000149 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
150 AttrNameLoc, ParmName, ParmLoc,
151 ArgExprs.take(), ArgExprs.size(),
152 CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 }
154 }
155 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000156 switch (Tok.getKind()) {
157 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 // __attribute__(( nonnull() ))
160 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000161 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000163 break;
164 case tok::kw_char:
165 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000166 case tok::kw_char16_t:
167 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000168 case tok::kw_bool:
169 case tok::kw_short:
170 case tok::kw_int:
171 case tok::kw_long:
172 case tok::kw_signed:
173 case tok::kw_unsigned:
174 case tok::kw_float:
175 case tok::kw_double:
176 case tok::kw_void:
177 case tok::kw_typeof:
178 // If it's a builtin type name, eat it and expect a rparen
179 // __attribute__(( vec_type_hint(char) ))
180 ConsumeToken();
Sean Huntbbd37c62009-11-21 08:43:09 +0000181 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Nate Begeman6f3d8382009-06-26 06:32:41 +0000182 0, SourceLocation(), 0, 0, CurrAttr);
183 if (Tok.is(tok::r_paren))
184 ConsumeParen();
185 break;
186 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000188 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 // now parse the list of expressions
192 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000193 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000194 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 ArgExprsOk = false;
196 SkipUntil(tok::r_paren);
197 break;
198 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000199 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000200 }
Chris Lattner04d66662007-10-09 17:33:22 +0000201 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 break;
203 ConsumeToken(); // Eat the comma, move to the next argument
204 }
205 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000206 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000208 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Sean Huntbbd37c62009-11-21 08:43:09 +0000209 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
210 ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 CurrAttr);
212 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000213 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 }
215 }
216 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000217 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 0, SourceLocation(), 0, 0, CurrAttr);
219 }
220 }
221 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000223 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000224 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
225 SkipUntil(tok::r_paren, false);
226 }
227 if (EndLoc)
228 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 }
230 return CurrAttr;
231}
232
Eli Friedmana23b4852009-06-08 07:21:15 +0000233/// ParseMicrosoftDeclSpec - Parse an __declspec construct
234///
235/// [MS] decl-specifier:
236/// __declspec ( extended-decl-modifier-seq )
237///
238/// [MS] extended-decl-modifier-seq:
239/// extended-decl-modifier[opt]
240/// extended-decl-modifier extended-decl-modifier-seq
241
Eli Friedman290eeb02009-06-08 23:27:34 +0000242AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000243 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000244
Steve Narofff59e17e2008-12-24 20:59:21 +0000245 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000246 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
247 "declspec")) {
248 SkipUntil(tok::r_paren, true); // skip until ) or ;
249 return CurrAttr;
250 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000251 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000252 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
253 SourceLocation AttrNameLoc = ConsumeToken();
254 if (Tok.is(tok::l_paren)) {
255 ConsumeParen();
256 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
257 // correctly.
258 OwningExprResult ArgExpr(ParseAssignmentExpression());
259 if (!ArgExpr.isInvalid()) {
260 ExprTy* ExprList = ArgExpr.take();
Sean Huntbbd37c62009-11-21 08:43:09 +0000261 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedmana23b4852009-06-08 07:21:15 +0000262 SourceLocation(), &ExprList, 1,
263 CurrAttr, true);
264 }
265 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
266 SkipUntil(tok::r_paren, false);
267 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000268 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
269 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000270 }
271 }
272 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
273 SkipUntil(tok::r_paren, false);
Eli Friedman290eeb02009-06-08 23:27:34 +0000274 return CurrAttr;
275}
276
277AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
278 // Treat these like attributes
279 // FIXME: Allow Sema to distinguish between these and real attributes!
280 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
281 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) ||
282 Tok.is(tok::kw___w64)) {
283 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
284 SourceLocation AttrNameLoc = ConsumeToken();
285 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
286 // FIXME: Support these properly!
287 continue;
Sean Huntbbd37c62009-11-21 08:43:09 +0000288 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman290eeb02009-06-08 23:27:34 +0000289 SourceLocation(), 0, 0, CurrAttr, true);
290 }
291 return CurrAttr;
Steve Narofff59e17e2008-12-24 20:59:21 +0000292}
293
Reid Spencer5f016e22007-07-11 17:01:13 +0000294/// ParseDeclaration - Parse a full 'declaration', which consists of
295/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000296/// 'Context' should be a Declarator::TheContext value. This returns the
297/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000298///
299/// declaration: [C99 6.7]
300/// block-declaration ->
301/// simple-declaration
302/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000303/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000304/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000305/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000306/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000307/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000308/// others... [FIXME]
309///
Chris Lattner97144fc2009-04-02 04:16:50 +0000310Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000311 SourceLocation &DeclEnd,
312 CXX0XAttributeList Attr) {
Chris Lattner682bf922009-03-29 16:50:03 +0000313 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000314 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000315 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000316 case tok::kw_export:
Sean Huntbbd37c62009-11-21 08:43:09 +0000317 if (Attr.HasAttr)
318 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
319 << Attr.Range;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000320 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000321 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000322 case tok::kw_namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +0000323 if (Attr.HasAttr)
324 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
325 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000326 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000327 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000328 case tok::kw_using:
Sean Huntbbd37c62009-11-21 08:43:09 +0000329 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner682bf922009-03-29 16:50:03 +0000330 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000331 case tok::kw_static_assert:
Sean Huntbbd37c62009-11-21 08:43:09 +0000332 if (Attr.HasAttr)
333 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
334 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000335 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000336 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000337 default:
Sean Huntbbd37c62009-11-21 08:43:09 +0000338 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000339 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000340
Chris Lattner682bf922009-03-29 16:50:03 +0000341 // This routine returns a DeclGroup, if the thing we parsed only contains a
342 // single decl, convert it now.
343 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000344}
345
346/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
347/// declaration-specifiers init-declarator-list[opt] ';'
348///[C90/C++]init-declarator-list ';' [TODO]
349/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000350///
351/// If RequireSemi is false, this does not check for a ';' at the end of the
352/// declaration.
353Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000354 SourceLocation &DeclEnd,
355 AttributeList *Attr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000357 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000358 if (Attr)
359 DS.AddAttributes(Attr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
363 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000364 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000366 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall54abf7d2009-11-04 02:18:39 +0000367 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000368 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 }
Mike Stump1eb44332009-09-09 15:08:12 +0000370
John McCalld8ac0572009-11-03 19:26:08 +0000371 DeclGroupPtrTy DG = ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false,
372 &DeclEnd);
373 return DG;
374}
Mike Stump1eb44332009-09-09 15:08:12 +0000375
John McCalld8ac0572009-11-03 19:26:08 +0000376/// ParseDeclGroup - Having concluded that this is either a function
377/// definition or a group of object declarations, actually parse the
378/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000379Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
380 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000381 bool AllowFunctionDefinitions,
382 SourceLocation *DeclEnd) {
383 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000384 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000385 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000386
John McCalld8ac0572009-11-03 19:26:08 +0000387 // Bail out if the first declarator didn't seem well-formed.
388 if (!D.hasName() && !D.mayOmitIdentifier()) {
389 // Skip until ; or }.
390 SkipUntil(tok::r_brace, true, true);
391 if (Tok.is(tok::semi))
392 ConsumeToken();
393 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000394 }
Mike Stump1eb44332009-09-09 15:08:12 +0000395
John McCalld8ac0572009-11-03 19:26:08 +0000396 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
397 if (isDeclarationAfterDeclarator()) {
398 // Fall though. We have to check this first, though, because
399 // __attribute__ might be the start of a function definition in
400 // (extended) K&R C.
401 } else if (isStartOfFunctionDefinition()) {
402 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
403 Diag(Tok, diag::err_function_declared_typedef);
404
405 // Recover by treating the 'typedef' as spurious.
406 DS.ClearStorageClassSpecs();
407 }
408
409 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
410 return Actions.ConvertDeclToDeclGroup(TheDecl);
411 } else {
412 Diag(Tok, diag::err_expected_fn_body);
413 SkipUntil(tok::semi);
414 return DeclGroupPtrTy();
415 }
416 }
417
418 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
419 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000420 D.complete(FirstDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000421 if (FirstDecl.get())
422 DeclsInGroup.push_back(FirstDecl);
423
424 // If we don't have a comma, it is either the end of the list (a ';') or an
425 // error, bail out.
426 while (Tok.is(tok::comma)) {
427 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000428 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000429
430 // Parse the next declarator.
431 D.clear();
432
433 // Accept attributes in an init-declarator. In the first declarator in a
434 // declaration, these would be part of the declspec. In subsequent
435 // declarators, they become part of the declarator itself, so that they
436 // don't apply to declarators after *this* one. Examples:
437 // short __attribute__((common)) var; -> declspec
438 // short var __attribute__((common)); -> declarator
439 // short x, __attribute__((common)) var; -> declarator
440 if (Tok.is(tok::kw___attribute)) {
441 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000442 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000443 D.AddAttributes(AttrList, Loc);
444 }
445
446 ParseDeclarator(D);
447
448 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000449 D.complete(ThisDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000450 if (ThisDecl.get())
451 DeclsInGroup.push_back(ThisDecl);
452 }
453
454 if (DeclEnd)
455 *DeclEnd = Tok.getLocation();
456
457 if (Context != Declarator::ForContext &&
458 ExpectAndConsume(tok::semi,
459 Context == Declarator::FileContext
460 ? diag::err_invalid_token_after_toplevel_declarator
461 : diag::err_expected_semi_declaration)) {
462 SkipUntil(tok::r_brace, true, true);
463 if (Tok.is(tok::semi))
464 ConsumeToken();
465 }
466
467 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
468 DeclsInGroup.data(),
469 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000470}
471
Douglas Gregor1426e532009-05-12 21:31:51 +0000472/// \brief Parse 'declaration' after parsing 'declaration-specifiers
473/// declarator'. This method parses the remainder of the declaration
474/// (including any attributes or initializer, among other things) and
475/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000476///
Reid Spencer5f016e22007-07-11 17:01:13 +0000477/// init-declarator: [C99 6.7]
478/// declarator
479/// declarator '=' initializer
480/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
481/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000482/// [C++] declarator initializer[opt]
483///
484/// [C++] initializer:
485/// [C++] '=' initializer-clause
486/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000487/// [C++0x] '=' 'default' [TODO]
488/// [C++0x] '=' 'delete'
489///
490/// According to the standard grammar, =default and =delete are function
491/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000492///
Douglas Gregore542c862009-06-23 23:11:28 +0000493Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
494 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000495 // If a simple-asm-expr is present, parse it.
496 if (Tok.is(tok::kw_asm)) {
497 SourceLocation Loc;
498 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
499 if (AsmLabel.isInvalid()) {
500 SkipUntil(tok::semi, true, true);
501 return DeclPtrTy();
502 }
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Douglas Gregor1426e532009-05-12 21:31:51 +0000504 D.setAsmLabel(AsmLabel.release());
505 D.SetRangeEnd(Loc);
506 }
Mike Stump1eb44332009-09-09 15:08:12 +0000507
Douglas Gregor1426e532009-05-12 21:31:51 +0000508 // If attributes are present, parse them.
509 if (Tok.is(tok::kw___attribute)) {
510 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000511 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000512 D.AddAttributes(AttrList, Loc);
513 }
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Douglas Gregor1426e532009-05-12 21:31:51 +0000515 // Inform the current actions module that we just parsed this declarator.
Douglas Gregord5a423b2009-09-25 18:43:00 +0000516 DeclPtrTy ThisDecl;
517 switch (TemplateInfo.Kind) {
518 case ParsedTemplateInfo::NonTemplate:
519 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
520 break;
521
522 case ParsedTemplateInfo::Template:
523 case ParsedTemplateInfo::ExplicitSpecialization:
524 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregore542c862009-06-23 23:11:28 +0000525 Action::MultiTemplateParamsArg(Actions,
526 TemplateInfo.TemplateParams->data(),
527 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000528 D);
529 break;
530
531 case ParsedTemplateInfo::ExplicitInstantiation: {
532 Action::DeclResult ThisRes
533 = Actions.ActOnExplicitInstantiation(CurScope,
534 TemplateInfo.ExternLoc,
535 TemplateInfo.TemplateLoc,
536 D);
537 if (ThisRes.isInvalid()) {
538 SkipUntil(tok::semi, true, true);
539 return DeclPtrTy();
540 }
541
542 ThisDecl = ThisRes.get();
543 break;
544 }
545 }
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Douglas Gregor1426e532009-05-12 21:31:51 +0000547 // Parse declarator '=' initializer.
548 if (Tok.is(tok::equal)) {
549 ConsumeToken();
550 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
551 SourceLocation DelLoc = ConsumeToken();
552 Actions.SetDeclDeleted(ThisDecl, DelLoc);
553 } else {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000554 if (getLang().CPlusPlus)
555 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
556
Douglas Gregor1426e532009-05-12 21:31:51 +0000557 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000558
559 if (getLang().CPlusPlus)
560 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
561
Douglas Gregor1426e532009-05-12 21:31:51 +0000562 if (Init.isInvalid()) {
563 SkipUntil(tok::semi, true, true);
564 return DeclPtrTy();
565 }
Anders Carlsson9abf2ae2009-08-16 05:13:48 +0000566 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000567 }
568 } else if (Tok.is(tok::l_paren)) {
569 // Parse C++ direct initializer: '(' expression-list ')'
570 SourceLocation LParenLoc = ConsumeParen();
571 ExprVector Exprs(Actions);
572 CommaLocsTy CommaLocs;
573
574 if (ParseExpressionList(Exprs, CommaLocs)) {
575 SkipUntil(tok::r_paren);
576 } else {
577 // Match the ')'.
578 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
579
580 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
581 "Unexpected number of commas!");
582 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
583 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000584 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000585 }
586 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000587 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000588 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
589 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000590 }
591
592 return ThisDecl;
593}
594
Reid Spencer5f016e22007-07-11 17:01:13 +0000595/// ParseSpecifierQualifierList
596/// specifier-qualifier-list:
597/// type-specifier specifier-qualifier-list[opt]
598/// type-qualifier specifier-qualifier-list[opt]
599/// [GNU] attributes specifier-qualifier-list[opt]
600///
601void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
602 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
603 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 // Validate declspec for type-name.
607 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000608 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
609 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 // Issue diagnostic and remove storage class if present.
613 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
614 if (DS.getStorageClassSpecLoc().isValid())
615 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
616 else
617 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
618 DS.ClearStorageClassSpecs();
619 }
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 // Issue diagnostic and remove function specfier if present.
622 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000623 if (DS.isInlineSpecified())
624 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
625 if (DS.isVirtualSpecified())
626 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
627 if (DS.isExplicitSpecified())
628 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 DS.ClearFunctionSpecs();
630 }
631}
632
Chris Lattnerc199ab32009-04-12 20:42:31 +0000633/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
634/// specified token is valid after the identifier in a declarator which
635/// immediately follows the declspec. For example, these things are valid:
636///
637/// int x [ 4]; // direct-declarator
638/// int x ( int y); // direct-declarator
639/// int(int x ) // direct-declarator
640/// int x ; // simple-declaration
641/// int x = 17; // init-declarator-list
642/// int x , y; // init-declarator-list
643/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000644/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000645/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000646///
647/// This is not, because 'x' does not immediately follow the declspec (though
648/// ')' happens to be valid anyway).
649/// int (x)
650///
651static bool isValidAfterIdentifierInDeclarator(const Token &T) {
652 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
653 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000654 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000655}
656
Chris Lattnere40c2952009-04-14 21:34:55 +0000657
658/// ParseImplicitInt - This method is called when we have an non-typename
659/// identifier in a declspec (which normally terminates the decl spec) when
660/// the declspec has no type specifier. In this case, the declspec is either
661/// malformed or is "implicit int" (in K&R and C89).
662///
663/// This method handles diagnosing this prettily and returns false if the
664/// declspec is done being processed. If it recovers and thinks there may be
665/// other pieces of declspec after it, it returns true.
666///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000667bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000668 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000669 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000670 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Chris Lattnere40c2952009-04-14 21:34:55 +0000672 SourceLocation Loc = Tok.getLocation();
673 // If we see an identifier that is not a type name, we normally would
674 // parse it as the identifer being declared. However, when a typename
675 // is typo'd or the definition is not included, this will incorrectly
676 // parse the typename as the identifier name and fall over misparsing
677 // later parts of the diagnostic.
678 //
679 // As such, we try to do some look-ahead in cases where this would
680 // otherwise be an "implicit-int" case to see if this is invalid. For
681 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
682 // an identifier with implicit int, we'd get a parse error because the
683 // next token is obviously invalid for a type. Parse these as a case
684 // with an invalid type specifier.
685 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000686
Chris Lattnere40c2952009-04-14 21:34:55 +0000687 // Since we know that this either implicit int (which is rare) or an
688 // error, we'd do lookahead to try to do better recovery.
689 if (isValidAfterIdentifierInDeclarator(NextToken())) {
690 // If this token is valid for implicit int, e.g. "static x = 4", then
691 // we just avoid eating the identifier, so it will be parsed as the
692 // identifier in the declarator.
693 return false;
694 }
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Chris Lattnere40c2952009-04-14 21:34:55 +0000696 // Otherwise, if we don't consume this token, we are going to emit an
697 // error anyway. Try to recover from various common problems. Check
698 // to see if this was a reference to a tag name without a tag specified.
699 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000700 //
701 // C++ doesn't need this, and isTagName doesn't take SS.
702 if (SS == 0) {
703 const char *TagName = 0;
704 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Chris Lattnere40c2952009-04-14 21:34:55 +0000706 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
707 default: break;
708 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
709 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
710 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
711 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Chris Lattnerf4382f52009-04-14 22:17:06 +0000714 if (TagName) {
715 Diag(Loc, diag::err_use_of_tag_name_without_tag)
716 << Tok.getIdentifierInfo() << TagName
717 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Chris Lattnerf4382f52009-04-14 22:17:06 +0000719 // Parse this as a tag as if the missing tag were present.
720 if (TagKind == tok::kw_enum)
721 ParseEnumSpecifier(Loc, DS, AS);
722 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000723 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000724 return true;
725 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000726 }
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Douglas Gregora786fdb2009-10-13 23:27:22 +0000728 // This is almost certainly an invalid type name. Let the action emit a
729 // diagnostic and attempt to recover.
730 Action::TypeTy *T = 0;
731 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
732 CurScope, SS, T)) {
733 // The action emitted a diagnostic, so we don't have to.
734 if (T) {
735 // The action has suggested that the type T could be used. Set that as
736 // the type in the declaration specifiers, consume the would-be type
737 // name token, and we're done.
738 const char *PrevSpec;
739 unsigned DiagID;
740 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
741 false);
742 DS.SetRangeEnd(Tok.getLocation());
743 ConsumeToken();
744
745 // There may be other declaration specifiers after this.
746 return true;
747 }
748
749 // Fall through; the action had no suggestion for us.
750 } else {
751 // The action did not emit a diagnostic, so emit one now.
752 SourceRange R;
753 if (SS) R = SS->getRange();
754 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Douglas Gregora786fdb2009-10-13 23:27:22 +0000757 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000758 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000759 unsigned DiagID;
760 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000761 DS.SetRangeEnd(Tok.getLocation());
762 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Chris Lattnere40c2952009-04-14 21:34:55 +0000764 // TODO: Could inject an invalid typedef decl in an enclosing scope to
765 // avoid rippling error messages on subsequent uses of the same type,
766 // could be useful if #include was forgotten.
767 return false;
768}
769
Reid Spencer5f016e22007-07-11 17:01:13 +0000770/// ParseDeclarationSpecifiers
771/// declaration-specifiers: [C99 6.7]
772/// storage-class-specifier declaration-specifiers[opt]
773/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000774/// [C99] function-specifier declaration-specifiers[opt]
775/// [GNU] attributes declaration-specifiers[opt]
776///
777/// storage-class-specifier: [C99 6.7.1]
778/// 'typedef'
779/// 'extern'
780/// 'static'
781/// 'auto'
782/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000783/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000784/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000785/// function-specifier: [C99 6.7.4]
786/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000787/// [C++] 'virtual'
788/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000789/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000790/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000791
Reid Spencer5f016e22007-07-11 17:01:13 +0000792///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000793void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000794 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000795 AccessSpecifier AS,
796 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000797 if (Tok.is(tok::code_completion)) {
798 Actions.CodeCompleteOrdinaryName(CurScope);
799 ConsumeToken();
800 }
801
Chris Lattner81c018d2008-03-13 06:29:04 +0000802 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000804 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000805 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000806 unsigned DiagID = 0;
807
Reid Spencer5f016e22007-07-11 17:01:13 +0000808 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000809
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000811 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000812 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 // If this is not a declaration specifier token, we're done reading decl
814 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000815 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Chris Lattner5e02c472009-01-05 00:07:25 +0000818 case tok::coloncolon: // ::foo::bar
819 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000820 if (TryAnnotateCXXScopeToken(true))
Chris Lattner5e02c472009-01-05 00:07:25 +0000821 continue;
822 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000823
824 case tok::annot_cxxscope: {
825 if (DS.hasTypeSpecifier())
826 goto DoneWithDeclSpec;
827
828 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000829 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000830 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000831 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000832 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000833 // We have a qualified template-id, e.g., N::A<int>
834 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000835 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000836 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000837 "ParseOptionalCXXScopeSpecifier not working");
838 AnnotateTemplateIdTokenAsType(&SS);
839 continue;
840 }
841
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000842 if (Next.is(tok::annot_typename)) {
843 // FIXME: is this scope-specifier getting dropped?
844 ConsumeToken(); // the scope-specifier
845 if (Tok.getAnnotationValue())
846 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
847 PrevSpec, DiagID,
848 Tok.getAnnotationValue());
849 else
850 DS.SetTypeSpecError();
851 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
852 ConsumeToken(); // The typename
853 }
854
Douglas Gregor9135c722009-03-25 15:40:00 +0000855 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000856 goto DoneWithDeclSpec;
857
858 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000859 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000860 SS.setRange(Tok.getAnnotationRange());
861
862 // If the next token is the name of the class type that the C++ scope
863 // denotes, followed by a '(', then this is a constructor declaration.
864 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000865 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000866 CurScope, &SS) &&
867 GetLookAheadToken(2).is(tok::l_paren))
868 goto DoneWithDeclSpec;
869
Douglas Gregorb696ea32009-02-04 17:00:24 +0000870 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
871 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000872
Chris Lattnerf4382f52009-04-14 22:17:06 +0000873 // If the referenced identifier is not a type, then this declspec is
874 // erroneous: We already checked about that it has no type specifier, and
875 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000876 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000877 if (TypeRep == 0) {
878 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000879 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000880 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000881 }
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000883 ConsumeToken(); // The C++ scope.
884
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000885 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000886 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000887 if (isInvalid)
888 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000890 DS.SetRangeEnd(Tok.getLocation());
891 ConsumeToken(); // The typename.
892
893 continue;
894 }
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Chris Lattner80d0c892009-01-21 19:48:37 +0000896 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000897 if (Tok.getAnnotationValue())
898 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000899 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +0000900 else
901 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000902 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
903 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Chris Lattner80d0c892009-01-21 19:48:37 +0000905 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
906 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
907 // Objective-C interface. If we don't have Objective-C or a '<', this is
908 // just a normal reference to a typedef name.
909 if (!Tok.is(tok::less) || !getLang().ObjC1)
910 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000912 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000913 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000914 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
915 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
916 LAngleLoc, EndProtoLoc);
917 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
918 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Chris Lattner80d0c892009-01-21 19:48:37 +0000920 DS.SetRangeEnd(EndProtoLoc);
921 continue;
922 }
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Chris Lattner3bd934a2008-07-26 01:18:38 +0000924 // typedef-name
925 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000926 // In C++, check to see if this is a scope specifier like foo::bar::, if
927 // so handle it as such. This is important for ctor parsing.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000928 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner837acd02009-01-21 19:19:26 +0000929 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Chris Lattner3bd934a2008-07-26 01:18:38 +0000931 // This identifier can only be a typedef name if we haven't already seen
932 // a type-specifier. Without this check we misparse:
933 // typedef int X; struct Y { short X; }; as 'short int'.
934 if (DS.hasTypeSpecifier())
935 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Chris Lattner3bd934a2008-07-26 01:18:38 +0000937 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +0000938 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +0000939 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000940
Chris Lattnerc199ab32009-04-12 20:42:31 +0000941 // If this is not a typedef name, don't parse it as part of the declspec,
942 // it must be an implicit int or an error.
943 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000944 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000945 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000946 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000947
Douglas Gregorb48fe382008-10-31 09:07:45 +0000948 // C++: If the identifier is actually the name of the class type
949 // being defined and the next token is a '(', then this is a
950 // constructor declaration. We're done with the decl-specifiers
951 // and will treat this token as an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000952 if (getLang().CPlusPlus &&
953 (CurScope->isClassScope() ||
954 (CurScope->isTemplateParamScope() &&
Douglas Gregordec06662009-08-21 18:42:58 +0000955 CurScope->getParent()->isClassScope())) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000956 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000957 NextToken().getKind() == tok::l_paren)
958 goto DoneWithDeclSpec;
959
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000960 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000961 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000962 if (isInvalid)
963 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Chris Lattner3bd934a2008-07-26 01:18:38 +0000965 DS.SetRangeEnd(Tok.getLocation());
966 ConsumeToken(); // The identifier
967
968 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
969 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
970 // Objective-C interface. If we don't have Objective-C or a '<', this is
971 // just a normal reference to a typedef name.
972 if (!Tok.is(tok::less) || !getLang().ObjC1)
973 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000975 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000976 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000977 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
978 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
979 LAngleLoc, EndProtoLoc);
980 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
981 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Chris Lattner3bd934a2008-07-26 01:18:38 +0000983 DS.SetRangeEnd(EndProtoLoc);
984
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000985 // Need to support trailing type qualifiers (e.g. "id<p> const").
986 // If a type specifier follows, it will be diagnosed elsewhere.
987 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000988 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000989
990 // type-name
991 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +0000992 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000993 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000994 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000995 // This template-id does not refer to a type name, so we're
996 // done with the type-specifiers.
997 goto DoneWithDeclSpec;
998 }
999
1000 // Turn the template-id annotation token into a type annotation
1001 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001002 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001003 continue;
1004 }
1005
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 // GNU attributes support.
1007 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001008 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001010
1011 // Microsoft declspec support.
1012 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001013 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001014 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Steve Naroff239f0732008-12-25 14:16:32 +00001016 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001017 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001018 // FIXME: Add handling here!
1019 break;
1020
1021 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001022 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001023 case tok::kw___cdecl:
1024 case tok::kw___stdcall:
1025 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001026 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1027 continue;
1028
Reid Spencer5f016e22007-07-11 17:01:13 +00001029 // storage-class-specifier
1030 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001031 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1032 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001033 break;
1034 case tok::kw_extern:
1035 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001036 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001037 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1038 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001040 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001041 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001042 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001043 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 case tok::kw_static:
1045 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001046 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001047 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1048 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 break;
1050 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001051 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001052 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1053 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001054 else
John McCallfec54012009-08-03 20:12:06 +00001055 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1056 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001057 break;
1058 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001059 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1060 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001061 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001062 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001063 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1064 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001065 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001067 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Reid Spencer5f016e22007-07-11 17:01:13 +00001070 // function-specifier
1071 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001072 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001074 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001075 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001076 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001077 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001078 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001079 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001080
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001081 // friend
1082 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001083 if (DSContext == DSC_class)
1084 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1085 else {
1086 PrevSpec = ""; // not actually used by the diagnostic
1087 DiagID = diag::err_friend_invalid_in_context;
1088 isInvalid = true;
1089 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001090 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Sebastian Redl2ac67232009-11-05 15:47:02 +00001092 // constexpr
1093 case tok::kw_constexpr:
1094 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1095 break;
1096
Chris Lattner80d0c892009-01-21 19:48:37 +00001097 // type-specifier
1098 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001099 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1100 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001101 break;
1102 case tok::kw_long:
1103 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001104 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1105 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001106 else
John McCallfec54012009-08-03 20:12:06 +00001107 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1108 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001109 break;
1110 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001111 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1112 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001113 break;
1114 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001115 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1116 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001117 break;
1118 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001119 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1120 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001121 break;
1122 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001123 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1124 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001125 break;
1126 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001127 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1128 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001129 break;
1130 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001131 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1132 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001133 break;
1134 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001135 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1136 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001137 break;
1138 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001139 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1140 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001141 break;
1142 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001143 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1144 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001145 break;
1146 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001147 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1148 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001149 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001150 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001151 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1152 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001153 break;
1154 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001155 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1156 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001157 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001158 case tok::kw_bool:
1159 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001160 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1161 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001162 break;
1163 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001164 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1165 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001166 break;
1167 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001168 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1169 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001170 break;
1171 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001172 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1173 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001174 break;
1175
1176 // class-specifier:
1177 case tok::kw_class:
1178 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001179 case tok::kw_union: {
1180 tok::TokenKind Kind = Tok.getKind();
1181 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001182 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001183 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001184 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001185
1186 // enum-specifier:
1187 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001188 ConsumeToken();
1189 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001190 continue;
1191
1192 // cv-qualifier:
1193 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001194 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1195 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001196 break;
1197 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001198 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1199 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001200 break;
1201 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001202 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1203 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001204 break;
1205
Douglas Gregord57959a2009-03-27 23:10:48 +00001206 // C++ typename-specifier:
1207 case tok::kw_typename:
1208 if (TryAnnotateTypeOrScopeToken())
1209 continue;
1210 break;
1211
Chris Lattner80d0c892009-01-21 19:48:37 +00001212 // GNU typeof support.
1213 case tok::kw_typeof:
1214 ParseTypeofSpecifier(DS);
1215 continue;
1216
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001217 case tok::kw_decltype:
1218 ParseDecltypeSpecifier(DS);
1219 continue;
1220
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001221 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001222 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001223 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1224 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001225 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001226 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Chris Lattnerbce61352008-07-26 00:20:22 +00001228 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001229 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001230 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001231 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1232 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1233 LAngleLoc, EndProtoLoc);
1234 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1235 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001236 DS.SetRangeEnd(EndProtoLoc);
1237
Chris Lattner1ab3b962008-11-18 07:48:38 +00001238 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001239 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001240 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001241 // Need to support trailing type qualifiers (e.g. "id<p> const").
1242 // If a type specifier follows, it will be diagnosed elsewhere.
1243 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001244 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001245 }
John McCallfec54012009-08-03 20:12:06 +00001246 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 if (isInvalid) {
1248 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001249 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001250 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001252 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 ConsumeToken();
1254 }
1255}
Douglas Gregoradcac882008-12-01 23:54:00 +00001256
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001257/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001258/// primarily follow the C++ grammar with additions for C99 and GNU,
1259/// which together subsume the C grammar. Note that the C++
1260/// type-specifier also includes the C type-qualifier (for const,
1261/// volatile, and C99 restrict). Returns true if a type-specifier was
1262/// found (and parsed), false otherwise.
1263///
1264/// type-specifier: [C++ 7.1.5]
1265/// simple-type-specifier
1266/// class-specifier
1267/// enum-specifier
1268/// elaborated-type-specifier [TODO]
1269/// cv-qualifier
1270///
1271/// cv-qualifier: [C++ 7.1.5.1]
1272/// 'const'
1273/// 'volatile'
1274/// [C99] 'restrict'
1275///
1276/// simple-type-specifier: [ C++ 7.1.5.2]
1277/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1278/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1279/// 'char'
1280/// 'wchar_t'
1281/// 'bool'
1282/// 'short'
1283/// 'int'
1284/// 'long'
1285/// 'signed'
1286/// 'unsigned'
1287/// 'float'
1288/// 'double'
1289/// 'void'
1290/// [C99] '_Bool'
1291/// [C99] '_Complex'
1292/// [C99] '_Imaginary' // Removed in TC2?
1293/// [GNU] '_Decimal32'
1294/// [GNU] '_Decimal64'
1295/// [GNU] '_Decimal128'
1296/// [GNU] typeof-specifier
1297/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1298/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001299/// [C++0x] 'decltype' ( expression )
John McCallfec54012009-08-03 20:12:06 +00001300bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001301 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001302 unsigned &DiagID,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001303 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001304 SourceLocation Loc = Tok.getLocation();
1305
1306 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001307 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001308 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001309 // Annotate typenames and C++ scope specifiers. If we get one, just
1310 // recurse to handle whatever we get.
1311 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001312 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1313 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001314 // Otherwise, not a type specifier.
1315 return false;
1316 case tok::coloncolon: // ::foo::bar
1317 if (NextToken().is(tok::kw_new) || // ::new
1318 NextToken().is(tok::kw_delete)) // ::delete
1319 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Chris Lattner166a8fc2009-01-04 23:41:41 +00001321 // Annotate typenames and C++ scope specifiers. If we get one, just
1322 // recurse to handle whatever we get.
1323 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001324 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1325 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001326 // Otherwise, not a type specifier.
1327 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Douglas Gregor12e083c2008-11-07 15:42:26 +00001329 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001330 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001331 if (Tok.getAnnotationValue())
1332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001333 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001334 else
1335 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001336 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1337 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001338
Douglas Gregor12e083c2008-11-07 15:42:26 +00001339 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1340 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1341 // Objective-C interface. If we don't have Objective-C or a '<', this is
1342 // just a normal reference to a typedef name.
1343 if (!Tok.is(tok::less) || !getLang().ObjC1)
1344 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001345
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001346 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001347 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001348 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1349 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1350 LAngleLoc, EndProtoLoc);
1351 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1352 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Douglas Gregor12e083c2008-11-07 15:42:26 +00001354 DS.SetRangeEnd(EndProtoLoc);
1355 return true;
1356 }
1357
1358 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001359 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001360 break;
1361 case tok::kw_long:
1362 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001363 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1364 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001365 else
John McCallfec54012009-08-03 20:12:06 +00001366 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1367 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001368 break;
1369 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001370 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001371 break;
1372 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001373 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1374 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001375 break;
1376 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001377 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1378 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001379 break;
1380 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001381 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1382 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001383 break;
1384 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001385 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001386 break;
1387 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001388 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001389 break;
1390 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001391 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001392 break;
1393 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001394 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001395 break;
1396 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001397 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001398 break;
1399 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001400 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001401 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001402 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001403 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001404 break;
1405 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001406 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001407 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001408 case tok::kw_bool:
1409 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001410 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001411 break;
1412 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001413 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1414 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001415 break;
1416 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001417 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1418 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001419 break;
1420 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001421 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1422 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001423 break;
1424
1425 // class-specifier:
1426 case tok::kw_class:
1427 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001428 case tok::kw_union: {
1429 tok::TokenKind Kind = Tok.getKind();
1430 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001431 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001432 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001433 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001434
1435 // enum-specifier:
1436 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001437 ConsumeToken();
1438 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001439 return true;
1440
1441 // cv-qualifier:
1442 case tok::kw_const:
1443 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001444 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001445 break;
1446 case tok::kw_volatile:
1447 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001448 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001449 break;
1450 case tok::kw_restrict:
1451 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001452 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001453 break;
1454
1455 // GNU typeof support.
1456 case tok::kw_typeof:
1457 ParseTypeofSpecifier(DS);
1458 return true;
1459
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001460 // C++0x decltype support.
1461 case tok::kw_decltype:
1462 ParseDecltypeSpecifier(DS);
1463 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001464
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001465 // C++0x auto support.
1466 case tok::kw_auto:
1467 if (!getLang().CPlusPlus0x)
1468 return false;
1469
John McCallfec54012009-08-03 20:12:06 +00001470 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001471 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001472 case tok::kw___ptr64:
1473 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001474 case tok::kw___cdecl:
1475 case tok::kw___stdcall:
1476 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001477 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001478 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001479
Douglas Gregor12e083c2008-11-07 15:42:26 +00001480 default:
1481 // Not a type-specifier; do nothing.
1482 return false;
1483 }
1484
1485 // If the specifier combination wasn't legal, issue a diagnostic.
1486 if (isInvalid) {
1487 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001488 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001489 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001490 }
1491 DS.SetRangeEnd(Tok.getLocation());
1492 ConsumeToken(); // whatever we parsed above.
1493 return true;
1494}
Reid Spencer5f016e22007-07-11 17:01:13 +00001495
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001496/// ParseStructDeclaration - Parse a struct declaration without the terminating
1497/// semicolon.
1498///
Reid Spencer5f016e22007-07-11 17:01:13 +00001499/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001500/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001501/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001502/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001503/// struct-declarator-list:
1504/// struct-declarator
1505/// struct-declarator-list ',' struct-declarator
1506/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1507/// struct-declarator:
1508/// declarator
1509/// [GNU] declarator attributes[opt]
1510/// declarator[opt] ':' constant-expression
1511/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1512///
Chris Lattnere1359422008-04-10 06:46:29 +00001513void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001514ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001515 if (Tok.is(tok::kw___extension__)) {
1516 // __extension__ silences extension warnings in the subexpression.
1517 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001518 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001519 return ParseStructDeclaration(DS, Fields);
1520 }
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Steve Naroff28a7ca82007-08-20 22:28:22 +00001522 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001523 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001524 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001526 // If there are no declarators, this is a free-standing declaration
1527 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001528 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001529 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001530 return;
1531 }
1532
1533 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001534 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001535 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001536 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001537 FieldDeclarator DeclaratorInfo(DS);
1538
1539 // Attributes are only allowed here on successive declarators.
1540 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1541 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001542 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001543 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1544 }
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Steve Naroff28a7ca82007-08-20 22:28:22 +00001546 /// struct-declarator: declarator
1547 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001548 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001549 ParseDeclarator(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00001550
Chris Lattner04d66662007-10-09 17:33:22 +00001551 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001552 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001553 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001554 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001555 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001556 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001557 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001558 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001559
Steve Naroff28a7ca82007-08-20 22:28:22 +00001560 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001561 if (Tok.is(tok::kw___attribute)) {
1562 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001563 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001564 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1565 }
1566
John McCallbdd563e2009-11-03 02:38:08 +00001567 // We're done with this declarator; invoke the callback.
John McCall54abf7d2009-11-04 02:18:39 +00001568 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1569 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001570
Steve Naroff28a7ca82007-08-20 22:28:22 +00001571 // If we don't have a comma, it is either the end of the list (a ';')
1572 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001573 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001574 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001575
Steve Naroff28a7ca82007-08-20 22:28:22 +00001576 // Consume the comma.
1577 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001578
John McCallbdd563e2009-11-03 02:38:08 +00001579 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001580 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001581}
1582
1583/// ParseStructUnionBody
1584/// struct-contents:
1585/// struct-declaration-list
1586/// [EXT] empty
1587/// [GNU] "struct-declaration-list" without terminatoring ';'
1588/// struct-declaration-list:
1589/// struct-declaration
1590/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001591/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001592///
Reid Spencer5f016e22007-07-11 17:01:13 +00001593void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001594 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001595 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1596 PP.getSourceManager(),
1597 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001601 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001602 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1603
Reid Spencer5f016e22007-07-11 17:01:13 +00001604 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1605 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001606 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001607 Diag(Tok, diag::ext_empty_struct_union_enum)
1608 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001609
Chris Lattnerb28317a2009-03-28 19:18:32 +00001610 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001611
Reid Spencer5f016e22007-07-11 17:01:13 +00001612 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001613 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001614 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001617 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001618 Diag(Tok, diag::ext_extra_struct_semi)
1619 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001620 ConsumeToken();
1621 continue;
1622 }
Chris Lattnere1359422008-04-10 06:46:29 +00001623
1624 // Parse all the comma separated declarators.
1625 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001626
John McCallbdd563e2009-11-03 02:38:08 +00001627 if (!Tok.is(tok::at)) {
1628 struct CFieldCallback : FieldCallback {
1629 Parser &P;
1630 DeclPtrTy TagDecl;
1631 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1632
1633 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1634 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1635 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1636
1637 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001638 // Install the declarator into the current TagDecl.
John McCall4ba39712009-11-03 21:13:47 +00001639 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1640 FD.D.getDeclSpec().getSourceRange().getBegin(),
1641 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001642 FieldDecls.push_back(Field);
1643 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001644 }
John McCallbdd563e2009-11-03 02:38:08 +00001645 } Callback(*this, TagDecl, FieldDecls);
1646
1647 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001648 } else { // Handle @defs
1649 ConsumeToken();
1650 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1651 Diag(Tok, diag::err_unexpected_at);
1652 SkipUntil(tok::semi, true, true);
1653 continue;
1654 }
1655 ConsumeToken();
1656 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1657 if (!Tok.is(tok::identifier)) {
1658 Diag(Tok, diag::err_expected_ident);
1659 SkipUntil(tok::semi, true, true);
1660 continue;
1661 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001662 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001663 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001664 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001665 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1666 ConsumeToken();
1667 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001668 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001669
Chris Lattner04d66662007-10-09 17:33:22 +00001670 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001671 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001672 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001673 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 break;
1675 } else {
1676 Diag(Tok, diag::err_expected_semi_decl_list);
1677 // Skip to end of block or statement
1678 SkipUntil(tok::r_brace, true, true);
1679 }
1680 }
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Steve Naroff60fccee2007-10-29 21:38:07 +00001682 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Reid Spencer5f016e22007-07-11 17:01:13 +00001684 AttributeList *AttrList = 0;
1685 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001686 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001687 AttrList = ParseGNUAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001688
1689 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001690 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001691 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001692 AttrList);
1693 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001694 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001695}
1696
1697
1698/// ParseEnumSpecifier
1699/// enum-specifier: [C99 6.7.2.2]
1700/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001701///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001702/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1703/// '}' attributes[opt]
1704/// 'enum' identifier
1705/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001706///
1707/// [C++] elaborated-type-specifier:
1708/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1709///
Chris Lattner4c97d762009-04-12 21:49:30 +00001710void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1711 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001712 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001713 if (Tok.is(tok::code_completion)) {
1714 // Code completion for an enum name.
1715 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1716 ConsumeToken();
1717 }
1718
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001719 AttributeList *Attr = 0;
1720 // If attributes exist after tag, parse them.
1721 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001722 Attr = ParseGNUAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001723
1724 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001725 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001726 if (Tok.isNot(tok::identifier)) {
1727 Diag(Tok, diag::err_expected_ident);
1728 if (Tok.isNot(tok::l_brace)) {
1729 // Has no name and is not a definition.
1730 // Skip the rest of this declarator, up until the comma or semicolon.
1731 SkipUntil(tok::comma, true);
1732 return;
1733 }
1734 }
1735 }
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001737 // Must have either 'enum name' or 'enum {...}'.
1738 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1739 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001741 // Skip the rest of this declarator, up until the comma or semicolon.
1742 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001743 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001744 }
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001746 // If an identifier is present, consume and remember it.
1747 IdentifierInfo *Name = 0;
1748 SourceLocation NameLoc;
1749 if (Tok.is(tok::identifier)) {
1750 Name = Tok.getIdentifierInfo();
1751 NameLoc = ConsumeToken();
1752 }
Mike Stump1eb44332009-09-09 15:08:12 +00001753
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001754 // There are three options here. If we have 'enum foo;', then this is a
1755 // forward declaration. If we have 'enum foo {...' then this is a
1756 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1757 //
1758 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1759 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1760 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1761 //
John McCall0f434ec2009-07-31 02:45:11 +00001762 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001763 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001764 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001765 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001766 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001767 else
John McCall0f434ec2009-07-31 02:45:11 +00001768 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001769 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001770 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00001771 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001772 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001773 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001774 Owned, IsDependent);
1775 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Chris Lattner04d66662007-10-09 17:33:22 +00001777 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001778 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001779
Reid Spencer5f016e22007-07-11 17:01:13 +00001780 // TODO: semantic analysis on the declspec for enums.
1781 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001782 unsigned DiagID;
1783 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001784 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001785 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001786}
1787
1788/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1789/// enumerator-list:
1790/// enumerator
1791/// enumerator-list ',' enumerator
1792/// enumerator:
1793/// enumeration-constant
1794/// enumeration-constant '=' constant-expression
1795/// enumeration-constant:
1796/// identifier
1797///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001798void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001799 // Enter the scope of the enum body and start the definition.
1800 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001801 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001802
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Chris Lattner7946dd32007-08-27 17:24:30 +00001805 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001806 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001807 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Chris Lattnerb28317a2009-03-28 19:18:32 +00001809 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001810
Chris Lattnerb28317a2009-03-28 19:18:32 +00001811 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Reid Spencer5f016e22007-07-11 17:01:13 +00001813 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001814 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1816 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Reid Spencer5f016e22007-07-11 17:01:13 +00001818 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001819 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001820 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001821 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001822 AssignedVal = ParseConstantExpression();
1823 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001824 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001825 }
Mike Stump1eb44332009-09-09 15:08:12 +00001826
Reid Spencer5f016e22007-07-11 17:01:13 +00001827 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001828 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1829 LastEnumConstDecl,
1830 IdentLoc, Ident,
1831 EqualLoc,
1832 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 EnumConstantDecls.push_back(EnumConstDecl);
1834 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Chris Lattner04d66662007-10-09 17:33:22 +00001836 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001837 break;
1838 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001839
1840 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001841 !(getLang().C99 || getLang().CPlusPlus0x))
1842 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1843 << getLang().CPlusPlus
1844 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001845 }
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Reid Spencer5f016e22007-07-11 17:01:13 +00001847 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001848 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001849
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001850 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001851 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001852 if (Tok.is(tok::kw___attribute))
Sean Huntbbd37c62009-11-21 08:43:09 +00001853 Attr = ParseGNUAttributes(); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00001854
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001855 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1856 EnumConstantDecls.data(), EnumConstantDecls.size(),
1857 CurScope, Attr);
Mike Stump1eb44332009-09-09 15:08:12 +00001858
Douglas Gregor72de6672009-01-08 20:45:30 +00001859 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001860 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001861}
1862
1863/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001864/// start of a type-qualifier-list.
1865bool Parser::isTypeQualifier() const {
1866 switch (Tok.getKind()) {
1867 default: return false;
1868 // type-qualifier
1869 case tok::kw_const:
1870 case tok::kw_volatile:
1871 case tok::kw_restrict:
1872 return true;
1873 }
1874}
1875
1876/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001877/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001878bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001879 switch (Tok.getKind()) {
1880 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001881
Chris Lattner166a8fc2009-01-04 23:41:41 +00001882 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001883 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001884 // Annotate typenames and C++ scope specifiers. If we get one, just
1885 // recurse to handle whatever we get.
1886 if (TryAnnotateTypeOrScopeToken())
1887 return isTypeSpecifierQualifier();
1888 // Otherwise, not a type specifier.
1889 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001890
Chris Lattner166a8fc2009-01-04 23:41:41 +00001891 case tok::coloncolon: // ::foo::bar
1892 if (NextToken().is(tok::kw_new) || // ::new
1893 NextToken().is(tok::kw_delete)) // ::delete
1894 return false;
1895
1896 // Annotate typenames and C++ scope specifiers. If we get one, just
1897 // recurse to handle whatever we get.
1898 if (TryAnnotateTypeOrScopeToken())
1899 return isTypeSpecifierQualifier();
1900 // Otherwise, not a type specifier.
1901 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001902
Reid Spencer5f016e22007-07-11 17:01:13 +00001903 // GNU attributes support.
1904 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001905 // GNU typeof support.
1906 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Reid Spencer5f016e22007-07-11 17:01:13 +00001908 // type-specifiers
1909 case tok::kw_short:
1910 case tok::kw_long:
1911 case tok::kw_signed:
1912 case tok::kw_unsigned:
1913 case tok::kw__Complex:
1914 case tok::kw__Imaginary:
1915 case tok::kw_void:
1916 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001917 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001918 case tok::kw_char16_t:
1919 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001920 case tok::kw_int:
1921 case tok::kw_float:
1922 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001923 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001924 case tok::kw__Bool:
1925 case tok::kw__Decimal32:
1926 case tok::kw__Decimal64:
1927 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00001928
Chris Lattner99dc9142008-04-13 18:59:07 +00001929 // struct-or-union-specifier (C99) or class-specifier (C++)
1930 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001931 case tok::kw_struct:
1932 case tok::kw_union:
1933 // enum-specifier
1934 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Reid Spencer5f016e22007-07-11 17:01:13 +00001936 // type-qualifier
1937 case tok::kw_const:
1938 case tok::kw_volatile:
1939 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001940
1941 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001942 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001943 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001944
Chris Lattner7c186be2008-10-20 00:25:30 +00001945 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1946 case tok::less:
1947 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00001948
Steve Naroff239f0732008-12-25 14:16:32 +00001949 case tok::kw___cdecl:
1950 case tok::kw___stdcall:
1951 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001952 case tok::kw___w64:
1953 case tok::kw___ptr64:
1954 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001955 }
1956}
1957
1958/// isDeclarationSpecifier() - Return true if the current token is part of a
1959/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001960bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001961 switch (Tok.getKind()) {
1962 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001963
Chris Lattner166a8fc2009-01-04 23:41:41 +00001964 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001965 // Unfortunate hack to support "Class.factoryMethod" notation.
1966 if (getLang().ObjC1 && NextToken().is(tok::period))
1967 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001968 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001969
Douglas Gregord57959a2009-03-27 23:10:48 +00001970 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001971 // Annotate typenames and C++ scope specifiers. If we get one, just
1972 // recurse to handle whatever we get.
1973 if (TryAnnotateTypeOrScopeToken())
1974 return isDeclarationSpecifier();
1975 // Otherwise, not a declaration specifier.
1976 return false;
1977 case tok::coloncolon: // ::foo::bar
1978 if (NextToken().is(tok::kw_new) || // ::new
1979 NextToken().is(tok::kw_delete)) // ::delete
1980 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Chris Lattner166a8fc2009-01-04 23:41:41 +00001982 // Annotate typenames and C++ scope specifiers. If we get one, just
1983 // recurse to handle whatever we get.
1984 if (TryAnnotateTypeOrScopeToken())
1985 return isDeclarationSpecifier();
1986 // Otherwise, not a declaration specifier.
1987 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001988
Reid Spencer5f016e22007-07-11 17:01:13 +00001989 // storage-class-specifier
1990 case tok::kw_typedef:
1991 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001992 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001993 case tok::kw_static:
1994 case tok::kw_auto:
1995 case tok::kw_register:
1996 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00001997
Reid Spencer5f016e22007-07-11 17:01:13 +00001998 // type-specifiers
1999 case tok::kw_short:
2000 case tok::kw_long:
2001 case tok::kw_signed:
2002 case tok::kw_unsigned:
2003 case tok::kw__Complex:
2004 case tok::kw__Imaginary:
2005 case tok::kw_void:
2006 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002007 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002008 case tok::kw_char16_t:
2009 case tok::kw_char32_t:
2010
Reid Spencer5f016e22007-07-11 17:01:13 +00002011 case tok::kw_int:
2012 case tok::kw_float:
2013 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002014 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002015 case tok::kw__Bool:
2016 case tok::kw__Decimal32:
2017 case tok::kw__Decimal64:
2018 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00002019
Chris Lattner99dc9142008-04-13 18:59:07 +00002020 // struct-or-union-specifier (C99) or class-specifier (C++)
2021 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002022 case tok::kw_struct:
2023 case tok::kw_union:
2024 // enum-specifier
2025 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002026
Reid Spencer5f016e22007-07-11 17:01:13 +00002027 // type-qualifier
2028 case tok::kw_const:
2029 case tok::kw_volatile:
2030 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002031
Reid Spencer5f016e22007-07-11 17:01:13 +00002032 // function-specifier
2033 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002034 case tok::kw_virtual:
2035 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002036
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002037 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002038 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002039
Chris Lattner1ef08762007-08-09 17:01:07 +00002040 // GNU typeof support.
2041 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002042
Chris Lattner1ef08762007-08-09 17:01:07 +00002043 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002044 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002045 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002046
Chris Lattnerf3948c42008-07-26 03:38:44 +00002047 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2048 case tok::less:
2049 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002050
Steve Naroff47f52092009-01-06 19:34:12 +00002051 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002052 case tok::kw___cdecl:
2053 case tok::kw___stdcall:
2054 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002055 case tok::kw___w64:
2056 case tok::kw___ptr64:
2057 case tok::kw___forceinline:
2058 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002059 }
2060}
2061
2062
2063/// ParseTypeQualifierListOpt
2064/// type-qualifier-list: [C99 6.7.5]
2065/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002066/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002067/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002068/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Sean Huntbbd37c62009-11-21 08:43:09 +00002069/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2070/// if CXX0XAttributesAllowed = true
Reid Spencer5f016e22007-07-11 17:01:13 +00002071///
Sean Huntbbd37c62009-11-21 08:43:09 +00002072void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2073 bool CXX0XAttributesAllowed) {
2074 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2075 SourceLocation Loc = Tok.getLocation();
2076 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2077 if (CXX0XAttributesAllowed)
2078 DS.AddAttributes(Attr.AttrList);
2079 else
2080 Diag(Loc, diag::err_attributes_not_allowed);
2081 }
2082
Reid Spencer5f016e22007-07-11 17:01:13 +00002083 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002084 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002085 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002086 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002087 SourceLocation Loc = Tok.getLocation();
2088
2089 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002090 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002091 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2092 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002093 break;
2094 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002095 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2096 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002097 break;
2098 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002099 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2100 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002101 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002102 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002103 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002104 case tok::kw___cdecl:
2105 case tok::kw___stdcall:
2106 case tok::kw___fastcall:
Sean Huntbbd37c62009-11-21 08:43:09 +00002107 if (GNUAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002108 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2109 continue;
2110 }
2111 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002112 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002113 if (GNUAttributesAllowed) {
2114 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002115 continue; // do *not* consume the next token!
2116 }
2117 // otherwise, FALL THROUGH!
2118 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002119 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002120 // If this is not a type-qualifier token, we're done reading type
2121 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002122 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002123 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002124 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002125
Reid Spencer5f016e22007-07-11 17:01:13 +00002126 // If the specifier combination wasn't legal, issue a diagnostic.
2127 if (isInvalid) {
2128 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002129 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002130 }
2131 ConsumeToken();
2132 }
2133}
2134
2135
2136/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2137///
2138void Parser::ParseDeclarator(Declarator &D) {
2139 /// This implements the 'declarator' production in the C grammar, then checks
2140 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002141 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002142}
2143
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002144/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2145/// is parsed by the function passed to it. Pass null, and the direct-declarator
2146/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002147/// ptr-operator production.
2148///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002149/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2150/// [C] pointer[opt] direct-declarator
2151/// [C++] direct-declarator
2152/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002153///
2154/// pointer: [C99 6.7.5]
2155/// '*' type-qualifier-list[opt]
2156/// '*' type-qualifier-list[opt] pointer
2157///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002158/// ptr-operator:
2159/// '*' cv-qualifier-seq[opt]
2160/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002161/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002162/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002163/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002164/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002165void Parser::ParseDeclaratorInternal(Declarator &D,
2166 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002167 if (Diags.hasAllExtensionsSilenced())
2168 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002169 // C++ member pointers start with a '::' or a nested-name.
2170 // Member pointers get special handling, since there's no place for the
2171 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002172 if (getLang().CPlusPlus &&
2173 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2174 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002175 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002176 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002177 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002178 // The scope spec really belongs to the direct-declarator.
2179 D.getCXXScopeSpec() = SS;
2180 if (DirectDeclParser)
2181 (this->*DirectDeclParser)(D);
2182 return;
2183 }
2184
2185 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002186 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002187 DeclSpec DS;
2188 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002189 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002190
2191 // Recurse to parse whatever is left.
2192 ParseDeclaratorInternal(D, DirectDeclParser);
2193
2194 // Sema will have to catch (syntactically invalid) pointers into global
2195 // scope. It has to catch pointers into namespace scope anyway.
2196 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002197 Loc, DS.TakeAttributes()),
2198 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002199 return;
2200 }
2201 }
2202
2203 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002204 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002205 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002206 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002207 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002208 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002209 if (DirectDeclParser)
2210 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002211 return;
2212 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002213
Sebastian Redl05532f22009-03-15 22:02:01 +00002214 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2215 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002216 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002217 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002218
Chris Lattner9af55002009-03-27 04:18:06 +00002219 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002220 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002221 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002222
Reid Spencer5f016e22007-07-11 17:01:13 +00002223 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002224 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002225
Reid Spencer5f016e22007-07-11 17:01:13 +00002226 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002227 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002228 if (Kind == tok::star)
2229 // Remember that we parsed a pointer type, and remember the type-quals.
2230 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002231 DS.TakeAttributes()),
2232 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002233 else
2234 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002235 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002236 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002237 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002238 } else {
2239 // Is a reference
2240 DeclSpec DS;
2241
Sebastian Redl743de1f2009-03-23 00:00:23 +00002242 // Complain about rvalue references in C++03, but then go on and build
2243 // the declarator.
2244 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2245 Diag(Loc, diag::err_rvalue_reference);
2246
Reid Spencer5f016e22007-07-11 17:01:13 +00002247 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2248 // cv-qualifiers are introduced through the use of a typedef or of a
2249 // template type argument, in which case the cv-qualifiers are ignored.
2250 //
2251 // [GNU] Retricted references are allowed.
2252 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002253 // [C++0x] Attributes on references are not allowed.
2254 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002255 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002256
2257 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2258 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2259 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002260 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002261 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2262 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002263 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002264 }
2265
2266 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002267 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002268
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002269 if (D.getNumTypeObjects() > 0) {
2270 // C++ [dcl.ref]p4: There shall be no references to references.
2271 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2272 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002273 if (const IdentifierInfo *II = D.getIdentifier())
2274 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2275 << II;
2276 else
2277 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2278 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002279
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002280 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002281 // can go ahead and build the (technically ill-formed)
2282 // declarator: reference collapsing will take care of it.
2283 }
2284 }
2285
Reid Spencer5f016e22007-07-11 17:01:13 +00002286 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002287 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002288 DS.TakeAttributes(),
2289 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002290 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002291 }
2292}
2293
2294/// ParseDirectDeclarator
2295/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002296/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002297/// '(' declarator ')'
2298/// [GNU] '(' attributes declarator ')'
2299/// [C90] direct-declarator '[' constant-expression[opt] ']'
2300/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2301/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2302/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2303/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2304/// direct-declarator '(' parameter-type-list ')'
2305/// direct-declarator '(' identifier-list[opt] ')'
2306/// [GNU] direct-declarator '(' parameter-forward-declarations
2307/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002308/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2309/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002310/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002311///
2312/// declarator-id: [C++ 8]
2313/// id-expression
2314/// '::'[opt] nested-name-specifier[opt] type-name
2315///
2316/// id-expression: [C++ 5.1]
2317/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002318/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002319///
2320/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002321/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002322/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002323/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002324/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002325/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002326///
Reid Spencer5f016e22007-07-11 17:01:13 +00002327void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002328 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002329
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002330 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2331 // ParseDeclaratorInternal might already have parsed the scope.
2332 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2333 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2334 true);
2335 if (afterCXXScope) {
2336 // Change the declaration context for name lookup, until this function
2337 // is exited (and the declarator has been parsed).
2338 DeclScopeObj.EnterDeclaratorScope();
2339 }
2340
2341 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2342 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2343 // We found something that indicates the start of an unqualified-id.
2344 // Parse that unqualified-id.
2345 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2346 /*EnteringContext=*/true,
2347 /*AllowDestructorName=*/true,
2348 /*AllowConstructorName=*/!D.getDeclSpec().hasTypeSpecifier(),
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002349 /*ObjectType=*/0,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002350 D.getName())) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002351 D.SetIdentifier(0, Tok.getLocation());
2352 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002353 } else {
2354 // Parsed the unqualified-id; update range information and move along.
2355 if (D.getSourceRange().getBegin().isInvalid())
2356 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2357 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002358 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002359 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002360 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002361 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002362 assert(!getLang().CPlusPlus &&
2363 "There's a C++-specific check for tok::identifier above");
2364 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2365 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2366 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002367 goto PastIdentifier;
2368 }
2369
2370 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002371 // direct-declarator: '(' declarator ')'
2372 // direct-declarator: '(' attributes declarator ')'
2373 // Example: 'char (*X)' or 'int (*XX)(void)'
2374 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002375 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002376 // This could be something simple like "int" (in which case the declarator
2377 // portion is empty), if an abstract-declarator is allowed.
2378 D.SetIdentifier(0, Tok.getLocation());
2379 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002380 if (D.getContext() == Declarator::MemberContext)
2381 Diag(Tok, diag::err_expected_member_name_or_semi)
2382 << D.getDeclSpec().getSourceRange();
2383 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002384 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002385 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002386 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002387 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002388 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002389 }
Mike Stump1eb44332009-09-09 15:08:12 +00002390
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002391 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002392 assert(D.isPastIdentifier() &&
2393 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002394
Sean Huntbbd37c62009-11-21 08:43:09 +00002395 // Don't parse attributes unless we have an identifier.
2396 if (D.getIdentifier() && getLang().CPlusPlus
2397 && isCXX0XAttributeSpecifier(true)) {
2398 SourceLocation AttrEndLoc;
2399 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2400 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2401 }
2402
Reid Spencer5f016e22007-07-11 17:01:13 +00002403 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002404 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002405 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2406 // In such a case, check if we actually have a function declarator; if it
2407 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002408 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2409 // When not in file scope, warn for ambiguous function declarators, just
2410 // in case the author intended it as a variable definition.
2411 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2412 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2413 break;
2414 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002415 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002416 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002417 ParseBracketDeclarator(D);
2418 } else {
2419 break;
2420 }
2421 }
2422}
2423
Chris Lattneref4715c2008-04-06 05:45:57 +00002424/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2425/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002426/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002427/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2428///
2429/// direct-declarator:
2430/// '(' declarator ')'
2431/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002432/// direct-declarator '(' parameter-type-list ')'
2433/// direct-declarator '(' identifier-list[opt] ')'
2434/// [GNU] direct-declarator '(' parameter-forward-declarations
2435/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002436///
2437void Parser::ParseParenDeclarator(Declarator &D) {
2438 SourceLocation StartLoc = ConsumeParen();
2439 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002440
Chris Lattner7399ee02008-10-20 02:05:46 +00002441 // Eat any attributes before we look at whether this is a grouping or function
2442 // declarator paren. If this is a grouping paren, the attribute applies to
2443 // the type being built up, for example:
2444 // int (__attribute__(()) *x)(long y)
2445 // If this ends up not being a grouping paren, the attribute applies to the
2446 // first argument, for example:
2447 // int (__attribute__(()) int x)
2448 // In either case, we need to eat any attributes to be able to determine what
2449 // sort of paren this is.
2450 //
2451 AttributeList *AttrList = 0;
2452 bool RequiresArg = false;
2453 if (Tok.is(tok::kw___attribute)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00002454 AttrList = ParseGNUAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00002455
Chris Lattner7399ee02008-10-20 02:05:46 +00002456 // We require that the argument list (if this is a non-grouping paren) be
2457 // present even if the attribute list was empty.
2458 RequiresArg = true;
2459 }
Steve Naroff239f0732008-12-25 14:16:32 +00002460 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002461 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2462 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2463 Tok.is(tok::kw___ptr64)) {
2464 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2465 }
Mike Stump1eb44332009-09-09 15:08:12 +00002466
Chris Lattneref4715c2008-04-06 05:45:57 +00002467 // If we haven't past the identifier yet (or where the identifier would be
2468 // stored, if this is an abstract declarator), then this is probably just
2469 // grouping parens. However, if this could be an abstract-declarator, then
2470 // this could also be the start of function arguments (consider 'void()').
2471 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002472
Chris Lattneref4715c2008-04-06 05:45:57 +00002473 if (!D.mayOmitIdentifier()) {
2474 // If this can't be an abstract-declarator, this *must* be a grouping
2475 // paren, because we haven't seen the identifier yet.
2476 isGrouping = true;
2477 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002478 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002479 isDeclarationSpecifier()) { // 'int(int)' is a function.
2480 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2481 // considered to be a type, not a K&R identifier-list.
2482 isGrouping = false;
2483 } else {
2484 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2485 isGrouping = true;
2486 }
Mike Stump1eb44332009-09-09 15:08:12 +00002487
Chris Lattneref4715c2008-04-06 05:45:57 +00002488 // If this is a grouping paren, handle:
2489 // direct-declarator: '(' declarator ')'
2490 // direct-declarator: '(' attributes declarator ')'
2491 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002492 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002493 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002494 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002495 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002496
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002497 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002498 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002499 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002500
2501 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002502 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002503 return;
2504 }
Mike Stump1eb44332009-09-09 15:08:12 +00002505
Chris Lattneref4715c2008-04-06 05:45:57 +00002506 // Okay, if this wasn't a grouping paren, it must be the start of a function
2507 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002508 // identifier (and remember where it would have been), then call into
2509 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002510 D.SetIdentifier(0, Tok.getLocation());
2511
Chris Lattner7399ee02008-10-20 02:05:46 +00002512 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002513}
2514
2515/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2516/// declarator D up to a paren, which indicates that we are parsing function
2517/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002518///
Chris Lattner7399ee02008-10-20 02:05:46 +00002519/// If AttrList is non-null, then the caller parsed those arguments immediately
2520/// after the open paren - they should be considered to be the first argument of
2521/// a parameter. If RequiresArg is true, then the first argument of the
2522/// function is required to be present and required to not be an identifier
2523/// list.
2524///
Reid Spencer5f016e22007-07-11 17:01:13 +00002525/// This method also handles this portion of the grammar:
2526/// parameter-type-list: [C99 6.7.5]
2527/// parameter-list
2528/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002529/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002530///
2531/// parameter-list: [C99 6.7.5]
2532/// parameter-declaration
2533/// parameter-list ',' parameter-declaration
2534///
2535/// parameter-declaration: [C99 6.7.5]
2536/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002537/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002538/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002539/// declaration-specifiers abstract-declarator[opt]
2540/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002541/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002542/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2543///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002544/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002545/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002546///
Chris Lattner7399ee02008-10-20 02:05:46 +00002547void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2548 AttributeList *AttrList,
2549 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002550 // lparen is already consumed!
2551 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002552
Chris Lattner7399ee02008-10-20 02:05:46 +00002553 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002554 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002555 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002556 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002557 delete AttrList;
2558 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002559
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002560 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2561 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002562
2563 // cv-qualifier-seq[opt].
2564 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002565 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002566 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002567 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002568 llvm::SmallVector<TypeTy*, 2> Exceptions;
2569 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002570 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002571 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002572 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002573 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002574
2575 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002576 if (Tok.is(tok::kw_throw)) {
2577 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002578 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002579 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002580 hasAnyExceptionSpec);
2581 assert(Exceptions.size() == ExceptionRanges.size() &&
2582 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002583 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002584 }
2585
Chris Lattnerf97409f2008-04-06 06:57:35 +00002586 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002587 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002588 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002589 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002590 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002591 /*arglist*/ 0, 0,
2592 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002593 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002594 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002595 Exceptions.data(),
2596 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002597 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002598 LParenLoc, RParenLoc, D),
2599 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002600 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002601 }
2602
Chris Lattner7399ee02008-10-20 02:05:46 +00002603 // Alternatively, this parameter list may be an identifier list form for a
2604 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002605 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002606 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002607 // K&R identifier lists can't have typedefs as identifiers, per
2608 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002609 if (RequiresArg) {
2610 Diag(Tok, diag::err_argument_required_after_attribute);
2611 delete AttrList;
2612 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002613 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2614 // normal declarators, not for abstract-declarators.
2615 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002616 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002617 }
Mike Stump1eb44332009-09-09 15:08:12 +00002618
Chris Lattnerf97409f2008-04-06 06:57:35 +00002619 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002620
Chris Lattnerf97409f2008-04-06 06:57:35 +00002621 // Build up an array of information about the parsed arguments.
2622 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002623
2624 // Enter function-declaration scope, limiting any declarators to the
2625 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002626 ParseScope PrototypeScope(this,
2627 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002628
Chris Lattnerf97409f2008-04-06 06:57:35 +00002629 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002630 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002631 while (1) {
2632 if (Tok.is(tok::ellipsis)) {
2633 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002634 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002635 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002636 }
Mike Stump1eb44332009-09-09 15:08:12 +00002637
Chris Lattnerf97409f2008-04-06 06:57:35 +00002638 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002639
Chris Lattnerf97409f2008-04-06 06:57:35 +00002640 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00002641 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002642 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002643
2644 // If the caller parsed attributes for the first argument, add them now.
2645 if (AttrList) {
2646 DS.AddAttributes(AttrList);
2647 AttrList = 0; // Only apply the attributes to the first parameter.
2648 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002649 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002650
Chris Lattnerf97409f2008-04-06 06:57:35 +00002651 // Parse the declarator. This is "PrototypeContext", because we must
2652 // accept either 'declarator' or 'abstract-declarator' here.
2653 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2654 ParseDeclarator(ParmDecl);
2655
2656 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002657 if (Tok.is(tok::kw___attribute)) {
2658 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00002659 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002660 ParmDecl.AddAttributes(AttrList, Loc);
2661 }
Mike Stump1eb44332009-09-09 15:08:12 +00002662
Chris Lattnerf97409f2008-04-06 06:57:35 +00002663 // Remember this parsed parameter in ParamInfo.
2664 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002665
Douglas Gregor72b505b2008-12-16 21:30:33 +00002666 // DefArgToks is used when the parsing of default arguments needs
2667 // to be delayed.
2668 CachedTokens *DefArgToks = 0;
2669
Chris Lattnerf97409f2008-04-06 06:57:35 +00002670 // If no parameter was specified, verify that *something* was specified,
2671 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002672 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2673 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002674 // Completely missing, emit error.
2675 Diag(DSStart, diag::err_missing_param);
2676 } else {
2677 // Otherwise, we have something. Add it and let semantic analysis try
2678 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00002679
Chris Lattnerf97409f2008-04-06 06:57:35 +00002680 // Inform the actions module about the parameter declarator, so it gets
2681 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002682 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002683
2684 // Parse the default argument, if any. We parse the default
2685 // arguments in all dialects; the semantic analysis in
2686 // ActOnParamDefaultArgument will reject the default argument in
2687 // C.
2688 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002689 SourceLocation EqualLoc = Tok.getLocation();
2690
Chris Lattner04421082008-04-08 04:40:51 +00002691 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002692 if (D.getContext() == Declarator::MemberContext) {
2693 // If we're inside a class definition, cache the tokens
2694 // corresponding to the default argument. We'll actually parse
2695 // them when we see the end of the class definition.
2696 // FIXME: Templates will require something similar.
2697 // FIXME: Can we use a smart pointer for Toks?
2698 DefArgToks = new CachedTokens;
2699
Mike Stump1eb44332009-09-09 15:08:12 +00002700 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002701 tok::semi, false)) {
2702 delete DefArgToks;
2703 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002704 Actions.ActOnParamDefaultArgumentError(Param);
2705 } else
Mike Stump1eb44332009-09-09 15:08:12 +00002706 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00002707 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002708 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002709 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002710 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002711
Douglas Gregor72b505b2008-12-16 21:30:33 +00002712 OwningExprResult DefArgResult(ParseAssignmentExpression());
2713 if (DefArgResult.isInvalid()) {
2714 Actions.ActOnParamDefaultArgumentError(Param);
2715 SkipUntil(tok::comma, tok::r_paren, true, true);
2716 } else {
2717 // Inform the actions module about the default argument
2718 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002719 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002720 }
Chris Lattner04421082008-04-08 04:40:51 +00002721 }
2722 }
Mike Stump1eb44332009-09-09 15:08:12 +00002723
2724 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2725 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002726 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002727 }
2728
2729 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00002730 if (Tok.isNot(tok::comma)) {
2731 if (Tok.is(tok::ellipsis)) {
2732 IsVariadic = true;
2733 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2734
2735 if (!getLang().CPlusPlus) {
2736 // We have ellipsis without a preceding ',', which is ill-formed
2737 // in C. Complain and provide the fix.
2738 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2739 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2740 }
2741 }
2742
2743 break;
2744 }
Mike Stump1eb44332009-09-09 15:08:12 +00002745
Chris Lattnerf97409f2008-04-06 06:57:35 +00002746 // Consume the comma.
2747 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002748 }
Mike Stump1eb44332009-09-09 15:08:12 +00002749
Chris Lattnerf97409f2008-04-06 06:57:35 +00002750 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002751 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00002752
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002753 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002754 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2755 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002756
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002757 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002758 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002759 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002760 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002761 llvm::SmallVector<TypeTy*, 2> Exceptions;
2762 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00002763
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002764 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002765 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002766 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002767 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002768 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002769
2770 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002771 if (Tok.is(tok::kw_throw)) {
2772 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002773 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002774 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002775 hasAnyExceptionSpec);
2776 assert(Exceptions.size() == ExceptionRanges.size() &&
2777 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002778 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002779 }
2780
Reid Spencer5f016e22007-07-11 17:01:13 +00002781 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002782 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002783 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002784 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002785 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002786 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002787 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002788 Exceptions.data(),
2789 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002790 Exceptions.size(),
2791 LParenLoc, RParenLoc, D),
2792 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002793}
2794
Chris Lattner66d28652008-04-06 06:34:08 +00002795/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2796/// we found a K&R-style identifier list instead of a type argument list. The
2797/// current token is known to be the first identifier in the list.
2798///
2799/// identifier-list: [C99 6.7.5]
2800/// identifier
2801/// identifier-list ',' identifier
2802///
2803void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2804 Declarator &D) {
2805 // Build up an array of information about the parsed arguments.
2806 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2807 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00002808
Chris Lattner66d28652008-04-06 06:34:08 +00002809 // If there was no identifier specified for the declarator, either we are in
2810 // an abstract-declarator, or we are in a parameter declarator which was found
2811 // to be abstract. In abstract-declarators, identifier lists are not valid:
2812 // diagnose this.
2813 if (!D.getIdentifier())
2814 Diag(Tok, diag::ext_ident_list_in_param);
2815
2816 // Tok is known to be the first identifier in the list. Remember this
2817 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002818 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002819 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002820 Tok.getLocation(),
2821 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00002822
Chris Lattner50c64772008-04-06 06:39:19 +00002823 ConsumeToken(); // eat the first identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00002824
Chris Lattner66d28652008-04-06 06:34:08 +00002825 while (Tok.is(tok::comma)) {
2826 // Eat the comma.
2827 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002828
Chris Lattner50c64772008-04-06 06:39:19 +00002829 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002830 if (Tok.isNot(tok::identifier)) {
2831 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002832 SkipUntil(tok::r_paren);
2833 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002834 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002835
Chris Lattner66d28652008-04-06 06:34:08 +00002836 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002837
2838 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002839 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002840 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00002841
Chris Lattner66d28652008-04-06 06:34:08 +00002842 // Verify that the argument identifier has not already been mentioned.
2843 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002844 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002845 } else {
2846 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002847 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002848 Tok.getLocation(),
2849 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002850 }
Mike Stump1eb44332009-09-09 15:08:12 +00002851
Chris Lattner66d28652008-04-06 06:34:08 +00002852 // Eat the identifier.
2853 ConsumeToken();
2854 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002855
2856 // If we have the closing ')', eat it and we're done.
2857 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2858
Chris Lattner50c64772008-04-06 06:39:19 +00002859 // Remember that we parsed a function type, and remember the attributes. This
2860 // function type is always a K&R style function type, which is not varargs and
2861 // has no prototype.
2862 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002863 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002864 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002865 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00002866 /*exception*/false,
2867 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002868 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002869 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002870}
Chris Lattneref4715c2008-04-06 05:45:57 +00002871
Reid Spencer5f016e22007-07-11 17:01:13 +00002872/// [C90] direct-declarator '[' constant-expression[opt] ']'
2873/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2874/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2875/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2876/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2877void Parser::ParseBracketDeclarator(Declarator &D) {
2878 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00002879
Chris Lattner378c7e42008-12-18 07:27:21 +00002880 // C array syntax has many features, but by-far the most common is [] and [4].
2881 // This code does a fast path to handle some of the most obvious cases.
2882 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002883 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002884 //FIXME: Use these
2885 CXX0XAttributeList Attr;
2886 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
2887 Attr = ParseCXX0XAttributes();
2888 }
2889
Chris Lattner378c7e42008-12-18 07:27:21 +00002890 // Remember that we parsed the empty array type.
2891 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002892 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2893 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002894 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002895 return;
2896 } else if (Tok.getKind() == tok::numeric_constant &&
2897 GetLookAheadToken(1).is(tok::r_square)) {
2898 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002899 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002900 ConsumeToken();
2901
Sebastian Redlab197ba2009-02-09 18:23:29 +00002902 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00002903 //FIXME: Use these
2904 CXX0XAttributeList Attr;
2905 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2906 Attr = ParseCXX0XAttributes();
2907 }
Chris Lattner378c7e42008-12-18 07:27:21 +00002908
2909 // If there was an error parsing the assignment-expression, recover.
2910 if (ExprRes.isInvalid())
2911 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00002912
Chris Lattner378c7e42008-12-18 07:27:21 +00002913 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002914 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2915 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002916 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002917 return;
2918 }
Mike Stump1eb44332009-09-09 15:08:12 +00002919
Reid Spencer5f016e22007-07-11 17:01:13 +00002920 // If valid, this location is the position where we read the 'static' keyword.
2921 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002922 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002923 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002924
Reid Spencer5f016e22007-07-11 17:01:13 +00002925 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002926 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002927 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002928 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00002929
Reid Spencer5f016e22007-07-11 17:01:13 +00002930 // If we haven't already read 'static', check to see if there is one after the
2931 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002932 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002933 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002934
Reid Spencer5f016e22007-07-11 17:01:13 +00002935 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2936 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002937 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00002938
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002939 // Handle the case where we have '[*]' as the array size. However, a leading
2940 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2941 // the the token after the star is a ']'. Since stars in arrays are
2942 // infrequent, use of lookahead is not costly here.
2943 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002944 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002945
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002946 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002947 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002948 StaticLoc = SourceLocation(); // Drop the static.
2949 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002950 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002951 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002952 // Note, in C89, this production uses the constant-expr production instead
2953 // of assignment-expr. The only difference is that assignment-expr allows
2954 // things like '=' and '*='. Sema rejects these in C89 mode because they
2955 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00002956
Douglas Gregore0762c92009-06-19 23:52:42 +00002957 // Parse the constant-expression or assignment-expression now (depending
2958 // on dialect).
2959 if (getLang().CPlusPlus)
2960 NumElements = ParseConstantExpression();
2961 else
2962 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00002963 }
Mike Stump1eb44332009-09-09 15:08:12 +00002964
Reid Spencer5f016e22007-07-11 17:01:13 +00002965 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002966 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002967 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002968 // If the expression was invalid, skip it.
2969 SkipUntil(tok::r_square);
2970 return;
2971 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002972
2973 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2974
Sean Huntbbd37c62009-11-21 08:43:09 +00002975 //FIXME: Use these
2976 CXX0XAttributeList Attr;
2977 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2978 Attr = ParseCXX0XAttributes();
2979 }
2980
Chris Lattner378c7e42008-12-18 07:27:21 +00002981 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002982 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2983 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002984 NumElements.release(),
2985 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002986 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002987}
2988
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002989/// [GNU] typeof-specifier:
2990/// typeof ( expressions )
2991/// typeof ( type-name )
2992/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002993///
2994void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002995 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002996 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002997 SourceLocation StartLoc = ConsumeToken();
2998
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002999 bool isCastExpr;
3000 TypeTy *CastTy;
3001 SourceRange CastRange;
3002 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3003 isCastExpr,
3004 CastTy,
3005 CastRange);
3006
3007 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003008 // FIXME: Not accurate, the range gets one token more than it should.
3009 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003010 else
3011 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003012
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003013 if (isCastExpr) {
3014 if (!CastTy) {
3015 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003016 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003017 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003018
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003019 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003020 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003021 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3022 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003023 DiagID, CastTy))
3024 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003025 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003026 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003027
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003028 // If we get here, the operand to the typeof was an expresion.
3029 if (Operand.isInvalid()) {
3030 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003031 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003032 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003033
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003034 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003035 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003036 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3037 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003038 DiagID, Operand.release()))
3039 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003040}