blob: 2bfda30a951b40d24c59dad51ec60b1285817d04 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000018#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redlef65f062009-05-29 18:02:33 +000031Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000035
Reid Spencer5f016e22007-07-11 17:01:13 +000036 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000039 if (Range)
40 *Range = DeclaratorInfo.getSourceRange();
41
Chris Lattnereaaebc72009-04-25 08:06:05 +000042 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000043 return true;
44
45 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000046}
47
48/// ParseAttributes - Parse a non-empty attributes list.
49///
50/// [GNU] attributes:
51/// attribute
52/// attributes attribute
53///
54/// [GNU] attribute:
55/// '__attribute__' '(' '(' attribute-list ')' ')'
56///
57/// [GNU] attribute-list:
58/// attrib
59/// attribute_list ',' attrib
60///
61/// [GNU] attrib:
62/// empty
63/// attrib-name
64/// attrib-name '(' identifier ')'
65/// attrib-name '(' identifier ',' nonempty-expr-list ')'
66/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
67///
68/// [GNU] attrib-name:
69/// identifier
70/// typespec
71/// typequal
72/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000073///
Reid Spencer5f016e22007-07-11 17:01:13 +000074/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000075/// token lookahead. Comment from gcc: "If they start with an identifier
76/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000077/// start with that identifier; otherwise they are an expression list."
78///
79/// At the moment, I am not doing 2 token lookahead. I am also unaware of
80/// any attributes that don't work (based on my limited testing). Most
81/// attributes are very simple in practice. Until we find a bug, I don't see
82/// a pressing need to implement the 2 token lookahead.
83
Sebastian Redlab197ba2009-02-09 18:23:29 +000084AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000085 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087 AttributeList *CurrAttr = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner04d66662007-10-09 17:33:22 +000089 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000090 ConsumeToken();
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
92 "attribute")) {
93 SkipUntil(tok::r_paren, true); // skip until ) or ;
94 return CurrAttr;
95 }
96 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
97 SkipUntil(tok::r_paren, true); // skip until ) or ;
98 return CurrAttr;
99 }
100 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000101 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
102 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000103
104 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
106 ConsumeToken();
107 continue;
108 }
109 // we have an identifier or declaration specifier (const, int, etc.)
110 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
111 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000114 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattner04d66662007-10-09 17:33:22 +0000117 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
119 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000120
121 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 // __attribute__(( mode(byte) ))
123 ConsumeParen(); // ignore the right paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000124 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000126 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 ConsumeToken();
128 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000129 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 // now parse the non-empty comma separated list of expressions
133 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000134 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000135 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 ArgExprsOk = false;
137 SkipUntil(tok::r_paren);
138 break;
139 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000140 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 }
Chris Lattner04d66662007-10-09 17:33:22 +0000142 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 break;
144 ConsumeToken(); // Eat the comma, move to the next argument
145 }
Chris Lattner04d66662007-10-09 17:33:22 +0000146 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 ConsumeParen(); // ignore the right paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000148 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000149 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 }
151 }
152 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000153 switch (Tok.getKind()) {
154 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000156 // __attribute__(( nonnull() ))
157 ConsumeParen(); // ignore the right paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000158 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000160 break;
161 case tok::kw_char:
162 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000163 case tok::kw_char16_t:
164 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000165 case tok::kw_bool:
166 case tok::kw_short:
167 case tok::kw_int:
168 case tok::kw_long:
169 case tok::kw_signed:
170 case tok::kw_unsigned:
171 case tok::kw_float:
172 case tok::kw_double:
173 case tok::kw_void:
174 case tok::kw_typeof:
175 // If it's a builtin type name, eat it and expect a rparen
176 // __attribute__(( vec_type_hint(char) ))
177 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000178 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Nate Begeman6f3d8382009-06-26 06:32:41 +0000179 0, SourceLocation(), 0, 0, CurrAttr);
180 if (Tok.is(tok::r_paren))
181 ConsumeParen();
182 break;
183 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000185 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 // now parse the list of expressions
189 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000190 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000191 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 ArgExprsOk = false;
193 SkipUntil(tok::r_paren);
194 break;
195 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000196 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 }
Chris Lattner04d66662007-10-09 17:33:22 +0000198 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 break;
200 ConsumeToken(); // Eat the comma, move to the next argument
201 }
202 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000203 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000205 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
206 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 CurrAttr);
208 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000209 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 }
211 }
212 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000213 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 0, SourceLocation(), 0, 0, CurrAttr);
215 }
216 }
217 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 SkipUntil(tok::r_paren, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000219 SourceLocation Loc = Tok.getLocation();;
220 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
221 SkipUntil(tok::r_paren, false);
222 }
223 if (EndLoc)
224 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000225 }
226 return CurrAttr;
227}
228
Eli Friedmana23b4852009-06-08 07:21:15 +0000229/// ParseMicrosoftDeclSpec - Parse an __declspec construct
230///
231/// [MS] decl-specifier:
232/// __declspec ( extended-decl-modifier-seq )
233///
234/// [MS] extended-decl-modifier-seq:
235/// extended-decl-modifier[opt]
236/// extended-decl-modifier extended-decl-modifier-seq
237
Eli Friedman290eeb02009-06-08 23:27:34 +0000238AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000239 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000240
Steve Narofff59e17e2008-12-24 20:59:21 +0000241 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000242 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
243 "declspec")) {
244 SkipUntil(tok::r_paren, true); // skip until ) or ;
245 return CurrAttr;
246 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000247 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000248 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
249 SourceLocation AttrNameLoc = ConsumeToken();
250 if (Tok.is(tok::l_paren)) {
251 ConsumeParen();
252 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
253 // correctly.
254 OwningExprResult ArgExpr(ParseAssignmentExpression());
255 if (!ArgExpr.isInvalid()) {
256 ExprTy* ExprList = ArgExpr.take();
257 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
258 SourceLocation(), &ExprList, 1,
259 CurrAttr, true);
260 }
261 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
262 SkipUntil(tok::r_paren, false);
263 } else {
264 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, SourceLocation(),
265 0, 0, CurrAttr, true);
266 }
267 }
268 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
269 SkipUntil(tok::r_paren, false);
Eli Friedman290eeb02009-06-08 23:27:34 +0000270 return CurrAttr;
271}
272
273AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
274 // Treat these like attributes
275 // FIXME: Allow Sema to distinguish between these and real attributes!
276 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
277 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) ||
278 Tok.is(tok::kw___w64)) {
279 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
280 SourceLocation AttrNameLoc = ConsumeToken();
281 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
282 // FIXME: Support these properly!
283 continue;
284 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
285 SourceLocation(), 0, 0, CurrAttr, true);
286 }
287 return CurrAttr;
Steve Narofff59e17e2008-12-24 20:59:21 +0000288}
289
Reid Spencer5f016e22007-07-11 17:01:13 +0000290/// ParseDeclaration - Parse a full 'declaration', which consists of
291/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000292/// 'Context' should be a Declarator::TheContext value. This returns the
293/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000294///
295/// declaration: [C99 6.7]
296/// block-declaration ->
297/// simple-declaration
298/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000299/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000300/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000301/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000302/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000303/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000304/// others... [FIXME]
305///
Chris Lattner97144fc2009-04-02 04:16:50 +0000306Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
307 SourceLocation &DeclEnd) {
Chris Lattner682bf922009-03-29 16:50:03 +0000308 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000309 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000310 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000311 case tok::kw_export:
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000312 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000313 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000314 case tok::kw_namespace:
Chris Lattner97144fc2009-04-02 04:16:50 +0000315 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000316 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000317 case tok::kw_using:
Chris Lattner97144fc2009-04-02 04:16:50 +0000318 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000319 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000320 case tok::kw_static_assert:
Chris Lattner97144fc2009-04-02 04:16:50 +0000321 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000322 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000323 default:
Chris Lattner97144fc2009-04-02 04:16:50 +0000324 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000325 }
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Chris Lattner682bf922009-03-29 16:50:03 +0000327 // This routine returns a DeclGroup, if the thing we parsed only contains a
328 // single decl, convert it now.
329 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000330}
331
332/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
333/// declaration-specifiers init-declarator-list[opt] ';'
334///[C90/C++]init-declarator-list ';' [TODO]
335/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000336///
337/// If RequireSemi is false, this does not check for a ';' at the end of the
338/// declaration.
339Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000340 SourceLocation &DeclEnd) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000342 ParsingDeclSpec DS(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
346 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000347 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000349 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall54abf7d2009-11-04 02:18:39 +0000350 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000351 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000352 }
Mike Stump1eb44332009-09-09 15:08:12 +0000353
John McCalld8ac0572009-11-03 19:26:08 +0000354 DeclGroupPtrTy DG = ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false,
355 &DeclEnd);
356 return DG;
357}
Mike Stump1eb44332009-09-09 15:08:12 +0000358
John McCalld8ac0572009-11-03 19:26:08 +0000359/// ParseDeclGroup - Having concluded that this is either a function
360/// definition or a group of object declarations, actually parse the
361/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000362Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
363 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000364 bool AllowFunctionDefinitions,
365 SourceLocation *DeclEnd) {
366 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000367 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000368 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000369
John McCalld8ac0572009-11-03 19:26:08 +0000370 // Bail out if the first declarator didn't seem well-formed.
371 if (!D.hasName() && !D.mayOmitIdentifier()) {
372 // Skip until ; or }.
373 SkipUntil(tok::r_brace, true, true);
374 if (Tok.is(tok::semi))
375 ConsumeToken();
376 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000377 }
Mike Stump1eb44332009-09-09 15:08:12 +0000378
John McCalld8ac0572009-11-03 19:26:08 +0000379 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
380 if (isDeclarationAfterDeclarator()) {
381 // Fall though. We have to check this first, though, because
382 // __attribute__ might be the start of a function definition in
383 // (extended) K&R C.
384 } else if (isStartOfFunctionDefinition()) {
385 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
386 Diag(Tok, diag::err_function_declared_typedef);
387
388 // Recover by treating the 'typedef' as spurious.
389 DS.ClearStorageClassSpecs();
390 }
391
392 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
393 return Actions.ConvertDeclToDeclGroup(TheDecl);
394 } else {
395 Diag(Tok, diag::err_expected_fn_body);
396 SkipUntil(tok::semi);
397 return DeclGroupPtrTy();
398 }
399 }
400
401 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
402 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000403 D.complete(FirstDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000404 if (FirstDecl.get())
405 DeclsInGroup.push_back(FirstDecl);
406
407 // If we don't have a comma, it is either the end of the list (a ';') or an
408 // error, bail out.
409 while (Tok.is(tok::comma)) {
410 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000411 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000412
413 // Parse the next declarator.
414 D.clear();
415
416 // Accept attributes in an init-declarator. In the first declarator in a
417 // declaration, these would be part of the declspec. In subsequent
418 // declarators, they become part of the declarator itself, so that they
419 // don't apply to declarators after *this* one. Examples:
420 // short __attribute__((common)) var; -> declspec
421 // short var __attribute__((common)); -> declarator
422 // short x, __attribute__((common)) var; -> declarator
423 if (Tok.is(tok::kw___attribute)) {
424 SourceLocation Loc;
425 AttributeList *AttrList = ParseAttributes(&Loc);
426 D.AddAttributes(AttrList, Loc);
427 }
428
429 ParseDeclarator(D);
430
431 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000432 D.complete(ThisDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000433 if (ThisDecl.get())
434 DeclsInGroup.push_back(ThisDecl);
435 }
436
437 if (DeclEnd)
438 *DeclEnd = Tok.getLocation();
439
440 if (Context != Declarator::ForContext &&
441 ExpectAndConsume(tok::semi,
442 Context == Declarator::FileContext
443 ? diag::err_invalid_token_after_toplevel_declarator
444 : diag::err_expected_semi_declaration)) {
445 SkipUntil(tok::r_brace, true, true);
446 if (Tok.is(tok::semi))
447 ConsumeToken();
448 }
449
450 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
451 DeclsInGroup.data(),
452 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000453}
454
Douglas Gregor1426e532009-05-12 21:31:51 +0000455/// \brief Parse 'declaration' after parsing 'declaration-specifiers
456/// declarator'. This method parses the remainder of the declaration
457/// (including any attributes or initializer, among other things) and
458/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000459///
Reid Spencer5f016e22007-07-11 17:01:13 +0000460/// init-declarator: [C99 6.7]
461/// declarator
462/// declarator '=' initializer
463/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
464/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000465/// [C++] declarator initializer[opt]
466///
467/// [C++] initializer:
468/// [C++] '=' initializer-clause
469/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000470/// [C++0x] '=' 'default' [TODO]
471/// [C++0x] '=' 'delete'
472///
473/// According to the standard grammar, =default and =delete are function
474/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000475///
Douglas Gregore542c862009-06-23 23:11:28 +0000476Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
477 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000478 // If a simple-asm-expr is present, parse it.
479 if (Tok.is(tok::kw_asm)) {
480 SourceLocation Loc;
481 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
482 if (AsmLabel.isInvalid()) {
483 SkipUntil(tok::semi, true, true);
484 return DeclPtrTy();
485 }
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Douglas Gregor1426e532009-05-12 21:31:51 +0000487 D.setAsmLabel(AsmLabel.release());
488 D.SetRangeEnd(Loc);
489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Douglas Gregor1426e532009-05-12 21:31:51 +0000491 // If attributes are present, parse them.
492 if (Tok.is(tok::kw___attribute)) {
493 SourceLocation Loc;
494 AttributeList *AttrList = ParseAttributes(&Loc);
495 D.AddAttributes(AttrList, Loc);
496 }
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Douglas Gregor1426e532009-05-12 21:31:51 +0000498 // Inform the current actions module that we just parsed this declarator.
Douglas Gregord5a423b2009-09-25 18:43:00 +0000499 DeclPtrTy ThisDecl;
500 switch (TemplateInfo.Kind) {
501 case ParsedTemplateInfo::NonTemplate:
502 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
503 break;
504
505 case ParsedTemplateInfo::Template:
506 case ParsedTemplateInfo::ExplicitSpecialization:
507 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregore542c862009-06-23 23:11:28 +0000508 Action::MultiTemplateParamsArg(Actions,
509 TemplateInfo.TemplateParams->data(),
510 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000511 D);
512 break;
513
514 case ParsedTemplateInfo::ExplicitInstantiation: {
515 Action::DeclResult ThisRes
516 = Actions.ActOnExplicitInstantiation(CurScope,
517 TemplateInfo.ExternLoc,
518 TemplateInfo.TemplateLoc,
519 D);
520 if (ThisRes.isInvalid()) {
521 SkipUntil(tok::semi, true, true);
522 return DeclPtrTy();
523 }
524
525 ThisDecl = ThisRes.get();
526 break;
527 }
528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Douglas Gregor1426e532009-05-12 21:31:51 +0000530 // Parse declarator '=' initializer.
531 if (Tok.is(tok::equal)) {
532 ConsumeToken();
533 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
534 SourceLocation DelLoc = ConsumeToken();
535 Actions.SetDeclDeleted(ThisDecl, DelLoc);
536 } else {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000537 if (getLang().CPlusPlus)
538 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
539
Douglas Gregor1426e532009-05-12 21:31:51 +0000540 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000541
542 if (getLang().CPlusPlus)
543 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
544
Douglas Gregor1426e532009-05-12 21:31:51 +0000545 if (Init.isInvalid()) {
546 SkipUntil(tok::semi, true, true);
547 return DeclPtrTy();
548 }
Anders Carlsson9abf2ae2009-08-16 05:13:48 +0000549 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000550 }
551 } else if (Tok.is(tok::l_paren)) {
552 // Parse C++ direct initializer: '(' expression-list ')'
553 SourceLocation LParenLoc = ConsumeParen();
554 ExprVector Exprs(Actions);
555 CommaLocsTy CommaLocs;
556
557 if (ParseExpressionList(Exprs, CommaLocs)) {
558 SkipUntil(tok::r_paren);
559 } else {
560 // Match the ')'.
561 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
562
563 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
564 "Unexpected number of commas!");
565 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
566 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000567 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000568 }
569 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000570 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000571 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
572 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000573 }
574
575 return ThisDecl;
576}
577
Reid Spencer5f016e22007-07-11 17:01:13 +0000578/// ParseSpecifierQualifierList
579/// specifier-qualifier-list:
580/// type-specifier specifier-qualifier-list[opt]
581/// type-qualifier specifier-qualifier-list[opt]
582/// [GNU] attributes specifier-qualifier-list[opt]
583///
584void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
585 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
586 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000587 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000588
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 // Validate declspec for type-name.
590 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000591 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
592 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 // Issue diagnostic and remove storage class if present.
596 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
597 if (DS.getStorageClassSpecLoc().isValid())
598 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
599 else
600 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
601 DS.ClearStorageClassSpecs();
602 }
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 // Issue diagnostic and remove function specfier if present.
605 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000606 if (DS.isInlineSpecified())
607 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
608 if (DS.isVirtualSpecified())
609 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
610 if (DS.isExplicitSpecified())
611 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 DS.ClearFunctionSpecs();
613 }
614}
615
Chris Lattnerc199ab32009-04-12 20:42:31 +0000616/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
617/// specified token is valid after the identifier in a declarator which
618/// immediately follows the declspec. For example, these things are valid:
619///
620/// int x [ 4]; // direct-declarator
621/// int x ( int y); // direct-declarator
622/// int(int x ) // direct-declarator
623/// int x ; // simple-declaration
624/// int x = 17; // init-declarator-list
625/// int x , y; // init-declarator-list
626/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000627/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000628/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000629///
630/// This is not, because 'x' does not immediately follow the declspec (though
631/// ')' happens to be valid anyway).
632/// int (x)
633///
634static bool isValidAfterIdentifierInDeclarator(const Token &T) {
635 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
636 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000637 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000638}
639
Chris Lattnere40c2952009-04-14 21:34:55 +0000640
641/// ParseImplicitInt - This method is called when we have an non-typename
642/// identifier in a declspec (which normally terminates the decl spec) when
643/// the declspec has no type specifier. In this case, the declspec is either
644/// malformed or is "implicit int" (in K&R and C89).
645///
646/// This method handles diagnosing this prettily and returns false if the
647/// declspec is done being processed. If it recovers and thinks there may be
648/// other pieces of declspec after it, it returns true.
649///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000650bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000651 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000652 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000653 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Chris Lattnere40c2952009-04-14 21:34:55 +0000655 SourceLocation Loc = Tok.getLocation();
656 // If we see an identifier that is not a type name, we normally would
657 // parse it as the identifer being declared. However, when a typename
658 // is typo'd or the definition is not included, this will incorrectly
659 // parse the typename as the identifier name and fall over misparsing
660 // later parts of the diagnostic.
661 //
662 // As such, we try to do some look-ahead in cases where this would
663 // otherwise be an "implicit-int" case to see if this is invalid. For
664 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
665 // an identifier with implicit int, we'd get a parse error because the
666 // next token is obviously invalid for a type. Parse these as a case
667 // with an invalid type specifier.
668 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Chris Lattnere40c2952009-04-14 21:34:55 +0000670 // Since we know that this either implicit int (which is rare) or an
671 // error, we'd do lookahead to try to do better recovery.
672 if (isValidAfterIdentifierInDeclarator(NextToken())) {
673 // If this token is valid for implicit int, e.g. "static x = 4", then
674 // we just avoid eating the identifier, so it will be parsed as the
675 // identifier in the declarator.
676 return false;
677 }
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Chris Lattnere40c2952009-04-14 21:34:55 +0000679 // Otherwise, if we don't consume this token, we are going to emit an
680 // error anyway. Try to recover from various common problems. Check
681 // to see if this was a reference to a tag name without a tag specified.
682 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000683 //
684 // C++ doesn't need this, and isTagName doesn't take SS.
685 if (SS == 0) {
686 const char *TagName = 0;
687 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Chris Lattnere40c2952009-04-14 21:34:55 +0000689 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
690 default: break;
691 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
692 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
693 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
694 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
695 }
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Chris Lattnerf4382f52009-04-14 22:17:06 +0000697 if (TagName) {
698 Diag(Loc, diag::err_use_of_tag_name_without_tag)
699 << Tok.getIdentifierInfo() << TagName
700 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Chris Lattnerf4382f52009-04-14 22:17:06 +0000702 // Parse this as a tag as if the missing tag were present.
703 if (TagKind == tok::kw_enum)
704 ParseEnumSpecifier(Loc, DS, AS);
705 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000706 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000707 return true;
708 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000709 }
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Douglas Gregora786fdb2009-10-13 23:27:22 +0000711 // This is almost certainly an invalid type name. Let the action emit a
712 // diagnostic and attempt to recover.
713 Action::TypeTy *T = 0;
714 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
715 CurScope, SS, T)) {
716 // The action emitted a diagnostic, so we don't have to.
717 if (T) {
718 // The action has suggested that the type T could be used. Set that as
719 // the type in the declaration specifiers, consume the would-be type
720 // name token, and we're done.
721 const char *PrevSpec;
722 unsigned DiagID;
723 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
724 false);
725 DS.SetRangeEnd(Tok.getLocation());
726 ConsumeToken();
727
728 // There may be other declaration specifiers after this.
729 return true;
730 }
731
732 // Fall through; the action had no suggestion for us.
733 } else {
734 // The action did not emit a diagnostic, so emit one now.
735 SourceRange R;
736 if (SS) R = SS->getRange();
737 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
738 }
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Douglas Gregora786fdb2009-10-13 23:27:22 +0000740 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000741 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000742 unsigned DiagID;
743 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000744 DS.SetRangeEnd(Tok.getLocation());
745 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Chris Lattnere40c2952009-04-14 21:34:55 +0000747 // TODO: Could inject an invalid typedef decl in an enclosing scope to
748 // avoid rippling error messages on subsequent uses of the same type,
749 // could be useful if #include was forgotten.
750 return false;
751}
752
Reid Spencer5f016e22007-07-11 17:01:13 +0000753/// ParseDeclarationSpecifiers
754/// declaration-specifiers: [C99 6.7]
755/// storage-class-specifier declaration-specifiers[opt]
756/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000757/// [C99] function-specifier declaration-specifiers[opt]
758/// [GNU] attributes declaration-specifiers[opt]
759///
760/// storage-class-specifier: [C99 6.7.1]
761/// 'typedef'
762/// 'extern'
763/// 'static'
764/// 'auto'
765/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000766/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000767/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000768/// function-specifier: [C99 6.7.4]
769/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000770/// [C++] 'virtual'
771/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000772/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000773/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000774
Reid Spencer5f016e22007-07-11 17:01:13 +0000775///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000776void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000777 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000778 AccessSpecifier AS,
779 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000780 if (Tok.is(tok::code_completion)) {
781 Actions.CodeCompleteOrdinaryName(CurScope);
782 ConsumeToken();
783 }
784
Chris Lattner81c018d2008-03-13 06:29:04 +0000785 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000786 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000787 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000789 unsigned DiagID = 0;
790
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000792
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000794 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000795 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000796 // If this is not a declaration specifier token, we're done reading decl
797 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000798 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000799 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Chris Lattner5e02c472009-01-05 00:07:25 +0000801 case tok::coloncolon: // ::foo::bar
802 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000803 if (TryAnnotateCXXScopeToken(true))
Chris Lattner5e02c472009-01-05 00:07:25 +0000804 continue;
805 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000806
807 case tok::annot_cxxscope: {
808 if (DS.hasTypeSpecifier())
809 goto DoneWithDeclSpec;
810
811 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000812 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000813 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000814 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000815 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000816 // We have a qualified template-id, e.g., N::A<int>
817 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000818 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000819 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000820 "ParseOptionalCXXScopeSpecifier not working");
821 AnnotateTemplateIdTokenAsType(&SS);
822 continue;
823 }
824
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000825 if (Next.is(tok::annot_typename)) {
826 // FIXME: is this scope-specifier getting dropped?
827 ConsumeToken(); // the scope-specifier
828 if (Tok.getAnnotationValue())
829 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
830 PrevSpec, DiagID,
831 Tok.getAnnotationValue());
832 else
833 DS.SetTypeSpecError();
834 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
835 ConsumeToken(); // The typename
836 }
837
Douglas Gregor9135c722009-03-25 15:40:00 +0000838 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000839 goto DoneWithDeclSpec;
840
841 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000842 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000843 SS.setRange(Tok.getAnnotationRange());
844
845 // If the next token is the name of the class type that the C++ scope
846 // denotes, followed by a '(', then this is a constructor declaration.
847 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000848 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000849 CurScope, &SS) &&
850 GetLookAheadToken(2).is(tok::l_paren))
851 goto DoneWithDeclSpec;
852
Douglas Gregorb696ea32009-02-04 17:00:24 +0000853 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
854 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000855
Chris Lattnerf4382f52009-04-14 22:17:06 +0000856 // If the referenced identifier is not a type, then this declspec is
857 // erroneous: We already checked about that it has no type specifier, and
858 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000859 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000860 if (TypeRep == 0) {
861 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000862 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000863 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000866 ConsumeToken(); // The C++ scope.
867
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000868 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000869 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000870 if (isInvalid)
871 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000873 DS.SetRangeEnd(Tok.getLocation());
874 ConsumeToken(); // The typename.
875
876 continue;
877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Chris Lattner80d0c892009-01-21 19:48:37 +0000879 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000880 if (Tok.getAnnotationValue())
881 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000882 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +0000883 else
884 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000885 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
886 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +0000887
Chris Lattner80d0c892009-01-21 19:48:37 +0000888 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
889 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
890 // Objective-C interface. If we don't have Objective-C or a '<', this is
891 // just a normal reference to a typedef name.
892 if (!Tok.is(tok::less) || !getLang().ObjC1)
893 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000895 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000896 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000897 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
898 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
899 LAngleLoc, EndProtoLoc);
900 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
901 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Chris Lattner80d0c892009-01-21 19:48:37 +0000903 DS.SetRangeEnd(EndProtoLoc);
904 continue;
905 }
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Chris Lattner3bd934a2008-07-26 01:18:38 +0000907 // typedef-name
908 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000909 // In C++, check to see if this is a scope specifier like foo::bar::, if
910 // so handle it as such. This is important for ctor parsing.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000911 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner837acd02009-01-21 19:19:26 +0000912 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Chris Lattner3bd934a2008-07-26 01:18:38 +0000914 // This identifier can only be a typedef name if we haven't already seen
915 // a type-specifier. Without this check we misparse:
916 // typedef int X; struct Y { short X; }; as 'short int'.
917 if (DS.hasTypeSpecifier())
918 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Chris Lattner3bd934a2008-07-26 01:18:38 +0000920 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +0000921 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +0000922 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000923
Chris Lattnerc199ab32009-04-12 20:42:31 +0000924 // If this is not a typedef name, don't parse it as part of the declspec,
925 // it must be an implicit int or an error.
926 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000927 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000928 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000929 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000930
Douglas Gregorb48fe382008-10-31 09:07:45 +0000931 // C++: If the identifier is actually the name of the class type
932 // being defined and the next token is a '(', then this is a
933 // constructor declaration. We're done with the decl-specifiers
934 // and will treat this token as an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000935 if (getLang().CPlusPlus &&
936 (CurScope->isClassScope() ||
937 (CurScope->isTemplateParamScope() &&
Douglas Gregordec06662009-08-21 18:42:58 +0000938 CurScope->getParent()->isClassScope())) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000939 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000940 NextToken().getKind() == tok::l_paren)
941 goto DoneWithDeclSpec;
942
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000943 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000944 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000945 if (isInvalid)
946 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Chris Lattner3bd934a2008-07-26 01:18:38 +0000948 DS.SetRangeEnd(Tok.getLocation());
949 ConsumeToken(); // The identifier
950
951 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
952 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
953 // Objective-C interface. If we don't have Objective-C or a '<', this is
954 // just a normal reference to a typedef name.
955 if (!Tok.is(tok::less) || !getLang().ObjC1)
956 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000958 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000959 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000960 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
961 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
962 LAngleLoc, EndProtoLoc);
963 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
964 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Chris Lattner3bd934a2008-07-26 01:18:38 +0000966 DS.SetRangeEnd(EndProtoLoc);
967
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000968 // Need to support trailing type qualifiers (e.g. "id<p> const").
969 // If a type specifier follows, it will be diagnosed elsewhere.
970 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000971 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000972
973 // type-name
974 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +0000975 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000976 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000977 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000978 // This template-id does not refer to a type name, so we're
979 // done with the type-specifiers.
980 goto DoneWithDeclSpec;
981 }
982
983 // Turn the template-id annotation token into a type annotation
984 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000985 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000986 continue;
987 }
988
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 // GNU attributes support.
990 case tok::kw___attribute:
991 DS.AddAttributes(ParseAttributes());
992 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000993
994 // Microsoft declspec support.
995 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +0000996 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +0000997 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Steve Naroff239f0732008-12-25 14:16:32 +0000999 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001000 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001001 // FIXME: Add handling here!
1002 break;
1003
1004 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001005 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001006 case tok::kw___cdecl:
1007 case tok::kw___stdcall:
1008 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001009 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1010 continue;
1011
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 // storage-class-specifier
1013 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001014 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1015 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 break;
1017 case tok::kw_extern:
1018 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001019 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001020 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1021 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001023 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001024 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001025 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001026 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 case tok::kw_static:
1028 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001029 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001030 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1031 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001032 break;
1033 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001034 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001035 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1036 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001037 else
John McCallfec54012009-08-03 20:12:06 +00001038 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1039 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001040 break;
1041 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001042 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1043 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001044 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001045 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001046 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1047 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001048 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001049 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001050 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001051 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 // function-specifier
1054 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001055 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001056 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001057 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001058 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001059 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001060 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001061 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001062 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001063
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001064 // friend
1065 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001066 if (DSContext == DSC_class)
1067 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1068 else {
1069 PrevSpec = ""; // not actually used by the diagnostic
1070 DiagID = diag::err_friend_invalid_in_context;
1071 isInvalid = true;
1072 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001073 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Sebastian Redl2ac67232009-11-05 15:47:02 +00001075 // constexpr
1076 case tok::kw_constexpr:
1077 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1078 break;
1079
Chris Lattner80d0c892009-01-21 19:48:37 +00001080 // type-specifier
1081 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001082 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1083 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001084 break;
1085 case tok::kw_long:
1086 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001087 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1088 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001089 else
John McCallfec54012009-08-03 20:12:06 +00001090 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1091 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001092 break;
1093 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001094 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1095 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001096 break;
1097 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001098 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1099 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001100 break;
1101 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001102 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1103 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001104 break;
1105 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001106 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1107 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001108 break;
1109 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001110 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1111 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001112 break;
1113 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001114 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1115 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001116 break;
1117 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001118 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1119 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001120 break;
1121 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001122 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1123 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001124 break;
1125 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001126 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1127 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001128 break;
1129 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001130 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1131 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001132 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001133 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001134 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1135 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001136 break;
1137 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001138 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1139 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001140 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001141 case tok::kw_bool:
1142 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001143 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1144 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001145 break;
1146 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001147 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1148 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001149 break;
1150 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001151 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1152 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001153 break;
1154 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001155 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1156 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001157 break;
1158
1159 // class-specifier:
1160 case tok::kw_class:
1161 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001162 case tok::kw_union: {
1163 tok::TokenKind Kind = Tok.getKind();
1164 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001165 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001166 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001167 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001168
1169 // enum-specifier:
1170 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001171 ConsumeToken();
1172 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001173 continue;
1174
1175 // cv-qualifier:
1176 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001177 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1178 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001179 break;
1180 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001181 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1182 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001183 break;
1184 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001185 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1186 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001187 break;
1188
Douglas Gregord57959a2009-03-27 23:10:48 +00001189 // C++ typename-specifier:
1190 case tok::kw_typename:
1191 if (TryAnnotateTypeOrScopeToken())
1192 continue;
1193 break;
1194
Chris Lattner80d0c892009-01-21 19:48:37 +00001195 // GNU typeof support.
1196 case tok::kw_typeof:
1197 ParseTypeofSpecifier(DS);
1198 continue;
1199
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001200 case tok::kw_decltype:
1201 ParseDecltypeSpecifier(DS);
1202 continue;
1203
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001204 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001205 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001206 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1207 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001208 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001209 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Chris Lattnerbce61352008-07-26 00:20:22 +00001211 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001212 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001213 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001214 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1215 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1216 LAngleLoc, EndProtoLoc);
1217 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1218 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001219 DS.SetRangeEnd(EndProtoLoc);
1220
Chris Lattner1ab3b962008-11-18 07:48:38 +00001221 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001222 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001223 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001224 // Need to support trailing type qualifiers (e.g. "id<p> const").
1225 // If a type specifier follows, it will be diagnosed elsewhere.
1226 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001227 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 }
John McCallfec54012009-08-03 20:12:06 +00001229 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001230 if (isInvalid) {
1231 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001232 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001233 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001235 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 ConsumeToken();
1237 }
1238}
Douglas Gregoradcac882008-12-01 23:54:00 +00001239
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001240/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001241/// primarily follow the C++ grammar with additions for C99 and GNU,
1242/// which together subsume the C grammar. Note that the C++
1243/// type-specifier also includes the C type-qualifier (for const,
1244/// volatile, and C99 restrict). Returns true if a type-specifier was
1245/// found (and parsed), false otherwise.
1246///
1247/// type-specifier: [C++ 7.1.5]
1248/// simple-type-specifier
1249/// class-specifier
1250/// enum-specifier
1251/// elaborated-type-specifier [TODO]
1252/// cv-qualifier
1253///
1254/// cv-qualifier: [C++ 7.1.5.1]
1255/// 'const'
1256/// 'volatile'
1257/// [C99] 'restrict'
1258///
1259/// simple-type-specifier: [ C++ 7.1.5.2]
1260/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1261/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1262/// 'char'
1263/// 'wchar_t'
1264/// 'bool'
1265/// 'short'
1266/// 'int'
1267/// 'long'
1268/// 'signed'
1269/// 'unsigned'
1270/// 'float'
1271/// 'double'
1272/// 'void'
1273/// [C99] '_Bool'
1274/// [C99] '_Complex'
1275/// [C99] '_Imaginary' // Removed in TC2?
1276/// [GNU] '_Decimal32'
1277/// [GNU] '_Decimal64'
1278/// [GNU] '_Decimal128'
1279/// [GNU] typeof-specifier
1280/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1281/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001282/// [C++0x] 'decltype' ( expression )
John McCallfec54012009-08-03 20:12:06 +00001283bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001284 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001285 unsigned &DiagID,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001286 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001287 SourceLocation Loc = Tok.getLocation();
1288
1289 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001290 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001291 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001292 // Annotate typenames and C++ scope specifiers. If we get one, just
1293 // recurse to handle whatever we get.
1294 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001295 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1296 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001297 // Otherwise, not a type specifier.
1298 return false;
1299 case tok::coloncolon: // ::foo::bar
1300 if (NextToken().is(tok::kw_new) || // ::new
1301 NextToken().is(tok::kw_delete)) // ::delete
1302 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001303
Chris Lattner166a8fc2009-01-04 23:41:41 +00001304 // Annotate typenames and C++ scope specifiers. If we get one, just
1305 // recurse to handle whatever we get.
1306 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001307 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1308 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001309 // Otherwise, not a type specifier.
1310 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Douglas Gregor12e083c2008-11-07 15:42:26 +00001312 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001313 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001314 if (Tok.getAnnotationValue())
1315 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001316 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001317 else
1318 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001319 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1320 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Douglas Gregor12e083c2008-11-07 15:42:26 +00001322 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1323 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1324 // Objective-C interface. If we don't have Objective-C or a '<', this is
1325 // just a normal reference to a typedef name.
1326 if (!Tok.is(tok::less) || !getLang().ObjC1)
1327 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001329 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001330 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001331 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1332 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1333 LAngleLoc, EndProtoLoc);
1334 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1335 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001336
Douglas Gregor12e083c2008-11-07 15:42:26 +00001337 DS.SetRangeEnd(EndProtoLoc);
1338 return true;
1339 }
1340
1341 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001342 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001343 break;
1344 case tok::kw_long:
1345 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001346 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1347 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001348 else
John McCallfec54012009-08-03 20:12:06 +00001349 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1350 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001351 break;
1352 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001353 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001354 break;
1355 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001356 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1357 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001358 break;
1359 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001360 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1361 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001362 break;
1363 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001364 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1365 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001366 break;
1367 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001368 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001369 break;
1370 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001371 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001372 break;
1373 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001374 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001375 break;
1376 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001377 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001378 break;
1379 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001380 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001381 break;
1382 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001383 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001384 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001385 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001386 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001387 break;
1388 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001389 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001390 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001391 case tok::kw_bool:
1392 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001393 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001394 break;
1395 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001396 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1397 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001398 break;
1399 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001400 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1401 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001402 break;
1403 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001404 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1405 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001406 break;
1407
1408 // class-specifier:
1409 case tok::kw_class:
1410 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001411 case tok::kw_union: {
1412 tok::TokenKind Kind = Tok.getKind();
1413 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001414 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001415 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001416 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001417
1418 // enum-specifier:
1419 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001420 ConsumeToken();
1421 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001422 return true;
1423
1424 // cv-qualifier:
1425 case tok::kw_const:
1426 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001427 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001428 break;
1429 case tok::kw_volatile:
1430 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001431 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001432 break;
1433 case tok::kw_restrict:
1434 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001435 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001436 break;
1437
1438 // GNU typeof support.
1439 case tok::kw_typeof:
1440 ParseTypeofSpecifier(DS);
1441 return true;
1442
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001443 // C++0x decltype support.
1444 case tok::kw_decltype:
1445 ParseDecltypeSpecifier(DS);
1446 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001447
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001448 // C++0x auto support.
1449 case tok::kw_auto:
1450 if (!getLang().CPlusPlus0x)
1451 return false;
1452
John McCallfec54012009-08-03 20:12:06 +00001453 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001454 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001455 case tok::kw___ptr64:
1456 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001457 case tok::kw___cdecl:
1458 case tok::kw___stdcall:
1459 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001460 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001461 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001462
Douglas Gregor12e083c2008-11-07 15:42:26 +00001463 default:
1464 // Not a type-specifier; do nothing.
1465 return false;
1466 }
1467
1468 // If the specifier combination wasn't legal, issue a diagnostic.
1469 if (isInvalid) {
1470 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001471 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001472 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001473 }
1474 DS.SetRangeEnd(Tok.getLocation());
1475 ConsumeToken(); // whatever we parsed above.
1476 return true;
1477}
Reid Spencer5f016e22007-07-11 17:01:13 +00001478
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001479/// ParseStructDeclaration - Parse a struct declaration without the terminating
1480/// semicolon.
1481///
Reid Spencer5f016e22007-07-11 17:01:13 +00001482/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001483/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001484/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001485/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001486/// struct-declarator-list:
1487/// struct-declarator
1488/// struct-declarator-list ',' struct-declarator
1489/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1490/// struct-declarator:
1491/// declarator
1492/// [GNU] declarator attributes[opt]
1493/// declarator[opt] ':' constant-expression
1494/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1495///
Chris Lattnere1359422008-04-10 06:46:29 +00001496void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001497ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001498 if (Tok.is(tok::kw___extension__)) {
1499 // __extension__ silences extension warnings in the subexpression.
1500 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001501 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001502 return ParseStructDeclaration(DS, Fields);
1503 }
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Steve Naroff28a7ca82007-08-20 22:28:22 +00001505 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001506 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001507 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001509 // If there are no declarators, this is a free-standing declaration
1510 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001511 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001512 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001513 return;
1514 }
1515
1516 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001517 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001518 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001519 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001520 FieldDeclarator DeclaratorInfo(DS);
1521
1522 // Attributes are only allowed here on successive declarators.
1523 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1524 SourceLocation Loc;
1525 AttributeList *AttrList = ParseAttributes(&Loc);
1526 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1527 }
Mike Stump1eb44332009-09-09 15:08:12 +00001528
Steve Naroff28a7ca82007-08-20 22:28:22 +00001529 /// struct-declarator: declarator
1530 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001531 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001532 ParseDeclarator(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Chris Lattner04d66662007-10-09 17:33:22 +00001534 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001535 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001536 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001537 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001538 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001539 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001540 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001541 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001542
Steve Naroff28a7ca82007-08-20 22:28:22 +00001543 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001544 if (Tok.is(tok::kw___attribute)) {
1545 SourceLocation Loc;
1546 AttributeList *AttrList = ParseAttributes(&Loc);
1547 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1548 }
1549
John McCallbdd563e2009-11-03 02:38:08 +00001550 // We're done with this declarator; invoke the callback.
John McCall54abf7d2009-11-04 02:18:39 +00001551 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1552 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001553
Steve Naroff28a7ca82007-08-20 22:28:22 +00001554 // If we don't have a comma, it is either the end of the list (a ';')
1555 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001556 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001557 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001558
Steve Naroff28a7ca82007-08-20 22:28:22 +00001559 // Consume the comma.
1560 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001561
John McCallbdd563e2009-11-03 02:38:08 +00001562 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001563 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001564}
1565
1566/// ParseStructUnionBody
1567/// struct-contents:
1568/// struct-declaration-list
1569/// [EXT] empty
1570/// [GNU] "struct-declaration-list" without terminatoring ';'
1571/// struct-declaration-list:
1572/// struct-declaration
1573/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001574/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001575///
Reid Spencer5f016e22007-07-11 17:01:13 +00001576void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001577 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001578 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1579 PP.getSourceManager(),
1580 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Reid Spencer5f016e22007-07-11 17:01:13 +00001582 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001583
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001584 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001585 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1586
Reid Spencer5f016e22007-07-11 17:01:13 +00001587 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1588 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001589 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001590 Diag(Tok, diag::ext_empty_struct_union_enum)
1591 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001592
Chris Lattnerb28317a2009-03-28 19:18:32 +00001593 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001594
Reid Spencer5f016e22007-07-11 17:01:13 +00001595 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001596 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001597 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001600 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001601 Diag(Tok, diag::ext_extra_struct_semi)
1602 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001603 ConsumeToken();
1604 continue;
1605 }
Chris Lattnere1359422008-04-10 06:46:29 +00001606
1607 // Parse all the comma separated declarators.
1608 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001609
John McCallbdd563e2009-11-03 02:38:08 +00001610 if (!Tok.is(tok::at)) {
1611 struct CFieldCallback : FieldCallback {
1612 Parser &P;
1613 DeclPtrTy TagDecl;
1614 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1615
1616 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1617 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1618 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1619
1620 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001621 // Install the declarator into the current TagDecl.
John McCall4ba39712009-11-03 21:13:47 +00001622 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1623 FD.D.getDeclSpec().getSourceRange().getBegin(),
1624 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001625 FieldDecls.push_back(Field);
1626 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001627 }
John McCallbdd563e2009-11-03 02:38:08 +00001628 } Callback(*this, TagDecl, FieldDecls);
1629
1630 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001631 } else { // Handle @defs
1632 ConsumeToken();
1633 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1634 Diag(Tok, diag::err_unexpected_at);
1635 SkipUntil(tok::semi, true, true);
1636 continue;
1637 }
1638 ConsumeToken();
1639 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1640 if (!Tok.is(tok::identifier)) {
1641 Diag(Tok, diag::err_expected_ident);
1642 SkipUntil(tok::semi, true, true);
1643 continue;
1644 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001645 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001646 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001647 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001648 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1649 ConsumeToken();
1650 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001651 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001652
Chris Lattner04d66662007-10-09 17:33:22 +00001653 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001654 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001655 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001656 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 break;
1658 } else {
1659 Diag(Tok, diag::err_expected_semi_decl_list);
1660 // Skip to end of block or statement
1661 SkipUntil(tok::r_brace, true, true);
1662 }
1663 }
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Steve Naroff60fccee2007-10-29 21:38:07 +00001665 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Reid Spencer5f016e22007-07-11 17:01:13 +00001667 AttributeList *AttrList = 0;
1668 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001669 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001670 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001671
1672 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001673 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001674 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001675 AttrList);
1676 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001677 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001678}
1679
1680
1681/// ParseEnumSpecifier
1682/// enum-specifier: [C99 6.7.2.2]
1683/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001684///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001685/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1686/// '}' attributes[opt]
1687/// 'enum' identifier
1688/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001689///
1690/// [C++] elaborated-type-specifier:
1691/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1692///
Chris Lattner4c97d762009-04-12 21:49:30 +00001693void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1694 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001695 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001696 if (Tok.is(tok::code_completion)) {
1697 // Code completion for an enum name.
1698 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1699 ConsumeToken();
1700 }
1701
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001702 AttributeList *Attr = 0;
1703 // If attributes exist after tag, parse them.
1704 if (Tok.is(tok::kw___attribute))
1705 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001706
1707 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001708 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001709 if (Tok.isNot(tok::identifier)) {
1710 Diag(Tok, diag::err_expected_ident);
1711 if (Tok.isNot(tok::l_brace)) {
1712 // Has no name and is not a definition.
1713 // Skip the rest of this declarator, up until the comma or semicolon.
1714 SkipUntil(tok::comma, true);
1715 return;
1716 }
1717 }
1718 }
Mike Stump1eb44332009-09-09 15:08:12 +00001719
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001720 // Must have either 'enum name' or 'enum {...}'.
1721 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1722 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001723
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001724 // Skip the rest of this declarator, up until the comma or semicolon.
1725 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001726 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001727 }
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001729 // If an identifier is present, consume and remember it.
1730 IdentifierInfo *Name = 0;
1731 SourceLocation NameLoc;
1732 if (Tok.is(tok::identifier)) {
1733 Name = Tok.getIdentifierInfo();
1734 NameLoc = ConsumeToken();
1735 }
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001737 // There are three options here. If we have 'enum foo;', then this is a
1738 // forward declaration. If we have 'enum foo {...' then this is a
1739 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1740 //
1741 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1742 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1743 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1744 //
John McCall0f434ec2009-07-31 02:45:11 +00001745 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001746 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001747 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001748 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001749 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001750 else
John McCall0f434ec2009-07-31 02:45:11 +00001751 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001752 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001753 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00001754 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001755 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001756 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001757 Owned, IsDependent);
1758 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Chris Lattner04d66662007-10-09 17:33:22 +00001760 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001761 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Reid Spencer5f016e22007-07-11 17:01:13 +00001763 // TODO: semantic analysis on the declspec for enums.
1764 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001765 unsigned DiagID;
1766 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001767 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001768 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001769}
1770
1771/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1772/// enumerator-list:
1773/// enumerator
1774/// enumerator-list ',' enumerator
1775/// enumerator:
1776/// enumeration-constant
1777/// enumeration-constant '=' constant-expression
1778/// enumeration-constant:
1779/// identifier
1780///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001781void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001782 // Enter the scope of the enum body and start the definition.
1783 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001784 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001785
Reid Spencer5f016e22007-07-11 17:01:13 +00001786 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Chris Lattner7946dd32007-08-27 17:24:30 +00001788 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001789 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001790 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Chris Lattnerb28317a2009-03-28 19:18:32 +00001792 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001793
Chris Lattnerb28317a2009-03-28 19:18:32 +00001794 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001795
Reid Spencer5f016e22007-07-11 17:01:13 +00001796 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001797 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001798 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1799 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001800
Reid Spencer5f016e22007-07-11 17:01:13 +00001801 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001802 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001803 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001804 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001805 AssignedVal = ParseConstantExpression();
1806 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001807 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001808 }
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Reid Spencer5f016e22007-07-11 17:01:13 +00001810 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001811 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1812 LastEnumConstDecl,
1813 IdentLoc, Ident,
1814 EqualLoc,
1815 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001816 EnumConstantDecls.push_back(EnumConstDecl);
1817 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Chris Lattner04d66662007-10-09 17:33:22 +00001819 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001820 break;
1821 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001822
1823 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001824 !(getLang().C99 || getLang().CPlusPlus0x))
1825 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1826 << getLang().CPlusPlus
1827 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001828 }
Mike Stump1eb44332009-09-09 15:08:12 +00001829
Reid Spencer5f016e22007-07-11 17:01:13 +00001830 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001831 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001832
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001833 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001834 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001835 if (Tok.is(tok::kw___attribute))
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001836 Attr = ParseAttributes();
Douglas Gregor72de6672009-01-08 20:45:30 +00001837
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001838 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1839 EnumConstantDecls.data(), EnumConstantDecls.size(),
1840 CurScope, Attr);
Mike Stump1eb44332009-09-09 15:08:12 +00001841
Douglas Gregor72de6672009-01-08 20:45:30 +00001842 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001843 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001844}
1845
1846/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001847/// start of a type-qualifier-list.
1848bool Parser::isTypeQualifier() const {
1849 switch (Tok.getKind()) {
1850 default: return false;
1851 // type-qualifier
1852 case tok::kw_const:
1853 case tok::kw_volatile:
1854 case tok::kw_restrict:
1855 return true;
1856 }
1857}
1858
1859/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001860/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001861bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001862 switch (Tok.getKind()) {
1863 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Chris Lattner166a8fc2009-01-04 23:41:41 +00001865 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001866 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001867 // Annotate typenames and C++ scope specifiers. If we get one, just
1868 // recurse to handle whatever we get.
1869 if (TryAnnotateTypeOrScopeToken())
1870 return isTypeSpecifierQualifier();
1871 // Otherwise, not a type specifier.
1872 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001873
Chris Lattner166a8fc2009-01-04 23:41:41 +00001874 case tok::coloncolon: // ::foo::bar
1875 if (NextToken().is(tok::kw_new) || // ::new
1876 NextToken().is(tok::kw_delete)) // ::delete
1877 return false;
1878
1879 // Annotate typenames and C++ scope specifiers. If we get one, just
1880 // recurse to handle whatever we get.
1881 if (TryAnnotateTypeOrScopeToken())
1882 return isTypeSpecifierQualifier();
1883 // Otherwise, not a type specifier.
1884 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Reid Spencer5f016e22007-07-11 17:01:13 +00001886 // GNU attributes support.
1887 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001888 // GNU typeof support.
1889 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00001890
Reid Spencer5f016e22007-07-11 17:01:13 +00001891 // type-specifiers
1892 case tok::kw_short:
1893 case tok::kw_long:
1894 case tok::kw_signed:
1895 case tok::kw_unsigned:
1896 case tok::kw__Complex:
1897 case tok::kw__Imaginary:
1898 case tok::kw_void:
1899 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001900 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001901 case tok::kw_char16_t:
1902 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001903 case tok::kw_int:
1904 case tok::kw_float:
1905 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001906 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001907 case tok::kw__Bool:
1908 case tok::kw__Decimal32:
1909 case tok::kw__Decimal64:
1910 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00001911
Chris Lattner99dc9142008-04-13 18:59:07 +00001912 // struct-or-union-specifier (C99) or class-specifier (C++)
1913 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001914 case tok::kw_struct:
1915 case tok::kw_union:
1916 // enum-specifier
1917 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Reid Spencer5f016e22007-07-11 17:01:13 +00001919 // type-qualifier
1920 case tok::kw_const:
1921 case tok::kw_volatile:
1922 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001923
1924 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001925 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001926 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Chris Lattner7c186be2008-10-20 00:25:30 +00001928 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1929 case tok::less:
1930 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Steve Naroff239f0732008-12-25 14:16:32 +00001932 case tok::kw___cdecl:
1933 case tok::kw___stdcall:
1934 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001935 case tok::kw___w64:
1936 case tok::kw___ptr64:
1937 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001938 }
1939}
1940
1941/// isDeclarationSpecifier() - Return true if the current token is part of a
1942/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001943bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001944 switch (Tok.getKind()) {
1945 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001946
Chris Lattner166a8fc2009-01-04 23:41:41 +00001947 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001948 // Unfortunate hack to support "Class.factoryMethod" notation.
1949 if (getLang().ObjC1 && NextToken().is(tok::period))
1950 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001951 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001952
Douglas Gregord57959a2009-03-27 23:10:48 +00001953 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001954 // Annotate typenames and C++ scope specifiers. If we get one, just
1955 // recurse to handle whatever we get.
1956 if (TryAnnotateTypeOrScopeToken())
1957 return isDeclarationSpecifier();
1958 // Otherwise, not a declaration specifier.
1959 return false;
1960 case tok::coloncolon: // ::foo::bar
1961 if (NextToken().is(tok::kw_new) || // ::new
1962 NextToken().is(tok::kw_delete)) // ::delete
1963 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001964
Chris Lattner166a8fc2009-01-04 23:41:41 +00001965 // Annotate typenames and C++ scope specifiers. If we get one, just
1966 // recurse to handle whatever we get.
1967 if (TryAnnotateTypeOrScopeToken())
1968 return isDeclarationSpecifier();
1969 // Otherwise, not a declaration specifier.
1970 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001971
Reid Spencer5f016e22007-07-11 17:01:13 +00001972 // storage-class-specifier
1973 case tok::kw_typedef:
1974 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001975 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001976 case tok::kw_static:
1977 case tok::kw_auto:
1978 case tok::kw_register:
1979 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00001980
Reid Spencer5f016e22007-07-11 17:01:13 +00001981 // type-specifiers
1982 case tok::kw_short:
1983 case tok::kw_long:
1984 case tok::kw_signed:
1985 case tok::kw_unsigned:
1986 case tok::kw__Complex:
1987 case tok::kw__Imaginary:
1988 case tok::kw_void:
1989 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001990 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001991 case tok::kw_char16_t:
1992 case tok::kw_char32_t:
1993
Reid Spencer5f016e22007-07-11 17:01:13 +00001994 case tok::kw_int:
1995 case tok::kw_float:
1996 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001997 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001998 case tok::kw__Bool:
1999 case tok::kw__Decimal32:
2000 case tok::kw__Decimal64:
2001 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00002002
Chris Lattner99dc9142008-04-13 18:59:07 +00002003 // struct-or-union-specifier (C99) or class-specifier (C++)
2004 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002005 case tok::kw_struct:
2006 case tok::kw_union:
2007 // enum-specifier
2008 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002009
Reid Spencer5f016e22007-07-11 17:01:13 +00002010 // type-qualifier
2011 case tok::kw_const:
2012 case tok::kw_volatile:
2013 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002014
Reid Spencer5f016e22007-07-11 17:01:13 +00002015 // function-specifier
2016 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002017 case tok::kw_virtual:
2018 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002019
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002020 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002021 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002022
Chris Lattner1ef08762007-08-09 17:01:07 +00002023 // GNU typeof support.
2024 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002025
Chris Lattner1ef08762007-08-09 17:01:07 +00002026 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002027 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002028 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002029
Chris Lattnerf3948c42008-07-26 03:38:44 +00002030 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2031 case tok::less:
2032 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002033
Steve Naroff47f52092009-01-06 19:34:12 +00002034 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002035 case tok::kw___cdecl:
2036 case tok::kw___stdcall:
2037 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002038 case tok::kw___w64:
2039 case tok::kw___ptr64:
2040 case tok::kw___forceinline:
2041 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002042 }
2043}
2044
2045
2046/// ParseTypeQualifierListOpt
2047/// type-qualifier-list: [C99 6.7.5]
2048/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002049/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002050/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002051/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002052///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002053void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002054 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002055 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002056 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002057 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002058 SourceLocation Loc = Tok.getLocation();
2059
2060 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002061 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002062 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2063 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002064 break;
2065 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002066 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2067 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002068 break;
2069 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002070 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2071 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002072 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002073 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002074 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002075 case tok::kw___cdecl:
2076 case tok::kw___stdcall:
2077 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002078 if (AttributesAllowed) {
2079 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2080 continue;
2081 }
2082 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002083 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002084 if (AttributesAllowed) {
2085 DS.AddAttributes(ParseAttributes());
2086 continue; // do *not* consume the next token!
2087 }
2088 // otherwise, FALL THROUGH!
2089 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002090 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002091 // If this is not a type-qualifier token, we're done reading type
2092 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002093 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002094 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002095 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002096
Reid Spencer5f016e22007-07-11 17:01:13 +00002097 // If the specifier combination wasn't legal, issue a diagnostic.
2098 if (isInvalid) {
2099 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002100 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002101 }
2102 ConsumeToken();
2103 }
2104}
2105
2106
2107/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2108///
2109void Parser::ParseDeclarator(Declarator &D) {
2110 /// This implements the 'declarator' production in the C grammar, then checks
2111 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002112 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002113}
2114
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002115/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2116/// is parsed by the function passed to it. Pass null, and the direct-declarator
2117/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002118/// ptr-operator production.
2119///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002120/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2121/// [C] pointer[opt] direct-declarator
2122/// [C++] direct-declarator
2123/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002124///
2125/// pointer: [C99 6.7.5]
2126/// '*' type-qualifier-list[opt]
2127/// '*' type-qualifier-list[opt] pointer
2128///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002129/// ptr-operator:
2130/// '*' cv-qualifier-seq[opt]
2131/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002132/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002133/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002134/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002135/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002136void Parser::ParseDeclaratorInternal(Declarator &D,
2137 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002138 if (Diags.hasAllExtensionsSilenced())
2139 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002140 // C++ member pointers start with a '::' or a nested-name.
2141 // Member pointers get special handling, since there's no place for the
2142 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002143 if (getLang().CPlusPlus &&
2144 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2145 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002146 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002147 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002148 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002149 // The scope spec really belongs to the direct-declarator.
2150 D.getCXXScopeSpec() = SS;
2151 if (DirectDeclParser)
2152 (this->*DirectDeclParser)(D);
2153 return;
2154 }
2155
2156 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002157 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002158 DeclSpec DS;
2159 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002160 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002161
2162 // Recurse to parse whatever is left.
2163 ParseDeclaratorInternal(D, DirectDeclParser);
2164
2165 // Sema will have to catch (syntactically invalid) pointers into global
2166 // scope. It has to catch pointers into namespace scope anyway.
2167 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002168 Loc, DS.TakeAttributes()),
2169 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002170 return;
2171 }
2172 }
2173
2174 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002175 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002176 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002177 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002178 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002179 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002180 if (DirectDeclParser)
2181 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002182 return;
2183 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002184
Sebastian Redl05532f22009-03-15 22:02:01 +00002185 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2186 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002187 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002188 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002189
Chris Lattner9af55002009-03-27 04:18:06 +00002190 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002191 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002192 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002193
Reid Spencer5f016e22007-07-11 17:01:13 +00002194 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002195 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002196
Reid Spencer5f016e22007-07-11 17:01:13 +00002197 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002198 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002199 if (Kind == tok::star)
2200 // Remember that we parsed a pointer type, and remember the type-quals.
2201 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002202 DS.TakeAttributes()),
2203 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002204 else
2205 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002206 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002207 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002208 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002209 } else {
2210 // Is a reference
2211 DeclSpec DS;
2212
Sebastian Redl743de1f2009-03-23 00:00:23 +00002213 // Complain about rvalue references in C++03, but then go on and build
2214 // the declarator.
2215 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2216 Diag(Loc, diag::err_rvalue_reference);
2217
Reid Spencer5f016e22007-07-11 17:01:13 +00002218 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2219 // cv-qualifiers are introduced through the use of a typedef or of a
2220 // template type argument, in which case the cv-qualifiers are ignored.
2221 //
2222 // [GNU] Retricted references are allowed.
2223 // [GNU] Attributes on references are allowed.
2224 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002225 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002226
2227 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2228 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2229 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002230 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002231 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2232 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002233 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002234 }
2235
2236 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002237 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002238
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002239 if (D.getNumTypeObjects() > 0) {
2240 // C++ [dcl.ref]p4: There shall be no references to references.
2241 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2242 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002243 if (const IdentifierInfo *II = D.getIdentifier())
2244 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2245 << II;
2246 else
2247 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2248 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002249
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002250 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002251 // can go ahead and build the (technically ill-formed)
2252 // declarator: reference collapsing will take care of it.
2253 }
2254 }
2255
Reid Spencer5f016e22007-07-11 17:01:13 +00002256 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002257 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002258 DS.TakeAttributes(),
2259 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002260 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002261 }
2262}
2263
2264/// ParseDirectDeclarator
2265/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002266/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002267/// '(' declarator ')'
2268/// [GNU] '(' attributes declarator ')'
2269/// [C90] direct-declarator '[' constant-expression[opt] ']'
2270/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2271/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2272/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2273/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2274/// direct-declarator '(' parameter-type-list ')'
2275/// direct-declarator '(' identifier-list[opt] ')'
2276/// [GNU] direct-declarator '(' parameter-forward-declarations
2277/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002278/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2279/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002280/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002281///
2282/// declarator-id: [C++ 8]
2283/// id-expression
2284/// '::'[opt] nested-name-specifier[opt] type-name
2285///
2286/// id-expression: [C++ 5.1]
2287/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002288/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002289///
2290/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002291/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002292/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002293/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002294/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002295/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002296///
Reid Spencer5f016e22007-07-11 17:01:13 +00002297void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002298 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002299
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002300 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2301 // ParseDeclaratorInternal might already have parsed the scope.
2302 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2303 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2304 true);
2305 if (afterCXXScope) {
2306 // Change the declaration context for name lookup, until this function
2307 // is exited (and the declarator has been parsed).
2308 DeclScopeObj.EnterDeclaratorScope();
2309 }
2310
2311 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2312 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2313 // We found something that indicates the start of an unqualified-id.
2314 // Parse that unqualified-id.
2315 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2316 /*EnteringContext=*/true,
2317 /*AllowDestructorName=*/true,
2318 /*AllowConstructorName=*/!D.getDeclSpec().hasTypeSpecifier(),
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002319 /*ObjectType=*/0,
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002320 D.getName())) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002321 D.SetIdentifier(0, Tok.getLocation());
2322 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002323 } else {
2324 // Parsed the unqualified-id; update range information and move along.
2325 if (D.getSourceRange().getBegin().isInvalid())
2326 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2327 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002328 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002329 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002330 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002331 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002332 assert(!getLang().CPlusPlus &&
2333 "There's a C++-specific check for tok::identifier above");
2334 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2335 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2336 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002337 goto PastIdentifier;
2338 }
2339
2340 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002341 // direct-declarator: '(' declarator ')'
2342 // direct-declarator: '(' attributes declarator ')'
2343 // Example: 'char (*X)' or 'int (*XX)(void)'
2344 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002345 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002346 // This could be something simple like "int" (in which case the declarator
2347 // portion is empty), if an abstract-declarator is allowed.
2348 D.SetIdentifier(0, Tok.getLocation());
2349 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002350 if (D.getContext() == Declarator::MemberContext)
2351 Diag(Tok, diag::err_expected_member_name_or_semi)
2352 << D.getDeclSpec().getSourceRange();
2353 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002354 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002355 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002356 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002357 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002358 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002359 }
Mike Stump1eb44332009-09-09 15:08:12 +00002360
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002361 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002362 assert(D.isPastIdentifier() &&
2363 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002364
Reid Spencer5f016e22007-07-11 17:01:13 +00002365 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002366 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002367 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2368 // In such a case, check if we actually have a function declarator; if it
2369 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002370 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2371 // When not in file scope, warn for ambiguous function declarators, just
2372 // in case the author intended it as a variable definition.
2373 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2374 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2375 break;
2376 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002377 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002378 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002379 ParseBracketDeclarator(D);
2380 } else {
2381 break;
2382 }
2383 }
2384}
2385
Chris Lattneref4715c2008-04-06 05:45:57 +00002386/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2387/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002388/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002389/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2390///
2391/// direct-declarator:
2392/// '(' declarator ')'
2393/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002394/// direct-declarator '(' parameter-type-list ')'
2395/// direct-declarator '(' identifier-list[opt] ')'
2396/// [GNU] direct-declarator '(' parameter-forward-declarations
2397/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002398///
2399void Parser::ParseParenDeclarator(Declarator &D) {
2400 SourceLocation StartLoc = ConsumeParen();
2401 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002402
Chris Lattner7399ee02008-10-20 02:05:46 +00002403 // Eat any attributes before we look at whether this is a grouping or function
2404 // declarator paren. If this is a grouping paren, the attribute applies to
2405 // the type being built up, for example:
2406 // int (__attribute__(()) *x)(long y)
2407 // If this ends up not being a grouping paren, the attribute applies to the
2408 // first argument, for example:
2409 // int (__attribute__(()) int x)
2410 // In either case, we need to eat any attributes to be able to determine what
2411 // sort of paren this is.
2412 //
2413 AttributeList *AttrList = 0;
2414 bool RequiresArg = false;
2415 if (Tok.is(tok::kw___attribute)) {
2416 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00002417
Chris Lattner7399ee02008-10-20 02:05:46 +00002418 // We require that the argument list (if this is a non-grouping paren) be
2419 // present even if the attribute list was empty.
2420 RequiresArg = true;
2421 }
Steve Naroff239f0732008-12-25 14:16:32 +00002422 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002423 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2424 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2425 Tok.is(tok::kw___ptr64)) {
2426 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2427 }
Mike Stump1eb44332009-09-09 15:08:12 +00002428
Chris Lattneref4715c2008-04-06 05:45:57 +00002429 // If we haven't past the identifier yet (or where the identifier would be
2430 // stored, if this is an abstract declarator), then this is probably just
2431 // grouping parens. However, if this could be an abstract-declarator, then
2432 // this could also be the start of function arguments (consider 'void()').
2433 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002434
Chris Lattneref4715c2008-04-06 05:45:57 +00002435 if (!D.mayOmitIdentifier()) {
2436 // If this can't be an abstract-declarator, this *must* be a grouping
2437 // paren, because we haven't seen the identifier yet.
2438 isGrouping = true;
2439 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002440 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002441 isDeclarationSpecifier()) { // 'int(int)' is a function.
2442 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2443 // considered to be a type, not a K&R identifier-list.
2444 isGrouping = false;
2445 } else {
2446 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2447 isGrouping = true;
2448 }
Mike Stump1eb44332009-09-09 15:08:12 +00002449
Chris Lattneref4715c2008-04-06 05:45:57 +00002450 // If this is a grouping paren, handle:
2451 // direct-declarator: '(' declarator ')'
2452 // direct-declarator: '(' attributes declarator ')'
2453 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002454 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002455 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002456 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002457 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002458
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002459 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002460 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002461 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002462
2463 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002464 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002465 return;
2466 }
Mike Stump1eb44332009-09-09 15:08:12 +00002467
Chris Lattneref4715c2008-04-06 05:45:57 +00002468 // Okay, if this wasn't a grouping paren, it must be the start of a function
2469 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002470 // identifier (and remember where it would have been), then call into
2471 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002472 D.SetIdentifier(0, Tok.getLocation());
2473
Chris Lattner7399ee02008-10-20 02:05:46 +00002474 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002475}
2476
2477/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2478/// declarator D up to a paren, which indicates that we are parsing function
2479/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002480///
Chris Lattner7399ee02008-10-20 02:05:46 +00002481/// If AttrList is non-null, then the caller parsed those arguments immediately
2482/// after the open paren - they should be considered to be the first argument of
2483/// a parameter. If RequiresArg is true, then the first argument of the
2484/// function is required to be present and required to not be an identifier
2485/// list.
2486///
Reid Spencer5f016e22007-07-11 17:01:13 +00002487/// This method also handles this portion of the grammar:
2488/// parameter-type-list: [C99 6.7.5]
2489/// parameter-list
2490/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002491/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002492///
2493/// parameter-list: [C99 6.7.5]
2494/// parameter-declaration
2495/// parameter-list ',' parameter-declaration
2496///
2497/// parameter-declaration: [C99 6.7.5]
2498/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002499/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002500/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002501/// declaration-specifiers abstract-declarator[opt]
2502/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002503/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002504/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2505///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002506/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002507/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002508///
Chris Lattner7399ee02008-10-20 02:05:46 +00002509void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2510 AttributeList *AttrList,
2511 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002512 // lparen is already consumed!
2513 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002514
Chris Lattner7399ee02008-10-20 02:05:46 +00002515 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002516 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002517 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002518 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002519 delete AttrList;
2520 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002521
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002522 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2523 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002524
2525 // cv-qualifier-seq[opt].
2526 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002527 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002528 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002529 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002530 llvm::SmallVector<TypeTy*, 2> Exceptions;
2531 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002532 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002533 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002534 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002535 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002536
2537 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002538 if (Tok.is(tok::kw_throw)) {
2539 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002540 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002541 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002542 hasAnyExceptionSpec);
2543 assert(Exceptions.size() == ExceptionRanges.size() &&
2544 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002545 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002546 }
2547
Chris Lattnerf97409f2008-04-06 06:57:35 +00002548 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002549 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002550 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002551 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002552 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002553 /*arglist*/ 0, 0,
2554 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002555 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002556 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002557 Exceptions.data(),
2558 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002559 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002560 LParenLoc, RParenLoc, D),
2561 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002562 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002563 }
2564
Chris Lattner7399ee02008-10-20 02:05:46 +00002565 // Alternatively, this parameter list may be an identifier list form for a
2566 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002567 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002568 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002569 // K&R identifier lists can't have typedefs as identifiers, per
2570 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002571 if (RequiresArg) {
2572 Diag(Tok, diag::err_argument_required_after_attribute);
2573 delete AttrList;
2574 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002575 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2576 // normal declarators, not for abstract-declarators.
2577 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002578 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002579 }
Mike Stump1eb44332009-09-09 15:08:12 +00002580
Chris Lattnerf97409f2008-04-06 06:57:35 +00002581 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002582
Chris Lattnerf97409f2008-04-06 06:57:35 +00002583 // Build up an array of information about the parsed arguments.
2584 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002585
2586 // Enter function-declaration scope, limiting any declarators to the
2587 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002588 ParseScope PrototypeScope(this,
2589 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002590
Chris Lattnerf97409f2008-04-06 06:57:35 +00002591 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002592 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002593 while (1) {
2594 if (Tok.is(tok::ellipsis)) {
2595 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002596 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002597 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002598 }
Mike Stump1eb44332009-09-09 15:08:12 +00002599
Chris Lattnerf97409f2008-04-06 06:57:35 +00002600 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002601
Chris Lattnerf97409f2008-04-06 06:57:35 +00002602 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00002603 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002604 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002605
2606 // If the caller parsed attributes for the first argument, add them now.
2607 if (AttrList) {
2608 DS.AddAttributes(AttrList);
2609 AttrList = 0; // Only apply the attributes to the first parameter.
2610 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002611 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002612
Chris Lattnerf97409f2008-04-06 06:57:35 +00002613 // Parse the declarator. This is "PrototypeContext", because we must
2614 // accept either 'declarator' or 'abstract-declarator' here.
2615 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2616 ParseDeclarator(ParmDecl);
2617
2618 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002619 if (Tok.is(tok::kw___attribute)) {
2620 SourceLocation Loc;
2621 AttributeList *AttrList = ParseAttributes(&Loc);
2622 ParmDecl.AddAttributes(AttrList, Loc);
2623 }
Mike Stump1eb44332009-09-09 15:08:12 +00002624
Chris Lattnerf97409f2008-04-06 06:57:35 +00002625 // Remember this parsed parameter in ParamInfo.
2626 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002627
Douglas Gregor72b505b2008-12-16 21:30:33 +00002628 // DefArgToks is used when the parsing of default arguments needs
2629 // to be delayed.
2630 CachedTokens *DefArgToks = 0;
2631
Chris Lattnerf97409f2008-04-06 06:57:35 +00002632 // If no parameter was specified, verify that *something* was specified,
2633 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002634 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2635 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002636 // Completely missing, emit error.
2637 Diag(DSStart, diag::err_missing_param);
2638 } else {
2639 // Otherwise, we have something. Add it and let semantic analysis try
2640 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00002641
Chris Lattnerf97409f2008-04-06 06:57:35 +00002642 // Inform the actions module about the parameter declarator, so it gets
2643 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002644 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002645
2646 // Parse the default argument, if any. We parse the default
2647 // arguments in all dialects; the semantic analysis in
2648 // ActOnParamDefaultArgument will reject the default argument in
2649 // C.
2650 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002651 SourceLocation EqualLoc = Tok.getLocation();
2652
Chris Lattner04421082008-04-08 04:40:51 +00002653 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002654 if (D.getContext() == Declarator::MemberContext) {
2655 // If we're inside a class definition, cache the tokens
2656 // corresponding to the default argument. We'll actually parse
2657 // them when we see the end of the class definition.
2658 // FIXME: Templates will require something similar.
2659 // FIXME: Can we use a smart pointer for Toks?
2660 DefArgToks = new CachedTokens;
2661
Mike Stump1eb44332009-09-09 15:08:12 +00002662 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002663 tok::semi, false)) {
2664 delete DefArgToks;
2665 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002666 Actions.ActOnParamDefaultArgumentError(Param);
2667 } else
Mike Stump1eb44332009-09-09 15:08:12 +00002668 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00002669 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002670 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002671 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002672 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002673
Douglas Gregor72b505b2008-12-16 21:30:33 +00002674 OwningExprResult DefArgResult(ParseAssignmentExpression());
2675 if (DefArgResult.isInvalid()) {
2676 Actions.ActOnParamDefaultArgumentError(Param);
2677 SkipUntil(tok::comma, tok::r_paren, true, true);
2678 } else {
2679 // Inform the actions module about the default argument
2680 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002681 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002682 }
Chris Lattner04421082008-04-08 04:40:51 +00002683 }
2684 }
Mike Stump1eb44332009-09-09 15:08:12 +00002685
2686 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2687 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002688 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002689 }
2690
2691 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00002692 if (Tok.isNot(tok::comma)) {
2693 if (Tok.is(tok::ellipsis)) {
2694 IsVariadic = true;
2695 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2696
2697 if (!getLang().CPlusPlus) {
2698 // We have ellipsis without a preceding ',', which is ill-formed
2699 // in C. Complain and provide the fix.
2700 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2701 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2702 }
2703 }
2704
2705 break;
2706 }
Mike Stump1eb44332009-09-09 15:08:12 +00002707
Chris Lattnerf97409f2008-04-06 06:57:35 +00002708 // Consume the comma.
2709 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002710 }
Mike Stump1eb44332009-09-09 15:08:12 +00002711
Chris Lattnerf97409f2008-04-06 06:57:35 +00002712 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002713 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00002714
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002715 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002716 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2717 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002718
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002719 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002720 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002721 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002722 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002723 llvm::SmallVector<TypeTy*, 2> Exceptions;
2724 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002725 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002726 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002727 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002728 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002729 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002730
2731 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002732 if (Tok.is(tok::kw_throw)) {
2733 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002734 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002735 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002736 hasAnyExceptionSpec);
2737 assert(Exceptions.size() == ExceptionRanges.size() &&
2738 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002739 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002740 }
2741
Reid Spencer5f016e22007-07-11 17:01:13 +00002742 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002743 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002744 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002745 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002746 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002747 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002748 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002749 Exceptions.data(),
2750 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002751 Exceptions.size(),
2752 LParenLoc, RParenLoc, D),
2753 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002754}
2755
Chris Lattner66d28652008-04-06 06:34:08 +00002756/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2757/// we found a K&R-style identifier list instead of a type argument list. The
2758/// current token is known to be the first identifier in the list.
2759///
2760/// identifier-list: [C99 6.7.5]
2761/// identifier
2762/// identifier-list ',' identifier
2763///
2764void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2765 Declarator &D) {
2766 // Build up an array of information about the parsed arguments.
2767 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2768 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00002769
Chris Lattner66d28652008-04-06 06:34:08 +00002770 // If there was no identifier specified for the declarator, either we are in
2771 // an abstract-declarator, or we are in a parameter declarator which was found
2772 // to be abstract. In abstract-declarators, identifier lists are not valid:
2773 // diagnose this.
2774 if (!D.getIdentifier())
2775 Diag(Tok, diag::ext_ident_list_in_param);
2776
2777 // Tok is known to be the first identifier in the list. Remember this
2778 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002779 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002780 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002781 Tok.getLocation(),
2782 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00002783
Chris Lattner50c64772008-04-06 06:39:19 +00002784 ConsumeToken(); // eat the first identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00002785
Chris Lattner66d28652008-04-06 06:34:08 +00002786 while (Tok.is(tok::comma)) {
2787 // Eat the comma.
2788 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002789
Chris Lattner50c64772008-04-06 06:39:19 +00002790 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002791 if (Tok.isNot(tok::identifier)) {
2792 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002793 SkipUntil(tok::r_paren);
2794 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002795 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002796
Chris Lattner66d28652008-04-06 06:34:08 +00002797 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002798
2799 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002800 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002801 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00002802
Chris Lattner66d28652008-04-06 06:34:08 +00002803 // Verify that the argument identifier has not already been mentioned.
2804 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002805 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002806 } else {
2807 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002808 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002809 Tok.getLocation(),
2810 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002811 }
Mike Stump1eb44332009-09-09 15:08:12 +00002812
Chris Lattner66d28652008-04-06 06:34:08 +00002813 // Eat the identifier.
2814 ConsumeToken();
2815 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002816
2817 // If we have the closing ')', eat it and we're done.
2818 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2819
Chris Lattner50c64772008-04-06 06:39:19 +00002820 // Remember that we parsed a function type, and remember the attributes. This
2821 // function type is always a K&R style function type, which is not varargs and
2822 // has no prototype.
2823 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002824 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002825 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002826 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00002827 /*exception*/false,
2828 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002829 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002830 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002831}
Chris Lattneref4715c2008-04-06 05:45:57 +00002832
Reid Spencer5f016e22007-07-11 17:01:13 +00002833/// [C90] direct-declarator '[' constant-expression[opt] ']'
2834/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2835/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2836/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2837/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2838void Parser::ParseBracketDeclarator(Declarator &D) {
2839 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00002840
Chris Lattner378c7e42008-12-18 07:27:21 +00002841 // C array syntax has many features, but by-far the most common is [] and [4].
2842 // This code does a fast path to handle some of the most obvious cases.
2843 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002844 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002845 // Remember that we parsed the empty array type.
2846 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002847 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2848 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002849 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002850 return;
2851 } else if (Tok.getKind() == tok::numeric_constant &&
2852 GetLookAheadToken(1).is(tok::r_square)) {
2853 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002854 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002855 ConsumeToken();
2856
Sebastian Redlab197ba2009-02-09 18:23:29 +00002857 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002858
2859 // If there was an error parsing the assignment-expression, recover.
2860 if (ExprRes.isInvalid())
2861 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00002862
Chris Lattner378c7e42008-12-18 07:27:21 +00002863 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002864 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2865 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002866 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002867 return;
2868 }
Mike Stump1eb44332009-09-09 15:08:12 +00002869
Reid Spencer5f016e22007-07-11 17:01:13 +00002870 // If valid, this location is the position where we read the 'static' keyword.
2871 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002872 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002873 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002874
Reid Spencer5f016e22007-07-11 17:01:13 +00002875 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002876 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002877 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002878 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00002879
Reid Spencer5f016e22007-07-11 17:01:13 +00002880 // If we haven't already read 'static', check to see if there is one after the
2881 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002882 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002883 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002884
Reid Spencer5f016e22007-07-11 17:01:13 +00002885 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2886 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002887 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00002888
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002889 // Handle the case where we have '[*]' as the array size. However, a leading
2890 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2891 // the the token after the star is a ']'. Since stars in arrays are
2892 // infrequent, use of lookahead is not costly here.
2893 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002894 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002895
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002896 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002897 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002898 StaticLoc = SourceLocation(); // Drop the static.
2899 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002900 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002901 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002902 // Note, in C89, this production uses the constant-expr production instead
2903 // of assignment-expr. The only difference is that assignment-expr allows
2904 // things like '=' and '*='. Sema rejects these in C89 mode because they
2905 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00002906
Douglas Gregore0762c92009-06-19 23:52:42 +00002907 // Parse the constant-expression or assignment-expression now (depending
2908 // on dialect).
2909 if (getLang().CPlusPlus)
2910 NumElements = ParseConstantExpression();
2911 else
2912 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00002913 }
Mike Stump1eb44332009-09-09 15:08:12 +00002914
Reid Spencer5f016e22007-07-11 17:01:13 +00002915 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002916 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002917 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002918 // If the expression was invalid, skip it.
2919 SkipUntil(tok::r_square);
2920 return;
2921 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002922
2923 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2924
Chris Lattner378c7e42008-12-18 07:27:21 +00002925 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002926 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2927 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002928 NumElements.release(),
2929 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002930 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002931}
2932
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002933/// [GNU] typeof-specifier:
2934/// typeof ( expressions )
2935/// typeof ( type-name )
2936/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002937///
2938void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002939 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002940 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002941 SourceLocation StartLoc = ConsumeToken();
2942
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002943 bool isCastExpr;
2944 TypeTy *CastTy;
2945 SourceRange CastRange;
2946 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2947 isCastExpr,
2948 CastTy,
2949 CastRange);
2950
2951 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002952 // FIXME: Not accurate, the range gets one token more than it should.
2953 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002954 else
2955 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00002956
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002957 if (isCastExpr) {
2958 if (!CastTy) {
2959 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002960 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002961 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002962
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002963 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002964 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002965 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2966 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002967 DiagID, CastTy))
2968 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002969 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002970 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002971
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002972 // If we get here, the operand to the typeof was an expresion.
2973 if (Operand.isInvalid()) {
2974 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002975 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002976 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002977
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002978 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002979 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002980 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2981 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002982 DiagID, Operand.release()))
2983 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002984}