blob: 6cee0b4bb69cf0b3d71e603f21df5b423114772b [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,
Chris Lattner97144fc2009-04-02 04:16:50 +0000339 SourceLocation &DeclEnd,
Chris Lattnercd147752009-03-29 17:27:48 +0000340 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 // Parse the common declaration-specifiers piece.
342 DeclSpec DS;
343 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
346 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000347 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000349 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
350 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000351 }
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 Declarator DeclaratorInfo(DS, (Declarator::TheContext)Context);
354 ParseDeclarator(DeclaratorInfo);
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chris Lattner23c4b182009-03-29 17:18:04 +0000356 DeclGroupPtrTy DG =
357 ParseInitDeclaratorListAfterFirstDeclarator(DeclaratorInfo);
Chris Lattnercd147752009-03-29 17:27:48 +0000358
Chris Lattner97144fc2009-04-02 04:16:50 +0000359 DeclEnd = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Chris Lattnercd147752009-03-29 17:27:48 +0000361 // If the client wants to check what comes after the declaration, just return
362 // immediately without checking anything!
363 if (!RequireSemi) return DG;
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Chris Lattner23c4b182009-03-29 17:18:04 +0000365 if (Tok.is(tok::semi)) {
366 ConsumeToken();
Chris Lattner23c4b182009-03-29 17:18:04 +0000367 return DG;
368 }
Mike Stump1eb44332009-09-09 15:08:12 +0000369
John McCall5c15fe12009-07-31 02:20:35 +0000370 Diag(Tok, diag::err_expected_semi_declaration);
Chris Lattner23c4b182009-03-29 17:18:04 +0000371 // Skip to end of block or statement
372 SkipUntil(tok::r_brace, true, true);
373 if (Tok.is(tok::semi))
374 ConsumeToken();
375 return DG;
Reid Spencer5f016e22007-07-11 17:01:13 +0000376}
377
Douglas Gregor1426e532009-05-12 21:31:51 +0000378/// \brief Parse 'declaration' after parsing 'declaration-specifiers
379/// declarator'. This method parses the remainder of the declaration
380/// (including any attributes or initializer, among other things) and
381/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000382///
Reid Spencer5f016e22007-07-11 17:01:13 +0000383/// init-declarator: [C99 6.7]
384/// declarator
385/// declarator '=' initializer
386/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
387/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000388/// [C++] declarator initializer[opt]
389///
390/// [C++] initializer:
391/// [C++] '=' initializer-clause
392/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000393/// [C++0x] '=' 'default' [TODO]
394/// [C++0x] '=' 'delete'
395///
396/// According to the standard grammar, =default and =delete are function
397/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000398///
Douglas Gregore542c862009-06-23 23:11:28 +0000399Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
400 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000401 // If a simple-asm-expr is present, parse it.
402 if (Tok.is(tok::kw_asm)) {
403 SourceLocation Loc;
404 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
405 if (AsmLabel.isInvalid()) {
406 SkipUntil(tok::semi, true, true);
407 return DeclPtrTy();
408 }
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Douglas Gregor1426e532009-05-12 21:31:51 +0000410 D.setAsmLabel(AsmLabel.release());
411 D.SetRangeEnd(Loc);
412 }
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Douglas Gregor1426e532009-05-12 21:31:51 +0000414 // If attributes are present, parse them.
415 if (Tok.is(tok::kw___attribute)) {
416 SourceLocation Loc;
417 AttributeList *AttrList = ParseAttributes(&Loc);
418 D.AddAttributes(AttrList, Loc);
419 }
Mike Stump1eb44332009-09-09 15:08:12 +0000420
Douglas Gregor1426e532009-05-12 21:31:51 +0000421 // Inform the current actions module that we just parsed this declarator.
Douglas Gregord5a423b2009-09-25 18:43:00 +0000422 DeclPtrTy ThisDecl;
423 switch (TemplateInfo.Kind) {
424 case ParsedTemplateInfo::NonTemplate:
425 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
426 break;
427
428 case ParsedTemplateInfo::Template:
429 case ParsedTemplateInfo::ExplicitSpecialization:
430 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregore542c862009-06-23 23:11:28 +0000431 Action::MultiTemplateParamsArg(Actions,
432 TemplateInfo.TemplateParams->data(),
433 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000434 D);
435 break;
436
437 case ParsedTemplateInfo::ExplicitInstantiation: {
438 Action::DeclResult ThisRes
439 = Actions.ActOnExplicitInstantiation(CurScope,
440 TemplateInfo.ExternLoc,
441 TemplateInfo.TemplateLoc,
442 D);
443 if (ThisRes.isInvalid()) {
444 SkipUntil(tok::semi, true, true);
445 return DeclPtrTy();
446 }
447
448 ThisDecl = ThisRes.get();
449 break;
450 }
451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Douglas Gregor1426e532009-05-12 21:31:51 +0000453 // Parse declarator '=' initializer.
454 if (Tok.is(tok::equal)) {
455 ConsumeToken();
456 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
457 SourceLocation DelLoc = ConsumeToken();
458 Actions.SetDeclDeleted(ThisDecl, DelLoc);
459 } else {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000460 if (getLang().CPlusPlus)
461 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
462
Douglas Gregor1426e532009-05-12 21:31:51 +0000463 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000464
465 if (getLang().CPlusPlus)
466 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
467
Douglas Gregor1426e532009-05-12 21:31:51 +0000468 if (Init.isInvalid()) {
469 SkipUntil(tok::semi, true, true);
470 return DeclPtrTy();
471 }
Anders Carlsson9abf2ae2009-08-16 05:13:48 +0000472 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000473 }
474 } else if (Tok.is(tok::l_paren)) {
475 // Parse C++ direct initializer: '(' expression-list ')'
476 SourceLocation LParenLoc = ConsumeParen();
477 ExprVector Exprs(Actions);
478 CommaLocsTy CommaLocs;
479
480 if (ParseExpressionList(Exprs, CommaLocs)) {
481 SkipUntil(tok::r_paren);
482 } else {
483 // Match the ')'.
484 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
485
486 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
487 "Unexpected number of commas!");
488 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
489 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000490 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000491 }
492 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000493 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000494 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
495 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000496 }
497
498 return ThisDecl;
499}
500
501/// ParseInitDeclaratorListAfterFirstDeclarator - Parse 'declaration' after
502/// parsing 'declaration-specifiers declarator'. This method is split out this
503/// way to handle the ambiguity between top-level function-definitions and
504/// declarations.
505///
506/// init-declarator-list: [C99 6.7]
507/// init-declarator
508/// init-declarator-list ',' init-declarator
509///
510/// According to the standard grammar, =default and =delete are function
511/// definitions, but that definitely doesn't fit with the parser here.
512///
Chris Lattner682bf922009-03-29 16:50:03 +0000513Parser::DeclGroupPtrTy Parser::
Reid Spencer5f016e22007-07-11 17:01:13 +0000514ParseInitDeclaratorListAfterFirstDeclarator(Declarator &D) {
Chris Lattner682bf922009-03-29 16:50:03 +0000515 // Declarators may be grouped together ("int X, *Y, Z();"). Remember the decls
516 // that we parse together here.
517 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
Mike Stump1eb44332009-09-09 15:08:12 +0000518
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 // At this point, we know that it is not a function definition. Parse the
520 // rest of the init-declarator-list.
521 while (1) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000522 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
523 if (ThisDecl.get())
524 DeclsInGroup.push_back(ThisDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 // If we don't have a comma, it is either the end of the list (a ';') or an
527 // error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +0000528 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000529 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 // Consume the comma.
532 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Reid Spencer5f016e22007-07-11 17:01:13 +0000534 // Parse the next declarator.
535 D.clear();
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chris Lattneraab740a2008-10-20 04:57:38 +0000537 // Accept attributes in an init-declarator. In the first declarator in a
538 // declaration, these would be part of the declspec. In subsequent
539 // declarators, they become part of the declarator itself, so that they
540 // don't apply to declarators after *this* one. Examples:
541 // short __attribute__((common)) var; -> declspec
542 // short var __attribute__((common)); -> declarator
543 // short x, __attribute__((common)) var; -> declarator
Sebastian Redlab197ba2009-02-09 18:23:29 +0000544 if (Tok.is(tok::kw___attribute)) {
545 SourceLocation Loc;
546 AttributeList *AttrList = ParseAttributes(&Loc);
547 D.AddAttributes(AttrList, Loc);
548 }
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 ParseDeclarator(D);
551 }
Mike Stump1eb44332009-09-09 15:08:12 +0000552
Eli Friedmanc1dc6532009-05-29 01:49:24 +0000553 return Actions.FinalizeDeclaratorGroup(CurScope, D.getDeclSpec(),
554 DeclsInGroup.data(),
Chris Lattner23c4b182009-03-29 17:18:04 +0000555 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000556}
557
558/// ParseSpecifierQualifierList
559/// specifier-qualifier-list:
560/// type-specifier specifier-qualifier-list[opt]
561/// type-qualifier specifier-qualifier-list[opt]
562/// [GNU] attributes specifier-qualifier-list[opt]
563///
564void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
565 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
566 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 // Validate declspec for type-name.
570 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000571 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
572 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 // Issue diagnostic and remove storage class if present.
576 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
577 if (DS.getStorageClassSpecLoc().isValid())
578 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
579 else
580 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
581 DS.ClearStorageClassSpecs();
582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 // Issue diagnostic and remove function specfier if present.
585 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000586 if (DS.isInlineSpecified())
587 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
588 if (DS.isVirtualSpecified())
589 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
590 if (DS.isExplicitSpecified())
591 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 DS.ClearFunctionSpecs();
593 }
594}
595
Chris Lattnerc199ab32009-04-12 20:42:31 +0000596/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
597/// specified token is valid after the identifier in a declarator which
598/// immediately follows the declspec. For example, these things are valid:
599///
600/// int x [ 4]; // direct-declarator
601/// int x ( int y); // direct-declarator
602/// int(int x ) // direct-declarator
603/// int x ; // simple-declaration
604/// int x = 17; // init-declarator-list
605/// int x , y; // init-declarator-list
606/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000607/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000608/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000609///
610/// This is not, because 'x' does not immediately follow the declspec (though
611/// ')' happens to be valid anyway).
612/// int (x)
613///
614static bool isValidAfterIdentifierInDeclarator(const Token &T) {
615 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
616 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000617 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000618}
619
Chris Lattnere40c2952009-04-14 21:34:55 +0000620
621/// ParseImplicitInt - This method is called when we have an non-typename
622/// identifier in a declspec (which normally terminates the decl spec) when
623/// the declspec has no type specifier. In this case, the declspec is either
624/// malformed or is "implicit int" (in K&R and C89).
625///
626/// This method handles diagnosing this prettily and returns false if the
627/// declspec is done being processed. If it recovers and thinks there may be
628/// other pieces of declspec after it, it returns true.
629///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000630bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000631 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000632 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000633 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Chris Lattnere40c2952009-04-14 21:34:55 +0000635 SourceLocation Loc = Tok.getLocation();
636 // If we see an identifier that is not a type name, we normally would
637 // parse it as the identifer being declared. However, when a typename
638 // is typo'd or the definition is not included, this will incorrectly
639 // parse the typename as the identifier name and fall over misparsing
640 // later parts of the diagnostic.
641 //
642 // As such, we try to do some look-ahead in cases where this would
643 // otherwise be an "implicit-int" case to see if this is invalid. For
644 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
645 // an identifier with implicit int, we'd get a parse error because the
646 // next token is obviously invalid for a type. Parse these as a case
647 // with an invalid type specifier.
648 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Chris Lattnere40c2952009-04-14 21:34:55 +0000650 // Since we know that this either implicit int (which is rare) or an
651 // error, we'd do lookahead to try to do better recovery.
652 if (isValidAfterIdentifierInDeclarator(NextToken())) {
653 // If this token is valid for implicit int, e.g. "static x = 4", then
654 // we just avoid eating the identifier, so it will be parsed as the
655 // identifier in the declarator.
656 return false;
657 }
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Chris Lattnere40c2952009-04-14 21:34:55 +0000659 // Otherwise, if we don't consume this token, we are going to emit an
660 // error anyway. Try to recover from various common problems. Check
661 // to see if this was a reference to a tag name without a tag specified.
662 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000663 //
664 // C++ doesn't need this, and isTagName doesn't take SS.
665 if (SS == 0) {
666 const char *TagName = 0;
667 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Chris Lattnere40c2952009-04-14 21:34:55 +0000669 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
670 default: break;
671 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
672 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
673 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
674 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
675 }
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Chris Lattnerf4382f52009-04-14 22:17:06 +0000677 if (TagName) {
678 Diag(Loc, diag::err_use_of_tag_name_without_tag)
679 << Tok.getIdentifierInfo() << TagName
680 << CodeModificationHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Chris Lattnerf4382f52009-04-14 22:17:06 +0000682 // Parse this as a tag as if the missing tag were present.
683 if (TagKind == tok::kw_enum)
684 ParseEnumSpecifier(Loc, DS, AS);
685 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000686 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000687 return true;
688 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Douglas Gregora786fdb2009-10-13 23:27:22 +0000691 // This is almost certainly an invalid type name. Let the action emit a
692 // diagnostic and attempt to recover.
693 Action::TypeTy *T = 0;
694 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
695 CurScope, SS, T)) {
696 // The action emitted a diagnostic, so we don't have to.
697 if (T) {
698 // The action has suggested that the type T could be used. Set that as
699 // the type in the declaration specifiers, consume the would-be type
700 // name token, and we're done.
701 const char *PrevSpec;
702 unsigned DiagID;
703 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
704 false);
705 DS.SetRangeEnd(Tok.getLocation());
706 ConsumeToken();
707
708 // There may be other declaration specifiers after this.
709 return true;
710 }
711
712 // Fall through; the action had no suggestion for us.
713 } else {
714 // The action did not emit a diagnostic, so emit one now.
715 SourceRange R;
716 if (SS) R = SS->getRange();
717 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
718 }
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Douglas Gregora786fdb2009-10-13 23:27:22 +0000720 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000721 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000722 unsigned DiagID;
723 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000724 DS.SetRangeEnd(Tok.getLocation());
725 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Chris Lattnere40c2952009-04-14 21:34:55 +0000727 // TODO: Could inject an invalid typedef decl in an enclosing scope to
728 // avoid rippling error messages on subsequent uses of the same type,
729 // could be useful if #include was forgotten.
730 return false;
731}
732
Reid Spencer5f016e22007-07-11 17:01:13 +0000733/// ParseDeclarationSpecifiers
734/// declaration-specifiers: [C99 6.7]
735/// storage-class-specifier declaration-specifiers[opt]
736/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000737/// [C99] function-specifier declaration-specifiers[opt]
738/// [GNU] attributes declaration-specifiers[opt]
739///
740/// storage-class-specifier: [C99 6.7.1]
741/// 'typedef'
742/// 'extern'
743/// 'static'
744/// 'auto'
745/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000746/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000747/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000748/// function-specifier: [C99 6.7.4]
749/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000750/// [C++] 'virtual'
751/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000752/// 'friend': [C++ dcl.friend]
753
Reid Spencer5f016e22007-07-11 17:01:13 +0000754///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000755void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000756 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000757 AccessSpecifier AS,
758 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000759 if (Tok.is(tok::code_completion)) {
760 Actions.CodeCompleteOrdinaryName(CurScope);
761 ConsumeToken();
762 }
763
Chris Lattner81c018d2008-03-13 06:29:04 +0000764 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000766 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000767 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000768 unsigned DiagID = 0;
769
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000771
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000773 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000774 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000775 // If this is not a declaration specifier token, we're done reading decl
776 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000777 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Chris Lattner5e02c472009-01-05 00:07:25 +0000780 case tok::coloncolon: // ::foo::bar
781 // Annotate C++ scope specifiers. If we get one, loop.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000782 if (TryAnnotateCXXScopeToken(true))
Chris Lattner5e02c472009-01-05 00:07:25 +0000783 continue;
784 goto DoneWithDeclSpec;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000785
786 case tok::annot_cxxscope: {
787 if (DS.hasTypeSpecifier())
788 goto DoneWithDeclSpec;
789
790 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000791 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000792 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000793 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000794 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000795 // We have a qualified template-id, e.g., N::A<int>
796 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +0000797 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000798 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000799 "ParseOptionalCXXScopeSpecifier not working");
800 AnnotateTemplateIdTokenAsType(&SS);
801 continue;
802 }
803
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000804 if (Next.is(tok::annot_typename)) {
805 // FIXME: is this scope-specifier getting dropped?
806 ConsumeToken(); // the scope-specifier
807 if (Tok.getAnnotationValue())
808 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
809 PrevSpec, DiagID,
810 Tok.getAnnotationValue());
811 else
812 DS.SetTypeSpecError();
813 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
814 ConsumeToken(); // The typename
815 }
816
Douglas Gregor9135c722009-03-25 15:40:00 +0000817 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000818 goto DoneWithDeclSpec;
819
820 CXXScopeSpec SS;
Douglas Gregor35073692009-03-26 23:56:24 +0000821 SS.setScopeRep(Tok.getAnnotationValue());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000822 SS.setRange(Tok.getAnnotationRange());
823
824 // If the next token is the name of the class type that the C++ scope
825 // denotes, followed by a '(', then this is a constructor declaration.
826 // We're done with the decl-specifiers.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000827 if (Actions.isCurrentClassName(*Next.getIdentifierInfo(),
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000828 CurScope, &SS) &&
829 GetLookAheadToken(2).is(tok::l_paren))
830 goto DoneWithDeclSpec;
831
Douglas Gregorb696ea32009-02-04 17:00:24 +0000832 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
833 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000834
Chris Lattnerf4382f52009-04-14 22:17:06 +0000835 // If the referenced identifier is not a type, then this declspec is
836 // erroneous: We already checked about that it has no type specifier, and
837 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000838 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000839 if (TypeRep == 0) {
840 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000841 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000842 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000843 }
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000845 ConsumeToken(); // The C++ scope.
846
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000847 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000848 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000849 if (isInvalid)
850 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000852 DS.SetRangeEnd(Tok.getLocation());
853 ConsumeToken(); // The typename.
854
855 continue;
856 }
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Chris Lattner80d0c892009-01-21 19:48:37 +0000858 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000859 if (Tok.getAnnotationValue())
860 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000861 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +0000862 else
863 DS.SetTypeSpecError();
Chris Lattner80d0c892009-01-21 19:48:37 +0000864 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
865 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Chris Lattner80d0c892009-01-21 19:48:37 +0000867 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
868 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
869 // Objective-C interface. If we don't have Objective-C or a '<', this is
870 // just a normal reference to a typedef name.
871 if (!Tok.is(tok::less) || !getLang().ObjC1)
872 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000874 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000875 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000876 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
877 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
878 LAngleLoc, EndProtoLoc);
879 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
880 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattner80d0c892009-01-21 19:48:37 +0000882 DS.SetRangeEnd(EndProtoLoc);
883 continue;
884 }
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Chris Lattner3bd934a2008-07-26 01:18:38 +0000886 // typedef-name
887 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +0000888 // In C++, check to see if this is a scope specifier like foo::bar::, if
889 // so handle it as such. This is important for ctor parsing.
Douglas Gregor495c35d2009-08-25 22:51:20 +0000890 if (getLang().CPlusPlus && TryAnnotateCXXScopeToken(true))
Chris Lattner837acd02009-01-21 19:19:26 +0000891 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Chris Lattner3bd934a2008-07-26 01:18:38 +0000893 // This identifier can only be a typedef name if we haven't already seen
894 // a type-specifier. Without this check we misparse:
895 // typedef int X; struct Y { short X; }; as 'short int'.
896 if (DS.hasTypeSpecifier())
897 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Chris Lattner3bd934a2008-07-26 01:18:38 +0000899 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +0000900 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +0000901 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000902
Chris Lattnerc199ab32009-04-12 20:42:31 +0000903 // If this is not a typedef name, don't parse it as part of the declspec,
904 // it must be an implicit int or an error.
905 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000906 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000907 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +0000908 }
Douglas Gregor55f6b142009-02-09 18:46:07 +0000909
Douglas Gregorb48fe382008-10-31 09:07:45 +0000910 // C++: If the identifier is actually the name of the class type
911 // being defined and the next token is a '(', then this is a
912 // constructor declaration. We're done with the decl-specifiers
913 // and will treat this token as an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000914 if (getLang().CPlusPlus &&
915 (CurScope->isClassScope() ||
916 (CurScope->isTemplateParamScope() &&
Douglas Gregordec06662009-08-21 18:42:58 +0000917 CurScope->getParent()->isClassScope())) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000918 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregorb48fe382008-10-31 09:07:45 +0000919 NextToken().getKind() == tok::l_paren)
920 goto DoneWithDeclSpec;
921
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000922 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000923 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +0000924 if (isInvalid)
925 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Chris Lattner3bd934a2008-07-26 01:18:38 +0000927 DS.SetRangeEnd(Tok.getLocation());
928 ConsumeToken(); // The identifier
929
930 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
931 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
932 // Objective-C interface. If we don't have Objective-C or a '<', this is
933 // just a normal reference to a typedef name.
934 if (!Tok.is(tok::less) || !getLang().ObjC1)
935 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000937 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000938 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000939 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
940 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
941 LAngleLoc, EndProtoLoc);
942 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
943 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Chris Lattner3bd934a2008-07-26 01:18:38 +0000945 DS.SetRangeEnd(EndProtoLoc);
946
Steve Naroff4f9b9f12008-09-22 10:28:57 +0000947 // Need to support trailing type qualifiers (e.g. "id<p> const").
948 // If a type specifier follows, it will be diagnosed elsewhere.
949 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +0000950 }
Douglas Gregor39a8de12009-02-25 19:37:18 +0000951
952 // type-name
953 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +0000954 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +0000955 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +0000956 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +0000957 // This template-id does not refer to a type name, so we're
958 // done with the type-specifiers.
959 goto DoneWithDeclSpec;
960 }
961
962 // Turn the template-id annotation token into a type annotation
963 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +0000964 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +0000965 continue;
966 }
967
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 // GNU attributes support.
969 case tok::kw___attribute:
970 DS.AddAttributes(ParseAttributes());
971 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +0000972
973 // Microsoft declspec support.
974 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +0000975 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +0000976 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Steve Naroff239f0732008-12-25 14:16:32 +0000978 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000979 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +0000980 // FIXME: Add handling here!
981 break;
982
983 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +0000984 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +0000985 case tok::kw___cdecl:
986 case tok::kw___stdcall:
987 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +0000988 DS.AddAttributes(ParseMicrosoftTypeAttributes());
989 continue;
990
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 // storage-class-specifier
992 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +0000993 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
994 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 break;
996 case tok::kw_extern:
997 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +0000998 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +0000999 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1000 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001001 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001002 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001003 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001004 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001005 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 case tok::kw_static:
1007 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001008 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001009 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1010 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 break;
1012 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001013 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001014 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1015 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001016 else
John McCallfec54012009-08-03 20:12:06 +00001017 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1018 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 break;
1020 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001021 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1022 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001023 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001024 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001025 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1026 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001027 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001028 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001029 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001030 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001031
Reid Spencer5f016e22007-07-11 17:01:13 +00001032 // function-specifier
1033 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001034 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001036 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001037 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001038 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001039 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001040 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001041 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001042
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001043 // friend
1044 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001045 if (DSContext == DSC_class)
1046 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1047 else {
1048 PrevSpec = ""; // not actually used by the diagnostic
1049 DiagID = diag::err_friend_invalid_in_context;
1050 isInvalid = true;
1051 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001052 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Chris Lattner80d0c892009-01-21 19:48:37 +00001054 // type-specifier
1055 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001056 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1057 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001058 break;
1059 case tok::kw_long:
1060 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001061 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1062 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001063 else
John McCallfec54012009-08-03 20:12:06 +00001064 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1065 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001066 break;
1067 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001068 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1069 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001070 break;
1071 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001072 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1073 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001074 break;
1075 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001076 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1077 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001078 break;
1079 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001080 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1081 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001082 break;
1083 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001084 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1085 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001086 break;
1087 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001088 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1089 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001090 break;
1091 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001092 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1093 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001094 break;
1095 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001096 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1097 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001098 break;
1099 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001100 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1101 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001102 break;
1103 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001104 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1105 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001106 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001107 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001108 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1109 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001110 break;
1111 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001112 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1113 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001114 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001115 case tok::kw_bool:
1116 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001117 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1118 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001119 break;
1120 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001121 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1122 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001123 break;
1124 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001125 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1126 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001127 break;
1128 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001129 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1130 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001131 break;
1132
1133 // class-specifier:
1134 case tok::kw_class:
1135 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001136 case tok::kw_union: {
1137 tok::TokenKind Kind = Tok.getKind();
1138 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001139 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001140 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001141 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001142
1143 // enum-specifier:
1144 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001145 ConsumeToken();
1146 ParseEnumSpecifier(Loc, DS, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001147 continue;
1148
1149 // cv-qualifier:
1150 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001151 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1152 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001153 break;
1154 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001155 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1156 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001157 break;
1158 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001159 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1160 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001161 break;
1162
Douglas Gregord57959a2009-03-27 23:10:48 +00001163 // C++ typename-specifier:
1164 case tok::kw_typename:
1165 if (TryAnnotateTypeOrScopeToken())
1166 continue;
1167 break;
1168
Chris Lattner80d0c892009-01-21 19:48:37 +00001169 // GNU typeof support.
1170 case tok::kw_typeof:
1171 ParseTypeofSpecifier(DS);
1172 continue;
1173
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001174 case tok::kw_decltype:
1175 ParseDecltypeSpecifier(DS);
1176 continue;
1177
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001178 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001179 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001180 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1181 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001182 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001183 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Chris Lattnerbce61352008-07-26 00:20:22 +00001185 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001186 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001187 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001188 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1189 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1190 LAngleLoc, EndProtoLoc);
1191 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1192 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001193 DS.SetRangeEnd(EndProtoLoc);
1194
Chris Lattner1ab3b962008-11-18 07:48:38 +00001195 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Chris Lattner75e36062009-04-03 18:38:42 +00001196 << CodeModificationHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001197 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001198 // Need to support trailing type qualifiers (e.g. "id<p> const").
1199 // If a type specifier follows, it will be diagnosed elsewhere.
1200 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001201 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 }
John McCallfec54012009-08-03 20:12:06 +00001203 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 if (isInvalid) {
1205 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001206 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001207 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001209 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 ConsumeToken();
1211 }
1212}
Douglas Gregoradcac882008-12-01 23:54:00 +00001213
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001214/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001215/// primarily follow the C++ grammar with additions for C99 and GNU,
1216/// which together subsume the C grammar. Note that the C++
1217/// type-specifier also includes the C type-qualifier (for const,
1218/// volatile, and C99 restrict). Returns true if a type-specifier was
1219/// found (and parsed), false otherwise.
1220///
1221/// type-specifier: [C++ 7.1.5]
1222/// simple-type-specifier
1223/// class-specifier
1224/// enum-specifier
1225/// elaborated-type-specifier [TODO]
1226/// cv-qualifier
1227///
1228/// cv-qualifier: [C++ 7.1.5.1]
1229/// 'const'
1230/// 'volatile'
1231/// [C99] 'restrict'
1232///
1233/// simple-type-specifier: [ C++ 7.1.5.2]
1234/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1235/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1236/// 'char'
1237/// 'wchar_t'
1238/// 'bool'
1239/// 'short'
1240/// 'int'
1241/// 'long'
1242/// 'signed'
1243/// 'unsigned'
1244/// 'float'
1245/// 'double'
1246/// 'void'
1247/// [C99] '_Bool'
1248/// [C99] '_Complex'
1249/// [C99] '_Imaginary' // Removed in TC2?
1250/// [GNU] '_Decimal32'
1251/// [GNU] '_Decimal64'
1252/// [GNU] '_Decimal128'
1253/// [GNU] typeof-specifier
1254/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1255/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001256/// [C++0x] 'decltype' ( expression )
John McCallfec54012009-08-03 20:12:06 +00001257bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001258 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001259 unsigned &DiagID,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001260 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001261 SourceLocation Loc = Tok.getLocation();
1262
1263 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001264 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001265 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001266 // Annotate typenames and C++ scope specifiers. If we get one, just
1267 // recurse to handle whatever we get.
1268 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001269 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1270 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001271 // Otherwise, not a type specifier.
1272 return false;
1273 case tok::coloncolon: // ::foo::bar
1274 if (NextToken().is(tok::kw_new) || // ::new
1275 NextToken().is(tok::kw_delete)) // ::delete
1276 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Chris Lattner166a8fc2009-01-04 23:41:41 +00001278 // Annotate typenames and C++ scope specifiers. If we get one, just
1279 // recurse to handle whatever we get.
1280 if (TryAnnotateTypeOrScopeToken())
John McCallfec54012009-08-03 20:12:06 +00001281 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1282 TemplateInfo);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001283 // Otherwise, not a type specifier.
1284 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001285
Douglas Gregor12e083c2008-11-07 15:42:26 +00001286 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001287 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001288 if (Tok.getAnnotationValue())
1289 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001290 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001291 else
1292 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001293 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1294 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Douglas Gregor12e083c2008-11-07 15:42:26 +00001296 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1297 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1298 // Objective-C interface. If we don't have Objective-C or a '<', this is
1299 // just a normal reference to a typedef name.
1300 if (!Tok.is(tok::less) || !getLang().ObjC1)
1301 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001302
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001303 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001304 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001305 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1306 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1307 LAngleLoc, EndProtoLoc);
1308 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1309 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Douglas Gregor12e083c2008-11-07 15:42:26 +00001311 DS.SetRangeEnd(EndProtoLoc);
1312 return true;
1313 }
1314
1315 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001316 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001317 break;
1318 case tok::kw_long:
1319 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001320 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1321 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001322 else
John McCallfec54012009-08-03 20:12:06 +00001323 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1324 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001325 break;
1326 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001327 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001328 break;
1329 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001330 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1331 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001332 break;
1333 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001334 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1335 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001336 break;
1337 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001338 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1339 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001340 break;
1341 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001342 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001343 break;
1344 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001345 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001346 break;
1347 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001348 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001349 break;
1350 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001351 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001352 break;
1353 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001354 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001355 break;
1356 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001357 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001358 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001359 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001360 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001361 break;
1362 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001363 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001364 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001365 case tok::kw_bool:
1366 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001367 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001368 break;
1369 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001370 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1371 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001372 break;
1373 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001374 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1375 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001376 break;
1377 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001378 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1379 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001380 break;
1381
1382 // class-specifier:
1383 case tok::kw_class:
1384 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001385 case tok::kw_union: {
1386 tok::TokenKind Kind = Tok.getKind();
1387 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001388 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001389 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001390 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001391
1392 // enum-specifier:
1393 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001394 ConsumeToken();
1395 ParseEnumSpecifier(Loc, DS);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001396 return true;
1397
1398 // cv-qualifier:
1399 case tok::kw_const:
1400 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001401 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001402 break;
1403 case tok::kw_volatile:
1404 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001405 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001406 break;
1407 case tok::kw_restrict:
1408 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001409 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001410 break;
1411
1412 // GNU typeof support.
1413 case tok::kw_typeof:
1414 ParseTypeofSpecifier(DS);
1415 return true;
1416
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001417 // C++0x decltype support.
1418 case tok::kw_decltype:
1419 ParseDecltypeSpecifier(DS);
1420 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001422 // C++0x auto support.
1423 case tok::kw_auto:
1424 if (!getLang().CPlusPlus0x)
1425 return false;
1426
John McCallfec54012009-08-03 20:12:06 +00001427 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001428 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001429 case tok::kw___ptr64:
1430 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001431 case tok::kw___cdecl:
1432 case tok::kw___stdcall:
1433 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001434 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001435 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001436
Douglas Gregor12e083c2008-11-07 15:42:26 +00001437 default:
1438 // Not a type-specifier; do nothing.
1439 return false;
1440 }
1441
1442 // If the specifier combination wasn't legal, issue a diagnostic.
1443 if (isInvalid) {
1444 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001445 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001446 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001447 }
1448 DS.SetRangeEnd(Tok.getLocation());
1449 ConsumeToken(); // whatever we parsed above.
1450 return true;
1451}
Reid Spencer5f016e22007-07-11 17:01:13 +00001452
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001453/// ParseStructDeclaration - Parse a struct declaration without the terminating
1454/// semicolon.
1455///
Reid Spencer5f016e22007-07-11 17:01:13 +00001456/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001457/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001458/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001459/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001460/// struct-declarator-list:
1461/// struct-declarator
1462/// struct-declarator-list ',' struct-declarator
1463/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1464/// struct-declarator:
1465/// declarator
1466/// [GNU] declarator attributes[opt]
1467/// declarator[opt] ':' constant-expression
1468/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1469///
Chris Lattnere1359422008-04-10 06:46:29 +00001470void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001471ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001472 if (Tok.is(tok::kw___extension__)) {
1473 // __extension__ silences extension warnings in the subexpression.
1474 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001475 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001476 return ParseStructDeclaration(DS, Fields);
1477 }
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Steve Naroff28a7ca82007-08-20 22:28:22 +00001479 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001480 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001481 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001483 // If there are no declarators, this is a free-standing declaration
1484 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001485 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001486 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001487 return;
1488 }
1489
1490 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001491 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001492 while (1) {
John McCallbdd563e2009-11-03 02:38:08 +00001493 FieldDeclarator DeclaratorInfo(DS);
1494
1495 // Attributes are only allowed here on successive declarators.
1496 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1497 SourceLocation Loc;
1498 AttributeList *AttrList = ParseAttributes(&Loc);
1499 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1500 }
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Steve Naroff28a7ca82007-08-20 22:28:22 +00001502 /// struct-declarator: declarator
1503 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner04d66662007-10-09 17:33:22 +00001504 if (Tok.isNot(tok::colon))
Chris Lattnere1359422008-04-10 06:46:29 +00001505 ParseDeclarator(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Chris Lattner04d66662007-10-09 17:33:22 +00001507 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001508 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001509 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001510 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001511 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001512 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001513 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001514 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001515
Steve Naroff28a7ca82007-08-20 22:28:22 +00001516 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001517 if (Tok.is(tok::kw___attribute)) {
1518 SourceLocation Loc;
1519 AttributeList *AttrList = ParseAttributes(&Loc);
1520 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1521 }
1522
John McCallbdd563e2009-11-03 02:38:08 +00001523 // We're done with this declarator; invoke the callback.
1524 (void) Fields.invoke(DeclaratorInfo);
1525
Steve Naroff28a7ca82007-08-20 22:28:22 +00001526 // If we don't have a comma, it is either the end of the list (a ';')
1527 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001528 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001529 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001530
Steve Naroff28a7ca82007-08-20 22:28:22 +00001531 // Consume the comma.
1532 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001533
John McCallbdd563e2009-11-03 02:38:08 +00001534 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001535 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001536}
1537
1538/// ParseStructUnionBody
1539/// struct-contents:
1540/// struct-declaration-list
1541/// [EXT] empty
1542/// [GNU] "struct-declaration-list" without terminatoring ';'
1543/// struct-declaration-list:
1544/// struct-declaration
1545/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001546/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001547///
Reid Spencer5f016e22007-07-11 17:01:13 +00001548void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001549 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001550 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1551 PP.getSourceManager(),
1552 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Reid Spencer5f016e22007-07-11 17:01:13 +00001554 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001556 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001557 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1558
Reid Spencer5f016e22007-07-11 17:01:13 +00001559 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1560 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001561 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001562 Diag(Tok, diag::ext_empty_struct_union_enum)
1563 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001564
Chris Lattnerb28317a2009-03-28 19:18:32 +00001565 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001566
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001568 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001569 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Reid Spencer5f016e22007-07-11 17:01:13 +00001571 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001572 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001573 Diag(Tok, diag::ext_extra_struct_semi)
1574 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +00001575 ConsumeToken();
1576 continue;
1577 }
Chris Lattnere1359422008-04-10 06:46:29 +00001578
1579 // Parse all the comma separated declarators.
1580 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001581
John McCallbdd563e2009-11-03 02:38:08 +00001582 if (!Tok.is(tok::at)) {
1583 struct CFieldCallback : FieldCallback {
1584 Parser &P;
1585 DeclPtrTy TagDecl;
1586 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1587
1588 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1589 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1590 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1591
1592 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
1593 const DeclSpec &DS = FD.D.getDeclSpec();
1594 DeclPtrTy Field;
1595
1596 // Install the declarator into the current TagDecl.
1597 if (FD.D.getExtension()) {
1598 // Silences extension warnings
1599 ExtensionRAIIObject O(P.Diags);
1600 Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1601 DS.getSourceRange().getBegin(),
1602 FD.D, FD.BitfieldSize);
1603 } else {
1604 Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1605 DS.getSourceRange().getBegin(),
1606 FD.D, FD.BitfieldSize);
1607 }
1608 FieldDecls.push_back(Field);
1609 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001610 }
John McCallbdd563e2009-11-03 02:38:08 +00001611 } Callback(*this, TagDecl, FieldDecls);
1612
1613 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001614 } else { // Handle @defs
1615 ConsumeToken();
1616 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1617 Diag(Tok, diag::err_unexpected_at);
1618 SkipUntil(tok::semi, true, true);
1619 continue;
1620 }
1621 ConsumeToken();
1622 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1623 if (!Tok.is(tok::identifier)) {
1624 Diag(Tok, diag::err_expected_ident);
1625 SkipUntil(tok::semi, true, true);
1626 continue;
1627 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001628 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001629 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001630 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001631 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1632 ConsumeToken();
1633 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001634 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001635
Chris Lattner04d66662007-10-09 17:33:22 +00001636 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001637 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001638 } else if (Tok.is(tok::r_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001639 Diag(Tok, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001640 break;
1641 } else {
1642 Diag(Tok, diag::err_expected_semi_decl_list);
1643 // Skip to end of block or statement
1644 SkipUntil(tok::r_brace, true, true);
1645 }
1646 }
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Steve Naroff60fccee2007-10-29 21:38:07 +00001648 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Reid Spencer5f016e22007-07-11 17:01:13 +00001650 AttributeList *AttrList = 0;
1651 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001652 if (Tok.is(tok::kw___attribute))
Daniel Dunbar5e592d82008-10-03 16:42:10 +00001653 AttrList = ParseAttributes();
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001654
1655 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001656 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001657 LBraceLoc, RBraceLoc,
Douglas Gregor72de6672009-01-08 20:45:30 +00001658 AttrList);
1659 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001660 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001661}
1662
1663
1664/// ParseEnumSpecifier
1665/// enum-specifier: [C99 6.7.2.2]
1666/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001667///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001668/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1669/// '}' attributes[opt]
1670/// 'enum' identifier
1671/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001672///
1673/// [C++] elaborated-type-specifier:
1674/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1675///
Chris Lattner4c97d762009-04-12 21:49:30 +00001676void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
1677 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001678 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001679 if (Tok.is(tok::code_completion)) {
1680 // Code completion for an enum name.
1681 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1682 ConsumeToken();
1683 }
1684
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001685 AttributeList *Attr = 0;
1686 // If attributes exist after tag, parse them.
1687 if (Tok.is(tok::kw___attribute))
1688 Attr = ParseAttributes();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001689
1690 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00001691 if (getLang().CPlusPlus && ParseOptionalCXXScopeSpecifier(SS, 0, false)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001692 if (Tok.isNot(tok::identifier)) {
1693 Diag(Tok, diag::err_expected_ident);
1694 if (Tok.isNot(tok::l_brace)) {
1695 // Has no name and is not a definition.
1696 // Skip the rest of this declarator, up until the comma or semicolon.
1697 SkipUntil(tok::comma, true);
1698 return;
1699 }
1700 }
1701 }
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001703 // Must have either 'enum name' or 'enum {...}'.
1704 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1705 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001707 // Skip the rest of this declarator, up until the comma or semicolon.
1708 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001709 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001710 }
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001712 // If an identifier is present, consume and remember it.
1713 IdentifierInfo *Name = 0;
1714 SourceLocation NameLoc;
1715 if (Tok.is(tok::identifier)) {
1716 Name = Tok.getIdentifierInfo();
1717 NameLoc = ConsumeToken();
1718 }
Mike Stump1eb44332009-09-09 15:08:12 +00001719
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001720 // There are three options here. If we have 'enum foo;', then this is a
1721 // forward declaration. If we have 'enum foo {...' then this is a
1722 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1723 //
1724 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1725 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1726 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1727 //
John McCall0f434ec2009-07-31 02:45:11 +00001728 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001729 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001730 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001731 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001732 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001733 else
John McCall0f434ec2009-07-31 02:45:11 +00001734 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001735 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001736 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00001737 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Douglas Gregor402abb52009-05-28 23:31:59 +00001738 StartLoc, SS, Name, NameLoc, Attr, AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001739 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001740 Owned, IsDependent);
1741 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Chris Lattner04d66662007-10-09 17:33:22 +00001743 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001744 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Reid Spencer5f016e22007-07-11 17:01:13 +00001746 // TODO: semantic analysis on the declspec for enums.
1747 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001748 unsigned DiagID;
1749 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001750 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001751 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001752}
1753
1754/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1755/// enumerator-list:
1756/// enumerator
1757/// enumerator-list ',' enumerator
1758/// enumerator:
1759/// enumeration-constant
1760/// enumeration-constant '=' constant-expression
1761/// enumeration-constant:
1762/// identifier
1763///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001764void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001765 // Enter the scope of the enum body and start the definition.
1766 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001767 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001768
Reid Spencer5f016e22007-07-11 17:01:13 +00001769 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Chris Lattner7946dd32007-08-27 17:24:30 +00001771 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001772 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001773 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Chris Lattnerb28317a2009-03-28 19:18:32 +00001775 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001776
Chris Lattnerb28317a2009-03-28 19:18:32 +00001777 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Reid Spencer5f016e22007-07-11 17:01:13 +00001779 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001780 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001781 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1782 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001785 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001786 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001787 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001788 AssignedVal = ParseConstantExpression();
1789 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001790 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 }
Mike Stump1eb44332009-09-09 15:08:12 +00001792
Reid Spencer5f016e22007-07-11 17:01:13 +00001793 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001794 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1795 LastEnumConstDecl,
1796 IdentLoc, Ident,
1797 EqualLoc,
1798 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 EnumConstantDecls.push_back(EnumConstDecl);
1800 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Chris Lattner04d66662007-10-09 17:33:22 +00001802 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 break;
1804 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001805
1806 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001807 !(getLang().C99 || getLang().CPlusPlus0x))
1808 Diag(CommaLoc, diag::ext_enumerator_list_comma)
1809 << getLang().CPlusPlus
1810 << CodeModificationHint::CreateRemoval((SourceRange(CommaLoc)));
Reid Spencer5f016e22007-07-11 17:01:13 +00001811 }
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Reid Spencer5f016e22007-07-11 17:01:13 +00001813 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00001814 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001815
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001816 AttributeList *Attr = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001817 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001818 if (Tok.is(tok::kw___attribute))
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001819 Attr = ParseAttributes();
Douglas Gregor72de6672009-01-08 20:45:30 +00001820
Edward O'Callaghanfee13812009-08-08 14:36:57 +00001821 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
1822 EnumConstantDecls.data(), EnumConstantDecls.size(),
1823 CurScope, Attr);
Mike Stump1eb44332009-09-09 15:08:12 +00001824
Douglas Gregor72de6672009-01-08 20:45:30 +00001825 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001826 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001827}
1828
1829/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00001830/// start of a type-qualifier-list.
1831bool Parser::isTypeQualifier() const {
1832 switch (Tok.getKind()) {
1833 default: return false;
1834 // type-qualifier
1835 case tok::kw_const:
1836 case tok::kw_volatile:
1837 case tok::kw_restrict:
1838 return true;
1839 }
1840}
1841
1842/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00001843/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001844bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001845 switch (Tok.getKind()) {
1846 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Chris Lattner166a8fc2009-01-04 23:41:41 +00001848 case tok::identifier: // foo::bar
Douglas Gregord57959a2009-03-27 23:10:48 +00001849 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001850 // Annotate typenames and C++ scope specifiers. If we get one, just
1851 // recurse to handle whatever we get.
1852 if (TryAnnotateTypeOrScopeToken())
1853 return isTypeSpecifierQualifier();
1854 // Otherwise, not a type specifier.
1855 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001856
Chris Lattner166a8fc2009-01-04 23:41:41 +00001857 case tok::coloncolon: // ::foo::bar
1858 if (NextToken().is(tok::kw_new) || // ::new
1859 NextToken().is(tok::kw_delete)) // ::delete
1860 return false;
1861
1862 // Annotate typenames and C++ scope specifiers. If we get one, just
1863 // recurse to handle whatever we get.
1864 if (TryAnnotateTypeOrScopeToken())
1865 return isTypeSpecifierQualifier();
1866 // Otherwise, not a type specifier.
1867 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001868
Reid Spencer5f016e22007-07-11 17:01:13 +00001869 // GNU attributes support.
1870 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001871 // GNU typeof support.
1872 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00001873
Reid Spencer5f016e22007-07-11 17:01:13 +00001874 // type-specifiers
1875 case tok::kw_short:
1876 case tok::kw_long:
1877 case tok::kw_signed:
1878 case tok::kw_unsigned:
1879 case tok::kw__Complex:
1880 case tok::kw__Imaginary:
1881 case tok::kw_void:
1882 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001883 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001884 case tok::kw_char16_t:
1885 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00001886 case tok::kw_int:
1887 case tok::kw_float:
1888 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001889 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001890 case tok::kw__Bool:
1891 case tok::kw__Decimal32:
1892 case tok::kw__Decimal64:
1893 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Chris Lattner99dc9142008-04-13 18:59:07 +00001895 // struct-or-union-specifier (C99) or class-specifier (C++)
1896 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001897 case tok::kw_struct:
1898 case tok::kw_union:
1899 // enum-specifier
1900 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Reid Spencer5f016e22007-07-11 17:01:13 +00001902 // type-qualifier
1903 case tok::kw_const:
1904 case tok::kw_volatile:
1905 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001906
1907 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00001908 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00001909 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Chris Lattner7c186be2008-10-20 00:25:30 +00001911 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
1912 case tok::less:
1913 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00001914
Steve Naroff239f0732008-12-25 14:16:32 +00001915 case tok::kw___cdecl:
1916 case tok::kw___stdcall:
1917 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001918 case tok::kw___w64:
1919 case tok::kw___ptr64:
1920 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001921 }
1922}
1923
1924/// isDeclarationSpecifier() - Return true if the current token is part of a
1925/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001926bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001927 switch (Tok.getKind()) {
1928 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Chris Lattner166a8fc2009-01-04 23:41:41 +00001930 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00001931 // Unfortunate hack to support "Class.factoryMethod" notation.
1932 if (getLang().ObjC1 && NextToken().is(tok::period))
1933 return false;
Douglas Gregord57959a2009-03-27 23:10:48 +00001934 // Fall through
Steve Naroff61f72cb2009-03-09 21:12:44 +00001935
Douglas Gregord57959a2009-03-27 23:10:48 +00001936 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00001937 // Annotate typenames and C++ scope specifiers. If we get one, just
1938 // recurse to handle whatever we get.
1939 if (TryAnnotateTypeOrScopeToken())
1940 return isDeclarationSpecifier();
1941 // Otherwise, not a declaration specifier.
1942 return false;
1943 case tok::coloncolon: // ::foo::bar
1944 if (NextToken().is(tok::kw_new) || // ::new
1945 NextToken().is(tok::kw_delete)) // ::delete
1946 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001947
Chris Lattner166a8fc2009-01-04 23:41:41 +00001948 // Annotate typenames and C++ scope specifiers. If we get one, just
1949 // recurse to handle whatever we get.
1950 if (TryAnnotateTypeOrScopeToken())
1951 return isDeclarationSpecifier();
1952 // Otherwise, not a declaration specifier.
1953 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001954
Reid Spencer5f016e22007-07-11 17:01:13 +00001955 // storage-class-specifier
1956 case tok::kw_typedef:
1957 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00001958 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00001959 case tok::kw_static:
1960 case tok::kw_auto:
1961 case tok::kw_register:
1962 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00001963
Reid Spencer5f016e22007-07-11 17:01:13 +00001964 // type-specifiers
1965 case tok::kw_short:
1966 case tok::kw_long:
1967 case tok::kw_signed:
1968 case tok::kw_unsigned:
1969 case tok::kw__Complex:
1970 case tok::kw__Imaginary:
1971 case tok::kw_void:
1972 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001973 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001974 case tok::kw_char16_t:
1975 case tok::kw_char32_t:
1976
Reid Spencer5f016e22007-07-11 17:01:13 +00001977 case tok::kw_int:
1978 case tok::kw_float:
1979 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00001980 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00001981 case tok::kw__Bool:
1982 case tok::kw__Decimal32:
1983 case tok::kw__Decimal64:
1984 case tok::kw__Decimal128:
Mike Stump1eb44332009-09-09 15:08:12 +00001985
Chris Lattner99dc9142008-04-13 18:59:07 +00001986 // struct-or-union-specifier (C99) or class-specifier (C++)
1987 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00001988 case tok::kw_struct:
1989 case tok::kw_union:
1990 // enum-specifier
1991 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00001992
Reid Spencer5f016e22007-07-11 17:01:13 +00001993 // type-qualifier
1994 case tok::kw_const:
1995 case tok::kw_volatile:
1996 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00001997
Reid Spencer5f016e22007-07-11 17:01:13 +00001998 // function-specifier
1999 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002000 case tok::kw_virtual:
2001 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002002
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002003 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002004 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002005
Chris Lattner1ef08762007-08-09 17:01:07 +00002006 // GNU typeof support.
2007 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Chris Lattner1ef08762007-08-09 17:01:07 +00002009 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002010 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002011 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002012
Chris Lattnerf3948c42008-07-26 03:38:44 +00002013 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2014 case tok::less:
2015 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Steve Naroff47f52092009-01-06 19:34:12 +00002017 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002018 case tok::kw___cdecl:
2019 case tok::kw___stdcall:
2020 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002021 case tok::kw___w64:
2022 case tok::kw___ptr64:
2023 case tok::kw___forceinline:
2024 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002025 }
2026}
2027
2028
2029/// ParseTypeQualifierListOpt
2030/// type-qualifier-list: [C99 6.7.5]
2031/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002032/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002033/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002034/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002035///
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002036void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool AttributesAllowed) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002037 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002038 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002039 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002040 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002041 SourceLocation Loc = Tok.getLocation();
2042
2043 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002044 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002045 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2046 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002047 break;
2048 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002049 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2050 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002051 break;
2052 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002053 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2054 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002055 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002056 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002057 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002058 case tok::kw___cdecl:
2059 case tok::kw___stdcall:
2060 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002061 if (AttributesAllowed) {
2062 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2063 continue;
2064 }
2065 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 case tok::kw___attribute:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002067 if (AttributesAllowed) {
2068 DS.AddAttributes(ParseAttributes());
2069 continue; // do *not* consume the next token!
2070 }
2071 // otherwise, FALL THROUGH!
2072 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002073 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002074 // If this is not a type-qualifier token, we're done reading type
2075 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002076 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002077 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002078 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002079
Reid Spencer5f016e22007-07-11 17:01:13 +00002080 // If the specifier combination wasn't legal, issue a diagnostic.
2081 if (isInvalid) {
2082 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002083 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002084 }
2085 ConsumeToken();
2086 }
2087}
2088
2089
2090/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2091///
2092void Parser::ParseDeclarator(Declarator &D) {
2093 /// This implements the 'declarator' production in the C grammar, then checks
2094 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002095 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002096}
2097
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002098/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2099/// is parsed by the function passed to it. Pass null, and the direct-declarator
2100/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002101/// ptr-operator production.
2102///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002103/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2104/// [C] pointer[opt] direct-declarator
2105/// [C++] direct-declarator
2106/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002107///
2108/// pointer: [C99 6.7.5]
2109/// '*' type-qualifier-list[opt]
2110/// '*' type-qualifier-list[opt] pointer
2111///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002112/// ptr-operator:
2113/// '*' cv-qualifier-seq[opt]
2114/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002115/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002116/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002117/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002118/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002119void Parser::ParseDeclaratorInternal(Declarator &D,
2120 DirectDeclParseFunction DirectDeclParser) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002121
Douglas Gregor91a28862009-08-26 14:27:30 +00002122 if (Diags.hasAllExtensionsSilenced())
2123 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002124 // C++ member pointers start with a '::' or a nested-name.
2125 // Member pointers get special handling, since there's no place for the
2126 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002127 if (getLang().CPlusPlus &&
2128 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2129 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002130 CXXScopeSpec SS;
Douglas Gregor2dd078a2009-09-02 22:59:36 +00002131 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002132 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002133 // The scope spec really belongs to the direct-declarator.
2134 D.getCXXScopeSpec() = SS;
2135 if (DirectDeclParser)
2136 (this->*DirectDeclParser)(D);
2137 return;
2138 }
2139
2140 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002141 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002142 DeclSpec DS;
2143 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002144 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002145
2146 // Recurse to parse whatever is left.
2147 ParseDeclaratorInternal(D, DirectDeclParser);
2148
2149 // Sema will have to catch (syntactically invalid) pointers into global
2150 // scope. It has to catch pointers into namespace scope anyway.
2151 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002152 Loc, DS.TakeAttributes()),
2153 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002154 return;
2155 }
2156 }
2157
2158 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002159 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002160 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002161 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002162 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002163 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002164 if (DirectDeclParser)
2165 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002166 return;
2167 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002168
Sebastian Redl05532f22009-03-15 22:02:01 +00002169 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2170 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002171 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002172 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002173
Chris Lattner9af55002009-03-27 04:18:06 +00002174 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002175 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002176 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002177
Reid Spencer5f016e22007-07-11 17:01:13 +00002178 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002179 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002180
Reid Spencer5f016e22007-07-11 17:01:13 +00002181 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002182 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002183 if (Kind == tok::star)
2184 // Remember that we parsed a pointer type, and remember the type-quals.
2185 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002186 DS.TakeAttributes()),
2187 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002188 else
2189 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002190 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002191 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002192 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002193 } else {
2194 // Is a reference
2195 DeclSpec DS;
2196
Sebastian Redl743de1f2009-03-23 00:00:23 +00002197 // Complain about rvalue references in C++03, but then go on and build
2198 // the declarator.
2199 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2200 Diag(Loc, diag::err_rvalue_reference);
2201
Reid Spencer5f016e22007-07-11 17:01:13 +00002202 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2203 // cv-qualifiers are introduced through the use of a typedef or of a
2204 // template type argument, in which case the cv-qualifiers are ignored.
2205 //
2206 // [GNU] Retricted references are allowed.
2207 // [GNU] Attributes on references are allowed.
2208 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002209 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002210
2211 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2212 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2213 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002214 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002215 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2216 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002217 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002218 }
2219
2220 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002221 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002222
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002223 if (D.getNumTypeObjects() > 0) {
2224 // C++ [dcl.ref]p4: There shall be no references to references.
2225 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2226 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002227 if (const IdentifierInfo *II = D.getIdentifier())
2228 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2229 << II;
2230 else
2231 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2232 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002233
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002234 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002235 // can go ahead and build the (technically ill-formed)
2236 // declarator: reference collapsing will take care of it.
2237 }
2238 }
2239
Reid Spencer5f016e22007-07-11 17:01:13 +00002240 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002241 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002242 DS.TakeAttributes(),
2243 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002244 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002245 }
2246}
2247
2248/// ParseDirectDeclarator
2249/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002250/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002251/// '(' declarator ')'
2252/// [GNU] '(' attributes declarator ')'
2253/// [C90] direct-declarator '[' constant-expression[opt] ']'
2254/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2255/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2256/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2257/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2258/// direct-declarator '(' parameter-type-list ')'
2259/// direct-declarator '(' identifier-list[opt] ')'
2260/// [GNU] direct-declarator '(' parameter-forward-declarations
2261/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002262/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2263/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002264/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002265///
2266/// declarator-id: [C++ 8]
2267/// id-expression
2268/// '::'[opt] nested-name-specifier[opt] type-name
2269///
2270/// id-expression: [C++ 5.1]
2271/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002272/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002273///
2274/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002275/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002276/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002277/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002278/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002279/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002280///
Reid Spencer5f016e22007-07-11 17:01:13 +00002281void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002282 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002283
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002284 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2285 // ParseDeclaratorInternal might already have parsed the scope.
2286 bool afterCXXScope = D.getCXXScopeSpec().isSet() ||
2287 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2288 true);
2289 if (afterCXXScope) {
2290 // Change the declaration context for name lookup, until this function
2291 // is exited (and the declarator has been parsed).
2292 DeclScopeObj.EnterDeclaratorScope();
2293 }
2294
2295 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2296 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2297 // We found something that indicates the start of an unqualified-id.
2298 // Parse that unqualified-id.
2299 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2300 /*EnteringContext=*/true,
2301 /*AllowDestructorName=*/true,
2302 /*AllowConstructorName=*/!D.getDeclSpec().hasTypeSpecifier(),
2303 D.getName())) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002304 D.SetIdentifier(0, Tok.getLocation());
2305 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002306 } else {
2307 // Parsed the unqualified-id; update range information and move along.
2308 if (D.getSourceRange().getBegin().isInvalid())
2309 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2310 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002311 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002312 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002313 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002314 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002315 assert(!getLang().CPlusPlus &&
2316 "There's a C++-specific check for tok::identifier above");
2317 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2318 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2319 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002320 goto PastIdentifier;
2321 }
2322
2323 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002324 // direct-declarator: '(' declarator ')'
2325 // direct-declarator: '(' attributes declarator ')'
2326 // Example: 'char (*X)' or 'int (*XX)(void)'
2327 ParseParenDeclarator(D);
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002328 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002329 // This could be something simple like "int" (in which case the declarator
2330 // portion is empty), if an abstract-declarator is allowed.
2331 D.SetIdentifier(0, Tok.getLocation());
2332 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002333 if (D.getContext() == Declarator::MemberContext)
2334 Diag(Tok, diag::err_expected_member_name_or_semi)
2335 << D.getDeclSpec().getSourceRange();
2336 else if (getLang().CPlusPlus)
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002337 Diag(Tok, diag::err_expected_unqualified_id);
2338 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002339 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002340 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002341 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002342 }
Mike Stump1eb44332009-09-09 15:08:12 +00002343
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002344 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002345 assert(D.isPastIdentifier() &&
2346 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002347
Reid Spencer5f016e22007-07-11 17:01:13 +00002348 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002349 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002350 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2351 // In such a case, check if we actually have a function declarator; if it
2352 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002353 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2354 // When not in file scope, warn for ambiguous function declarators, just
2355 // in case the author intended it as a variable definition.
2356 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2357 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2358 break;
2359 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002360 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002361 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002362 ParseBracketDeclarator(D);
2363 } else {
2364 break;
2365 }
2366 }
2367}
2368
Chris Lattneref4715c2008-04-06 05:45:57 +00002369/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2370/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002371/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002372/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2373///
2374/// direct-declarator:
2375/// '(' declarator ')'
2376/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002377/// direct-declarator '(' parameter-type-list ')'
2378/// direct-declarator '(' identifier-list[opt] ')'
2379/// [GNU] direct-declarator '(' parameter-forward-declarations
2380/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002381///
2382void Parser::ParseParenDeclarator(Declarator &D) {
2383 SourceLocation StartLoc = ConsumeParen();
2384 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Chris Lattner7399ee02008-10-20 02:05:46 +00002386 // Eat any attributes before we look at whether this is a grouping or function
2387 // declarator paren. If this is a grouping paren, the attribute applies to
2388 // the type being built up, for example:
2389 // int (__attribute__(()) *x)(long y)
2390 // If this ends up not being a grouping paren, the attribute applies to the
2391 // first argument, for example:
2392 // int (__attribute__(()) int x)
2393 // In either case, we need to eat any attributes to be able to determine what
2394 // sort of paren this is.
2395 //
2396 AttributeList *AttrList = 0;
2397 bool RequiresArg = false;
2398 if (Tok.is(tok::kw___attribute)) {
2399 AttrList = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +00002400
Chris Lattner7399ee02008-10-20 02:05:46 +00002401 // We require that the argument list (if this is a non-grouping paren) be
2402 // present even if the attribute list was empty.
2403 RequiresArg = true;
2404 }
Steve Naroff239f0732008-12-25 14:16:32 +00002405 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002406 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2407 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2408 Tok.is(tok::kw___ptr64)) {
2409 AttrList = ParseMicrosoftTypeAttributes(AttrList);
2410 }
Mike Stump1eb44332009-09-09 15:08:12 +00002411
Chris Lattneref4715c2008-04-06 05:45:57 +00002412 // If we haven't past the identifier yet (or where the identifier would be
2413 // stored, if this is an abstract declarator), then this is probably just
2414 // grouping parens. However, if this could be an abstract-declarator, then
2415 // this could also be the start of function arguments (consider 'void()').
2416 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002417
Chris Lattneref4715c2008-04-06 05:45:57 +00002418 if (!D.mayOmitIdentifier()) {
2419 // If this can't be an abstract-declarator, this *must* be a grouping
2420 // paren, because we haven't seen the identifier yet.
2421 isGrouping = true;
2422 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002423 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002424 isDeclarationSpecifier()) { // 'int(int)' is a function.
2425 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2426 // considered to be a type, not a K&R identifier-list.
2427 isGrouping = false;
2428 } else {
2429 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2430 isGrouping = true;
2431 }
Mike Stump1eb44332009-09-09 15:08:12 +00002432
Chris Lattneref4715c2008-04-06 05:45:57 +00002433 // If this is a grouping paren, handle:
2434 // direct-declarator: '(' declarator ')'
2435 // direct-declarator: '(' attributes declarator ')'
2436 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002437 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002438 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002439 if (AttrList)
Sebastian Redlab197ba2009-02-09 18:23:29 +00002440 D.AddAttributes(AttrList, SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002441
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002442 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002443 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002444 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002445
2446 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002447 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002448 return;
2449 }
Mike Stump1eb44332009-09-09 15:08:12 +00002450
Chris Lattneref4715c2008-04-06 05:45:57 +00002451 // Okay, if this wasn't a grouping paren, it must be the start of a function
2452 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002453 // identifier (and remember where it would have been), then call into
2454 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002455 D.SetIdentifier(0, Tok.getLocation());
2456
Chris Lattner7399ee02008-10-20 02:05:46 +00002457 ParseFunctionDeclarator(StartLoc, D, AttrList, RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002458}
2459
2460/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2461/// declarator D up to a paren, which indicates that we are parsing function
2462/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002463///
Chris Lattner7399ee02008-10-20 02:05:46 +00002464/// If AttrList is non-null, then the caller parsed those arguments immediately
2465/// after the open paren - they should be considered to be the first argument of
2466/// a parameter. If RequiresArg is true, then the first argument of the
2467/// function is required to be present and required to not be an identifier
2468/// list.
2469///
Reid Spencer5f016e22007-07-11 17:01:13 +00002470/// This method also handles this portion of the grammar:
2471/// parameter-type-list: [C99 6.7.5]
2472/// parameter-list
2473/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002474/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002475///
2476/// parameter-list: [C99 6.7.5]
2477/// parameter-declaration
2478/// parameter-list ',' parameter-declaration
2479///
2480/// parameter-declaration: [C99 6.7.5]
2481/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002482/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002483/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002484/// declaration-specifiers abstract-declarator[opt]
2485/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002486/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002487/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2488///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002489/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002490/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002491///
Chris Lattner7399ee02008-10-20 02:05:46 +00002492void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2493 AttributeList *AttrList,
2494 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002495 // lparen is already consumed!
2496 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002497
Chris Lattner7399ee02008-10-20 02:05:46 +00002498 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002499 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002500 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002501 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002502 delete AttrList;
2503 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002504
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002505 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2506 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002507
2508 // cv-qualifier-seq[opt].
2509 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002510 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002511 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002512 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002513 llvm::SmallVector<TypeTy*, 2> Exceptions;
2514 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002515 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002516 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002517 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002518 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002519
2520 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002521 if (Tok.is(tok::kw_throw)) {
2522 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002523 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002524 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002525 hasAnyExceptionSpec);
2526 assert(Exceptions.size() == ExceptionRanges.size() &&
2527 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002528 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002529 }
2530
Chris Lattnerf97409f2008-04-06 06:57:35 +00002531 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002532 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002533 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002534 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002535 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002536 /*arglist*/ 0, 0,
2537 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002538 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002539 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002540 Exceptions.data(),
2541 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002542 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002543 LParenLoc, RParenLoc, D),
2544 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002545 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002546 }
2547
Chris Lattner7399ee02008-10-20 02:05:46 +00002548 // Alternatively, this parameter list may be an identifier list form for a
2549 // K&R-style function: void foo(a,b,c)
Steve Naroff2d081c42009-01-28 19:16:40 +00002550 if (!getLang().CPlusPlus && Tok.is(tok::identifier)) {
Steve Narofff64ef622009-01-30 14:23:32 +00002551 if (!TryAnnotateTypeOrScopeToken()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002552 // K&R identifier lists can't have typedefs as identifiers, per
2553 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002554 if (RequiresArg) {
2555 Diag(Tok, diag::err_argument_required_after_attribute);
2556 delete AttrList;
2557 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002558 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2559 // normal declarators, not for abstract-declarators.
2560 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002561 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002562 }
Mike Stump1eb44332009-09-09 15:08:12 +00002563
Chris Lattnerf97409f2008-04-06 06:57:35 +00002564 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002565
Chris Lattnerf97409f2008-04-06 06:57:35 +00002566 // Build up an array of information about the parsed arguments.
2567 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002568
2569 // Enter function-declaration scope, limiting any declarators to the
2570 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002571 ParseScope PrototypeScope(this,
2572 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002573
Chris Lattnerf97409f2008-04-06 06:57:35 +00002574 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002575 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002576 while (1) {
2577 if (Tok.is(tok::ellipsis)) {
2578 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002579 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002580 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002581 }
Mike Stump1eb44332009-09-09 15:08:12 +00002582
Chris Lattnerf97409f2008-04-06 06:57:35 +00002583 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002584
Chris Lattnerf97409f2008-04-06 06:57:35 +00002585 // Parse the declaration-specifiers.
2586 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002587
2588 // If the caller parsed attributes for the first argument, add them now.
2589 if (AttrList) {
2590 DS.AddAttributes(AttrList);
2591 AttrList = 0; // Only apply the attributes to the first parameter.
2592 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002593 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002594
Chris Lattnerf97409f2008-04-06 06:57:35 +00002595 // Parse the declarator. This is "PrototypeContext", because we must
2596 // accept either 'declarator' or 'abstract-declarator' here.
2597 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2598 ParseDeclarator(ParmDecl);
2599
2600 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002601 if (Tok.is(tok::kw___attribute)) {
2602 SourceLocation Loc;
2603 AttributeList *AttrList = ParseAttributes(&Loc);
2604 ParmDecl.AddAttributes(AttrList, Loc);
2605 }
Mike Stump1eb44332009-09-09 15:08:12 +00002606
Chris Lattnerf97409f2008-04-06 06:57:35 +00002607 // Remember this parsed parameter in ParamInfo.
2608 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Douglas Gregor72b505b2008-12-16 21:30:33 +00002610 // DefArgToks is used when the parsing of default arguments needs
2611 // to be delayed.
2612 CachedTokens *DefArgToks = 0;
2613
Chris Lattnerf97409f2008-04-06 06:57:35 +00002614 // If no parameter was specified, verify that *something* was specified,
2615 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002616 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2617 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002618 // Completely missing, emit error.
2619 Diag(DSStart, diag::err_missing_param);
2620 } else {
2621 // Otherwise, we have something. Add it and let semantic analysis try
2622 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00002623
Chris Lattnerf97409f2008-04-06 06:57:35 +00002624 // Inform the actions module about the parameter declarator, so it gets
2625 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002626 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002627
2628 // Parse the default argument, if any. We parse the default
2629 // arguments in all dialects; the semantic analysis in
2630 // ActOnParamDefaultArgument will reject the default argument in
2631 // C.
2632 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002633 SourceLocation EqualLoc = Tok.getLocation();
2634
Chris Lattner04421082008-04-08 04:40:51 +00002635 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002636 if (D.getContext() == Declarator::MemberContext) {
2637 // If we're inside a class definition, cache the tokens
2638 // corresponding to the default argument. We'll actually parse
2639 // them when we see the end of the class definition.
2640 // FIXME: Templates will require something similar.
2641 // FIXME: Can we use a smart pointer for Toks?
2642 DefArgToks = new CachedTokens;
2643
Mike Stump1eb44332009-09-09 15:08:12 +00002644 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002645 tok::semi, false)) {
2646 delete DefArgToks;
2647 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002648 Actions.ActOnParamDefaultArgumentError(Param);
2649 } else
Mike Stump1eb44332009-09-09 15:08:12 +00002650 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00002651 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002652 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002653 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00002654 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002655
Douglas Gregor72b505b2008-12-16 21:30:33 +00002656 OwningExprResult DefArgResult(ParseAssignmentExpression());
2657 if (DefArgResult.isInvalid()) {
2658 Actions.ActOnParamDefaultArgumentError(Param);
2659 SkipUntil(tok::comma, tok::r_paren, true, true);
2660 } else {
2661 // Inform the actions module about the default argument
2662 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00002663 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00002664 }
Chris Lattner04421082008-04-08 04:40:51 +00002665 }
2666 }
Mike Stump1eb44332009-09-09 15:08:12 +00002667
2668 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
2669 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00002670 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00002671 }
2672
2673 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00002674 if (Tok.isNot(tok::comma)) {
2675 if (Tok.is(tok::ellipsis)) {
2676 IsVariadic = true;
2677 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
2678
2679 if (!getLang().CPlusPlus) {
2680 // We have ellipsis without a preceding ',', which is ill-formed
2681 // in C. Complain and provide the fix.
2682 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
2683 << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
2684 }
2685 }
2686
2687 break;
2688 }
Mike Stump1eb44332009-09-09 15:08:12 +00002689
Chris Lattnerf97409f2008-04-06 06:57:35 +00002690 // Consume the comma.
2691 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002692 }
Mike Stump1eb44332009-09-09 15:08:12 +00002693
Chris Lattnerf97409f2008-04-06 06:57:35 +00002694 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00002695 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00002696
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002697 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002698 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2699 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002700
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002701 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002702 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002703 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002704 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002705 llvm::SmallVector<TypeTy*, 2> Exceptions;
2706 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002707 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002708 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002709 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002710 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002711 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002712
2713 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002714 if (Tok.is(tok::kw_throw)) {
2715 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002716 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002717 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002718 hasAnyExceptionSpec);
2719 assert(Exceptions.size() == ExceptionRanges.size() &&
2720 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002721 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002722 }
2723
Reid Spencer5f016e22007-07-11 17:01:13 +00002724 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002725 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002726 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00002727 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002728 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002729 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002730 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002731 Exceptions.data(),
2732 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002733 Exceptions.size(),
2734 LParenLoc, RParenLoc, D),
2735 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002736}
2737
Chris Lattner66d28652008-04-06 06:34:08 +00002738/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
2739/// we found a K&R-style identifier list instead of a type argument list. The
2740/// current token is known to be the first identifier in the list.
2741///
2742/// identifier-list: [C99 6.7.5]
2743/// identifier
2744/// identifier-list ',' identifier
2745///
2746void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
2747 Declarator &D) {
2748 // Build up an array of information about the parsed arguments.
2749 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
2750 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00002751
Chris Lattner66d28652008-04-06 06:34:08 +00002752 // If there was no identifier specified for the declarator, either we are in
2753 // an abstract-declarator, or we are in a parameter declarator which was found
2754 // to be abstract. In abstract-declarators, identifier lists are not valid:
2755 // diagnose this.
2756 if (!D.getIdentifier())
2757 Diag(Tok, diag::ext_ident_list_in_param);
2758
2759 // Tok is known to be the first identifier in the list. Remember this
2760 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00002761 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00002762 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00002763 Tok.getLocation(),
2764 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00002765
Chris Lattner50c64772008-04-06 06:39:19 +00002766 ConsumeToken(); // eat the first identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00002767
Chris Lattner66d28652008-04-06 06:34:08 +00002768 while (Tok.is(tok::comma)) {
2769 // Eat the comma.
2770 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002771
Chris Lattner50c64772008-04-06 06:39:19 +00002772 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00002773 if (Tok.isNot(tok::identifier)) {
2774 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00002775 SkipUntil(tok::r_paren);
2776 return;
Chris Lattner66d28652008-04-06 06:34:08 +00002777 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002778
Chris Lattner66d28652008-04-06 06:34:08 +00002779 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00002780
2781 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00002782 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00002783 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00002784
Chris Lattner66d28652008-04-06 06:34:08 +00002785 // Verify that the argument identifier has not already been mentioned.
2786 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002787 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00002788 } else {
2789 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00002790 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00002791 Tok.getLocation(),
2792 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00002793 }
Mike Stump1eb44332009-09-09 15:08:12 +00002794
Chris Lattner66d28652008-04-06 06:34:08 +00002795 // Eat the identifier.
2796 ConsumeToken();
2797 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002798
2799 // If we have the closing ')', eat it and we're done.
2800 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2801
Chris Lattner50c64772008-04-06 06:39:19 +00002802 // Remember that we parsed a function type, and remember the attributes. This
2803 // function type is always a K&R style function type, which is not varargs and
2804 // has no prototype.
2805 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002806 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00002807 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002808 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00002809 /*exception*/false,
2810 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002811 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002812 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00002813}
Chris Lattneref4715c2008-04-06 05:45:57 +00002814
Reid Spencer5f016e22007-07-11 17:01:13 +00002815/// [C90] direct-declarator '[' constant-expression[opt] ']'
2816/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2817/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2818/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2819/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2820void Parser::ParseBracketDeclarator(Declarator &D) {
2821 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00002822
Chris Lattner378c7e42008-12-18 07:27:21 +00002823 // C array syntax has many features, but by-far the most common is [] and [4].
2824 // This code does a fast path to handle some of the most obvious cases.
2825 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00002826 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002827 // Remember that we parsed the empty array type.
2828 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002829 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
2830 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002831 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002832 return;
2833 } else if (Tok.getKind() == tok::numeric_constant &&
2834 GetLookAheadToken(1).is(tok::r_square)) {
2835 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00002836 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00002837 ConsumeToken();
2838
Sebastian Redlab197ba2009-02-09 18:23:29 +00002839 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002840
2841 // If there was an error parsing the assignment-expression, recover.
2842 if (ExprRes.isInvalid())
2843 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00002844
Chris Lattner378c7e42008-12-18 07:27:21 +00002845 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002846 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
2847 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002848 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00002849 return;
2850 }
Mike Stump1eb44332009-09-09 15:08:12 +00002851
Reid Spencer5f016e22007-07-11 17:01:13 +00002852 // If valid, this location is the position where we read the 'static' keyword.
2853 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00002854 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002855 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002856
Reid Spencer5f016e22007-07-11 17:01:13 +00002857 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002858 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00002859 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002860 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00002861
Reid Spencer5f016e22007-07-11 17:01:13 +00002862 // If we haven't already read 'static', check to see if there is one after the
2863 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00002864 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00002865 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002866
Reid Spencer5f016e22007-07-11 17:01:13 +00002867 // Handle "direct-declarator [ type-qual-list[opt] * ]".
2868 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00002869 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00002870
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002871 // Handle the case where we have '[*]' as the array size. However, a leading
2872 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
2873 // the the token after the star is a ']'. Since stars in arrays are
2874 // infrequent, use of lookahead is not costly here.
2875 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00002876 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002877
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002878 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002879 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002880 StaticLoc = SourceLocation(); // Drop the static.
2881 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00002882 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00002883 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00002884 // Note, in C89, this production uses the constant-expr production instead
2885 // of assignment-expr. The only difference is that assignment-expr allows
2886 // things like '=' and '*='. Sema rejects these in C89 mode because they
2887 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00002888
Douglas Gregore0762c92009-06-19 23:52:42 +00002889 // Parse the constant-expression or assignment-expression now (depending
2890 // on dialect).
2891 if (getLang().CPlusPlus)
2892 NumElements = ParseConstantExpression();
2893 else
2894 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00002895 }
Mike Stump1eb44332009-09-09 15:08:12 +00002896
Reid Spencer5f016e22007-07-11 17:01:13 +00002897 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002898 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00002899 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002900 // If the expression was invalid, skip it.
2901 SkipUntil(tok::r_square);
2902 return;
2903 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002904
2905 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
2906
Chris Lattner378c7e42008-12-18 07:27:21 +00002907 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00002908 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
2909 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002910 NumElements.release(),
2911 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002912 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002913}
2914
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002915/// [GNU] typeof-specifier:
2916/// typeof ( expressions )
2917/// typeof ( type-name )
2918/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00002919///
2920void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00002921 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002922 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002923 SourceLocation StartLoc = ConsumeToken();
2924
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002925 bool isCastExpr;
2926 TypeTy *CastTy;
2927 SourceRange CastRange;
2928 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
2929 isCastExpr,
2930 CastTy,
2931 CastRange);
2932
2933 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002934 // FIXME: Not accurate, the range gets one token more than it should.
2935 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002936 else
2937 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00002938
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002939 if (isCastExpr) {
2940 if (!CastTy) {
2941 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002942 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00002943 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002944
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002945 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002946 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002947 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2948 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002949 DiagID, CastTy))
2950 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00002951 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002952 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002953
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002954 // If we get here, the operand to the typeof was an expresion.
2955 if (Operand.isInvalid()) {
2956 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00002957 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002958 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00002959
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002960 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002961 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00002962 // Check for duplicate type specifiers (e.g. "int typeof(int)").
2963 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002964 DiagID, Operand.release()))
2965 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00002966}