blob: 6e3a3484309db92a90c177520232b3e447dea67e [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"
Chris Lattnerc46d1a12008-10-20 06:45:43 +000017#include "ExtensionRAIIObject.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/ADT/SmallSet.h"
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// C99 6.7: Declarations.
23//===----------------------------------------------------------------------===//
24
25/// ParseTypeName
26/// type-name: [C99 6.7.6]
27/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000028///
29/// Called type-id in C++.
Sebastian Redlef65f062009-05-29 18:02:33 +000030Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 // Parse the common declaration-specifiers piece.
32 DeclSpec DS;
33 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000034
Reid Spencer5f016e22007-07-11 17:01:13 +000035 // Parse the abstract-declarator, if present.
36 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
37 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000038 if (Range)
39 *Range = DeclaratorInfo.getSourceRange();
40
Chris Lattnereaaebc72009-04-25 08:06:05 +000041 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000042 return true;
43
44 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000045}
46
47/// ParseAttributes - Parse a non-empty attributes list.
48///
49/// [GNU] attributes:
50/// attribute
51/// attributes attribute
52///
53/// [GNU] attribute:
54/// '__attribute__' '(' '(' attribute-list ')' ')'
55///
56/// [GNU] attribute-list:
57/// attrib
58/// attribute_list ',' attrib
59///
60/// [GNU] attrib:
61/// empty
62/// attrib-name
63/// attrib-name '(' identifier ')'
64/// attrib-name '(' identifier ',' nonempty-expr-list ')'
65/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
66///
67/// [GNU] attrib-name:
68/// identifier
69/// typespec
70/// typequal
71/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000072///
Reid Spencer5f016e22007-07-11 17:01:13 +000073/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000074/// token lookahead. Comment from gcc: "If they start with an identifier
75/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000076/// start with that identifier; otherwise they are an expression list."
77///
78/// At the moment, I am not doing 2 token lookahead. I am also unaware of
79/// any attributes that don't work (based on my limited testing). Most
80/// attributes are very simple in practice. Until we find a bug, I don't see
81/// a pressing need to implement the 2 token lookahead.
82
Sebastian Redlab197ba2009-02-09 18:23:29 +000083AttributeList *Parser::ParseAttributes(SourceLocation *EndLoc) {
Chris Lattner04d66662007-10-09 17:33:22 +000084 assert(Tok.is(tok::kw___attribute) && "Not an attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000085
Reid Spencer5f016e22007-07-11 17:01:13 +000086 AttributeList *CurrAttr = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner04d66662007-10-09 17:33:22 +000088 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000089 ConsumeToken();
90 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
91 "attribute")) {
92 SkipUntil(tok::r_paren, true); // skip until ) or ;
93 return CurrAttr;
94 }
95 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
96 SkipUntil(tok::r_paren, true); // skip until ) or ;
97 return CurrAttr;
98 }
99 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000100 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
101 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000102
103 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
105 ConsumeToken();
106 continue;
107 }
108 // we have an identifier or declaration specifier (const, int, etc.)
109 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
110 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 // check if we have a "paramterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000113 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Chris Lattner04d66662007-10-09 17:33:22 +0000116 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
118 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000119
120 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 // __attribute__(( mode(byte) ))
122 ConsumeParen(); // ignore the right paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000123 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000125 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 ConsumeToken();
127 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000128 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 // now parse the non-empty comma separated list of expressions
132 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000133 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000134 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 ArgExprsOk = false;
136 SkipUntil(tok::r_paren);
137 break;
138 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000139 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000140 }
Chris Lattner04d66662007-10-09 17:33:22 +0000141 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 break;
143 ConsumeToken(); // Eat the comma, move to the next argument
144 }
Chris Lattner04d66662007-10-09 17:33:22 +0000145 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 ConsumeParen(); // ignore the right paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000147 CurrAttr = new AttributeList(AttrName, AttrNameLoc, ParmName,
Sebastian Redla55e52c2008-11-25 22:21:31 +0000148 ParmLoc, ArgExprs.take(), ArgExprs.size(), CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 }
150 }
151 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000152 switch (Tok.getKind()) {
153 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000155 // __attribute__(( nonnull() ))
156 ConsumeParen(); // ignore the right paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000157 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000159 break;
160 case tok::kw_char:
161 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000162 case tok::kw_char16_t:
163 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000164 case tok::kw_bool:
165 case tok::kw_short:
166 case tok::kw_int:
167 case tok::kw_long:
168 case tok::kw_signed:
169 case tok::kw_unsigned:
170 case tok::kw_float:
171 case tok::kw_double:
172 case tok::kw_void:
173 case tok::kw_typeof:
174 // If it's a builtin type name, eat it and expect a rparen
175 // __attribute__(( vec_type_hint(char) ))
176 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000177 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Nate Begeman6f3d8382009-06-26 06:32:41 +0000178 0, SourceLocation(), 0, 0, CurrAttr);
179 if (Tok.is(tok::r_paren))
180 ConsumeParen();
181 break;
182 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000184 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 // now parse the list of expressions
188 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000189 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000190 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 ArgExprsOk = false;
192 SkipUntil(tok::r_paren);
193 break;
194 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000195 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 }
Chris Lattner04d66662007-10-09 17:33:22 +0000197 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000198 break;
199 ConsumeToken(); // Eat the comma, move to the next argument
200 }
201 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000202 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000204 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
205 SourceLocation(), ArgExprs.take(), ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 CurrAttr);
207 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000208 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 }
210 }
211 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000212 CurrAttr = new AttributeList(AttrName, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 0, SourceLocation(), 0, 0, CurrAttr);
214 }
215 }
216 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 SkipUntil(tok::r_paren, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +0000218 SourceLocation Loc = Tok.getLocation();;
219 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
220 SkipUntil(tok::r_paren, false);
221 }
222 if (EndLoc)
223 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000224 }
225 return CurrAttr;
226}
227
Eli Friedmana23b4852009-06-08 07:21:15 +0000228/// ParseMicrosoftDeclSpec - Parse an __declspec construct
229///
230/// [MS] decl-specifier:
231/// __declspec ( extended-decl-modifier-seq )
232///
233/// [MS] extended-decl-modifier-seq:
234/// extended-decl-modifier[opt]
235/// extended-decl-modifier extended-decl-modifier-seq
236
Eli Friedman290eeb02009-06-08 23:27:34 +0000237AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000238 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000239
Steve Narofff59e17e2008-12-24 20:59:21 +0000240 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000241 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
242 "declspec")) {
243 SkipUntil(tok::r_paren, true); // skip until ) or ;
244 return CurrAttr;
245 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000246 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000247 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
248 SourceLocation AttrNameLoc = ConsumeToken();
249 if (Tok.is(tok::l_paren)) {
250 ConsumeParen();
251 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
252 // correctly.
253 OwningExprResult ArgExpr(ParseAssignmentExpression());
254 if (!ArgExpr.isInvalid()) {
255 ExprTy* ExprList = ArgExpr.take();
256 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
257 SourceLocation(), &ExprList, 1,
258 CurrAttr, true);
259 }
260 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
261 SkipUntil(tok::r_paren, false);
262 } else {
263 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, SourceLocation(),
264 0, 0, CurrAttr, true);
265 }
266 }
267 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
268 SkipUntil(tok::r_paren, false);
Eli Friedman290eeb02009-06-08 23:27:34 +0000269 return CurrAttr;
270}
271
272AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
273 // Treat these like attributes
274 // FIXME: Allow Sema to distinguish between these and real attributes!
275 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
276 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) ||
277 Tok.is(tok::kw___w64)) {
278 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
279 SourceLocation AttrNameLoc = ConsumeToken();
280 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
281 // FIXME: Support these properly!
282 continue;
283 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
284 SourceLocation(), 0, 0, CurrAttr, true);
285 }
286 return CurrAttr;
Steve Narofff59e17e2008-12-24 20:59:21 +0000287}
288
Reid Spencer5f016e22007-07-11 17:01:13 +0000289/// ParseDeclaration - Parse a full 'declaration', which consists of
290/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000291/// 'Context' should be a Declarator::TheContext value. This returns the
292/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000293///
294/// declaration: [C99 6.7]
295/// block-declaration ->
296/// simple-declaration
297/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000298/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000299/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000300/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000301/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000302/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000303/// others... [FIXME]
304///
Chris Lattner97144fc2009-04-02 04:16:50 +0000305Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
306 SourceLocation &DeclEnd) {
Chris Lattner682bf922009-03-29 16:50:03 +0000307 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000308 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000309 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000310 case tok::kw_export:
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000311 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000312 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000313 case tok::kw_namespace:
Chris Lattner97144fc2009-04-02 04:16:50 +0000314 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000315 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000316 case tok::kw_using:
Chris Lattner97144fc2009-04-02 04:16:50 +0000317 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000318 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000319 case tok::kw_static_assert:
Chris Lattner97144fc2009-04-02 04:16:50 +0000320 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000321 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000322 default:
Chris Lattner97144fc2009-04-02 04:16:50 +0000323 return ParseSimpleDeclaration(Context, DeclEnd);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000324 }
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattner682bf922009-03-29 16:50:03 +0000326 // This routine returns a DeclGroup, if the thing we parsed only contains a
327 // single decl, convert it now.
328 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000329}
330
331/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
332/// declaration-specifiers init-declarator-list[opt] ';'
333///[C90/C++]init-declarator-list ';' [TODO]
334/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000335///
336/// If RequireSemi is false, this does not check for a ';' at the end of the
337/// declaration.
338Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000339 SourceLocation &DeclEnd) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 // Parse the common declaration-specifiers piece.
341 DeclSpec DS;
342 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Reid Spencer5f016e22007-07-11 17:01:13 +0000344 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
345 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000346 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000348 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
349 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 }
Mike Stump1eb44332009-09-09 15:08:12 +0000351
John McCalld8ac0572009-11-03 19:26:08 +0000352 DeclGroupPtrTy DG = ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false,
353 &DeclEnd);
354 return DG;
355}
Mike Stump1eb44332009-09-09 15:08:12 +0000356
John McCalld8ac0572009-11-03 19:26:08 +0000357/// ParseDeclGroup - Having concluded that this is either a function
358/// definition or a group of object declarations, actually parse the
359/// result.
360Parser::DeclGroupPtrTy Parser::ParseDeclGroup(DeclSpec &DS, unsigned Context,
361 bool AllowFunctionDefinitions,
362 SourceLocation *DeclEnd) {
363 // Parse the first declarator.
364 Declarator D(DS, static_cast<Declarator::TheContext>(Context));
365 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000366
John McCalld8ac0572009-11-03 19:26:08 +0000367 // Bail out if the first declarator didn't seem well-formed.
368 if (!D.hasName() && !D.mayOmitIdentifier()) {
369 // Skip until ; or }.
370 SkipUntil(tok::r_brace, true, true);
371 if (Tok.is(tok::semi))
372 ConsumeToken();
373 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000374 }
Mike Stump1eb44332009-09-09 15:08:12 +0000375
John McCalld8ac0572009-11-03 19:26:08 +0000376 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
377 if (isDeclarationAfterDeclarator()) {
378 // Fall though. We have to check this first, though, because
379 // __attribute__ might be the start of a function definition in
380 // (extended) K&R C.
381 } else if (isStartOfFunctionDefinition()) {
382 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
383 Diag(Tok, diag::err_function_declared_typedef);
384
385 // Recover by treating the 'typedef' as spurious.
386 DS.ClearStorageClassSpecs();
387 }
388
389 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
390 return Actions.ConvertDeclToDeclGroup(TheDecl);
391 } else {
392 Diag(Tok, diag::err_expected_fn_body);
393 SkipUntil(tok::semi);
394 return DeclGroupPtrTy();
395 }
396 }
397
398 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
399 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
400 if (FirstDecl.get())
401 DeclsInGroup.push_back(FirstDecl);
402
403 // If we don't have a comma, it is either the end of the list (a ';') or an
404 // error, bail out.
405 while (Tok.is(tok::comma)) {
406 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000407 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000408
409 // Parse the next declarator.
410 D.clear();
411
412 // Accept attributes in an init-declarator. In the first declarator in a
413 // declaration, these would be part of the declspec. In subsequent
414 // declarators, they become part of the declarator itself, so that they
415 // don't apply to declarators after *this* one. Examples:
416 // short __attribute__((common)) var; -> declspec
417 // short var __attribute__((common)); -> declarator
418 // short x, __attribute__((common)) var; -> declarator
419 if (Tok.is(tok::kw___attribute)) {
420 SourceLocation Loc;
421 AttributeList *AttrList = ParseAttributes(&Loc);
422 D.AddAttributes(AttrList, Loc);
423 }
424
425 ParseDeclarator(D);
426
427 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
428 if (ThisDecl.get())
429 DeclsInGroup.push_back(ThisDecl);
430 }
431
432 if (DeclEnd)
433 *DeclEnd = Tok.getLocation();
434
435 if (Context != Declarator::ForContext &&
436 ExpectAndConsume(tok::semi,
437 Context == Declarator::FileContext
438 ? diag::err_invalid_token_after_toplevel_declarator
439 : diag::err_expected_semi_declaration)) {
440 SkipUntil(tok::r_brace, true, true);
441 if (Tok.is(tok::semi))
442 ConsumeToken();
443 }
444
445 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
446 DeclsInGroup.data(),
447 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000448}
449
Douglas Gregor1426e532009-05-12 21:31:51 +0000450/// \brief Parse 'declaration' after parsing 'declaration-specifiers
451/// declarator'. This method parses the remainder of the declaration
452/// (including any attributes or initializer, among other things) and
453/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000454///
Reid Spencer5f016e22007-07-11 17:01:13 +0000455/// init-declarator: [C99 6.7]
456/// declarator
457/// declarator '=' initializer
458/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
459/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000460/// [C++] declarator initializer[opt]
461///
462/// [C++] initializer:
463/// [C++] '=' initializer-clause
464/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000465/// [C++0x] '=' 'default' [TODO]
466/// [C++0x] '=' 'delete'
467///
468/// According to the standard grammar, =default and =delete are function
469/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000470///
Douglas Gregore542c862009-06-23 23:11:28 +0000471Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
472 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000473 // If a simple-asm-expr is present, parse it.
474 if (Tok.is(tok::kw_asm)) {
475 SourceLocation Loc;
476 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
477 if (AsmLabel.isInvalid()) {
478 SkipUntil(tok::semi, true, true);
479 return DeclPtrTy();
480 }
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Douglas Gregor1426e532009-05-12 21:31:51 +0000482 D.setAsmLabel(AsmLabel.release());
483 D.SetRangeEnd(Loc);
484 }
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Douglas Gregor1426e532009-05-12 21:31:51 +0000486 // If attributes are present, parse them.
487 if (Tok.is(tok::kw___attribute)) {
488 SourceLocation Loc;
489 AttributeList *AttrList = ParseAttributes(&Loc);
490 D.AddAttributes(AttrList, Loc);
491 }
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Douglas Gregor1426e532009-05-12 21:31:51 +0000493 // Inform the current actions module that we just parsed this declarator.
Douglas Gregord5a423b2009-09-25 18:43:00 +0000494 DeclPtrTy ThisDecl;
495 switch (TemplateInfo.Kind) {
496 case ParsedTemplateInfo::NonTemplate:
497 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
498 break;
499
500 case ParsedTemplateInfo::Template:
501 case ParsedTemplateInfo::ExplicitSpecialization:
502 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregore542c862009-06-23 23:11:28 +0000503 Action::MultiTemplateParamsArg(Actions,
504 TemplateInfo.TemplateParams->data(),
505 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000506 D);
507 break;
508
509 case ParsedTemplateInfo::ExplicitInstantiation: {
510 Action::DeclResult ThisRes
511 = Actions.ActOnExplicitInstantiation(CurScope,
512 TemplateInfo.ExternLoc,
513 TemplateInfo.TemplateLoc,
514 D);
515 if (ThisRes.isInvalid()) {
516 SkipUntil(tok::semi, true, true);
517 return DeclPtrTy();
518 }
519
520 ThisDecl = ThisRes.get();
521 break;
522 }
523 }
Mike Stump1eb44332009-09-09 15:08:12 +0000524
Douglas Gregor1426e532009-05-12 21:31:51 +0000525 // Parse declarator '=' initializer.
526 if (Tok.is(tok::equal)) {
527 ConsumeToken();
528 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
529 SourceLocation DelLoc = ConsumeToken();
530 Actions.SetDeclDeleted(ThisDecl, DelLoc);
531 } else {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000532 if (getLang().CPlusPlus)
533 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
534
Douglas Gregor1426e532009-05-12 21:31:51 +0000535 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000536
537 if (getLang().CPlusPlus)
538 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
539
Douglas Gregor1426e532009-05-12 21:31:51 +0000540 if (Init.isInvalid()) {
541 SkipUntil(tok::semi, true, true);
542 return DeclPtrTy();
543 }
Anders Carlsson9abf2ae2009-08-16 05:13:48 +0000544 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000545 }
546 } else if (Tok.is(tok::l_paren)) {
547 // Parse C++ direct initializer: '(' expression-list ')'
548 SourceLocation LParenLoc = ConsumeParen();
549 ExprVector Exprs(Actions);
550 CommaLocsTy CommaLocs;
551
552 if (ParseExpressionList(Exprs, CommaLocs)) {
553 SkipUntil(tok::r_paren);
554 } else {
555 // Match the ')'.
556 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
557
558 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
559 "Unexpected number of commas!");
560 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
561 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000562 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000563 }
564 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000565 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000566 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
567 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000568 }
569
570 return ThisDecl;
571}
572
Reid Spencer5f016e22007-07-11 17:01:13 +0000573/// ParseSpecifierQualifierList
574/// specifier-qualifier-list:
575/// type-specifier specifier-qualifier-list[opt]
576/// type-qualifier specifier-qualifier-list[opt]
577/// [GNU] attributes specifier-qualifier-list[opt]
578///
579void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
580 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
581 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 // Validate declspec for type-name.
585 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000586 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
587 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000589
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 // Issue diagnostic and remove storage class if present.
591 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
592 if (DS.getStorageClassSpecLoc().isValid())
593 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
594 else
595 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
596 DS.ClearStorageClassSpecs();
597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 // Issue diagnostic and remove function specfier if present.
600 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000601 if (DS.isInlineSpecified())
602 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
603 if (DS.isVirtualSpecified())
604 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
605 if (DS.isExplicitSpecified())
606 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000607 DS.ClearFunctionSpecs();
608 }
609}
610
Chris Lattnerc199ab32009-04-12 20:42:31 +0000611/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
612/// specified token is valid after the identifier in a declarator which
613/// immediately follows the declspec. For example, these things are valid:
614///
615/// int x [ 4]; // direct-declarator
616/// int x ( int y); // direct-declarator
617/// int(int x ) // direct-declarator
618/// int x ; // simple-declaration
619/// int x = 17; // init-declarator-list
620/// int x , y; // init-declarator-list
621/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000622/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000623/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000624///
625/// This is not, because 'x' does not immediately follow the declspec (though
626/// ')' happens to be valid anyway).
627/// int (x)
628///
629static bool isValidAfterIdentifierInDeclarator(const Token &T) {
630 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
631 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000632 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000633}
634
Chris Lattnere40c2952009-04-14 21:34:55 +0000635
636/// ParseImplicitInt - This method is called when we have an non-typename
637/// identifier in a declspec (which normally terminates the decl spec) when
638/// the declspec has no type specifier. In this case, the declspec is either
639/// malformed or is "implicit int" (in K&R and C89).
640///
641/// This method handles diagnosing this prettily and returns false if the
642/// declspec is done being processed. If it recovers and thinks there may be
643/// other pieces of declspec after it, it returns true.
644///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000645bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000646 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000647 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000648 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Chris Lattnere40c2952009-04-14 21:34:55 +0000650 SourceLocation Loc = Tok.getLocation();
651 // If we see an identifier that is not a type name, we normally would
652 // parse it as the identifer being declared. However, when a typename
653 // is typo'd or the definition is not included, this will incorrectly
654 // parse the typename as the identifier name and fall over misparsing
655 // later parts of the diagnostic.
656 //
657 // As such, we try to do some look-ahead in cases where this would
658 // otherwise be an "implicit-int" case to see if this is invalid. For
659 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
660 // an identifier with implicit int, we'd get a parse error because the
661 // next token is obviously invalid for a type. Parse these as a case
662 // with an invalid type specifier.
663 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Chris Lattnere40c2952009-04-14 21:34:55 +0000665 // Since we know that this either implicit int (which is rare) or an
666 // error, we'd do lookahead to try to do better recovery.
667 if (isValidAfterIdentifierInDeclarator(NextToken())) {
668 // If this token is valid for implicit int, e.g. "static x = 4", then
669 // we just avoid eating the identifier, so it will be parsed as the
670 // identifier in the declarator.
671 return false;
672 }
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Chris Lattnere40c2952009-04-14 21:34:55 +0000674 // Otherwise, if we don't consume this token, we are going to emit an
675 // error anyway. Try to recover from various common problems. Check
676 // to see if this was a reference to a tag name without a tag specified.
677 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000678 //
679 // C++ doesn't need this, and isTagName doesn't take SS.
680 if (SS == 0) {
681 const char *TagName = 0;
682 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000683
Chris Lattnere40c2952009-04-14 21:34:55 +0000684 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
685 default: break;
686 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
687 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
688 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
689 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
690 }
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Chris Lattnerf4382f52009-04-14 22:17:06 +0000692 if (TagName) {
693 Diag(Loc, diag::err_use_of_tag_name_without_tag)
694 << Tok.getIdentifierInfo() << TagName
695 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Chris Lattnerf4382f52009-04-14 22:17:06 +0000697 // Parse this as a tag as if the missing tag were present.
698 if (TagKind == tok::kw_enum)
699 ParseEnumSpecifier(Loc, DS, AS);
700 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000701 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000702 return true;
703 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000704 }
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Douglas Gregora786fdb2009-10-13 23:27:22 +0000706 // This is almost certainly an invalid type name. Let the action emit a
707 // diagnostic and attempt to recover.
708 Action::TypeTy *T = 0;
709 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
710 CurScope, SS, T)) {
711 // The action emitted a diagnostic, so we don't have to.
712 if (T) {
713 // The action has suggested that the type T could be used. Set that as
714 // the type in the declaration specifiers, consume the would-be type
715 // name token, and we're done.
716 const char *PrevSpec;
717 unsigned DiagID;
718 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
719 false);
720 DS.SetRangeEnd(Tok.getLocation());
721 ConsumeToken();
722
723 // There may be other declaration specifiers after this.
724 return true;
725 }
726
727 // Fall through; the action had no suggestion for us.
728 } else {
729 // The action did not emit a diagnostic, so emit one now.
730 SourceRange R;
731 if (SS) R = SS->getRange();
732 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
733 }
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Douglas Gregora786fdb2009-10-13 23:27:22 +0000735 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000736 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000737 unsigned DiagID;
738 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000739 DS.SetRangeEnd(Tok.getLocation());
740 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Chris Lattnere40c2952009-04-14 21:34:55 +0000742 // TODO: Could inject an invalid typedef decl in an enclosing scope to
743 // avoid rippling error messages on subsequent uses of the same type,
744 // could be useful if #include was forgotten.
745 return false;
746}
747
Reid Spencer5f016e22007-07-11 17:01:13 +0000748/// ParseDeclarationSpecifiers
749/// declaration-specifiers: [C99 6.7]
750/// storage-class-specifier declaration-specifiers[opt]
751/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000752/// [C99] function-specifier declaration-specifiers[opt]
753/// [GNU] attributes declaration-specifiers[opt]
754///
755/// storage-class-specifier: [C99 6.7.1]
756/// 'typedef'
757/// 'extern'
758/// 'static'
759/// 'auto'
760/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000761/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000762/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000763/// function-specifier: [C99 6.7.4]
764/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000765/// [C++] 'virtual'
766/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000767/// 'friend': [C++ dcl.friend]
768
Reid Spencer5f016e22007-07-11 17:01:13 +0000769///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000770void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000771 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000772 AccessSpecifier AS,
773 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000774 if (Tok.is(tok::code_completion)) {
775 Actions.CodeCompleteOrdinaryName(CurScope);
776 ConsumeToken();
777 }
778
Chris Lattner81c018d2008-03-13 06:29:04 +0000779 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000781 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000782 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000783 unsigned DiagID = 0;
784
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000786
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000788 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000789 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000790 // If this is not a declaration specifier token, we're done reading decl
791 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000792 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000793 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Chris Lattner5e02c472009-01-05 00:07:25 +0000795 case tok::coloncolon: // ::foo::bar
796 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000797 if (TryAnnotateCXXScopeToken(true))
Chris Lattner5e02c472009-01-05 00:07:25 +0000798 continue;
799 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000800
801 case tok::annot_cxxscope: {
802 if (DS.hasTypeSpecifier())
803 goto DoneWithDeclSpec;
804
805 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000806 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000807 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000808 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000809 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000810 // We have a qualified template-id, e.g., N::A<int>
811 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000812 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000813 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000814 "ParseOptionalCXXScopeSpecifier not working");
815 AnnotateTemplateIdTokenAsType(&SS);
816 continue;
817 }
818
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000819 if (Next.is(tok::annot_typename)) {
820 // FIXME: is this scope-specifier getting dropped?
821 ConsumeToken(); // the scope-specifier
822 if (Tok.getAnnotationValue())
823 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
824 PrevSpec, DiagID,
825 Tok.getAnnotationValue());
826 else
827 DS.SetTypeSpecError();
828 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
829 ConsumeToken(); // The typename
830 }
831
Douglas Gregor9135c722009-03-25 15:40:00 +0000832 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000833 goto DoneWithDeclSpec;
834
835 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000836 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000837 SS.setRange(Tok.getAnnotationRange());
838
839 // If the next token is the name of the class type that the C++ scope
840 // denotes, followed by a '(', then this is a constructor declaration.
841 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000842 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000843 CurScope, &SS) &&
844 GetLookAheadToken(2).is(tok::l_paren))
845 goto DoneWithDeclSpec;
846
Douglas Gregorb696ea32009-02-04 17:00:24 +0000847 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
848 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000849
Chris Lattnerf4382f52009-04-14 22:17:06 +0000850 // If the referenced identifier is not a type, then this declspec is
851 // erroneous: We already checked about that it has no type specifier, and
852 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000853 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000854 if (TypeRep == 0) {
855 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000856 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000857 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000858 }
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000860 ConsumeToken(); // The C++ scope.
861
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000862 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000863 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000864 if (isInvalid)
865 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000867 DS.SetRangeEnd(Tok.getLocation());
868 ConsumeToken(); // The typename.
869
870 continue;
871 }
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Chris Lattner80d0c892009-01-21 19:48:37 +0000873 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000874 if (Tok.getAnnotationValue())
875 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000876 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +0000877 else
878 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000879 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
880 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattner80d0c892009-01-21 19:48:37 +0000882 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
883 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
884 // Objective-C interface. If we don't have Objective-C or a '<', this is
885 // just a normal reference to a typedef name.
886 if (!Tok.is(tok::less) || !getLang().ObjC1)
887 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000889 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000890 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000891 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
892 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
893 LAngleLoc, EndProtoLoc);
894 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
895 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Chris Lattner80d0c892009-01-21 19:48:37 +0000897 DS.SetRangeEnd(EndProtoLoc);
898 continue;
899 }
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Chris Lattner3bd934a2008-07-26 01:18:38 +0000901 // typedef-name
902 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000903 // In C++, check to see if this is a scope specifier like foo::bar::, if
904 // so handle it as such. This is important for ctor parsing.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000905 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner837acd02009-01-21 19:19:26 +0000906 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Chris Lattner3bd934a2008-07-26 01:18:38 +0000908 // This identifier can only be a typedef name if we haven't already seen
909 // a type-specifier. Without this check we misparse:
910 // typedef int X; struct Y { short X; }; as 'short int'.
911 if (DS.hasTypeSpecifier())
912 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +0000913
Chris Lattner3bd934a2008-07-26 01:18:38 +0000914 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +0000915 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +0000916 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000917
Chris Lattnerc199ab32009-04-12 20:42:31 +0000918 // If this is not a typedef name, don't parse it as part of the declspec,
919 // it must be an implicit int or an error.
920 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000921 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000922 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000923 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000924
Douglas Gregorb48fe382008-10-31 09:07:45 +0000925 // C++: If the identifier is actually the name of the class type
926 // being defined and the next token is a '(', then this is a
927 // constructor declaration. We're done with the decl-specifiers
928 // and will treat this token as an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000929 if (getLang().CPlusPlus &&
930 (CurScope->isClassScope() ||
931 (CurScope->isTemplateParamScope() &&
Douglas Gregordec06662009-08-21 18:42:58 +0000932 CurScope->getParent()->isClassScope())) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000933 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000934 NextToken().getKind() == tok::l_paren)
935 goto DoneWithDeclSpec;
936
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000937 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000938 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000939 if (isInvalid)
940 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Chris Lattner3bd934a2008-07-26 01:18:38 +0000942 DS.SetRangeEnd(Tok.getLocation());
943 ConsumeToken(); // The identifier
944
945 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
946 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
947 // Objective-C interface. If we don't have Objective-C or a '<', this is
948 // just a normal reference to a typedef name.
949 if (!Tok.is(tok::less) || !getLang().ObjC1)
950 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000952 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000953 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000954 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
955 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
956 LAngleLoc, EndProtoLoc);
957 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
958 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Chris Lattner3bd934a2008-07-26 01:18:38 +0000960 DS.SetRangeEnd(EndProtoLoc);
961
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000962 // Need to support trailing type qualifiers (e.g. "id<p> const").
963 // If a type specifier follows, it will be diagnosed elsewhere.
964 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000965 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000966
967 // type-name
968 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +0000969 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000970 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000971 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000972 // This template-id does not refer to a type name, so we're
973 // done with the type-specifiers.
974 goto DoneWithDeclSpec;
975 }
976
977 // Turn the template-id annotation token into a type annotation
978 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000979 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000980 continue;
981 }
982
Reid Spencer5f016e22007-07-11 17:01:13 +0000983 // GNU attributes support.
984 case tok::kw___attribute:
985 DS.AddAttributes(ParseAttributes());
986 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000987
988 // Microsoft declspec support.
989 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +0000990 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +0000991 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Steve Naroff239f0732008-12-25 14:16:32 +0000993 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000994 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +0000995 // FIXME: Add handling here!
996 break;
997
998 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000999 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001000 case tok::kw___cdecl:
1001 case tok::kw___stdcall:
1002 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001003 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1004 continue;
1005
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 // storage-class-specifier
1007 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001008 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1009 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 break;
1011 case tok::kw_extern:
1012 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001013 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001014 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1015 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001017 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001018 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001019 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001020 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 case tok::kw_static:
1022 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001023 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001024 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1025 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001026 break;
1027 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001028 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001029 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1030 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001031 else
John McCallfec54012009-08-03 20:12:06 +00001032 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1033 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001034 break;
1035 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001036 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1037 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001038 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001039 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001040 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1041 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001042 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001043 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001044 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001046
Reid Spencer5f016e22007-07-11 17:01:13 +00001047 // function-specifier
1048 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001049 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001051 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001052 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001053 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001054 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001055 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001056 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001057
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001058 // friend
1059 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001060 if (DSContext == DSC_class)
1061 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1062 else {
1063 PrevSpec = ""; // not actually used by the diagnostic
1064 DiagID = diag::err_friend_invalid_in_context;
1065 isInvalid = true;
1066 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001067 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Chris Lattner80d0c892009-01-21 19:48:37 +00001069 // type-specifier
1070 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001071 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1072 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001073 break;
1074 case tok::kw_long:
1075 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001076 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1077 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001078 else
John McCallfec54012009-08-03 20:12:06 +00001079 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1080 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001081 break;
1082 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001083 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1084 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001085 break;
1086 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001087 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1088 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001089 break;
1090 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001091 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1092 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001093 break;
1094 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001095 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1096 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001097 break;
1098 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001099 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1100 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001101 break;
1102 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001103 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1104 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001105 break;
1106 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001107 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1108 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001109 break;
1110 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001111 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1112 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001113 break;
1114 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001115 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1116 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001117 break;
1118 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001119 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1120 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001121 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001122 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001123 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1124 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001125 break;
1126 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001127 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1128 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001129 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001130 case tok::kw_bool:
1131 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001132 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1133 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001134 break;
1135 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001136 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1137 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001138 break;
1139 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001140 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1141 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001142 break;
1143 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001144 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1145 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001146 break;
1147
1148 // class-specifier:
1149 case tok::kw_class:
1150 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001151 case tok::kw_union: {
1152 tok::TokenKind Kind = Tok.getKind();
1153 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001154 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001155 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001156 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001157
1158 // enum-specifier:
1159 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001160 ConsumeToken();
1161 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001162 continue;
1163
1164 // cv-qualifier:
1165 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001166 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1167 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001168 break;
1169 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001170 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1171 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001172 break;
1173 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001174 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1175 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001176 break;
1177
Douglas Gregord57959a2009-03-27 23:10:48 +00001178 // C++ typename-specifier:
1179 case tok::kw_typename:
1180 if (TryAnnotateTypeOrScopeToken())
1181 continue;
1182 break;
1183
Chris Lattner80d0c892009-01-21 19:48:37 +00001184 // GNU typeof support.
1185 case tok::kw_typeof:
1186 ParseTypeofSpecifier(DS);
1187 continue;
1188
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001189 case tok::kw_decltype:
1190 ParseDecltypeSpecifier(DS);
1191 continue;
1192
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001193 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001194 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001195 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1196 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001197 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001198 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Chris Lattnerbce61352008-07-26 00:20:22 +00001200 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001201 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001202 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001203 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1204 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1205 LAngleLoc, EndProtoLoc);
1206 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1207 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001208 DS.SetRangeEnd(EndProtoLoc);
1209
Chris Lattner1ab3b962008-11-18 07:48:38 +00001210 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001211 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001212 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001213 // Need to support trailing type qualifiers (e.g. "id<p> const").
1214 // If a type specifier follows, it will be diagnosed elsewhere.
1215 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001216 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001217 }
John McCallfec54012009-08-03 20:12:06 +00001218 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 if (isInvalid) {
1220 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001221 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001222 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001223 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001224 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001225 ConsumeToken();
1226 }
1227}
Douglas Gregoradcac882008-12-01 23:54:00 +00001228
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001229/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001230/// primarily follow the C++ grammar with additions for C99 and GNU,
1231/// which together subsume the C grammar. Note that the C++
1232/// type-specifier also includes the C type-qualifier (for const,
1233/// volatile, and C99 restrict). Returns true if a type-specifier was
1234/// found (and parsed), false otherwise.
1235///
1236/// type-specifier: [C++ 7.1.5]
1237/// simple-type-specifier
1238/// class-specifier
1239/// enum-specifier
1240/// elaborated-type-specifier [TODO]
1241/// cv-qualifier
1242///
1243/// cv-qualifier: [C++ 7.1.5.1]
1244/// 'const'
1245/// 'volatile'
1246/// [C99] 'restrict'
1247///
1248/// simple-type-specifier: [ C++ 7.1.5.2]
1249/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1250/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1251/// 'char'
1252/// 'wchar_t'
1253/// 'bool'
1254/// 'short'
1255/// 'int'
1256/// 'long'
1257/// 'signed'
1258/// 'unsigned'
1259/// 'float'
1260/// 'double'
1261/// 'void'
1262/// [C99] '_Bool'
1263/// [C99] '_Complex'
1264/// [C99] '_Imaginary' // Removed in TC2?
1265/// [GNU] '_Decimal32'
1266/// [GNU] '_Decimal64'
1267/// [GNU] '_Decimal128'
1268/// [GNU] typeof-specifier
1269/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1270/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001271/// [C++0x] 'decltype' ( expression )
John McCallfec54012009-08-03 20:12:06 +00001272bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001273 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001274 unsigned &DiagID,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001275 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001276 SourceLocation Loc = Tok.getLocation();
1277
1278 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001279 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001280 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001281 // Annotate typenames and C++ scope specifiers. If we get one, just
1282 // recurse to handle whatever we get.
1283 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001284 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1285 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001286 // Otherwise, not a type specifier.
1287 return false;
1288 case tok::coloncolon: // ::foo::bar
1289 if (NextToken().is(tok::kw_new) || // ::new
1290 NextToken().is(tok::kw_delete)) // ::delete
1291 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Chris Lattner166a8fc2009-01-04 23:41:41 +00001293 // Annotate typenames and C++ scope specifiers. If we get one, just
1294 // recurse to handle whatever we get.
1295 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001296 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1297 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001298 // Otherwise, not a type specifier.
1299 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Douglas Gregor12e083c2008-11-07 15:42:26 +00001301 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001302 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001303 if (Tok.getAnnotationValue())
1304 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001305 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001306 else
1307 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001308 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1309 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Douglas Gregor12e083c2008-11-07 15:42:26 +00001311 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1312 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1313 // Objective-C interface. If we don't have Objective-C or a '<', this is
1314 // just a normal reference to a typedef name.
1315 if (!Tok.is(tok::less) || !getLang().ObjC1)
1316 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001318 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001319 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001320 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1321 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1322 LAngleLoc, EndProtoLoc);
1323 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1324 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Douglas Gregor12e083c2008-11-07 15:42:26 +00001326 DS.SetRangeEnd(EndProtoLoc);
1327 return true;
1328 }
1329
1330 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001331 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001332 break;
1333 case tok::kw_long:
1334 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001335 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1336 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001337 else
John McCallfec54012009-08-03 20:12:06 +00001338 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1339 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001340 break;
1341 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001342 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001343 break;
1344 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001345 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1346 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001347 break;
1348 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001349 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1350 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001351 break;
1352 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001353 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1354 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001355 break;
1356 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001357 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001358 break;
1359 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001360 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001361 break;
1362 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001363 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001364 break;
1365 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001366 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001367 break;
1368 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001369 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001370 break;
1371 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001372 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001373 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001374 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001375 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001376 break;
1377 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001378 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001379 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001380 case tok::kw_bool:
1381 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001382 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001383 break;
1384 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001385 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1386 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001387 break;
1388 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001389 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1390 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001391 break;
1392 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001393 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1394 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001395 break;
1396
1397 // class-specifier:
1398 case tok::kw_class:
1399 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001400 case tok::kw_union: {
1401 tok::TokenKind Kind = Tok.getKind();
1402 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001403 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001404 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001405 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001406
1407 // enum-specifier:
1408 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001409 ConsumeToken();
1410 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001411 return true;
1412
1413 // cv-qualifier:
1414 case tok::kw_const:
1415 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001416 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001417 break;
1418 case tok::kw_volatile:
1419 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001420 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001421 break;
1422 case tok::kw_restrict:
1423 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001424 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001425 break;
1426
1427 // GNU typeof support.
1428 case tok::kw_typeof:
1429 ParseTypeofSpecifier(DS);
1430 return true;
1431
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001432 // C++0x decltype support.
1433 case tok::kw_decltype:
1434 ParseDecltypeSpecifier(DS);
1435 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001437 // C++0x auto support.
1438 case tok::kw_auto:
1439 if (!getLang().CPlusPlus0x)
1440 return false;
1441
John McCallfec54012009-08-03 20:12:06 +00001442 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001443 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001444 case tok::kw___ptr64:
1445 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001446 case tok::kw___cdecl:
1447 case tok::kw___stdcall:
1448 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001449 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001450 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001451
Douglas Gregor12e083c2008-11-07 15:42:26 +00001452 default:
1453 // Not a type-specifier; do nothing.
1454 return false;
1455 }
1456
1457 // If the specifier combination wasn't legal, issue a diagnostic.
1458 if (isInvalid) {
1459 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001460 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001461 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001462 }
1463 DS.SetRangeEnd(Tok.getLocation());
1464 ConsumeToken(); // whatever we parsed above.
1465 return true;
1466}
Reid Spencer5f016e22007-07-11 17:01:13 +00001467
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001468/// ParseStructDeclaration - Parse a struct declaration without the terminating
1469/// semicolon.
1470///
Reid Spencer5f016e22007-07-11 17:01:13 +00001471/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001472/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001473/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001474/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001475/// struct-declarator-list:
1476/// struct-declarator
1477/// struct-declarator-list ',' struct-declarator
1478/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1479/// struct-declarator:
1480/// declarator
1481/// [GNU] declarator attributes[opt]
1482/// declarator[opt] ':' constant-expression
1483/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1484///
Chris Lattnere1359422008-04-10 06:46:29 +00001485void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001486ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001487 if (Tok.is(tok::kw___extension__)) {
1488 // __extension__ silences extension warnings in the subexpression.
1489 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001490 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001491 return ParseStructDeclaration(DS, Fields);
1492 }
Mike Stump1eb44332009-09-09 15:08:12 +00001493
Steve Naroff28a7ca82007-08-20 22:28:22 +00001494 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001495 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001496 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001498 // If there are no declarators, this is a free-standing declaration
1499 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001500 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001501 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001502 return;
1503 }
1504
1505 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001506 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001507 while (1) {
John McCallbdd563e2009-11-03 02:38:08 +00001508 FieldDeclarator DeclaratorInfo(DS);
1509
1510 // Attributes are only allowed here on successive declarators.
1511 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1512 SourceLocation Loc;
1513 AttributeList *AttrList = ParseAttributes(&Loc);
1514 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1515 }
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Steve Naroff28a7ca82007-08-20 22:28:22 +00001517 /// struct-declarator: declarator
1518 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001519 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001520 ParseDeclarator(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Chris Lattner04d66662007-10-09 17:33:22 +00001522 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001523 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001524 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001525 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001526 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001527 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001528 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001529 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001530
Steve Naroff28a7ca82007-08-20 22:28:22 +00001531 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001532 if (Tok.is(tok::kw___attribute)) {
1533 SourceLocation Loc;
1534 AttributeList *AttrList = ParseAttributes(&Loc);
1535 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1536 }
1537
John McCallbdd563e2009-11-03 02:38:08 +00001538 // We're done with this declarator; invoke the callback.
1539 (void) Fields.invoke(DeclaratorInfo);
1540
Steve Naroff28a7ca82007-08-20 22:28:22 +00001541 // If we don't have a comma, it is either the end of the list (a ';')
1542 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001543 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001544 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001545
Steve Naroff28a7ca82007-08-20 22:28:22 +00001546 // Consume the comma.
1547 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001548
John McCallbdd563e2009-11-03 02:38:08 +00001549 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001550 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001551}
1552
1553/// ParseStructUnionBody
1554/// struct-contents:
1555/// struct-declaration-list
1556/// [EXT] empty
1557/// [GNU] "struct-declaration-list" without terminatoring ';'
1558/// struct-declaration-list:
1559/// struct-declaration
1560/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001561/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001562///
Reid Spencer5f016e22007-07-11 17:01:13 +00001563void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001564 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001565 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1566 PP.getSourceManager(),
1567 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001568
Reid Spencer5f016e22007-07-11 17:01:13 +00001569 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001571 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001572 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1573
Reid Spencer5f016e22007-07-11 17:01:13 +00001574 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1575 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001576 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001577 Diag(Tok, diag::ext_empty_struct_union_enum)
1578 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001579
Chris Lattnerb28317a2009-03-28 19:18:32 +00001580 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001581
Reid Spencer5f016e22007-07-11 17:01:13 +00001582 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001583 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001584 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Reid Spencer5f016e22007-07-11 17:01:13 +00001586 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001587 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001588 Diag(Tok, diag::ext_extra_struct_semi)
1589 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001590 ConsumeToken();
1591 continue;
1592 }
Chris Lattnere1359422008-04-10 06:46:29 +00001593
1594 // Parse all the comma separated declarators.
1595 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001596
John McCallbdd563e2009-11-03 02:38:08 +00001597 if (!Tok.is(tok::at)) {
1598 struct CFieldCallback : FieldCallback {
1599 Parser &P;
1600 DeclPtrTy TagDecl;
1601 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1602
1603 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1604 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1605 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1606
1607 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
1608 const DeclSpec &DS = FD.D.getDeclSpec();
1609 DeclPtrTy Field;
1610
1611 // Install the declarator into the current TagDecl.
1612 if (FD.D.getExtension()) {
1613 // Silences extension warnings
1614 ExtensionRAIIObject O(P.Diags);
1615 Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1616 DS.getSourceRange().getBegin(),
1617 FD.D, FD.BitfieldSize);
1618 } else {
1619 Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1620 DS.getSourceRange().getBegin(),
1621 FD.D, FD.BitfieldSize);
1622 }
1623 FieldDecls.push_back(Field);
1624 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001625 }
John McCallbdd563e2009-11-03 02:38:08 +00001626 } Callback(*this, TagDecl, FieldDecls);
1627
1628 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001629 } else { // Handle @defs
1630 ConsumeToken();
1631 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1632 Diag(Tok, diag::err_unexpected_at);
1633 SkipUntil(tok::semi, true, true);
1634 continue;
1635 }
1636 ConsumeToken();
1637 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1638 if (!Tok.is(tok::identifier)) {
1639 Diag(Tok, diag::err_expected_ident);
1640 SkipUntil(tok::semi, true, true);
1641 continue;
1642 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001643 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001644 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001645 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001646 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1647 ConsumeToken();
1648 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001649 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001650
Chris Lattner04d66662007-10-09 17:33:22 +00001651 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001652 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001653 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001654 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001655 break;
1656 } else {
1657 Diag(Tok, diag::err_expected_semi_decl_list);
1658 // Skip to end of block or statement
1659 SkipUntil(tok::r_brace, true, true);
1660 }
1661 }
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Steve Naroff60fccee2007-10-29 21:38:07 +00001663 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Reid Spencer5f016e22007-07-11 17:01:13 +00001665 AttributeList *AttrList = 0;
1666 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001667 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001668 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001669
1670 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001671 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001672 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001673 AttrList);
1674 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001675 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001676}
1677
1678
1679/// ParseEnumSpecifier
1680/// enum-specifier: [C99 6.7.2.2]
1681/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001682///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001683/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1684/// '}' attributes[opt]
1685/// 'enum' identifier
1686/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001687///
1688/// [C++] elaborated-type-specifier:
1689/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1690///
Chris Lattner4c97d762009-04-12 21:49:30 +00001691void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1692 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001693 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001694 if (Tok.is(tok::code_completion)) {
1695 // Code completion for an enum name.
1696 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1697 ConsumeToken();
1698 }
1699
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001700 AttributeList *Attr = 0;
1701 // If attributes exist after tag, parse them.
1702 if (Tok.is(tok::kw___attribute))
1703 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001704
1705 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001706 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001707 if (Tok.isNot(tok::identifier)) {
1708 Diag(Tok, diag::err_expected_ident);
1709 if (Tok.isNot(tok::l_brace)) {
1710 // Has no name and is not a definition.
1711 // Skip the rest of this declarator, up until the comma or semicolon.
1712 SkipUntil(tok::comma, true);
1713 return;
1714 }
1715 }
1716 }
Mike Stump1eb44332009-09-09 15:08:12 +00001717
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001718 // Must have either 'enum name' or 'enum {...}'.
1719 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1720 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001722 // Skip the rest of this declarator, up until the comma or semicolon.
1723 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001724 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001725 }
Mike Stump1eb44332009-09-09 15:08:12 +00001726
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001727 // If an identifier is present, consume and remember it.
1728 IdentifierInfo *Name = 0;
1729 SourceLocation NameLoc;
1730 if (Tok.is(tok::identifier)) {
1731 Name = Tok.getIdentifierInfo();
1732 NameLoc = ConsumeToken();
1733 }
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001735 // There are three options here. If we have 'enum foo;', then this is a
1736 // forward declaration. If we have 'enum foo {...' then this is a
1737 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1738 //
1739 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1740 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1741 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1742 //
John McCall0f434ec2009-07-31 02:45:11 +00001743 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001744 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001745 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001746 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001747 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001748 else
John McCall0f434ec2009-07-31 02:45:11 +00001749 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001750 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001751 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00001752 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001753 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001754 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001755 Owned, IsDependent);
1756 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Chris Lattner04d66662007-10-09 17:33:22 +00001758 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001759 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Reid Spencer5f016e22007-07-11 17:01:13 +00001761 // TODO: semantic analysis on the declspec for enums.
1762 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001763 unsigned DiagID;
1764 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001765 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001766 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001767}
1768
1769/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1770/// enumerator-list:
1771/// enumerator
1772/// enumerator-list ',' enumerator
1773/// enumerator:
1774/// enumeration-constant
1775/// enumeration-constant '=' constant-expression
1776/// enumeration-constant:
1777/// identifier
1778///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001779void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001780 // Enter the scope of the enum body and start the definition.
1781 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001782 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001783
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001785
Chris Lattner7946dd32007-08-27 17:24:30 +00001786 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001787 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001788 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Chris Lattnerb28317a2009-03-28 19:18:32 +00001790 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001791
Chris Lattnerb28317a2009-03-28 19:18:32 +00001792 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Reid Spencer5f016e22007-07-11 17:01:13 +00001794 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001795 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001796 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1797 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001798
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001800 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001801 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001802 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001803 AssignedVal = ParseConstantExpression();
1804 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001805 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001806 }
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Reid Spencer5f016e22007-07-11 17:01:13 +00001808 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001809 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1810 LastEnumConstDecl,
1811 IdentLoc, Ident,
1812 EqualLoc,
1813 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001814 EnumConstantDecls.push_back(EnumConstDecl);
1815 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001816
Chris Lattner04d66662007-10-09 17:33:22 +00001817 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001818 break;
1819 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001820
1821 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001822 !(getLang().C99 || getLang().CPlusPlus0x))
1823 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1824 << getLang().CPlusPlus
1825 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001826 }
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Reid Spencer5f016e22007-07-11 17:01:13 +00001828 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001829 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001830
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001831 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001832 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001833 if (Tok.is(tok::kw___attribute))
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001834 Attr = ParseAttributes();
Douglas Gregor72de6672009-01-08 20:45:30 +00001835
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001836 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1837 EnumConstantDecls.data(), EnumConstantDecls.size(),
1838 CurScope, Attr);
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Douglas Gregor72de6672009-01-08 20:45:30 +00001840 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001841 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001842}
1843
1844/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001845/// start of a type-qualifier-list.
1846bool Parser::isTypeQualifier() const {
1847 switch (Tok.getKind()) {
1848 default: return false;
1849 // type-qualifier
1850 case tok::kw_const:
1851 case tok::kw_volatile:
1852 case tok::kw_restrict:
1853 return true;
1854 }
1855}
1856
1857/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001858/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001859bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001860 switch (Tok.getKind()) {
1861 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Chris Lattner166a8fc2009-01-04 23:41:41 +00001863 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001864 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001865 // Annotate typenames and C++ scope specifiers. If we get one, just
1866 // recurse to handle whatever we get.
1867 if (TryAnnotateTypeOrScopeToken())
1868 return isTypeSpecifierQualifier();
1869 // Otherwise, not a type specifier.
1870 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001871
Chris Lattner166a8fc2009-01-04 23:41:41 +00001872 case tok::coloncolon: // ::foo::bar
1873 if (NextToken().is(tok::kw_new) || // ::new
1874 NextToken().is(tok::kw_delete)) // ::delete
1875 return false;
1876
1877 // Annotate typenames and C++ scope specifiers. If we get one, just
1878 // recurse to handle whatever we get.
1879 if (TryAnnotateTypeOrScopeToken())
1880 return isTypeSpecifierQualifier();
1881 // Otherwise, not a type specifier.
1882 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Reid Spencer5f016e22007-07-11 17:01:13 +00001884 // GNU attributes support.
1885 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001886 // GNU typeof support.
1887 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Reid Spencer5f016e22007-07-11 17:01:13 +00001889 // type-specifiers
1890 case tok::kw_short:
1891 case tok::kw_long:
1892 case tok::kw_signed:
1893 case tok::kw_unsigned:
1894 case tok::kw__Complex:
1895 case tok::kw__Imaginary:
1896 case tok::kw_void:
1897 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001898 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001899 case tok::kw_char16_t:
1900 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001901 case tok::kw_int:
1902 case tok::kw_float:
1903 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001904 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001905 case tok::kw__Bool:
1906 case tok::kw__Decimal32:
1907 case tok::kw__Decimal64:
1908 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00001909
Chris Lattner99dc9142008-04-13 18:59:07 +00001910 // struct-or-union-specifier (C99) or class-specifier (C++)
1911 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001912 case tok::kw_struct:
1913 case tok::kw_union:
1914 // enum-specifier
1915 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00001916
Reid Spencer5f016e22007-07-11 17:01:13 +00001917 // type-qualifier
1918 case tok::kw_const:
1919 case tok::kw_volatile:
1920 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001921
1922 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001923 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001924 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001925
Chris Lattner7c186be2008-10-20 00:25:30 +00001926 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1927 case tok::less:
1928 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Steve Naroff239f0732008-12-25 14:16:32 +00001930 case tok::kw___cdecl:
1931 case tok::kw___stdcall:
1932 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001933 case tok::kw___w64:
1934 case tok::kw___ptr64:
1935 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001936 }
1937}
1938
1939/// isDeclarationSpecifier() - Return true if the current token is part of a
1940/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001941bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001942 switch (Tok.getKind()) {
1943 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001944
Chris Lattner166a8fc2009-01-04 23:41:41 +00001945 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001946 // Unfortunate hack to support "Class.factoryMethod" notation.
1947 if (getLang().ObjC1 && NextToken().is(tok::period))
1948 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001949 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001950
Douglas Gregord57959a2009-03-27 23:10:48 +00001951 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001952 // Annotate typenames and C++ scope specifiers. If we get one, just
1953 // recurse to handle whatever we get.
1954 if (TryAnnotateTypeOrScopeToken())
1955 return isDeclarationSpecifier();
1956 // Otherwise, not a declaration specifier.
1957 return false;
1958 case tok::coloncolon: // ::foo::bar
1959 if (NextToken().is(tok::kw_new) || // ::new
1960 NextToken().is(tok::kw_delete)) // ::delete
1961 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001962
Chris Lattner166a8fc2009-01-04 23:41:41 +00001963 // Annotate typenames and C++ scope specifiers. If we get one, just
1964 // recurse to handle whatever we get.
1965 if (TryAnnotateTypeOrScopeToken())
1966 return isDeclarationSpecifier();
1967 // Otherwise, not a declaration specifier.
1968 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001969
Reid Spencer5f016e22007-07-11 17:01:13 +00001970 // storage-class-specifier
1971 case tok::kw_typedef:
1972 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001973 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001974 case tok::kw_static:
1975 case tok::kw_auto:
1976 case tok::kw_register:
1977 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Reid Spencer5f016e22007-07-11 17:01:13 +00001979 // type-specifiers
1980 case tok::kw_short:
1981 case tok::kw_long:
1982 case tok::kw_signed:
1983 case tok::kw_unsigned:
1984 case tok::kw__Complex:
1985 case tok::kw__Imaginary:
1986 case tok::kw_void:
1987 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001988 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001989 case tok::kw_char16_t:
1990 case tok::kw_char32_t:
1991
Reid Spencer5f016e22007-07-11 17:01:13 +00001992 case tok::kw_int:
1993 case tok::kw_float:
1994 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001995 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001996 case tok::kw__Bool:
1997 case tok::kw__Decimal32:
1998 case tok::kw__Decimal64:
1999 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00002000
Chris Lattner99dc9142008-04-13 18:59:07 +00002001 // struct-or-union-specifier (C99) or class-specifier (C++)
2002 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002003 case tok::kw_struct:
2004 case tok::kw_union:
2005 // enum-specifier
2006 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Reid Spencer5f016e22007-07-11 17:01:13 +00002008 // type-qualifier
2009 case tok::kw_const:
2010 case tok::kw_volatile:
2011 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002012
Reid Spencer5f016e22007-07-11 17:01:13 +00002013 // function-specifier
2014 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002015 case tok::kw_virtual:
2016 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002017
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002018 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002019 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002020
Chris Lattner1ef08762007-08-09 17:01:07 +00002021 // GNU typeof support.
2022 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Chris Lattner1ef08762007-08-09 17:01:07 +00002024 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002025 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002026 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002027
Chris Lattnerf3948c42008-07-26 03:38:44 +00002028 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2029 case tok::less:
2030 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002031
Steve Naroff47f52092009-01-06 19:34:12 +00002032 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002033 case tok::kw___cdecl:
2034 case tok::kw___stdcall:
2035 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002036 case tok::kw___w64:
2037 case tok::kw___ptr64:
2038 case tok::kw___forceinline:
2039 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002040 }
2041}
2042
2043
2044/// ParseTypeQualifierListOpt
2045/// type-qualifier-list: [C99 6.7.5]
2046/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002047/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002048/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002049/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002050///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002051void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002052 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002053 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002054 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002055 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002056 SourceLocation Loc = Tok.getLocation();
2057
2058 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002059 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002060 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2061 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002062 break;
2063 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002064 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2065 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 break;
2067 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002068 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2069 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002070 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002071 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002072 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002073 case tok::kw___cdecl:
2074 case tok::kw___stdcall:
2075 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002076 if (AttributesAllowed) {
2077 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2078 continue;
2079 }
2080 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002081 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002082 if (AttributesAllowed) {
2083 DS.AddAttributes(ParseAttributes());
2084 continue; // do *not* consume the next token!
2085 }
2086 // otherwise, FALL THROUGH!
2087 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002088 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002089 // If this is not a type-qualifier token, we're done reading type
2090 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002091 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002092 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002093 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002094
Reid Spencer5f016e22007-07-11 17:01:13 +00002095 // If the specifier combination wasn't legal, issue a diagnostic.
2096 if (isInvalid) {
2097 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002098 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002099 }
2100 ConsumeToken();
2101 }
2102}
2103
2104
2105/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2106///
2107void Parser::ParseDeclarator(Declarator &D) {
2108 /// This implements the 'declarator' production in the C grammar, then checks
2109 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002110 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002111}
2112
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002113/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2114/// is parsed by the function passed to it. Pass null, and the direct-declarator
2115/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002116/// ptr-operator production.
2117///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002118/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2119/// [C] pointer[opt] direct-declarator
2120/// [C++] direct-declarator
2121/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002122///
2123/// pointer: [C99 6.7.5]
2124/// '*' type-qualifier-list[opt]
2125/// '*' type-qualifier-list[opt] pointer
2126///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002127/// ptr-operator:
2128/// '*' cv-qualifier-seq[opt]
2129/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002130/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002131/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002132/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002133/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002134void Parser::ParseDeclaratorInternal(Declarator &D,
2135 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002136 if (Diags.hasAllExtensionsSilenced())
2137 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002138 // C++ member pointers start with a '::' or a nested-name.
2139 // Member pointers get special handling, since there's no place for the
2140 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002141 if (getLang().CPlusPlus &&
2142 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2143 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002144 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002145 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002146 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002147 // The scope spec really belongs to the direct-declarator.
2148 D.getCXXScopeSpec() = SS;
2149 if (DirectDeclParser)
2150 (this->*DirectDeclParser)(D);
2151 return;
2152 }
2153
2154 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002155 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002156 DeclSpec DS;
2157 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002158 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002159
2160 // Recurse to parse whatever is left.
2161 ParseDeclaratorInternal(D, DirectDeclParser);
2162
2163 // Sema will have to catch (syntactically invalid) pointers into global
2164 // scope. It has to catch pointers into namespace scope anyway.
2165 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002166 Loc, DS.TakeAttributes()),
2167 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002168 return;
2169 }
2170 }
2171
2172 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002173 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002174 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002175 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002176 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002177 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002178 if (DirectDeclParser)
2179 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002180 return;
2181 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002182
Sebastian Redl05532f22009-03-15 22:02:01 +00002183 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2184 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002185 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002186 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002187
Chris Lattner9af55002009-03-27 04:18:06 +00002188 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002189 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002190 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002191
Reid Spencer5f016e22007-07-11 17:01:13 +00002192 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002193 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002194
Reid Spencer5f016e22007-07-11 17:01:13 +00002195 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002196 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002197 if (Kind == tok::star)
2198 // Remember that we parsed a pointer type, and remember the type-quals.
2199 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002200 DS.TakeAttributes()),
2201 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002202 else
2203 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002204 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002205 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002206 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002207 } else {
2208 // Is a reference
2209 DeclSpec DS;
2210
Sebastian Redl743de1f2009-03-23 00:00:23 +00002211 // Complain about rvalue references in C++03, but then go on and build
2212 // the declarator.
2213 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2214 Diag(Loc, diag::err_rvalue_reference);
2215
Reid Spencer5f016e22007-07-11 17:01:13 +00002216 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2217 // cv-qualifiers are introduced through the use of a typedef or of a
2218 // template type argument, in which case the cv-qualifiers are ignored.
2219 //
2220 // [GNU] Retricted references are allowed.
2221 // [GNU] Attributes on references are allowed.
2222 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002223 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002224
2225 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2226 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2227 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002228 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002229 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2230 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002231 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002232 }
2233
2234 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002235 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002236
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002237 if (D.getNumTypeObjects() > 0) {
2238 // C++ [dcl.ref]p4: There shall be no references to references.
2239 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2240 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002241 if (const IdentifierInfo *II = D.getIdentifier())
2242 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2243 << II;
2244 else
2245 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2246 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002247
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002248 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002249 // can go ahead and build the (technically ill-formed)
2250 // declarator: reference collapsing will take care of it.
2251 }
2252 }
2253
Reid Spencer5f016e22007-07-11 17:01:13 +00002254 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002255 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002256 DS.TakeAttributes(),
2257 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002258 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002259 }
2260}
2261
2262/// ParseDirectDeclarator
2263/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002264/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002265/// '(' declarator ')'
2266/// [GNU] '(' attributes declarator ')'
2267/// [C90] direct-declarator '[' constant-expression[opt] ']'
2268/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2269/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2270/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2271/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2272/// direct-declarator '(' parameter-type-list ')'
2273/// direct-declarator '(' identifier-list[opt] ')'
2274/// [GNU] direct-declarator '(' parameter-forward-declarations
2275/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002276/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2277/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002278/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002279///
2280/// declarator-id: [C++ 8]
2281/// id-expression
2282/// '::'[opt] nested-name-specifier[opt] type-name
2283///
2284/// id-expression: [C++ 5.1]
2285/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002286/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002287///
2288/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002289/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002290/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002291/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002292/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002293/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002294///
Reid Spencer5f016e22007-07-11 17:01:13 +00002295void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002296 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002297
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002298 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2299 // ParseDeclaratorInternal might already have parsed the scope.
2300 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2301 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2302 true);
2303 if (afterCXXScope) {
2304 // Change the declaration context for name lookup, until this function
2305 // is exited (and the declarator has been parsed).
2306 DeclScopeObj.EnterDeclaratorScope();
2307 }
2308
2309 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2310 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2311 // We found something that indicates the start of an unqualified-id.
2312 // Parse that unqualified-id.
2313 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2314 /*EnteringContext=*/true,
2315 /*AllowDestructorName=*/true,
2316 /*AllowConstructorName=*/!D.getDeclSpec().hasTypeSpecifier(),
2317 D.getName())) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002318 D.SetIdentifier(0, Tok.getLocation());
2319 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002320 } else {
2321 // Parsed the unqualified-id; update range information and move along.
2322 if (D.getSourceRange().getBegin().isInvalid())
2323 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2324 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002325 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002326 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002327 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002328 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002329 assert(!getLang().CPlusPlus &&
2330 "There's a C++-specific check for tok::identifier above");
2331 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2332 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2333 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002334 goto PastIdentifier;
2335 }
2336
2337 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002338 // direct-declarator: '(' declarator ')'
2339 // direct-declarator: '(' attributes declarator ')'
2340 // Example: 'char (*X)' or 'int (*XX)(void)'
2341 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002342 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002343 // This could be something simple like "int" (in which case the declarator
2344 // portion is empty), if an abstract-declarator is allowed.
2345 D.SetIdentifier(0, Tok.getLocation());
2346 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002347 if (D.getContext() == Declarator::MemberContext)
2348 Diag(Tok, diag::err_expected_member_name_or_semi)
2349 << D.getDeclSpec().getSourceRange();
2350 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002351 Diag(Tok, diag::err_expected_unqualified_id);
2352 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002353 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002354 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002355 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002356 }
Mike Stump1eb44332009-09-09 15:08:12 +00002357
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002358 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002359 assert(D.isPastIdentifier() &&
2360 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002361
Reid Spencer5f016e22007-07-11 17:01:13 +00002362 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002363 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002364 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2365 // In such a case, check if we actually have a function declarator; if it
2366 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002367 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2368 // When not in file scope, warn for ambiguous function declarators, just
2369 // in case the author intended it as a variable definition.
2370 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2371 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2372 break;
2373 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002374 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002375 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002376 ParseBracketDeclarator(D);
2377 } else {
2378 break;
2379 }
2380 }
2381}
2382
Chris Lattneref4715c2008-04-06 05:45:57 +00002383/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2384/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002385/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002386/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2387///
2388/// direct-declarator:
2389/// '(' declarator ')'
2390/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002391/// direct-declarator '(' parameter-type-list ')'
2392/// direct-declarator '(' identifier-list[opt] ')'
2393/// [GNU] direct-declarator '(' parameter-forward-declarations
2394/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002395///
2396void Parser::ParseParenDeclarator(Declarator &D) {
2397 SourceLocation StartLoc = ConsumeParen();
2398 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002399
Chris Lattner7399ee02008-10-20 02:05:46 +00002400 // Eat any attributes before we look at whether this is a grouping or function
2401 // declarator paren. If this is a grouping paren, the attribute applies to
2402 // the type being built up, for example:
2403 // int (__attribute__(()) *x)(long y)
2404 // If this ends up not being a grouping paren, the attribute applies to the
2405 // first argument, for example:
2406 // int (__attribute__(()) int x)
2407 // In either case, we need to eat any attributes to be able to determine what
2408 // sort of paren this is.
2409 //
2410 AttributeList *AttrList = 0;
2411 bool RequiresArg = false;
2412 if (Tok.is(tok::kw___attribute)) {
2413 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00002414
Chris Lattner7399ee02008-10-20 02:05:46 +00002415 // We require that the argument list (if this is a non-grouping paren) be
2416 // present even if the attribute list was empty.
2417 RequiresArg = true;
2418 }
Steve Naroff239f0732008-12-25 14:16:32 +00002419 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002420 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2421 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2422 Tok.is(tok::kw___ptr64)) {
2423 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2424 }
Mike Stump1eb44332009-09-09 15:08:12 +00002425
Chris Lattneref4715c2008-04-06 05:45:57 +00002426 // If we haven't past the identifier yet (or where the identifier would be
2427 // stored, if this is an abstract declarator), then this is probably just
2428 // grouping parens. However, if this could be an abstract-declarator, then
2429 // this could also be the start of function arguments (consider 'void()').
2430 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002431
Chris Lattneref4715c2008-04-06 05:45:57 +00002432 if (!D.mayOmitIdentifier()) {
2433 // If this can't be an abstract-declarator, this *must* be a grouping
2434 // paren, because we haven't seen the identifier yet.
2435 isGrouping = true;
2436 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002437 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002438 isDeclarationSpecifier()) { // 'int(int)' is a function.
2439 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2440 // considered to be a type, not a K&R identifier-list.
2441 isGrouping = false;
2442 } else {
2443 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2444 isGrouping = true;
2445 }
Mike Stump1eb44332009-09-09 15:08:12 +00002446
Chris Lattneref4715c2008-04-06 05:45:57 +00002447 // If this is a grouping paren, handle:
2448 // direct-declarator: '(' declarator ')'
2449 // direct-declarator: '(' attributes declarator ')'
2450 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002451 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002452 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002453 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002454 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002455
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002456 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002457 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002458 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002459
2460 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002461 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002462 return;
2463 }
Mike Stump1eb44332009-09-09 15:08:12 +00002464
Chris Lattneref4715c2008-04-06 05:45:57 +00002465 // Okay, if this wasn't a grouping paren, it must be the start of a function
2466 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002467 // identifier (and remember where it would have been), then call into
2468 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002469 D.SetIdentifier(0, Tok.getLocation());
2470
Chris Lattner7399ee02008-10-20 02:05:46 +00002471 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002472}
2473
2474/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2475/// declarator D up to a paren, which indicates that we are parsing function
2476/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002477///
Chris Lattner7399ee02008-10-20 02:05:46 +00002478/// If AttrList is non-null, then the caller parsed those arguments immediately
2479/// after the open paren - they should be considered to be the first argument of
2480/// a parameter. If RequiresArg is true, then the first argument of the
2481/// function is required to be present and required to not be an identifier
2482/// list.
2483///
Reid Spencer5f016e22007-07-11 17:01:13 +00002484/// This method also handles this portion of the grammar:
2485/// parameter-type-list: [C99 6.7.5]
2486/// parameter-list
2487/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002488/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002489///
2490/// parameter-list: [C99 6.7.5]
2491/// parameter-declaration
2492/// parameter-list ',' parameter-declaration
2493///
2494/// parameter-declaration: [C99 6.7.5]
2495/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002496/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002497/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002498/// declaration-specifiers abstract-declarator[opt]
2499/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002500/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002501/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2502///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002503/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002504/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002505///
Chris Lattner7399ee02008-10-20 02:05:46 +00002506void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2507 AttributeList *AttrList,
2508 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002509 // lparen is already consumed!
2510 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002511
Chris Lattner7399ee02008-10-20 02:05:46 +00002512 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002513 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002514 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002515 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002516 delete AttrList;
2517 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002518
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002519 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2520 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002521
2522 // cv-qualifier-seq[opt].
2523 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002524 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002525 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002526 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002527 llvm::SmallVector<TypeTy*, 2> Exceptions;
2528 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002529 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002530 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002531 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002532 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002533
2534 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002535 if (Tok.is(tok::kw_throw)) {
2536 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002537 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002538 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002539 hasAnyExceptionSpec);
2540 assert(Exceptions.size() == ExceptionRanges.size() &&
2541 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002542 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002543 }
2544
Chris Lattnerf97409f2008-04-06 06:57:35 +00002545 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002546 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002547 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002548 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002549 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002550 /*arglist*/ 0, 0,
2551 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002552 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002553 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002554 Exceptions.data(),
2555 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002556 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002557 LParenLoc, RParenLoc, D),
2558 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002559 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002560 }
2561
Chris Lattner7399ee02008-10-20 02:05:46 +00002562 // Alternatively, this parameter list may be an identifier list form for a
2563 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002564 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002565 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002566 // K&R identifier lists can't have typedefs as identifiers, per
2567 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002568 if (RequiresArg) {
2569 Diag(Tok, diag::err_argument_required_after_attribute);
2570 delete AttrList;
2571 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002572 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2573 // normal declarators, not for abstract-declarators.
2574 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002575 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002576 }
Mike Stump1eb44332009-09-09 15:08:12 +00002577
Chris Lattnerf97409f2008-04-06 06:57:35 +00002578 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002579
Chris Lattnerf97409f2008-04-06 06:57:35 +00002580 // Build up an array of information about the parsed arguments.
2581 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002582
2583 // Enter function-declaration scope, limiting any declarators to the
2584 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002585 ParseScope PrototypeScope(this,
2586 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Chris Lattnerf97409f2008-04-06 06:57:35 +00002588 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002589 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002590 while (1) {
2591 if (Tok.is(tok::ellipsis)) {
2592 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002593 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002594 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002595 }
Mike Stump1eb44332009-09-09 15:08:12 +00002596
Chris Lattnerf97409f2008-04-06 06:57:35 +00002597 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002598
Chris Lattnerf97409f2008-04-06 06:57:35 +00002599 // Parse the declaration-specifiers.
2600 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002601
2602 // If the caller parsed attributes for the first argument, add them now.
2603 if (AttrList) {
2604 DS.AddAttributes(AttrList);
2605 AttrList = 0; // Only apply the attributes to the first parameter.
2606 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002607 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Chris Lattnerf97409f2008-04-06 06:57:35 +00002609 // Parse the declarator. This is "PrototypeContext", because we must
2610 // accept either 'declarator' or 'abstract-declarator' here.
2611 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2612 ParseDeclarator(ParmDecl);
2613
2614 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002615 if (Tok.is(tok::kw___attribute)) {
2616 SourceLocation Loc;
2617 AttributeList *AttrList = ParseAttributes(&Loc);
2618 ParmDecl.AddAttributes(AttrList, Loc);
2619 }
Mike Stump1eb44332009-09-09 15:08:12 +00002620
Chris Lattnerf97409f2008-04-06 06:57:35 +00002621 // Remember this parsed parameter in ParamInfo.
2622 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002623
Douglas Gregor72b505b2008-12-16 21:30:33 +00002624 // DefArgToks is used when the parsing of default arguments needs
2625 // to be delayed.
2626 CachedTokens *DefArgToks = 0;
2627
Chris Lattnerf97409f2008-04-06 06:57:35 +00002628 // If no parameter was specified, verify that *something* was specified,
2629 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002630 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2631 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002632 // Completely missing, emit error.
2633 Diag(DSStart, diag::err_missing_param);
2634 } else {
2635 // Otherwise, we have something. Add it and let semantic analysis try
2636 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00002637
Chris Lattnerf97409f2008-04-06 06:57:35 +00002638 // Inform the actions module about the parameter declarator, so it gets
2639 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002640 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002641
2642 // Parse the default argument, if any. We parse the default
2643 // arguments in all dialects; the semantic analysis in
2644 // ActOnParamDefaultArgument will reject the default argument in
2645 // C.
2646 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002647 SourceLocation EqualLoc = Tok.getLocation();
2648
Chris Lattner04421082008-04-08 04:40:51 +00002649 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002650 if (D.getContext() == Declarator::MemberContext) {
2651 // If we're inside a class definition, cache the tokens
2652 // corresponding to the default argument. We'll actually parse
2653 // them when we see the end of the class definition.
2654 // FIXME: Templates will require something similar.
2655 // FIXME: Can we use a smart pointer for Toks?
2656 DefArgToks = new CachedTokens;
2657
Mike Stump1eb44332009-09-09 15:08:12 +00002658 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002659 tok::semi, false)) {
2660 delete DefArgToks;
2661 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002662 Actions.ActOnParamDefaultArgumentError(Param);
2663 } else
Mike Stump1eb44332009-09-09 15:08:12 +00002664 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00002665 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002666 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002667 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002668 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002669
Douglas Gregor72b505b2008-12-16 21:30:33 +00002670 OwningExprResult DefArgResult(ParseAssignmentExpression());
2671 if (DefArgResult.isInvalid()) {
2672 Actions.ActOnParamDefaultArgumentError(Param);
2673 SkipUntil(tok::comma, tok::r_paren, true, true);
2674 } else {
2675 // Inform the actions module about the default argument
2676 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002677 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002678 }
Chris Lattner04421082008-04-08 04:40:51 +00002679 }
2680 }
Mike Stump1eb44332009-09-09 15:08:12 +00002681
2682 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2683 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002684 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002685 }
2686
2687 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00002688 if (Tok.isNot(tok::comma)) {
2689 if (Tok.is(tok::ellipsis)) {
2690 IsVariadic = true;
2691 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2692
2693 if (!getLang().CPlusPlus) {
2694 // We have ellipsis without a preceding ',', which is ill-formed
2695 // in C. Complain and provide the fix.
2696 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2697 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2698 }
2699 }
2700
2701 break;
2702 }
Mike Stump1eb44332009-09-09 15:08:12 +00002703
Chris Lattnerf97409f2008-04-06 06:57:35 +00002704 // Consume the comma.
2705 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002706 }
Mike Stump1eb44332009-09-09 15:08:12 +00002707
Chris Lattnerf97409f2008-04-06 06:57:35 +00002708 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002709 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00002710
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002711 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002712 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2713 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002714
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002715 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002716 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002717 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002718 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002719 llvm::SmallVector<TypeTy*, 2> Exceptions;
2720 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002721 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002722 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002723 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002724 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002725 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002726
2727 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002728 if (Tok.is(tok::kw_throw)) {
2729 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002730 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002731 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002732 hasAnyExceptionSpec);
2733 assert(Exceptions.size() == ExceptionRanges.size() &&
2734 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002735 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002736 }
2737
Reid Spencer5f016e22007-07-11 17:01:13 +00002738 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002739 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002740 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002741 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002742 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002743 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002744 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002745 Exceptions.data(),
2746 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002747 Exceptions.size(),
2748 LParenLoc, RParenLoc, D),
2749 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002750}
2751
Chris Lattner66d28652008-04-06 06:34:08 +00002752/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2753/// we found a K&R-style identifier list instead of a type argument list. The
2754/// current token is known to be the first identifier in the list.
2755///
2756/// identifier-list: [C99 6.7.5]
2757/// identifier
2758/// identifier-list ',' identifier
2759///
2760void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2761 Declarator &D) {
2762 // Build up an array of information about the parsed arguments.
2763 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2764 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00002765
Chris Lattner66d28652008-04-06 06:34:08 +00002766 // If there was no identifier specified for the declarator, either we are in
2767 // an abstract-declarator, or we are in a parameter declarator which was found
2768 // to be abstract. In abstract-declarators, identifier lists are not valid:
2769 // diagnose this.
2770 if (!D.getIdentifier())
2771 Diag(Tok, diag::ext_ident_list_in_param);
2772
2773 // Tok is known to be the first identifier in the list. Remember this
2774 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002775 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002776 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002777 Tok.getLocation(),
2778 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00002779
Chris Lattner50c64772008-04-06 06:39:19 +00002780 ConsumeToken(); // eat the first identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00002781
Chris Lattner66d28652008-04-06 06:34:08 +00002782 while (Tok.is(tok::comma)) {
2783 // Eat the comma.
2784 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002785
Chris Lattner50c64772008-04-06 06:39:19 +00002786 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002787 if (Tok.isNot(tok::identifier)) {
2788 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002789 SkipUntil(tok::r_paren);
2790 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002791 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002792
Chris Lattner66d28652008-04-06 06:34:08 +00002793 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002794
2795 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002796 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002797 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00002798
Chris Lattner66d28652008-04-06 06:34:08 +00002799 // Verify that the argument identifier has not already been mentioned.
2800 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002801 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002802 } else {
2803 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002804 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002805 Tok.getLocation(),
2806 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002807 }
Mike Stump1eb44332009-09-09 15:08:12 +00002808
Chris Lattner66d28652008-04-06 06:34:08 +00002809 // Eat the identifier.
2810 ConsumeToken();
2811 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002812
2813 // If we have the closing ')', eat it and we're done.
2814 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2815
Chris Lattner50c64772008-04-06 06:39:19 +00002816 // Remember that we parsed a function type, and remember the attributes. This
2817 // function type is always a K&R style function type, which is not varargs and
2818 // has no prototype.
2819 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002820 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002821 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002822 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00002823 /*exception*/false,
2824 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002825 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002826 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002827}
Chris Lattneref4715c2008-04-06 05:45:57 +00002828
Reid Spencer5f016e22007-07-11 17:01:13 +00002829/// [C90] direct-declarator '[' constant-expression[opt] ']'
2830/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2831/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2832/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2833/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2834void Parser::ParseBracketDeclarator(Declarator &D) {
2835 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00002836
Chris Lattner378c7e42008-12-18 07:27:21 +00002837 // C array syntax has many features, but by-far the most common is [] and [4].
2838 // This code does a fast path to handle some of the most obvious cases.
2839 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002840 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002841 // Remember that we parsed the empty array type.
2842 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002843 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2844 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002845 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002846 return;
2847 } else if (Tok.getKind() == tok::numeric_constant &&
2848 GetLookAheadToken(1).is(tok::r_square)) {
2849 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002850 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002851 ConsumeToken();
2852
Sebastian Redlab197ba2009-02-09 18:23:29 +00002853 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002854
2855 // If there was an error parsing the assignment-expression, recover.
2856 if (ExprRes.isInvalid())
2857 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00002858
Chris Lattner378c7e42008-12-18 07:27:21 +00002859 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002860 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2861 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002862 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002863 return;
2864 }
Mike Stump1eb44332009-09-09 15:08:12 +00002865
Reid Spencer5f016e22007-07-11 17:01:13 +00002866 // If valid, this location is the position where we read the 'static' keyword.
2867 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002868 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002869 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002870
Reid Spencer5f016e22007-07-11 17:01:13 +00002871 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002872 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002873 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002874 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00002875
Reid Spencer5f016e22007-07-11 17:01:13 +00002876 // If we haven't already read 'static', check to see if there is one after the
2877 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002878 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002879 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002880
Reid Spencer5f016e22007-07-11 17:01:13 +00002881 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2882 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002883 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00002884
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002885 // Handle the case where we have '[*]' as the array size. However, a leading
2886 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2887 // the the token after the star is a ']'. Since stars in arrays are
2888 // infrequent, use of lookahead is not costly here.
2889 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002890 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002891
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002892 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002893 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002894 StaticLoc = SourceLocation(); // Drop the static.
2895 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002896 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002897 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002898 // Note, in C89, this production uses the constant-expr production instead
2899 // of assignment-expr. The only difference is that assignment-expr allows
2900 // things like '=' and '*='. Sema rejects these in C89 mode because they
2901 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00002902
Douglas Gregore0762c92009-06-19 23:52:42 +00002903 // Parse the constant-expression or assignment-expression now (depending
2904 // on dialect).
2905 if (getLang().CPlusPlus)
2906 NumElements = ParseConstantExpression();
2907 else
2908 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00002909 }
Mike Stump1eb44332009-09-09 15:08:12 +00002910
Reid Spencer5f016e22007-07-11 17:01:13 +00002911 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002912 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002913 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002914 // If the expression was invalid, skip it.
2915 SkipUntil(tok::r_square);
2916 return;
2917 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002918
2919 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2920
Chris Lattner378c7e42008-12-18 07:27:21 +00002921 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002922 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2923 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002924 NumElements.release(),
2925 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002926 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002927}
2928
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002929/// [GNU] typeof-specifier:
2930/// typeof ( expressions )
2931/// typeof ( type-name )
2932/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002933///
2934void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002935 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002936 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002937 SourceLocation StartLoc = ConsumeToken();
2938
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002939 bool isCastExpr;
2940 TypeTy *CastTy;
2941 SourceRange CastRange;
2942 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2943 isCastExpr,
2944 CastTy,
2945 CastRange);
2946
2947 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002948 // FIXME: Not accurate, the range gets one token more than it should.
2949 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002950 else
2951 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00002952
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002953 if (isCastExpr) {
2954 if (!CastTy) {
2955 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002956 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002957 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002958
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002959 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002960 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002961 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2962 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002963 DiagID, CastTy))
2964 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002965 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002966 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002967
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002968 // If we get here, the operand to the typeof was an expresion.
2969 if (Operand.isInvalid()) {
2970 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002971 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002972 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002973
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002974 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002975 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002976 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2977 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002978 DiagID, Operand.release()))
2979 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002980}