blob: a034a325f4fc4f44bb1f19736368af5b98cbe754 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Chris Lattner31e05722007-08-26 06:24:45 +000016#include "clang/Parse/Scope.h"
Douglas Gregor314b97f2009-11-10 19:49:08 +000017#include "clang/Parse/Template.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000018#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000019#include "llvm/ADT/SmallSet.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// C99 6.7: Declarations.
24//===----------------------------------------------------------------------===//
25
26/// ParseTypeName
27/// type-name: [C99 6.7.6]
28/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000029///
30/// Called type-id in C++.
Sebastian Redlef65f062009-05-29 18:02:33 +000031Action::TypeResult Parser::ParseTypeName(SourceRange *Range) {
Reid Spencer5f016e22007-07-11 17:01:13 +000032 // Parse the common declaration-specifiers piece.
33 DeclSpec DS;
34 ParseSpecifierQualifierList(DS);
Sebastian Redlef65f062009-05-29 18:02:33 +000035
Reid Spencer5f016e22007-07-11 17:01:13 +000036 // Parse the abstract-declarator, if present.
37 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
38 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000039 if (Range)
40 *Range = DeclaratorInfo.getSourceRange();
41
Chris Lattnereaaebc72009-04-25 08:06:05 +000042 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000043 return true;
44
45 return Actions.ActOnTypeName(CurScope, DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000046}
47
Sean Huntbbd37c62009-11-21 08:43:09 +000048/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000049///
50/// [GNU] attributes:
51/// attribute
52/// attributes attribute
53///
54/// [GNU] attribute:
55/// '__attribute__' '(' '(' attribute-list ')' ')'
56///
57/// [GNU] attribute-list:
58/// attrib
59/// attribute_list ',' attrib
60///
61/// [GNU] attrib:
62/// empty
63/// attrib-name
64/// attrib-name '(' identifier ')'
65/// attrib-name '(' identifier ',' nonempty-expr-list ')'
66/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
67///
68/// [GNU] attrib-name:
69/// identifier
70/// typespec
71/// typequal
72/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000073///
Reid Spencer5f016e22007-07-11 17:01:13 +000074/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000075/// token lookahead. Comment from gcc: "If they start with an identifier
76/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +000077/// start with that identifier; otherwise they are an expression list."
78///
79/// At the moment, I am not doing 2 token lookahead. I am also unaware of
80/// any attributes that don't work (based on my limited testing). Most
81/// attributes are very simple in practice. Until we find a bug, I don't see
82/// a pressing need to implement the 2 token lookahead.
83
Sean Huntbbd37c62009-11-21 08:43:09 +000084AttributeList *Parser::ParseGNUAttributes(SourceLocation *EndLoc) {
85 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +000086
Reid Spencer5f016e22007-07-11 17:01:13 +000087 AttributeList *CurrAttr = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000088
Chris Lattner04d66662007-10-09 17:33:22 +000089 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000090 ConsumeToken();
91 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
92 "attribute")) {
93 SkipUntil(tok::r_paren, true); // skip until ) or ;
94 return CurrAttr;
95 }
96 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
97 SkipUntil(tok::r_paren, true); // skip until ) or ;
98 return CurrAttr;
99 }
100 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000101 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
102 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000103
104 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
106 ConsumeToken();
107 continue;
108 }
109 // we have an identifier or declaration specifier (const, int, etc.)
110 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
111 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Douglas Gregorec1afbf2010-03-16 19:09:18 +0000113 // check if we have a "parameterized" attribute
Chris Lattner04d66662007-10-09 17:33:22 +0000114 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 ConsumeParen(); // ignore the left paren loc for now
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattner04d66662007-10-09 17:33:22 +0000117 if (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 IdentifierInfo *ParmName = Tok.getIdentifierInfo();
119 SourceLocation ParmLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000120
121 if (Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 // __attribute__(( mode(byte) ))
123 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000124 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 ParmName, ParmLoc, 0, 0, CurrAttr);
Chris Lattner04d66662007-10-09 17:33:22 +0000126 } else if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 ConsumeToken();
128 // __attribute__(( format(printf, 1, 2) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000129 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 // now parse the non-empty comma separated list of expressions
133 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000134 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000135 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 ArgExprsOk = false;
137 SkipUntil(tok::r_paren);
138 break;
139 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000140 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 }
Chris Lattner04d66662007-10-09 17:33:22 +0000142 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000143 break;
144 ConsumeToken(); // Eat the comma, move to the next argument
145 }
Chris Lattner04d66662007-10-09 17:33:22 +0000146 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000148 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
149 AttrNameLoc, ParmName, ParmLoc,
150 ArgExprs.take(), ArgExprs.size(),
151 CurrAttr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000152 }
153 }
154 } else { // not an identifier
Nate Begeman6f3d8382009-06-26 06:32:41 +0000155 switch (Tok.getKind()) {
156 case tok::r_paren:
Reid Spencer5f016e22007-07-11 17:01:13 +0000157 // parse a possibly empty comma separated list of expressions
Reid Spencer5f016e22007-07-11 17:01:13 +0000158 // __attribute__(( nonnull() ))
159 ConsumeParen(); // ignore the right paren loc for now
Sean Huntbbd37c62009-11-21 08:43:09 +0000160 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 0, SourceLocation(), 0, 0, CurrAttr);
Nate Begeman6f3d8382009-06-26 06:32:41 +0000162 break;
163 case tok::kw_char:
164 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000165 case tok::kw_char16_t:
166 case tok::kw_char32_t:
Nate Begeman6f3d8382009-06-26 06:32:41 +0000167 case tok::kw_bool:
168 case tok::kw_short:
169 case tok::kw_int:
170 case tok::kw_long:
171 case tok::kw_signed:
172 case tok::kw_unsigned:
173 case tok::kw_float:
174 case tok::kw_double:
175 case tok::kw_void:
176 case tok::kw_typeof:
177 // If it's a builtin type name, eat it and expect a rparen
178 // __attribute__(( vec_type_hint(char) ))
179 ConsumeToken();
Sean Huntbbd37c62009-11-21 08:43:09 +0000180 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Nate Begeman6f3d8382009-06-26 06:32:41 +0000181 0, SourceLocation(), 0, 0, CurrAttr);
182 if (Tok.is(tok::r_paren))
183 ConsumeParen();
184 break;
185 default:
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 // __attribute__(( aligned(16) ))
Sebastian Redla55e52c2008-11-25 22:21:31 +0000187 ExprVector ArgExprs(Actions);
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 bool ArgExprsOk = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 // now parse the list of expressions
191 while (1) {
Sebastian Redl2f7ece72008-12-11 21:36:32 +0000192 OwningExprResult ArgExpr(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +0000193 if (ArgExpr.isInvalid()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 ArgExprsOk = false;
195 SkipUntil(tok::r_paren);
196 break;
197 } else {
Sebastian Redleffa8d12008-12-10 00:02:53 +0000198 ArgExprs.push_back(ArgExpr.release());
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 }
Chris Lattner04d66662007-10-09 17:33:22 +0000200 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 break;
202 ConsumeToken(); // Eat the comma, move to the next argument
203 }
204 // Match the ')'.
Chris Lattner04d66662007-10-09 17:33:22 +0000205 if (ArgExprsOk && Tok.is(tok::r_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 ConsumeParen(); // ignore the right paren loc for now
Sebastian Redla55e52c2008-11-25 22:21:31 +0000207 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0,
Sean Huntbbd37c62009-11-21 08:43:09 +0000208 AttrNameLoc, 0, SourceLocation(), ArgExprs.take(),
209 ArgExprs.size(),
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 CurrAttr);
211 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000212 break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 }
214 }
215 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000216 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 0, SourceLocation(), 0, 0, CurrAttr);
218 }
219 }
220 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000222 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000223 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
224 SkipUntil(tok::r_paren, false);
225 }
226 if (EndLoc)
227 *EndLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 }
229 return CurrAttr;
230}
231
Eli Friedmana23b4852009-06-08 07:21:15 +0000232/// ParseMicrosoftDeclSpec - Parse an __declspec construct
233///
234/// [MS] decl-specifier:
235/// __declspec ( extended-decl-modifier-seq )
236///
237/// [MS] extended-decl-modifier-seq:
238/// extended-decl-modifier[opt]
239/// extended-decl-modifier extended-decl-modifier-seq
240
Eli Friedman290eeb02009-06-08 23:27:34 +0000241AttributeList* Parser::ParseMicrosoftDeclSpec(AttributeList *CurrAttr) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000242 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000243
Steve Narofff59e17e2008-12-24 20:59:21 +0000244 ConsumeToken();
Eli Friedmana23b4852009-06-08 07:21:15 +0000245 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
246 "declspec")) {
247 SkipUntil(tok::r_paren, true); // skip until ) or ;
248 return CurrAttr;
249 }
Eli Friedman290eeb02009-06-08 23:27:34 +0000250 while (Tok.getIdentifierInfo()) {
Eli Friedmana23b4852009-06-08 07:21:15 +0000251 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
252 SourceLocation AttrNameLoc = ConsumeToken();
253 if (Tok.is(tok::l_paren)) {
254 ConsumeParen();
255 // FIXME: This doesn't parse __declspec(property(get=get_func_name))
256 // correctly.
257 OwningExprResult ArgExpr(ParseAssignmentExpression());
258 if (!ArgExpr.isInvalid()) {
259 ExprTy* ExprList = ArgExpr.take();
Sean Huntbbd37c62009-11-21 08:43:09 +0000260 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedmana23b4852009-06-08 07:21:15 +0000261 SourceLocation(), &ExprList, 1,
262 CurrAttr, true);
263 }
264 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
265 SkipUntil(tok::r_paren, false);
266 } else {
Sean Huntbbd37c62009-11-21 08:43:09 +0000267 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc,
268 0, SourceLocation(), 0, 0, CurrAttr, true);
Eli Friedmana23b4852009-06-08 07:21:15 +0000269 }
270 }
271 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
272 SkipUntil(tok::r_paren, false);
Eli Friedman290eeb02009-06-08 23:27:34 +0000273 return CurrAttr;
274}
275
276AttributeList* Parser::ParseMicrosoftTypeAttributes(AttributeList *CurrAttr) {
277 // Treat these like attributes
278 // FIXME: Allow Sema to distinguish between these and real attributes!
279 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
280 Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___ptr64) ||
281 Tok.is(tok::kw___w64)) {
282 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
283 SourceLocation AttrNameLoc = ConsumeToken();
284 if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64))
285 // FIXME: Support these properly!
286 continue;
Sean Huntbbd37c62009-11-21 08:43:09 +0000287 CurrAttr = new AttributeList(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Eli Friedman290eeb02009-06-08 23:27:34 +0000288 SourceLocation(), 0, 0, CurrAttr, true);
289 }
290 return CurrAttr;
Steve Narofff59e17e2008-12-24 20:59:21 +0000291}
292
Reid Spencer5f016e22007-07-11 17:01:13 +0000293/// ParseDeclaration - Parse a full 'declaration', which consists of
294/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +0000295/// 'Context' should be a Declarator::TheContext value. This returns the
296/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +0000297///
298/// declaration: [C99 6.7]
299/// block-declaration ->
300/// simple-declaration
301/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +0000302/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000303/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +0000304/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +0000305/// [C++] using-declaration
Sebastian Redl50de12f2009-03-24 22:27:57 +0000306/// [C++0x] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +0000307/// others... [FIXME]
308///
Chris Lattner97144fc2009-04-02 04:16:50 +0000309Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000310 SourceLocation &DeclEnd,
311 CXX0XAttributeList Attr) {
Chris Lattner682bf922009-03-29 16:50:03 +0000312 DeclPtrTy SingleDecl;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000313 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +0000314 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +0000315 case tok::kw_export:
Sean Huntbbd37c62009-11-21 08:43:09 +0000316 if (Attr.HasAttr)
317 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
318 << Attr.Range;
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000319 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000320 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000321 case tok::kw_namespace:
Sean Huntbbd37c62009-11-21 08:43:09 +0000322 if (Attr.HasAttr)
323 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
324 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000325 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000326 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +0000327 case tok::kw_using:
Sean Huntbbd37c62009-11-21 08:43:09 +0000328 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, DeclEnd, Attr);
Chris Lattner682bf922009-03-29 16:50:03 +0000329 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +0000330 case tok::kw_static_assert:
Sean Huntbbd37c62009-11-21 08:43:09 +0000331 if (Attr.HasAttr)
332 Diag(Attr.Range.getBegin(), diag::err_attributes_not_allowed)
333 << Attr.Range;
Chris Lattner97144fc2009-04-02 04:16:50 +0000334 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +0000335 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +0000336 default:
Chris Lattner5c5db552010-04-05 18:18:31 +0000337 return ParseSimpleDeclaration(Context, DeclEnd, Attr.AttrList, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000338 }
Sean Huntbbd37c62009-11-21 08:43:09 +0000339
Chris Lattner682bf922009-03-29 16:50:03 +0000340 // This routine returns a DeclGroup, if the thing we parsed only contains a
341 // single decl, convert it now.
342 return Actions.ConvertDeclToDeclGroup(SingleDecl);
Chris Lattner8f08cb72007-08-25 06:57:03 +0000343}
344
345/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
346/// declaration-specifiers init-declarator-list[opt] ';'
347///[C90/C++]init-declarator-list ';' [TODO]
348/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +0000349///
350/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +0000351/// declaration. If it is true, it checks for and eats it.
Chris Lattnercd147752009-03-29 17:27:48 +0000352Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +0000353 SourceLocation &DeclEnd,
Chris Lattner5c5db552010-04-05 18:18:31 +0000354 AttributeList *Attr,
355 bool RequireSemi) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +0000357 ParsingDeclSpec DS(*this);
Sean Huntbbd37c62009-11-21 08:43:09 +0000358 if (Attr)
359 DS.AddAttributes(Attr);
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000360 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
361 getDeclSpecContextFromDeclaratorContext(Context));
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
364 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +0000365 if (Tok.is(tok::semi)) {
Chris Lattner5c5db552010-04-05 18:18:31 +0000366 if (RequireSemi) ConsumeToken();
Chris Lattner682bf922009-03-29 16:50:03 +0000367 DeclPtrTy TheDecl = Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
John McCall54abf7d2009-11-04 02:18:39 +0000368 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +0000369 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +0000370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattner5c5db552010-04-05 18:18:31 +0000372 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd);
John McCalld8ac0572009-11-03 19:26:08 +0000373}
Mike Stump1eb44332009-09-09 15:08:12 +0000374
John McCalld8ac0572009-11-03 19:26:08 +0000375/// ParseDeclGroup - Having concluded that this is either a function
376/// definition or a group of object declarations, actually parse the
377/// result.
John McCall54abf7d2009-11-04 02:18:39 +0000378Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
379 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +0000380 bool AllowFunctionDefinitions,
381 SourceLocation *DeclEnd) {
382 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +0000383 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +0000384 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +0000385
John McCalld8ac0572009-11-03 19:26:08 +0000386 // Bail out if the first declarator didn't seem well-formed.
387 if (!D.hasName() && !D.mayOmitIdentifier()) {
388 // Skip until ; or }.
389 SkipUntil(tok::r_brace, true, true);
390 if (Tok.is(tok::semi))
391 ConsumeToken();
392 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +0000393 }
Mike Stump1eb44332009-09-09 15:08:12 +0000394
John McCalld8ac0572009-11-03 19:26:08 +0000395 if (AllowFunctionDefinitions && D.isFunctionDeclarator()) {
396 if (isDeclarationAfterDeclarator()) {
397 // Fall though. We have to check this first, though, because
398 // __attribute__ might be the start of a function definition in
399 // (extended) K&R C.
400 } else if (isStartOfFunctionDefinition()) {
401 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
402 Diag(Tok, diag::err_function_declared_typedef);
403
404 // Recover by treating the 'typedef' as spurious.
405 DS.ClearStorageClassSpecs();
406 }
407
408 DeclPtrTy TheDecl = ParseFunctionDefinition(D);
409 return Actions.ConvertDeclToDeclGroup(TheDecl);
410 } else {
411 Diag(Tok, diag::err_expected_fn_body);
412 SkipUntil(tok::semi);
413 return DeclGroupPtrTy();
414 }
415 }
416
417 llvm::SmallVector<DeclPtrTy, 8> DeclsInGroup;
418 DeclPtrTy FirstDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000419 D.complete(FirstDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000420 if (FirstDecl.get())
421 DeclsInGroup.push_back(FirstDecl);
422
423 // If we don't have a comma, it is either the end of the list (a ';') or an
424 // error, bail out.
425 while (Tok.is(tok::comma)) {
426 // Consume the comma.
Chris Lattner23c4b182009-03-29 17:18:04 +0000427 ConsumeToken();
John McCalld8ac0572009-11-03 19:26:08 +0000428
429 // Parse the next declarator.
430 D.clear();
431
432 // Accept attributes in an init-declarator. In the first declarator in a
433 // declaration, these would be part of the declspec. In subsequent
434 // declarators, they become part of the declarator itself, so that they
435 // don't apply to declarators after *this* one. Examples:
436 // short __attribute__((common)) var; -> declspec
437 // short var __attribute__((common)); -> declarator
438 // short x, __attribute__((common)) var; -> declarator
439 if (Tok.is(tok::kw___attribute)) {
440 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000441 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCalld8ac0572009-11-03 19:26:08 +0000442 D.AddAttributes(AttrList, Loc);
443 }
444
445 ParseDeclarator(D);
446
447 DeclPtrTy ThisDecl = ParseDeclarationAfterDeclarator(D);
John McCall54abf7d2009-11-04 02:18:39 +0000448 D.complete(ThisDecl);
John McCalld8ac0572009-11-03 19:26:08 +0000449 if (ThisDecl.get())
450 DeclsInGroup.push_back(ThisDecl);
451 }
452
453 if (DeclEnd)
454 *DeclEnd = Tok.getLocation();
455
456 if (Context != Declarator::ForContext &&
457 ExpectAndConsume(tok::semi,
458 Context == Declarator::FileContext
459 ? diag::err_invalid_token_after_toplevel_declarator
460 : diag::err_expected_semi_declaration)) {
461 SkipUntil(tok::r_brace, true, true);
462 if (Tok.is(tok::semi))
463 ConsumeToken();
464 }
465
466 return Actions.FinalizeDeclaratorGroup(CurScope, DS,
467 DeclsInGroup.data(),
468 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +0000469}
470
Douglas Gregor1426e532009-05-12 21:31:51 +0000471/// \brief Parse 'declaration' after parsing 'declaration-specifiers
472/// declarator'. This method parses the remainder of the declaration
473/// (including any attributes or initializer, among other things) and
474/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +0000475///
Reid Spencer5f016e22007-07-11 17:01:13 +0000476/// init-declarator: [C99 6.7]
477/// declarator
478/// declarator '=' initializer
479/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
480/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +0000481/// [C++] declarator initializer[opt]
482///
483/// [C++] initializer:
484/// [C++] '=' initializer-clause
485/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +0000486/// [C++0x] '=' 'default' [TODO]
487/// [C++0x] '=' 'delete'
488///
489/// According to the standard grammar, =default and =delete are function
490/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +0000491///
Douglas Gregore542c862009-06-23 23:11:28 +0000492Parser::DeclPtrTy Parser::ParseDeclarationAfterDeclarator(Declarator &D,
493 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +0000494 // If a simple-asm-expr is present, parse it.
495 if (Tok.is(tok::kw_asm)) {
496 SourceLocation Loc;
497 OwningExprResult AsmLabel(ParseSimpleAsm(&Loc));
498 if (AsmLabel.isInvalid()) {
499 SkipUntil(tok::semi, true, true);
500 return DeclPtrTy();
501 }
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Douglas Gregor1426e532009-05-12 21:31:51 +0000503 D.setAsmLabel(AsmLabel.release());
504 D.SetRangeEnd(Loc);
505 }
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Douglas Gregor1426e532009-05-12 21:31:51 +0000507 // If attributes are present, parse them.
508 if (Tok.is(tok::kw___attribute)) {
509 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +0000510 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000511 D.AddAttributes(AttrList, Loc);
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Douglas Gregor1426e532009-05-12 21:31:51 +0000514 // Inform the current actions module that we just parsed this declarator.
Douglas Gregord5a423b2009-09-25 18:43:00 +0000515 DeclPtrTy ThisDecl;
516 switch (TemplateInfo.Kind) {
517 case ParsedTemplateInfo::NonTemplate:
518 ThisDecl = Actions.ActOnDeclarator(CurScope, D);
519 break;
520
521 case ParsedTemplateInfo::Template:
522 case ParsedTemplateInfo::ExplicitSpecialization:
523 ThisDecl = Actions.ActOnTemplateDeclarator(CurScope,
Douglas Gregore542c862009-06-23 23:11:28 +0000524 Action::MultiTemplateParamsArg(Actions,
525 TemplateInfo.TemplateParams->data(),
526 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +0000527 D);
528 break;
529
530 case ParsedTemplateInfo::ExplicitInstantiation: {
531 Action::DeclResult ThisRes
532 = Actions.ActOnExplicitInstantiation(CurScope,
533 TemplateInfo.ExternLoc,
534 TemplateInfo.TemplateLoc,
535 D);
536 if (ThisRes.isInvalid()) {
537 SkipUntil(tok::semi, true, true);
538 return DeclPtrTy();
539 }
540
541 ThisDecl = ThisRes.get();
542 break;
543 }
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Douglas Gregor1426e532009-05-12 21:31:51 +0000546 // Parse declarator '=' initializer.
547 if (Tok.is(tok::equal)) {
548 ConsumeToken();
549 if (getLang().CPlusPlus0x && Tok.is(tok::kw_delete)) {
550 SourceLocation DelLoc = ConsumeToken();
551 Actions.SetDeclDeleted(ThisDecl, DelLoc);
552 } else {
John McCall731ad842009-12-19 09:28:58 +0000553 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
554 EnterScope(0);
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000555 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000556 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000557
Douglas Gregor1426e532009-05-12 21:31:51 +0000558 OwningExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000559
John McCall731ad842009-12-19 09:28:58 +0000560 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000561 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
John McCall731ad842009-12-19 09:28:58 +0000562 ExitScope();
563 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +0000564
Douglas Gregor1426e532009-05-12 21:31:51 +0000565 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +0000566 SkipUntil(tok::comma, true, true);
567 Actions.ActOnInitializerError(ThisDecl);
568 } else
569 Actions.AddInitializerToDecl(ThisDecl, move(Init));
Douglas Gregor1426e532009-05-12 21:31:51 +0000570 }
571 } else if (Tok.is(tok::l_paren)) {
572 // Parse C++ direct initializer: '(' expression-list ')'
573 SourceLocation LParenLoc = ConsumeParen();
574 ExprVector Exprs(Actions);
575 CommaLocsTy CommaLocs;
576
Douglas Gregorb4debae2009-12-22 17:47:17 +0000577 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
578 EnterScope(0);
579 Actions.ActOnCXXEnterDeclInitializer(CurScope, ThisDecl);
580 }
581
Douglas Gregor1426e532009-05-12 21:31:51 +0000582 if (ParseExpressionList(Exprs, CommaLocs)) {
583 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +0000584
585 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
586 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
587 ExitScope();
588 }
Douglas Gregor1426e532009-05-12 21:31:51 +0000589 } else {
590 // Match the ')'.
591 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
592
593 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
594 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +0000595
596 if (getLang().CPlusPlus && D.getCXXScopeSpec().isSet()) {
597 Actions.ActOnCXXExitDeclInitializer(CurScope, ThisDecl);
598 ExitScope();
599 }
600
Douglas Gregor1426e532009-05-12 21:31:51 +0000601 Actions.AddCXXDirectInitializerToDecl(ThisDecl, LParenLoc,
602 move_arg(Exprs),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000603 CommaLocs.data(), RParenLoc);
Douglas Gregor1426e532009-05-12 21:31:51 +0000604 }
605 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000606 bool TypeContainsUndeducedAuto =
Anders Carlsson6a75cd92009-07-11 00:34:39 +0000607 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
608 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsUndeducedAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +0000609 }
610
611 return ThisDecl;
612}
613
Reid Spencer5f016e22007-07-11 17:01:13 +0000614/// ParseSpecifierQualifierList
615/// specifier-qualifier-list:
616/// type-specifier specifier-qualifier-list[opt]
617/// type-qualifier specifier-qualifier-list[opt]
618/// [GNU] attributes specifier-qualifier-list[opt]
619///
620void Parser::ParseSpecifierQualifierList(DeclSpec &DS) {
621 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
622 /// parse declaration-specifiers and complain about extra stuff.
Reid Spencer5f016e22007-07-11 17:01:13 +0000623 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 // Validate declspec for type-name.
626 unsigned Specs = DS.getParsedSpecifiers();
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000627 if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
628 !DS.getAttributes())
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 Diag(Tok, diag::err_typename_requires_specqual);
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Reid Spencer5f016e22007-07-11 17:01:13 +0000631 // Issue diagnostic and remove storage class if present.
632 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
633 if (DS.getStorageClassSpecLoc().isValid())
634 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
635 else
636 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
637 DS.ClearStorageClassSpecs();
638 }
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Reid Spencer5f016e22007-07-11 17:01:13 +0000640 // Issue diagnostic and remove function specfier if present.
641 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +0000642 if (DS.isInlineSpecified())
643 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
644 if (DS.isVirtualSpecified())
645 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
646 if (DS.isExplicitSpecified())
647 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 DS.ClearFunctionSpecs();
649 }
650}
651
Chris Lattnerc199ab32009-04-12 20:42:31 +0000652/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
653/// specified token is valid after the identifier in a declarator which
654/// immediately follows the declspec. For example, these things are valid:
655///
656/// int x [ 4]; // direct-declarator
657/// int x ( int y); // direct-declarator
658/// int(int x ) // direct-declarator
659/// int x ; // simple-declaration
660/// int x = 17; // init-declarator-list
661/// int x , y; // init-declarator-list
662/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000663/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +0000664/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +0000665///
666/// This is not, because 'x' does not immediately follow the declspec (though
667/// ')' happens to be valid anyway).
668/// int (x)
669///
670static bool isValidAfterIdentifierInDeclarator(const Token &T) {
671 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
672 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +0000673 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +0000674}
675
Chris Lattnere40c2952009-04-14 21:34:55 +0000676
677/// ParseImplicitInt - This method is called when we have an non-typename
678/// identifier in a declspec (which normally terminates the decl spec) when
679/// the declspec has no type specifier. In this case, the declspec is either
680/// malformed or is "implicit int" (in K&R and C89).
681///
682/// This method handles diagnosing this prettily and returns false if the
683/// declspec is done being processed. If it recovers and thinks there may be
684/// other pieces of declspec after it, it returns true.
685///
Chris Lattnerf4382f52009-04-14 22:17:06 +0000686bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000687 const ParsedTemplateInfo &TemplateInfo,
Chris Lattnere40c2952009-04-14 21:34:55 +0000688 AccessSpecifier AS) {
Chris Lattnerf4382f52009-04-14 22:17:06 +0000689 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Chris Lattnere40c2952009-04-14 21:34:55 +0000691 SourceLocation Loc = Tok.getLocation();
692 // If we see an identifier that is not a type name, we normally would
693 // parse it as the identifer being declared. However, when a typename
694 // is typo'd or the definition is not included, this will incorrectly
695 // parse the typename as the identifier name and fall over misparsing
696 // later parts of the diagnostic.
697 //
698 // As such, we try to do some look-ahead in cases where this would
699 // otherwise be an "implicit-int" case to see if this is invalid. For
700 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
701 // an identifier with implicit int, we'd get a parse error because the
702 // next token is obviously invalid for a type. Parse these as a case
703 // with an invalid type specifier.
704 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Chris Lattnere40c2952009-04-14 21:34:55 +0000706 // Since we know that this either implicit int (which is rare) or an
707 // error, we'd do lookahead to try to do better recovery.
708 if (isValidAfterIdentifierInDeclarator(NextToken())) {
709 // If this token is valid for implicit int, e.g. "static x = 4", then
710 // we just avoid eating the identifier, so it will be parsed as the
711 // identifier in the declarator.
712 return false;
713 }
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Chris Lattnere40c2952009-04-14 21:34:55 +0000715 // Otherwise, if we don't consume this token, we are going to emit an
716 // error anyway. Try to recover from various common problems. Check
717 // to see if this was a reference to a tag name without a tag specified.
718 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +0000719 //
720 // C++ doesn't need this, and isTagName doesn't take SS.
721 if (SS == 0) {
722 const char *TagName = 0;
723 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Chris Lattnere40c2952009-04-14 21:34:55 +0000725 switch (Actions.isTagName(*Tok.getIdentifierInfo(), CurScope)) {
726 default: break;
727 case DeclSpec::TST_enum: TagName="enum" ;TagKind=tok::kw_enum ;break;
728 case DeclSpec::TST_union: TagName="union" ;TagKind=tok::kw_union ;break;
729 case DeclSpec::TST_struct:TagName="struct";TagKind=tok::kw_struct;break;
730 case DeclSpec::TST_class: TagName="class" ;TagKind=tok::kw_class ;break;
731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Chris Lattnerf4382f52009-04-14 22:17:06 +0000733 if (TagName) {
734 Diag(Loc, diag::err_use_of_tag_name_without_tag)
John McCall23e907a2010-02-14 01:03:10 +0000735 << Tok.getIdentifierInfo() << TagName << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +0000736 << FixItHint::CreateInsertion(Tok.getLocation(),TagName);
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Chris Lattnerf4382f52009-04-14 22:17:06 +0000738 // Parse this as a tag as if the missing tag were present.
739 if (TagKind == tok::kw_enum)
Douglas Gregor9b9edd62010-03-02 17:53:14 +0000740 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000741 else
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000742 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS);
Chris Lattnerf4382f52009-04-14 22:17:06 +0000743 return true;
744 }
Chris Lattnere40c2952009-04-14 21:34:55 +0000745 }
Mike Stump1eb44332009-09-09 15:08:12 +0000746
Douglas Gregora786fdb2009-10-13 23:27:22 +0000747 // This is almost certainly an invalid type name. Let the action emit a
748 // diagnostic and attempt to recover.
749 Action::TypeTy *T = 0;
750 if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc,
751 CurScope, SS, T)) {
752 // The action emitted a diagnostic, so we don't have to.
753 if (T) {
754 // The action has suggested that the type T could be used. Set that as
755 // the type in the declaration specifiers, consume the would-be type
756 // name token, and we're done.
757 const char *PrevSpec;
758 unsigned DiagID;
759 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
760 false);
761 DS.SetRangeEnd(Tok.getLocation());
762 ConsumeToken();
763
764 // There may be other declaration specifiers after this.
765 return true;
766 }
767
768 // Fall through; the action had no suggestion for us.
769 } else {
770 // The action did not emit a diagnostic, so emit one now.
771 SourceRange R;
772 if (SS) R = SS->getRange();
773 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Douglas Gregora786fdb2009-10-13 23:27:22 +0000776 // Mark this as an error.
Chris Lattnere40c2952009-04-14 21:34:55 +0000777 const char *PrevSpec;
John McCallfec54012009-08-03 20:12:06 +0000778 unsigned DiagID;
779 DS.SetTypeSpecType(DeclSpec::TST_error, Loc, PrevSpec, DiagID);
Chris Lattnere40c2952009-04-14 21:34:55 +0000780 DS.SetRangeEnd(Tok.getLocation());
781 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattnere40c2952009-04-14 21:34:55 +0000783 // TODO: Could inject an invalid typedef decl in an enclosing scope to
784 // avoid rippling error messages on subsequent uses of the same type,
785 // could be useful if #include was forgotten.
786 return false;
787}
788
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000789/// \brief Determine the declaration specifier context from the declarator
790/// context.
791///
792/// \param Context the declarator context, which is one of the
793/// Declarator::TheContext enumerator values.
794Parser::DeclSpecContext
795Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
796 if (Context == Declarator::MemberContext)
797 return DSC_class;
798 if (Context == Declarator::FileContext)
799 return DSC_top_level;
800 return DSC_normal;
801}
802
Reid Spencer5f016e22007-07-11 17:01:13 +0000803/// ParseDeclarationSpecifiers
804/// declaration-specifiers: [C99 6.7]
805/// storage-class-specifier declaration-specifiers[opt]
806/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +0000807/// [C99] function-specifier declaration-specifiers[opt]
808/// [GNU] attributes declaration-specifiers[opt]
809///
810/// storage-class-specifier: [C99 6.7.1]
811/// 'typedef'
812/// 'extern'
813/// 'static'
814/// 'auto'
815/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +0000816/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +0000817/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +0000818/// function-specifier: [C99 6.7.4]
819/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +0000820/// [C++] 'virtual'
821/// [C++] 'explicit'
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000822/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +0000823/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +0000824
Reid Spencer5f016e22007-07-11 17:01:13 +0000825///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +0000826void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000827 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +0000828 AccessSpecifier AS,
829 DeclSpecContext DSContext) {
Douglas Gregor791215b2009-09-21 20:51:25 +0000830 if (Tok.is(tok::code_completion)) {
Douglas Gregor01dfea02010-01-10 23:08:15 +0000831 Action::CodeCompletionContext CCC = Action::CCC_Namespace;
832 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
833 CCC = DSContext == DSC_class? Action::CCC_MemberTemplate
834 : Action::CCC_Template;
835 else if (DSContext == DSC_class)
836 CCC = Action::CCC_Class;
Douglas Gregorb6ac2452010-01-13 21:24:21 +0000837 else if (ObjCImpDecl)
838 CCC = Action::CCC_ObjCImplementation;
839
Douglas Gregor01dfea02010-01-10 23:08:15 +0000840 Actions.CodeCompleteOrdinaryName(CurScope, CCC);
Douglas Gregor791215b2009-09-21 20:51:25 +0000841 ConsumeToken();
842 }
843
Chris Lattner81c018d2008-03-13 06:29:04 +0000844 DS.SetRangeStart(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 while (1) {
John McCallfec54012009-08-03 20:12:06 +0000846 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +0000848 unsigned DiagID = 0;
849
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +0000851
Reid Spencer5f016e22007-07-11 17:01:13 +0000852 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000853 default:
Chris Lattnerbce61352008-07-26 00:20:22 +0000854 DoneWithDeclSpec:
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 // If this is not a declaration specifier token, we're done reading decl
856 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +0000857 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Chris Lattner5e02c472009-01-05 00:07:25 +0000860 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +0000861 // C++ scope specifier. Annotate and loop, or bail out on error.
862 if (TryAnnotateCXXScopeToken(true)) {
863 if (!DS.hasTypeSpecifier())
864 DS.SetTypeSpecError();
865 goto DoneWithDeclSpec;
866 }
John McCall2e0a7152010-03-01 18:20:46 +0000867 if (Tok.is(tok::coloncolon)) // ::new or ::delete
868 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +0000869 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000870
871 case tok::annot_cxxscope: {
872 if (DS.hasTypeSpecifier())
873 goto DoneWithDeclSpec;
874
John McCallaa87d332009-12-12 11:40:51 +0000875 CXXScopeSpec SS;
876 SS.setScopeRep(Tok.getAnnotationValue());
877 SS.setRange(Tok.getAnnotationRange());
878
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000879 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +0000880 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000881 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000882 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +0000883 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +0000884 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000885
886 // C++ [class.qual]p2:
887 // In a lookup in which the constructor is an acceptable lookup
888 // result and the nested-name-specifier nominates a class C:
889 //
890 // - if the name specified after the
891 // nested-name-specifier, when looked up in C, is the
892 // injected-class-name of C (Clause 9), or
893 //
894 // - if the name specified after the nested-name-specifier
895 // is the same as the identifier or the
896 // simple-template-id's template-name in the last
897 // component of the nested-name-specifier,
898 //
899 // the name is instead considered to name the constructor of
900 // class C.
901 //
902 // Thus, if the template-name is actually the constructor
903 // name, then the code is ill-formed; this interpretation is
904 // reinforced by the NAD status of core issue 635.
905 TemplateIdAnnotation *TemplateId
906 = static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue());
John McCallba9d8532010-04-13 06:39:49 +0000907 if ((DSContext == DSC_top_level ||
908 (DSContext == DSC_class && DS.isFriendSpecified())) &&
909 TemplateId->Name &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000910 Actions.isCurrentClassName(*TemplateId->Name, CurScope, &SS)) {
911 if (isConstructorDeclarator()) {
912 // The user meant this to be an out-of-line constructor
913 // definition, but template arguments are not allowed
914 // there. Just allow this as a constructor; we'll
915 // complain about it later.
916 goto DoneWithDeclSpec;
917 }
918
919 // The user meant this to name a type, but it actually names
920 // a constructor with some extraneous template
921 // arguments. Complain, then parse it as a type as the user
922 // intended.
923 Diag(TemplateId->TemplateNameLoc,
924 diag::err_out_of_line_template_id_names_constructor)
925 << TemplateId->Name;
926 }
927
John McCallaa87d332009-12-12 11:40:51 +0000928 DS.getTypeSpecScope() = SS;
929 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +0000930 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +0000931 "ParseOptionalCXXScopeSpecifier not working");
932 AnnotateTemplateIdTokenAsType(&SS);
933 continue;
934 }
935
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000936 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +0000937 DS.getTypeSpecScope() = SS;
938 ConsumeToken(); // The C++ scope.
Douglas Gregor9d7b3532009-09-28 07:26:33 +0000939 if (Tok.getAnnotationValue())
940 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc,
941 PrevSpec, DiagID,
942 Tok.getAnnotationValue());
943 else
944 DS.SetTypeSpecError();
945 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
946 ConsumeToken(); // The typename
947 }
948
Douglas Gregor9135c722009-03-25 15:40:00 +0000949 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000950 goto DoneWithDeclSpec;
951
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000952 // If we're in a context where the identifier could be a class name,
953 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +0000954 if ((DSContext == DSC_top_level ||
955 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +0000956 Actions.isCurrentClassName(*Next.getIdentifierInfo(), CurScope,
957 &SS)) {
958 if (isConstructorDeclarator())
959 goto DoneWithDeclSpec;
960
961 // As noted in C++ [class.qual]p2 (cited above), when the name
962 // of the class is qualified in a context where it could name
963 // a constructor, its a constructor name. However, we've
964 // looked at the declarator, and the user probably meant this
965 // to be a type. Complain that it isn't supposed to be treated
966 // as a type, then proceed to parse it as a type.
967 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
968 << Next.getIdentifierInfo();
969 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000970
Douglas Gregorb696ea32009-02-04 17:00:24 +0000971 TypeTy *TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
972 Next.getLocation(), CurScope, &SS);
Douglas Gregor55f6b142009-02-09 18:46:07 +0000973
Chris Lattnerf4382f52009-04-14 22:17:06 +0000974 // If the referenced identifier is not a type, then this declspec is
975 // erroneous: We already checked about that it has no type specifier, and
976 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +0000977 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +0000978 if (TypeRep == 0) {
979 ConsumeToken(); // Eat the scope spec so the identifier is current.
Douglas Gregor4d9a16f2009-05-12 23:25:50 +0000980 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000981 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +0000982 }
Mike Stump1eb44332009-09-09 15:08:12 +0000983
John McCallaa87d332009-12-12 11:40:51 +0000984 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000985 ConsumeToken(); // The C++ scope.
986
Douglas Gregor1a51b4a2009-02-09 15:09:02 +0000987 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +0000988 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000989 if (isInvalid)
990 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +0000992 DS.SetRangeEnd(Tok.getLocation());
993 ConsumeToken(); // The typename.
994
995 continue;
996 }
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattner80d0c892009-01-21 19:48:37 +0000998 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +0000999 if (Tok.getAnnotationValue())
1000 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001001 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001002 else
1003 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00001004
1005 if (isInvalid)
1006 break;
1007
Chris Lattner80d0c892009-01-21 19:48:37 +00001008 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1009 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Chris Lattner80d0c892009-01-21 19:48:37 +00001011 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1012 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1013 // Objective-C interface. If we don't have Objective-C or a '<', this is
1014 // just a normal reference to a typedef name.
1015 if (!Tok.is(tok::less) || !getLang().ObjC1)
1016 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001018 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001019 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001020 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1021 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1022 LAngleLoc, EndProtoLoc);
1023 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1024 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Chris Lattner80d0c892009-01-21 19:48:37 +00001026 DS.SetRangeEnd(EndProtoLoc);
1027 continue;
1028 }
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Chris Lattner3bd934a2008-07-26 01:18:38 +00001030 // typedef-name
1031 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00001032 // In C++, check to see if this is a scope specifier like foo::bar::, if
1033 // so handle it as such. This is important for ctor parsing.
John McCall9ba61662010-02-26 08:45:28 +00001034 if (getLang().CPlusPlus) {
1035 if (TryAnnotateCXXScopeToken(true)) {
1036 if (!DS.hasTypeSpecifier())
1037 DS.SetTypeSpecError();
1038 goto DoneWithDeclSpec;
1039 }
1040 if (!Tok.is(tok::identifier))
1041 continue;
1042 }
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Chris Lattner3bd934a2008-07-26 01:18:38 +00001044 // This identifier can only be a typedef name if we haven't already seen
1045 // a type-specifier. Without this check we misparse:
1046 // typedef int X; struct Y { short X; }; as 'short int'.
1047 if (DS.hasTypeSpecifier())
1048 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001049
John Thompson82287d12010-02-05 00:12:22 +00001050 // Check for need to substitute AltiVec keyword tokens.
1051 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1052 break;
1053
Chris Lattner3bd934a2008-07-26 01:18:38 +00001054 // It has to be available as a typedef too!
Mike Stump1eb44332009-09-09 15:08:12 +00001055 TypeTy *TypeRep = Actions.getTypeName(*Tok.getIdentifierInfo(),
Douglas Gregorb696ea32009-02-04 17:00:24 +00001056 Tok.getLocation(), CurScope);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001057
Chris Lattnerc199ab32009-04-12 20:42:31 +00001058 // If this is not a typedef name, don't parse it as part of the declspec,
1059 // it must be an implicit int or an error.
1060 if (TypeRep == 0) {
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001061 if (ParseImplicitInt(DS, 0, TemplateInfo, AS)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001062 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00001063 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00001064
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001065 // If we're in a context where the identifier could be a class name,
1066 // check whether this is a constructor declaration.
1067 if (getLang().CPlusPlus && DSContext == DSC_class &&
Mike Stump1eb44332009-09-09 15:08:12 +00001068 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), CurScope) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001069 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00001070 goto DoneWithDeclSpec;
1071
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00001072 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001073 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001074 if (isInvalid)
1075 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Chris Lattner3bd934a2008-07-26 01:18:38 +00001077 DS.SetRangeEnd(Tok.getLocation());
1078 ConsumeToken(); // The identifier
1079
1080 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1081 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1082 // Objective-C interface. If we don't have Objective-C or a '<', this is
1083 // just a normal reference to a typedef name.
1084 if (!Tok.is(tok::less) || !getLang().ObjC1)
1085 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001087 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001088 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001089 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1090 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1091 LAngleLoc, EndProtoLoc);
1092 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1093 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Chris Lattner3bd934a2008-07-26 01:18:38 +00001095 DS.SetRangeEnd(EndProtoLoc);
1096
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001097 // Need to support trailing type qualifiers (e.g. "id<p> const").
1098 // If a type specifier follows, it will be diagnosed elsewhere.
1099 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00001100 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00001101
1102 // type-name
1103 case tok::annot_template_id: {
Mike Stump1eb44332009-09-09 15:08:12 +00001104 TemplateIdAnnotation *TemplateId
Douglas Gregor39a8de12009-02-25 19:37:18 +00001105 = static_cast<TemplateIdAnnotation *>(Tok.getAnnotationValue());
Douglas Gregorc45c2322009-03-31 00:43:58 +00001106 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00001107 // This template-id does not refer to a type name, so we're
1108 // done with the type-specifiers.
1109 goto DoneWithDeclSpec;
1110 }
1111
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001112 // If we're in a context where the template-id could be a
1113 // constructor name or specialization, check whether this is a
1114 // constructor declaration.
1115 if (getLang().CPlusPlus && DSContext == DSC_class &&
1116 Actions.isCurrentClassName(*TemplateId->Name, CurScope) &&
1117 isConstructorDeclarator())
1118 goto DoneWithDeclSpec;
1119
Douglas Gregor39a8de12009-02-25 19:37:18 +00001120 // Turn the template-id annotation token into a type annotation
1121 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00001122 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00001123 continue;
1124 }
1125
Reid Spencer5f016e22007-07-11 17:01:13 +00001126 // GNU attributes support.
1127 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00001128 DS.AddAttributes(ParseGNUAttributes());
Reid Spencer5f016e22007-07-11 17:01:13 +00001129 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00001130
1131 // Microsoft declspec support.
1132 case tok::kw___declspec:
Eli Friedmana23b4852009-06-08 07:21:15 +00001133 DS.AddAttributes(ParseMicrosoftDeclSpec());
Steve Narofff59e17e2008-12-24 20:59:21 +00001134 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Steve Naroff239f0732008-12-25 14:16:32 +00001136 // Microsoft single token adornments.
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001137 case tok::kw___forceinline:
Eli Friedman290eeb02009-06-08 23:27:34 +00001138 // FIXME: Add handling here!
1139 break;
1140
1141 case tok::kw___ptr64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00001142 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001143 case tok::kw___cdecl:
1144 case tok::kw___stdcall:
1145 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001146 DS.AddAttributes(ParseMicrosoftTypeAttributes());
1147 continue;
1148
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 // storage-class-specifier
1150 case tok::kw_typedef:
John McCallfec54012009-08-03 20:12:06 +00001151 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_typedef, Loc, PrevSpec,
1152 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 break;
1154 case tok::kw_extern:
1155 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001156 Diag(Tok, diag::ext_thread_before) << "extern";
John McCallfec54012009-08-03 20:12:06 +00001157 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_extern, Loc, PrevSpec,
1158 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00001160 case tok::kw___private_extern__:
Chris Lattnerf97409f2008-04-06 06:57:35 +00001161 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_private_extern, Loc,
John McCallfec54012009-08-03 20:12:06 +00001162 PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00001163 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 case tok::kw_static:
1165 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00001166 Diag(Tok, diag::ext_thread_before) << "static";
John McCallfec54012009-08-03 20:12:06 +00001167 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_static, Loc, PrevSpec,
1168 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001169 break;
1170 case tok::kw_auto:
Anders Carlssone89d1592009-06-26 18:41:36 +00001171 if (getLang().CPlusPlus0x)
John McCallfec54012009-08-03 20:12:06 +00001172 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
1173 DiagID);
Anders Carlssone89d1592009-06-26 18:41:36 +00001174 else
John McCallfec54012009-08-03 20:12:06 +00001175 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_auto, Loc, PrevSpec,
1176 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 break;
1178 case tok::kw_register:
John McCallfec54012009-08-03 20:12:06 +00001179 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_register, Loc, PrevSpec,
1180 DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001181 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00001182 case tok::kw_mutable:
John McCallfec54012009-08-03 20:12:06 +00001183 isInvalid = DS.SetStorageClassSpec(DeclSpec::SCS_mutable, Loc, PrevSpec,
1184 DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00001185 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00001187 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Reid Spencer5f016e22007-07-11 17:01:13 +00001190 // function-specifier
1191 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00001192 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001194 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00001195 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001196 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00001197 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00001198 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00001199 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001200
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001201 // friend
1202 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00001203 if (DSContext == DSC_class)
1204 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
1205 else {
1206 PrevSpec = ""; // not actually used by the diagnostic
1207 DiagID = diag::err_friend_invalid_in_context;
1208 isInvalid = true;
1209 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00001210 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001211
Sebastian Redl2ac67232009-11-05 15:47:02 +00001212 // constexpr
1213 case tok::kw_constexpr:
1214 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
1215 break;
1216
Chris Lattner80d0c892009-01-21 19:48:37 +00001217 // type-specifier
1218 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001219 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
1220 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001221 break;
1222 case tok::kw_long:
1223 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001224 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1225 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001226 else
John McCallfec54012009-08-03 20:12:06 +00001227 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1228 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001229 break;
1230 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001231 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
1232 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001233 break;
1234 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001235 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1236 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001237 break;
1238 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001239 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1240 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001241 break;
1242 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001243 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1244 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001245 break;
1246 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001247 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
1248 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001249 break;
1250 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001251 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
1252 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001253 break;
1254 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001255 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
1256 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001257 break;
1258 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001259 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
1260 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001261 break;
1262 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001263 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
1264 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001265 break;
1266 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001267 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
1268 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001269 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001270 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001271 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
1272 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001273 break;
1274 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001275 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
1276 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001277 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001278 case tok::kw_bool:
1279 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001280 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
1281 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001282 break;
1283 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001284 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1285 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001286 break;
1287 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001288 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1289 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001290 break;
1291 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001292 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1293 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00001294 break;
John Thompson82287d12010-02-05 00:12:22 +00001295 case tok::kw___vector:
1296 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1297 break;
1298 case tok::kw___pixel:
1299 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1300 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00001301
1302 // class-specifier:
1303 case tok::kw_class:
1304 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001305 case tok::kw_union: {
1306 tok::TokenKind Kind = Tok.getKind();
1307 ConsumeToken();
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001308 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001309 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00001310 }
Chris Lattner80d0c892009-01-21 19:48:37 +00001311
1312 // enum-specifier:
1313 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001314 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001315 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS);
Chris Lattner80d0c892009-01-21 19:48:37 +00001316 continue;
1317
1318 // cv-qualifier:
1319 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00001320 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
1321 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001322 break;
1323 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00001324 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
1325 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001326 break;
1327 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00001328 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
1329 getLang());
Chris Lattner80d0c892009-01-21 19:48:37 +00001330 break;
1331
Douglas Gregord57959a2009-03-27 23:10:48 +00001332 // C++ typename-specifier:
1333 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00001334 if (TryAnnotateTypeOrScopeToken()) {
1335 DS.SetTypeSpecError();
1336 goto DoneWithDeclSpec;
1337 }
1338 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00001339 continue;
1340 break;
1341
Chris Lattner80d0c892009-01-21 19:48:37 +00001342 // GNU typeof support.
1343 case tok::kw_typeof:
1344 ParseTypeofSpecifier(DS);
1345 continue;
1346
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001347 case tok::kw_decltype:
1348 ParseDecltypeSpecifier(DS);
1349 continue;
1350
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001351 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00001352 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00001353 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
1354 // but we support it.
Chris Lattner3bd934a2008-07-26 01:18:38 +00001355 if (DS.hasTypeSpecifier() || !getLang().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00001356 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00001357
Chris Lattnerbce61352008-07-26 00:20:22 +00001358 {
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001359 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001360 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001361 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1362 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1363 LAngleLoc, EndProtoLoc);
1364 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1365 ProtocolLocs.data(), LAngleLoc);
Chris Lattner3bd934a2008-07-26 01:18:38 +00001366 DS.SetRangeEnd(EndProtoLoc);
1367
Chris Lattner1ab3b962008-11-18 07:48:38 +00001368 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
Douglas Gregor849b2432010-03-31 17:46:05 +00001369 << FixItHint::CreateInsertion(Loc, "id")
Chris Lattner1ab3b962008-11-18 07:48:38 +00001370 << SourceRange(Loc, EndProtoLoc);
Steve Naroff4f9b9f12008-09-22 10:28:57 +00001371 // Need to support trailing type qualifiers (e.g. "id<p> const").
1372 // If a type specifier follows, it will be diagnosed elsewhere.
1373 continue;
Steve Naroffd3ded1f2008-06-05 00:02:44 +00001374 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001375 }
John McCallfec54012009-08-03 20:12:06 +00001376 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 if (isInvalid) {
1378 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00001379 assert(DiagID);
Chris Lattner1ab3b962008-11-18 07:48:38 +00001380 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001381 }
Chris Lattner81c018d2008-03-13 06:29:04 +00001382 DS.SetRangeEnd(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 ConsumeToken();
1384 }
1385}
Douglas Gregoradcac882008-12-01 23:54:00 +00001386
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001387/// ParseOptionalTypeSpecifier - Try to parse a single type-specifier. We
Douglas Gregor12e083c2008-11-07 15:42:26 +00001388/// primarily follow the C++ grammar with additions for C99 and GNU,
1389/// which together subsume the C grammar. Note that the C++
1390/// type-specifier also includes the C type-qualifier (for const,
1391/// volatile, and C99 restrict). Returns true if a type-specifier was
1392/// found (and parsed), false otherwise.
1393///
1394/// type-specifier: [C++ 7.1.5]
1395/// simple-type-specifier
1396/// class-specifier
1397/// enum-specifier
1398/// elaborated-type-specifier [TODO]
1399/// cv-qualifier
1400///
1401/// cv-qualifier: [C++ 7.1.5.1]
1402/// 'const'
1403/// 'volatile'
1404/// [C99] 'restrict'
1405///
1406/// simple-type-specifier: [ C++ 7.1.5.2]
1407/// '::'[opt] nested-name-specifier[opt] type-name [TODO]
1408/// '::'[opt] nested-name-specifier 'template' template-id [TODO]
1409/// 'char'
1410/// 'wchar_t'
1411/// 'bool'
1412/// 'short'
1413/// 'int'
1414/// 'long'
1415/// 'signed'
1416/// 'unsigned'
1417/// 'float'
1418/// 'double'
1419/// 'void'
1420/// [C99] '_Bool'
1421/// [C99] '_Complex'
1422/// [C99] '_Imaginary' // Removed in TC2?
1423/// [GNU] '_Decimal32'
1424/// [GNU] '_Decimal64'
1425/// [GNU] '_Decimal128'
1426/// [GNU] typeof-specifier
1427/// [OBJC] class-name objc-protocol-refs[opt] [TODO]
1428/// [OBJC] typedef-name objc-protocol-refs[opt] [TODO]
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001429/// [C++0x] 'decltype' ( expression )
John Thompson82287d12010-02-05 00:12:22 +00001430/// [AltiVec] '__vector'
John McCallfec54012009-08-03 20:12:06 +00001431bool Parser::ParseOptionalTypeSpecifier(DeclSpec &DS, bool& isInvalid,
Chris Lattner7a0ab5f2009-01-06 06:59:53 +00001432 const char *&PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001433 unsigned &DiagID,
Sebastian Redld9bafa72010-02-03 21:21:43 +00001434 const ParsedTemplateInfo &TemplateInfo,
1435 bool SuppressDeclarations) {
Douglas Gregor12e083c2008-11-07 15:42:26 +00001436 SourceLocation Loc = Tok.getLocation();
1437
1438 switch (Tok.getKind()) {
Chris Lattner166a8fc2009-01-04 23:41:41 +00001439 case tok::identifier: // foo::bar
Douglas Gregorc0b39642010-04-15 23:40:53 +00001440 // If we already have a type specifier, this identifier is not a type.
1441 if (DS.getTypeSpecType() != DeclSpec::TST_unspecified ||
1442 DS.getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
1443 DS.getTypeSpecSign() != DeclSpec::TSS_unspecified)
1444 return false;
John Thompson82287d12010-02-05 00:12:22 +00001445 // Check for need to substitute AltiVec keyword tokens.
1446 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
1447 break;
1448 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00001449 case tok::kw_typename: // typename foo::bar
Chris Lattner166a8fc2009-01-04 23:41:41 +00001450 // Annotate typenames and C++ scope specifiers. If we get one, just
1451 // recurse to handle whatever we get.
1452 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001453 return true;
1454 if (Tok.is(tok::identifier))
1455 return false;
1456 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1457 TemplateInfo, SuppressDeclarations);
Chris Lattner166a8fc2009-01-04 23:41:41 +00001458 case tok::coloncolon: // ::foo::bar
1459 if (NextToken().is(tok::kw_new) || // ::new
1460 NextToken().is(tok::kw_delete)) // ::delete
1461 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Chris Lattner166a8fc2009-01-04 23:41:41 +00001463 // Annotate typenames and C++ scope specifiers. If we get one, just
1464 // recurse to handle whatever we get.
1465 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00001466 return true;
1467 return ParseOptionalTypeSpecifier(DS, isInvalid, PrevSpec, DiagID,
1468 TemplateInfo, SuppressDeclarations);
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Douglas Gregor12e083c2008-11-07 15:42:26 +00001470 // simple-type-specifier:
Chris Lattnerb31757b2009-01-06 05:06:21 +00001471 case tok::annot_typename: {
Douglas Gregor31a19b62009-04-01 21:51:26 +00001472 if (Tok.getAnnotationValue())
1473 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001474 DiagID, Tok.getAnnotationValue());
Douglas Gregor31a19b62009-04-01 21:51:26 +00001475 else
1476 DS.SetTypeSpecError();
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001477 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
1478 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Douglas Gregor12e083c2008-11-07 15:42:26 +00001480 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
1481 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
1482 // Objective-C interface. If we don't have Objective-C or a '<', this is
1483 // just a normal reference to a typedef name.
1484 if (!Tok.is(tok::less) || !getLang().ObjC1)
1485 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001487 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001488 llvm::SmallVector<DeclPtrTy, 8> ProtocolDecl;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001489 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1490 ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1491 LAngleLoc, EndProtoLoc);
1492 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1493 ProtocolLocs.data(), LAngleLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Douglas Gregor12e083c2008-11-07 15:42:26 +00001495 DS.SetRangeEnd(EndProtoLoc);
1496 return true;
1497 }
1498
1499 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00001500 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001501 break;
1502 case tok::kw_long:
1503 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00001504 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
1505 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001506 else
John McCallfec54012009-08-03 20:12:06 +00001507 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
1508 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001509 break;
1510 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00001511 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001512 break;
1513 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00001514 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
1515 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001516 break;
1517 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00001518 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
1519 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001520 break;
1521 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00001522 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
1523 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001524 break;
1525 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00001526 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001527 break;
1528 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00001529 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001530 break;
1531 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00001532 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001533 break;
1534 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00001535 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001536 break;
1537 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00001538 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001539 break;
1540 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00001541 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001542 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001543 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00001544 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001545 break;
1546 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00001547 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00001548 break;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001549 case tok::kw_bool:
1550 case tok::kw__Bool:
John McCallfec54012009-08-03 20:12:06 +00001551 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001552 break;
1553 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00001554 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
1555 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001556 break;
1557 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00001558 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
1559 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001560 break;
1561 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00001562 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
1563 DiagID);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001564 break;
John Thompson82287d12010-02-05 00:12:22 +00001565 case tok::kw___vector:
1566 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
1567 break;
1568 case tok::kw___pixel:
1569 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
1570 break;
1571
Douglas Gregor12e083c2008-11-07 15:42:26 +00001572 // class-specifier:
1573 case tok::kw_class:
1574 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00001575 case tok::kw_union: {
1576 tok::TokenKind Kind = Tok.getKind();
1577 ConsumeToken();
Sebastian Redld9bafa72010-02-03 21:21:43 +00001578 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS_none,
1579 SuppressDeclarations);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001580 return true;
Chris Lattner4c97d762009-04-12 21:49:30 +00001581 }
Douglas Gregor12e083c2008-11-07 15:42:26 +00001582
1583 // enum-specifier:
1584 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00001585 ConsumeToken();
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001586 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS_none);
Douglas Gregor12e083c2008-11-07 15:42:26 +00001587 return true;
1588
1589 // cv-qualifier:
1590 case tok::kw_const:
1591 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001592 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001593 break;
1594 case tok::kw_volatile:
1595 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001596 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001597 break;
1598 case tok::kw_restrict:
1599 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00001600 DiagID, getLang());
Douglas Gregor12e083c2008-11-07 15:42:26 +00001601 break;
1602
1603 // GNU typeof support.
1604 case tok::kw_typeof:
1605 ParseTypeofSpecifier(DS);
1606 return true;
1607
Anders Carlsson6fd634f2009-06-24 17:47:40 +00001608 // C++0x decltype support.
1609 case tok::kw_decltype:
1610 ParseDecltypeSpecifier(DS);
1611 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001613 // C++0x auto support.
1614 case tok::kw_auto:
1615 if (!getLang().CPlusPlus0x)
1616 return false;
1617
John McCallfec54012009-08-03 20:12:06 +00001618 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, DiagID);
Anders Carlsson0b7f7892009-06-26 23:44:14 +00001619 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00001620 case tok::kw___ptr64:
1621 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00001622 case tok::kw___cdecl:
1623 case tok::kw___stdcall:
1624 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00001625 DS.AddAttributes(ParseMicrosoftTypeAttributes());
Chris Lattner837acd02009-01-21 19:19:26 +00001626 return true;
Steve Naroff239f0732008-12-25 14:16:32 +00001627
Douglas Gregor12e083c2008-11-07 15:42:26 +00001628 default:
1629 // Not a type-specifier; do nothing.
1630 return false;
1631 }
1632
1633 // If the specifier combination wasn't legal, issue a diagnostic.
1634 if (isInvalid) {
1635 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00001636 // Pick between error or extwarn.
Chris Lattner1ab3b962008-11-18 07:48:38 +00001637 Diag(Tok, DiagID) << PrevSpec;
Douglas Gregor12e083c2008-11-07 15:42:26 +00001638 }
1639 DS.SetRangeEnd(Tok.getLocation());
1640 ConsumeToken(); // whatever we parsed above.
1641 return true;
1642}
Reid Spencer5f016e22007-07-11 17:01:13 +00001643
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001644/// ParseStructDeclaration - Parse a struct declaration without the terminating
1645/// semicolon.
1646///
Reid Spencer5f016e22007-07-11 17:01:13 +00001647/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001648/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001649/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001650/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00001651/// struct-declarator-list:
1652/// struct-declarator
1653/// struct-declarator-list ',' struct-declarator
1654/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
1655/// struct-declarator:
1656/// declarator
1657/// [GNU] declarator attributes[opt]
1658/// declarator[opt] ':' constant-expression
1659/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
1660///
Chris Lattnere1359422008-04-10 06:46:29 +00001661void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00001662ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001663 if (Tok.is(tok::kw___extension__)) {
1664 // __extension__ silences extension warnings in the subexpression.
1665 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00001666 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00001667 return ParseStructDeclaration(DS, Fields);
1668 }
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Steve Naroff28a7ca82007-08-20 22:28:22 +00001670 // Parse the common specifier-qualifiers-list piece.
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001671 SourceLocation DSStart = Tok.getLocation();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001672 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001674 // If there are no declarators, this is a free-standing declaration
1675 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00001676 if (Tok.is(tok::semi)) {
Douglas Gregor4920f1f2009-01-12 22:49:06 +00001677 Actions.ParsedFreeStandingDeclSpec(CurScope, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00001678 return;
1679 }
1680
1681 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00001682 bool FirstDeclarator = true;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001683 while (1) {
John McCall54abf7d2009-11-04 02:18:39 +00001684 ParsingDeclRAIIObject PD(*this);
John McCallbdd563e2009-11-03 02:38:08 +00001685 FieldDeclarator DeclaratorInfo(DS);
1686
1687 // Attributes are only allowed here on successive declarators.
1688 if (!FirstDeclarator && Tok.is(tok::kw___attribute)) {
1689 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001690 AttributeList *AttrList = ParseGNUAttributes(&Loc);
John McCallbdd563e2009-11-03 02:38:08 +00001691 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1692 }
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Steve Naroff28a7ca82007-08-20 22:28:22 +00001694 /// struct-declarator: declarator
1695 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001696 if (Tok.isNot(tok::colon)) {
1697 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
1698 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00001699 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00001700 }
Mike Stump1eb44332009-09-09 15:08:12 +00001701
Chris Lattner04d66662007-10-09 17:33:22 +00001702 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00001703 ConsumeToken();
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001704 OwningExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001705 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00001706 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00001707 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00001708 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00001709 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00001710
Steve Naroff28a7ca82007-08-20 22:28:22 +00001711 // If attributes exist after the declarator, parse them.
Sebastian Redlab197ba2009-02-09 18:23:29 +00001712 if (Tok.is(tok::kw___attribute)) {
1713 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00001714 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00001715 DeclaratorInfo.D.AddAttributes(AttrList, Loc);
1716 }
1717
John McCallbdd563e2009-11-03 02:38:08 +00001718 // We're done with this declarator; invoke the callback.
John McCall54abf7d2009-11-04 02:18:39 +00001719 DeclPtrTy D = Fields.invoke(DeclaratorInfo);
1720 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00001721
Steve Naroff28a7ca82007-08-20 22:28:22 +00001722 // If we don't have a comma, it is either the end of the list (a ';')
1723 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00001724 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00001725 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00001726
Steve Naroff28a7ca82007-08-20 22:28:22 +00001727 // Consume the comma.
1728 ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00001729
John McCallbdd563e2009-11-03 02:38:08 +00001730 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00001731 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00001732}
1733
1734/// ParseStructUnionBody
1735/// struct-contents:
1736/// struct-declaration-list
1737/// [EXT] empty
1738/// [GNU] "struct-declaration-list" without terminatoring ';'
1739/// struct-declaration-list:
1740/// struct-declaration
1741/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001742/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00001743///
Reid Spencer5f016e22007-07-11 17:01:13 +00001744void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
Chris Lattnerb28317a2009-03-28 19:18:32 +00001745 unsigned TagType, DeclPtrTy TagDecl) {
Chris Lattner49f28ca2009-03-05 08:00:35 +00001746 PrettyStackTraceActionsDecl CrashInfo(TagDecl, RecordLoc, Actions,
1747 PP.getSourceManager(),
1748 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00001749
Reid Spencer5f016e22007-07-11 17:01:13 +00001750 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Douglas Gregor3218c4b2009-01-09 22:42:13 +00001752 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001753 Actions.ActOnTagStartDefinition(CurScope, TagDecl);
1754
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
1756 // C++.
Douglas Gregore37ac4f2008-04-13 21:30:24 +00001757 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001758 Diag(Tok, diag::ext_empty_struct_union_enum)
1759 << DeclSpec::getSpecifierName((DeclSpec::TST)TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00001760
Chris Lattnerb28317a2009-03-28 19:18:32 +00001761 llvm::SmallVector<DeclPtrTy, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00001762
Reid Spencer5f016e22007-07-11 17:01:13 +00001763 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00001764 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Reid Spencer5f016e22007-07-11 17:01:13 +00001767 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00001768 if (Tok.is(tok::semi)) {
Douglas Gregor9b3064b2009-04-01 22:41:11 +00001769 Diag(Tok, diag::ext_extra_struct_semi)
Douglas Gregor849b2432010-03-31 17:46:05 +00001770 << FixItHint::CreateRemoval(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00001771 ConsumeToken();
1772 continue;
1773 }
Chris Lattnere1359422008-04-10 06:46:29 +00001774
1775 // Parse all the comma separated declarators.
1776 DeclSpec DS;
Mike Stump1eb44332009-09-09 15:08:12 +00001777
John McCallbdd563e2009-11-03 02:38:08 +00001778 if (!Tok.is(tok::at)) {
1779 struct CFieldCallback : FieldCallback {
1780 Parser &P;
1781 DeclPtrTy TagDecl;
1782 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls;
1783
1784 CFieldCallback(Parser &P, DeclPtrTy TagDecl,
1785 llvm::SmallVectorImpl<DeclPtrTy> &FieldDecls) :
1786 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
1787
1788 virtual DeclPtrTy invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00001789 // Install the declarator into the current TagDecl.
John McCall4ba39712009-11-03 21:13:47 +00001790 DeclPtrTy Field = P.Actions.ActOnField(P.CurScope, TagDecl,
1791 FD.D.getDeclSpec().getSourceRange().getBegin(),
1792 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00001793 FieldDecls.push_back(Field);
1794 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00001795 }
John McCallbdd563e2009-11-03 02:38:08 +00001796 } Callback(*this, TagDecl, FieldDecls);
1797
1798 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001799 } else { // Handle @defs
1800 ConsumeToken();
1801 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
1802 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001803 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001804 continue;
1805 }
1806 ConsumeToken();
1807 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
1808 if (!Tok.is(tok::identifier)) {
1809 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001810 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001811 continue;
1812 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001813 llvm::SmallVector<DeclPtrTy, 16> Fields;
Mike Stump1eb44332009-09-09 15:08:12 +00001814 Actions.ActOnDefs(CurScope, TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001815 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00001816 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
1817 ConsumeToken();
1818 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00001819 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001820
Chris Lattner04d66662007-10-09 17:33:22 +00001821 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001822 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00001823 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001824 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00001825 break;
1826 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00001827 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
1828 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001829 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00001830 // If we stopped at a ';', eat it.
1831 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00001832 }
1833 }
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Steve Naroff60fccee2007-10-29 21:38:07 +00001835 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Ted Kremenek1e377652010-02-11 02:19:13 +00001837 llvm::OwningPtr<AttributeList> AttrList;
Reid Spencer5f016e22007-07-11 17:01:13 +00001838 // If attributes exist after struct contents, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00001839 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001840 AttrList.reset(ParseGNUAttributes());
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001841
1842 Actions.ActOnFields(CurScope,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001843 RecordLoc, TagDecl, FieldDecls.data(), FieldDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001844 LBraceLoc, RBraceLoc,
Ted Kremenek1e377652010-02-11 02:19:13 +00001845 AttrList.get());
Douglas Gregor72de6672009-01-08 20:45:30 +00001846 StructScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00001847 Actions.ActOnTagFinishDefinition(CurScope, TagDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001848}
1849
1850
1851/// ParseEnumSpecifier
1852/// enum-specifier: [C99 6.7.2.2]
1853/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001854///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001855/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
1856/// '}' attributes[opt]
1857/// 'enum' identifier
1858/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001859///
1860/// [C++] elaborated-type-specifier:
1861/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
1862///
Chris Lattner4c97d762009-04-12 21:49:30 +00001863void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001864 const ParsedTemplateInfo &TemplateInfo,
Chris Lattner4c97d762009-04-12 21:49:30 +00001865 AccessSpecifier AS) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001866 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00001867 if (Tok.is(tok::code_completion)) {
1868 // Code completion for an enum name.
1869 Actions.CodeCompleteTag(CurScope, DeclSpec::TST_enum);
1870 ConsumeToken();
1871 }
1872
Ted Kremenek1e377652010-02-11 02:19:13 +00001873 llvm::OwningPtr<AttributeList> Attr;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001874 // If attributes exist after tag, parse them.
1875 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00001876 Attr.reset(ParseGNUAttributes());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001877
1878 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00001879 if (getLang().CPlusPlus) {
1880 if (ParseOptionalCXXScopeSpecifier(SS, 0, false))
1881 return;
1882
1883 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00001884 Diag(Tok, diag::err_expected_ident);
1885 if (Tok.isNot(tok::l_brace)) {
1886 // Has no name and is not a definition.
1887 // Skip the rest of this declarator, up until the comma or semicolon.
1888 SkipUntil(tok::comma, true);
1889 return;
1890 }
1891 }
1892 }
Mike Stump1eb44332009-09-09 15:08:12 +00001893
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001894 // Must have either 'enum name' or 'enum {...}'.
1895 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace)) {
1896 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00001897
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001898 // Skip the rest of this declarator, up until the comma or semicolon.
1899 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001900 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001901 }
Mike Stump1eb44332009-09-09 15:08:12 +00001902
Douglas Gregor9b9edd62010-03-02 17:53:14 +00001903 // enums cannot be templates.
1904 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
1905 Diag(Tok, diag::err_enum_template);
1906
1907 // Skip the rest of this declarator, up until the comma or semicolon.
1908 SkipUntil(tok::comma, true);
1909 return;
1910 }
1911
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001912 // If an identifier is present, consume and remember it.
1913 IdentifierInfo *Name = 0;
1914 SourceLocation NameLoc;
1915 if (Tok.is(tok::identifier)) {
1916 Name = Tok.getIdentifierInfo();
1917 NameLoc = ConsumeToken();
1918 }
Mike Stump1eb44332009-09-09 15:08:12 +00001919
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001920 // There are three options here. If we have 'enum foo;', then this is a
1921 // forward declaration. If we have 'enum foo {...' then this is a
1922 // definition. Otherwise we have something like 'enum foo xyz', a reference.
1923 //
1924 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
1925 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
1926 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
1927 //
John McCall0f434ec2009-07-31 02:45:11 +00001928 Action::TagUseKind TUK;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001929 if (Tok.is(tok::l_brace))
John McCall0f434ec2009-07-31 02:45:11 +00001930 TUK = Action::TUK_Definition;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001931 else if (Tok.is(tok::semi))
John McCall0f434ec2009-07-31 02:45:11 +00001932 TUK = Action::TUK_Declaration;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00001933 else
John McCall0f434ec2009-07-31 02:45:11 +00001934 TUK = Action::TUK_Reference;
Douglas Gregor402abb52009-05-28 23:31:59 +00001935 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00001936 bool IsDependent = false;
John McCall0f434ec2009-07-31 02:45:11 +00001937 DeclPtrTy TagDecl = Actions.ActOnTag(CurScope, DeclSpec::TST_enum, TUK,
Ted Kremenek1e377652010-02-11 02:19:13 +00001938 StartLoc, SS, Name, NameLoc, Attr.get(),
1939 AS,
Douglas Gregorbd1099e2009-07-23 16:36:45 +00001940 Action::MultiTemplateParamsArg(Actions),
John McCallc4e70192009-09-11 04:59:25 +00001941 Owned, IsDependent);
1942 assert(!IsDependent && "didn't expect dependent enum");
Mike Stump1eb44332009-09-09 15:08:12 +00001943
Chris Lattner04d66662007-10-09 17:33:22 +00001944 if (Tok.is(tok::l_brace))
Reid Spencer5f016e22007-07-11 17:01:13 +00001945 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001946
Douglas Gregorb988f9c2010-01-25 16:33:23 +00001947 // FIXME: The DeclSpec should keep the locations of both the keyword and the
1948 // name (if there is one).
1949 SourceLocation TSTLoc = NameLoc.isValid()? NameLoc : StartLoc;
Reid Spencer5f016e22007-07-11 17:01:13 +00001950 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00001951 unsigned DiagID;
Douglas Gregorb988f9c2010-01-25 16:33:23 +00001952 if (DS.SetTypeSpecType(DeclSpec::TST_enum, TSTLoc, PrevSpec, DiagID,
Douglas Gregor402abb52009-05-28 23:31:59 +00001953 TagDecl.getAs<void>(), Owned))
John McCallfec54012009-08-03 20:12:06 +00001954 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00001955}
1956
1957/// ParseEnumBody - Parse a {} enclosed enumerator-list.
1958/// enumerator-list:
1959/// enumerator
1960/// enumerator-list ',' enumerator
1961/// enumerator:
1962/// enumeration-constant
1963/// enumeration-constant '=' constant-expression
1964/// enumeration-constant:
1965/// identifier
1966///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001967void Parser::ParseEnumBody(SourceLocation StartLoc, DeclPtrTy EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00001968 // Enter the scope of the enum body and start the definition.
1969 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor72de6672009-01-08 20:45:30 +00001970 Actions.ActOnTagStartDefinition(CurScope, EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00001971
Reid Spencer5f016e22007-07-11 17:01:13 +00001972 SourceLocation LBraceLoc = ConsumeBrace();
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Chris Lattner7946dd32007-08-27 17:24:30 +00001974 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
Chris Lattner04d66662007-10-09 17:33:22 +00001975 if (Tok.is(tok::r_brace) && !getLang().CPlusPlus)
Chris Lattner1ab3b962008-11-18 07:48:38 +00001976 Diag(Tok, diag::ext_empty_struct_union_enum) << "enum";
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Chris Lattnerb28317a2009-03-28 19:18:32 +00001978 llvm::SmallVector<DeclPtrTy, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00001979
Chris Lattnerb28317a2009-03-28 19:18:32 +00001980 DeclPtrTy LastEnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Reid Spencer5f016e22007-07-11 17:01:13 +00001982 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00001983 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001984 IdentifierInfo *Ident = Tok.getIdentifierInfo();
1985 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001986
Reid Spencer5f016e22007-07-11 17:01:13 +00001987 SourceLocation EqualLoc;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001988 OwningExprResult AssignedVal(Actions);
Chris Lattner04d66662007-10-09 17:33:22 +00001989 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001990 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001991 AssignedVal = ParseConstantExpression();
1992 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00001993 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001994 }
Mike Stump1eb44332009-09-09 15:08:12 +00001995
Reid Spencer5f016e22007-07-11 17:01:13 +00001996 // Install the enumerator constant into EnumDecl.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001997 DeclPtrTy EnumConstDecl = Actions.ActOnEnumConstant(CurScope, EnumDecl,
1998 LastEnumConstDecl,
1999 IdentLoc, Ident,
2000 EqualLoc,
2001 AssignedVal.release());
Reid Spencer5f016e22007-07-11 17:01:13 +00002002 EnumConstantDecls.push_back(EnumConstDecl);
2003 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Chris Lattner04d66662007-10-09 17:33:22 +00002005 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00002006 break;
2007 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002008
2009 if (Tok.isNot(tok::identifier) &&
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002010 !(getLang().C99 || getLang().CPlusPlus0x))
2011 Diag(CommaLoc, diag::ext_enumerator_list_comma)
2012 << getLang().CPlusPlus
Douglas Gregor849b2432010-03-31 17:46:05 +00002013 << FixItHint::CreateRemoval(CommaLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002014 }
Mike Stump1eb44332009-09-09 15:08:12 +00002015
Reid Spencer5f016e22007-07-11 17:01:13 +00002016 // Eat the }.
Mike Stumpc6e35aa2009-05-16 07:06:02 +00002017 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002018
Ted Kremenek1e377652010-02-11 02:19:13 +00002019 llvm::OwningPtr<AttributeList> Attr;
Reid Spencer5f016e22007-07-11 17:01:13 +00002020 // If attributes exist after the identifier list, parse them.
Chris Lattner04d66662007-10-09 17:33:22 +00002021 if (Tok.is(tok::kw___attribute))
Ted Kremenek1e377652010-02-11 02:19:13 +00002022 Attr.reset(ParseGNUAttributes()); // FIXME: where do they do?
Douglas Gregor72de6672009-01-08 20:45:30 +00002023
Edward O'Callaghanfee13812009-08-08 14:36:57 +00002024 Actions.ActOnEnumBody(StartLoc, LBraceLoc, RBraceLoc, EnumDecl,
2025 EnumConstantDecls.data(), EnumConstantDecls.size(),
Ted Kremenek1e377652010-02-11 02:19:13 +00002026 CurScope, Attr.get());
Mike Stump1eb44332009-09-09 15:08:12 +00002027
Douglas Gregor72de6672009-01-08 20:45:30 +00002028 EnumScope.Exit();
Argyrios Kyrtzidis07a5b282009-07-14 03:17:52 +00002029 Actions.ActOnTagFinishDefinition(CurScope, EnumDecl, RBraceLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002030}
2031
2032/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00002033/// start of a type-qualifier-list.
2034bool Parser::isTypeQualifier() const {
2035 switch (Tok.getKind()) {
2036 default: return false;
2037 // type-qualifier
2038 case tok::kw_const:
2039 case tok::kw_volatile:
2040 case tok::kw_restrict:
2041 return true;
2042 }
2043}
2044
Chris Lattnerb3a4e432010-02-28 18:18:36 +00002045/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
2046/// is definitely a type-specifier. Return false if it isn't part of a type
2047/// specifier or if we're not sure.
2048bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
2049 switch (Tok.getKind()) {
2050 default: return false;
2051 // type-specifiers
2052 case tok::kw_short:
2053 case tok::kw_long:
2054 case tok::kw_signed:
2055 case tok::kw_unsigned:
2056 case tok::kw__Complex:
2057 case tok::kw__Imaginary:
2058 case tok::kw_void:
2059 case tok::kw_char:
2060 case tok::kw_wchar_t:
2061 case tok::kw_char16_t:
2062 case tok::kw_char32_t:
2063 case tok::kw_int:
2064 case tok::kw_float:
2065 case tok::kw_double:
2066 case tok::kw_bool:
2067 case tok::kw__Bool:
2068 case tok::kw__Decimal32:
2069 case tok::kw__Decimal64:
2070 case tok::kw__Decimal128:
2071 case tok::kw___vector:
2072
2073 // struct-or-union-specifier (C99) or class-specifier (C++)
2074 case tok::kw_class:
2075 case tok::kw_struct:
2076 case tok::kw_union:
2077 // enum-specifier
2078 case tok::kw_enum:
2079
2080 // typedef-name
2081 case tok::annot_typename:
2082 return true;
2083 }
2084}
2085
Steve Naroff5f8aa692008-02-11 23:15:56 +00002086/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00002087/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002088bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002089 switch (Tok.getKind()) {
2090 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002091
Chris Lattner166a8fc2009-01-04 23:41:41 +00002092 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00002093 if (TryAltiVecVectorToken())
2094 return true;
2095 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002096 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002097 // Annotate typenames and C++ scope specifiers. If we get one, just
2098 // recurse to handle whatever we get.
2099 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002100 return true;
2101 if (Tok.is(tok::identifier))
2102 return false;
2103 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00002104
Chris Lattner166a8fc2009-01-04 23:41:41 +00002105 case tok::coloncolon: // ::foo::bar
2106 if (NextToken().is(tok::kw_new) || // ::new
2107 NextToken().is(tok::kw_delete)) // ::delete
2108 return false;
2109
Chris Lattner166a8fc2009-01-04 23:41:41 +00002110 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002111 return true;
2112 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002113
Reid Spencer5f016e22007-07-11 17:01:13 +00002114 // GNU attributes support.
2115 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002116 // GNU typeof support.
2117 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002118
Reid Spencer5f016e22007-07-11 17:01:13 +00002119 // type-specifiers
2120 case tok::kw_short:
2121 case tok::kw_long:
2122 case tok::kw_signed:
2123 case tok::kw_unsigned:
2124 case tok::kw__Complex:
2125 case tok::kw__Imaginary:
2126 case tok::kw_void:
2127 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002128 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002129 case tok::kw_char16_t:
2130 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00002131 case tok::kw_int:
2132 case tok::kw_float:
2133 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002134 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002135 case tok::kw__Bool:
2136 case tok::kw__Decimal32:
2137 case tok::kw__Decimal64:
2138 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002139 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002140
Chris Lattner99dc9142008-04-13 18:59:07 +00002141 // struct-or-union-specifier (C99) or class-specifier (C++)
2142 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002143 case tok::kw_struct:
2144 case tok::kw_union:
2145 // enum-specifier
2146 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002147
Reid Spencer5f016e22007-07-11 17:01:13 +00002148 // type-qualifier
2149 case tok::kw_const:
2150 case tok::kw_volatile:
2151 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002152
2153 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002154 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00002155 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002156
Chris Lattner7c186be2008-10-20 00:25:30 +00002157 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2158 case tok::less:
2159 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002160
Steve Naroff239f0732008-12-25 14:16:32 +00002161 case tok::kw___cdecl:
2162 case tok::kw___stdcall:
2163 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002164 case tok::kw___w64:
2165 case tok::kw___ptr64:
2166 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002167 }
2168}
2169
2170/// isDeclarationSpecifier() - Return true if the current token is part of a
2171/// declaration specifier.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002172bool Parser::isDeclarationSpecifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002173 switch (Tok.getKind()) {
2174 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002175
Chris Lattner166a8fc2009-01-04 23:41:41 +00002176 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00002177 // Unfortunate hack to support "Class.factoryMethod" notation.
2178 if (getLang().ObjC1 && NextToken().is(tok::period))
2179 return false;
John Thompson82287d12010-02-05 00:12:22 +00002180 if (TryAltiVecVectorToken())
2181 return true;
2182 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00002183 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00002184 // Annotate typenames and C++ scope specifiers. If we get one, just
2185 // recurse to handle whatever we get.
2186 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002187 return true;
2188 if (Tok.is(tok::identifier))
2189 return false;
2190 return isDeclarationSpecifier();
2191
Chris Lattner166a8fc2009-01-04 23:41:41 +00002192 case tok::coloncolon: // ::foo::bar
2193 if (NextToken().is(tok::kw_new) || // ::new
2194 NextToken().is(tok::kw_delete)) // ::delete
2195 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002196
Chris Lattner166a8fc2009-01-04 23:41:41 +00002197 // Annotate typenames and C++ scope specifiers. If we get one, just
2198 // recurse to handle whatever we get.
2199 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00002200 return true;
2201 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002202
Reid Spencer5f016e22007-07-11 17:01:13 +00002203 // storage-class-specifier
2204 case tok::kw_typedef:
2205 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00002206 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00002207 case tok::kw_static:
2208 case tok::kw_auto:
2209 case tok::kw_register:
2210 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Reid Spencer5f016e22007-07-11 17:01:13 +00002212 // type-specifiers
2213 case tok::kw_short:
2214 case tok::kw_long:
2215 case tok::kw_signed:
2216 case tok::kw_unsigned:
2217 case tok::kw__Complex:
2218 case tok::kw__Imaginary:
2219 case tok::kw_void:
2220 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002221 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002222 case tok::kw_char16_t:
2223 case tok::kw_char32_t:
2224
Reid Spencer5f016e22007-07-11 17:01:13 +00002225 case tok::kw_int:
2226 case tok::kw_float:
2227 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00002228 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00002229 case tok::kw__Bool:
2230 case tok::kw__Decimal32:
2231 case tok::kw__Decimal64:
2232 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00002233 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00002234
Chris Lattner99dc9142008-04-13 18:59:07 +00002235 // struct-or-union-specifier (C99) or class-specifier (C++)
2236 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00002237 case tok::kw_struct:
2238 case tok::kw_union:
2239 // enum-specifier
2240 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00002241
Reid Spencer5f016e22007-07-11 17:01:13 +00002242 // type-qualifier
2243 case tok::kw_const:
2244 case tok::kw_volatile:
2245 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00002246
Reid Spencer5f016e22007-07-11 17:01:13 +00002247 // function-specifier
2248 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00002249 case tok::kw_virtual:
2250 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002251
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002252 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00002253 case tok::annot_typename:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002254
Chris Lattner1ef08762007-08-09 17:01:07 +00002255 // GNU typeof support.
2256 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00002257
Chris Lattner1ef08762007-08-09 17:01:07 +00002258 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00002259 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00002260 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00002261
Chris Lattnerf3948c42008-07-26 03:38:44 +00002262 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
2263 case tok::less:
2264 return getLang().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00002265
Steve Naroff47f52092009-01-06 19:34:12 +00002266 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00002267 case tok::kw___cdecl:
2268 case tok::kw___stdcall:
2269 case tok::kw___fastcall:
Eli Friedman290eeb02009-06-08 23:27:34 +00002270 case tok::kw___w64:
2271 case tok::kw___ptr64:
2272 case tok::kw___forceinline:
2273 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00002274 }
2275}
2276
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002277bool Parser::isConstructorDeclarator() {
2278 TentativeParsingAction TPA(*this);
2279
2280 // Parse the C++ scope specifier.
2281 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00002282 if (ParseOptionalCXXScopeSpecifier(SS, 0, true)) {
2283 TPA.Revert();
2284 return false;
2285 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002286
2287 // Parse the constructor name.
2288 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
2289 // We already know that we have a constructor name; just consume
2290 // the token.
2291 ConsumeToken();
2292 } else {
2293 TPA.Revert();
2294 return false;
2295 }
2296
2297 // Current class name must be followed by a left parentheses.
2298 if (Tok.isNot(tok::l_paren)) {
2299 TPA.Revert();
2300 return false;
2301 }
2302 ConsumeParen();
2303
2304 // A right parentheses or ellipsis signals that we have a constructor.
2305 if (Tok.is(tok::r_paren) || Tok.is(tok::ellipsis)) {
2306 TPA.Revert();
2307 return true;
2308 }
2309
2310 // If we need to, enter the specified scope.
2311 DeclaratorScopeObj DeclScopeObj(*this, SS);
2312 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(CurScope, SS))
2313 DeclScopeObj.EnterDeclaratorScope();
2314
2315 // Check whether the next token(s) are part of a declaration
2316 // specifier, in which case we have the start of a parameter and,
2317 // therefore, we know that this is a constructor.
2318 bool IsConstructor = isDeclarationSpecifier();
2319 TPA.Revert();
2320 return IsConstructor;
2321}
Reid Spencer5f016e22007-07-11 17:01:13 +00002322
2323/// ParseTypeQualifierListOpt
2324/// type-qualifier-list: [C99 6.7.5]
2325/// type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002326/// [GNU] attributes [ only if AttributesAllowed=true ]
Reid Spencer5f016e22007-07-11 17:01:13 +00002327/// type-qualifier-list type-qualifier
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002328/// [GNU] type-qualifier-list attributes [ only if AttributesAllowed=true ]
Sean Huntbbd37c62009-11-21 08:43:09 +00002329/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
2330/// if CXX0XAttributesAllowed = true
Reid Spencer5f016e22007-07-11 17:01:13 +00002331///
Sean Huntbbd37c62009-11-21 08:43:09 +00002332void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, bool GNUAttributesAllowed,
2333 bool CXX0XAttributesAllowed) {
2334 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
2335 SourceLocation Loc = Tok.getLocation();
2336 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2337 if (CXX0XAttributesAllowed)
2338 DS.AddAttributes(Attr.AttrList);
2339 else
2340 Diag(Loc, diag::err_attributes_not_allowed);
2341 }
2342
Reid Spencer5f016e22007-07-11 17:01:13 +00002343 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002344 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002345 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002346 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002347 SourceLocation Loc = Tok.getLocation();
2348
2349 switch (Tok.getKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002350 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002351 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
2352 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002353 break;
2354 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002355 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
2356 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002357 break;
2358 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002359 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
2360 getLang());
Reid Spencer5f016e22007-07-11 17:01:13 +00002361 break;
Eli Friedman290eeb02009-06-08 23:27:34 +00002362 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002363 case tok::kw___ptr64:
Steve Naroff239f0732008-12-25 14:16:32 +00002364 case tok::kw___cdecl:
2365 case tok::kw___stdcall:
2366 case tok::kw___fastcall:
Sean Huntbbd37c62009-11-21 08:43:09 +00002367 if (GNUAttributesAllowed) {
Eli Friedman290eeb02009-06-08 23:27:34 +00002368 DS.AddAttributes(ParseMicrosoftTypeAttributes());
2369 continue;
2370 }
2371 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00002372 case tok::kw___attribute:
Sean Huntbbd37c62009-11-21 08:43:09 +00002373 if (GNUAttributesAllowed) {
2374 DS.AddAttributes(ParseGNUAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002375 continue; // do *not* consume the next token!
2376 }
2377 // otherwise, FALL THROUGH!
2378 default:
Steve Naroff239f0732008-12-25 14:16:32 +00002379 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002380 // If this is not a type-qualifier token, we're done reading type
2381 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002382 DS.Finish(Diags, PP);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002383 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00002384 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00002385
Reid Spencer5f016e22007-07-11 17:01:13 +00002386 // If the specifier combination wasn't legal, issue a diagnostic.
2387 if (isInvalid) {
2388 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00002389 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002390 }
2391 ConsumeToken();
2392 }
2393}
2394
2395
2396/// ParseDeclarator - Parse and verify a newly-initialized declarator.
2397///
2398void Parser::ParseDeclarator(Declarator &D) {
2399 /// This implements the 'declarator' production in the C grammar, then checks
2400 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002401 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00002402}
2403
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002404/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
2405/// is parsed by the function passed to it. Pass null, and the direct-declarator
2406/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002407/// ptr-operator production.
2408///
Sebastian Redlf30208a2009-01-24 21:16:55 +00002409/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
2410/// [C] pointer[opt] direct-declarator
2411/// [C++] direct-declarator
2412/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00002413///
2414/// pointer: [C99 6.7.5]
2415/// '*' type-qualifier-list[opt]
2416/// '*' type-qualifier-list[opt] pointer
2417///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002418/// ptr-operator:
2419/// '*' cv-qualifier-seq[opt]
2420/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00002421/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002422/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00002423/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00002424/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002425void Parser::ParseDeclaratorInternal(Declarator &D,
2426 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00002427 if (Diags.hasAllExtensionsSilenced())
2428 D.setExtension();
Sebastian Redlf30208a2009-01-24 21:16:55 +00002429 // C++ member pointers start with a '::' or a nested-name.
2430 // Member pointers get special handling, since there's no place for the
2431 // scope spec in the generic path below.
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002432 if (getLang().CPlusPlus &&
2433 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
2434 Tok.is(tok::annot_cxxscope))) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002435 CXXScopeSpec SS;
John McCall9ba61662010-02-26 08:45:28 +00002436 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/0, true); // ignore fail
2437
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00002438 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002439 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00002440 // The scope spec really belongs to the direct-declarator.
2441 D.getCXXScopeSpec() = SS;
2442 if (DirectDeclParser)
2443 (this->*DirectDeclParser)(D);
2444 return;
2445 }
2446
2447 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002448 D.SetRangeEnd(Loc);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002449 DeclSpec DS;
2450 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002451 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002452
2453 // Recurse to parse whatever is left.
2454 ParseDeclaratorInternal(D, DirectDeclParser);
2455
2456 // Sema will have to catch (syntactically invalid) pointers into global
2457 // scope. It has to catch pointers into namespace scope anyway.
2458 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002459 Loc, DS.TakeAttributes()),
2460 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00002461 return;
2462 }
2463 }
2464
2465 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00002466 // Not a pointer, C++ reference, or block.
Chris Lattner9af55002009-03-27 04:18:06 +00002467 if (Kind != tok::star && Kind != tok::caret &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00002468 (Kind != tok::amp || !getLang().CPlusPlus) &&
Sebastian Redl743de1f2009-03-23 00:00:23 +00002469 // We parse rvalue refs in C++03, because otherwise the errors are scary.
Chris Lattner9af55002009-03-27 04:18:06 +00002470 (Kind != tok::ampamp || !getLang().CPlusPlus)) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002471 if (DirectDeclParser)
2472 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002473 return;
2474 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00002475
Sebastian Redl05532f22009-03-15 22:02:01 +00002476 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
2477 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00002478 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002479 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002480
Chris Lattner9af55002009-03-27 04:18:06 +00002481 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00002482 // Is a pointer.
Reid Spencer5f016e22007-07-11 17:01:13 +00002483 DeclSpec DS;
Sebastian Redlf30208a2009-01-24 21:16:55 +00002484
Reid Spencer5f016e22007-07-11 17:01:13 +00002485 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002486 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00002487
Reid Spencer5f016e22007-07-11 17:01:13 +00002488 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002489 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00002490 if (Kind == tok::star)
2491 // Remember that we parsed a pointer type, and remember the type-quals.
2492 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Sebastian Redlab197ba2009-02-09 18:23:29 +00002493 DS.TakeAttributes()),
2494 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00002495 else
2496 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00002497 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
Mike Stump75b163f2009-04-21 00:51:43 +00002498 Loc, DS.TakeAttributes()),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002499 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002500 } else {
2501 // Is a reference
2502 DeclSpec DS;
2503
Sebastian Redl743de1f2009-03-23 00:00:23 +00002504 // Complain about rvalue references in C++03, but then go on and build
2505 // the declarator.
2506 if (Kind == tok::ampamp && !getLang().CPlusPlus0x)
2507 Diag(Loc, diag::err_rvalue_reference);
2508
Reid Spencer5f016e22007-07-11 17:01:13 +00002509 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
2510 // cv-qualifiers are introduced through the use of a typedef or of a
2511 // template type argument, in which case the cv-qualifiers are ignored.
2512 //
2513 // [GNU] Retricted references are allowed.
2514 // [GNU] Attributes on references are allowed.
Sean Huntbbd37c62009-11-21 08:43:09 +00002515 // [C++0x] Attributes on references are not allowed.
2516 ParseTypeQualifierListOpt(DS, true, false);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002517 D.ExtendWithDeclSpec(DS);
Reid Spencer5f016e22007-07-11 17:01:13 +00002518
2519 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2520 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
2521 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002522 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00002523 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
2524 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00002525 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00002526 }
2527
2528 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002529 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00002530
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002531 if (D.getNumTypeObjects() > 0) {
2532 // C++ [dcl.ref]p4: There shall be no references to references.
2533 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
2534 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00002535 if (const IdentifierInfo *II = D.getIdentifier())
2536 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2537 << II;
2538 else
2539 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
2540 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002541
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002542 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00002543 // can go ahead and build the (technically ill-formed)
2544 // declarator: reference collapsing will take care of it.
2545 }
2546 }
2547
Reid Spencer5f016e22007-07-11 17:01:13 +00002548 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00002549 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00002550 DS.TakeAttributes(),
2551 Kind == tok::amp),
Sebastian Redlab197ba2009-02-09 18:23:29 +00002552 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002553 }
2554}
2555
2556/// ParseDirectDeclarator
2557/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00002558/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00002559/// '(' declarator ')'
2560/// [GNU] '(' attributes declarator ')'
2561/// [C90] direct-declarator '[' constant-expression[opt] ']'
2562/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
2563/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
2564/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
2565/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
2566/// direct-declarator '(' parameter-type-list ')'
2567/// direct-declarator '(' identifier-list[opt] ')'
2568/// [GNU] direct-declarator '(' parameter-forward-declarations
2569/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002570/// [C++] direct-declarator '(' parameter-declaration-clause ')'
2571/// cv-qualifier-seq[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00002572/// [C++] declarator-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002573///
2574/// declarator-id: [C++ 8]
2575/// id-expression
2576/// '::'[opt] nested-name-specifier[opt] type-name
2577///
2578/// id-expression: [C++ 5.1]
2579/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002580/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00002581///
2582/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00002583/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002584/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00002585/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00002586/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00002587/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00002588///
Reid Spencer5f016e22007-07-11 17:01:13 +00002589void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002590 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002591
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002592 if (getLang().CPlusPlus && D.mayHaveIdentifier()) {
2593 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002594 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002595 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), /*ObjectType=*/0,
2596 true);
John McCall9ba61662010-02-26 08:45:28 +00002597 }
2598
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002599 if (D.getCXXScopeSpec().isValid()) {
John McCalle7e278b2009-12-11 20:04:54 +00002600 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2601 // Change the declaration context for name lookup, until this function
2602 // is exited (and the declarator has been parsed).
2603 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002604 }
2605
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002606 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
2607 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
2608 // We found something that indicates the start of an unqualified-id.
2609 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00002610 bool AllowConstructorName;
2611 if (D.getDeclSpec().hasTypeSpecifier())
2612 AllowConstructorName = false;
2613 else if (D.getCXXScopeSpec().isSet())
2614 AllowConstructorName =
2615 (D.getContext() == Declarator::FileContext ||
2616 (D.getContext() == Declarator::MemberContext &&
2617 D.getDeclSpec().isFriendSpecified()));
2618 else
2619 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
2620
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002621 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
2622 /*EnteringContext=*/true,
2623 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002624 AllowConstructorName,
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002625 /*ObjectType=*/0,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00002626 D.getName()) ||
2627 // Once we're past the identifier, if the scope was bad, mark the
2628 // whole declarator bad.
2629 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002630 D.SetIdentifier(0, Tok.getLocation());
2631 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002632 } else {
2633 // Parsed the unqualified-id; update range information and move along.
2634 if (D.getSourceRange().getBegin().isInvalid())
2635 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
2636 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00002637 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002638 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00002639 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002640 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002641 assert(!getLang().CPlusPlus &&
2642 "There's a C++-specific check for tok::identifier above");
2643 assert(Tok.getIdentifierInfo() && "Not an identifier?");
2644 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
2645 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00002646 goto PastIdentifier;
2647 }
2648
2649 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002650 // direct-declarator: '(' declarator ')'
2651 // direct-declarator: '(' attributes declarator ')'
2652 // Example: 'char (*X)' or 'int (*XX)(void)'
2653 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002654
2655 // If the declarator was parenthesized, we entered the declarator
2656 // scope when parsing the parenthesized declarator, then exited
2657 // the scope already. Re-enter the scope, if we need to.
2658 if (D.getCXXScopeSpec().isSet()) {
2659 if (Actions.ShouldEnterDeclaratorScope(CurScope, D.getCXXScopeSpec()))
2660 // Change the declaration context for name lookup, until this function
2661 // is exited (and the declarator has been parsed).
2662 DeclScopeObj.EnterDeclaratorScope();
2663 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002664 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002665 // This could be something simple like "int" (in which case the declarator
2666 // portion is empty), if an abstract-declarator is allowed.
2667 D.SetIdentifier(0, Tok.getLocation());
2668 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00002669 if (D.getContext() == Declarator::MemberContext)
2670 Diag(Tok, diag::err_expected_member_name_or_semi)
2671 << D.getDeclSpec().getSourceRange();
2672 else if (getLang().CPlusPlus)
Douglas Gregor2d1c2142009-11-03 19:44:04 +00002673 Diag(Tok, diag::err_expected_unqualified_id) << getLang().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002674 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00002675 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00002676 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00002677 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00002678 }
Mike Stump1eb44332009-09-09 15:08:12 +00002679
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00002680 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00002681 assert(D.isPastIdentifier() &&
2682 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00002683
Sean Huntbbd37c62009-11-21 08:43:09 +00002684 // Don't parse attributes unless we have an identifier.
Douglas Gregor3c3aaf92010-02-19 16:47:56 +00002685 if (D.getIdentifier() && getLang().CPlusPlus0x
Sean Huntbbd37c62009-11-21 08:43:09 +00002686 && isCXX0XAttributeSpecifier(true)) {
2687 SourceLocation AttrEndLoc;
2688 CXX0XAttributeList Attr = ParseCXX0XAttributes();
2689 D.AddAttributes(Attr.AttrList, AttrEndLoc);
2690 }
2691
Reid Spencer5f016e22007-07-11 17:01:13 +00002692 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00002693 if (Tok.is(tok::l_paren)) {
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002694 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
2695 // In such a case, check if we actually have a function declarator; if it
2696 // is not, the declarator has been fully parsed.
Chris Lattner7399ee02008-10-20 02:05:46 +00002697 if (getLang().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
2698 // When not in file scope, warn for ambiguous function declarators, just
2699 // in case the author intended it as a variable definition.
2700 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
2701 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
2702 break;
2703 }
Chris Lattneref4715c2008-04-06 05:45:57 +00002704 ParseFunctionDeclarator(ConsumeParen(), D);
Chris Lattner04d66662007-10-09 17:33:22 +00002705 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002706 ParseBracketDeclarator(D);
2707 } else {
2708 break;
2709 }
2710 }
2711}
2712
Chris Lattneref4715c2008-04-06 05:45:57 +00002713/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
2714/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00002715/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00002716/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
2717///
2718/// direct-declarator:
2719/// '(' declarator ')'
2720/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00002721/// direct-declarator '(' parameter-type-list ')'
2722/// direct-declarator '(' identifier-list[opt] ')'
2723/// [GNU] direct-declarator '(' parameter-forward-declarations
2724/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00002725///
2726void Parser::ParseParenDeclarator(Declarator &D) {
2727 SourceLocation StartLoc = ConsumeParen();
2728 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00002729
Chris Lattner7399ee02008-10-20 02:05:46 +00002730 // Eat any attributes before we look at whether this is a grouping or function
2731 // declarator paren. If this is a grouping paren, the attribute applies to
2732 // the type being built up, for example:
2733 // int (__attribute__(()) *x)(long y)
2734 // If this ends up not being a grouping paren, the attribute applies to the
2735 // first argument, for example:
2736 // int (__attribute__(()) int x)
2737 // In either case, we need to eat any attributes to be able to determine what
2738 // sort of paren this is.
2739 //
Ted Kremenek1e377652010-02-11 02:19:13 +00002740 llvm::OwningPtr<AttributeList> AttrList;
Chris Lattner7399ee02008-10-20 02:05:46 +00002741 bool RequiresArg = false;
2742 if (Tok.is(tok::kw___attribute)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002743 AttrList.reset(ParseGNUAttributes());
Mike Stump1eb44332009-09-09 15:08:12 +00002744
Chris Lattner7399ee02008-10-20 02:05:46 +00002745 // We require that the argument list (if this is a non-grouping paren) be
2746 // present even if the attribute list was empty.
2747 RequiresArg = true;
2748 }
Steve Naroff239f0732008-12-25 14:16:32 +00002749 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00002750 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
2751 Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___w64) ||
2752 Tok.is(tok::kw___ptr64)) {
Ted Kremenek1e377652010-02-11 02:19:13 +00002753 AttrList.reset(ParseMicrosoftTypeAttributes(AttrList.take()));
Eli Friedman290eeb02009-06-08 23:27:34 +00002754 }
Mike Stump1eb44332009-09-09 15:08:12 +00002755
Chris Lattneref4715c2008-04-06 05:45:57 +00002756 // If we haven't past the identifier yet (or where the identifier would be
2757 // stored, if this is an abstract declarator), then this is probably just
2758 // grouping parens. However, if this could be an abstract-declarator, then
2759 // this could also be the start of function arguments (consider 'void()').
2760 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00002761
Chris Lattneref4715c2008-04-06 05:45:57 +00002762 if (!D.mayOmitIdentifier()) {
2763 // If this can't be an abstract-declarator, this *must* be a grouping
2764 // paren, because we haven't seen the identifier yet.
2765 isGrouping = true;
2766 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Argyrios Kyrtzidise25d2702008-10-06 00:07:55 +00002767 (getLang().CPlusPlus && Tok.is(tok::ellipsis)) || // C++ int(...)
Chris Lattneref4715c2008-04-06 05:45:57 +00002768 isDeclarationSpecifier()) { // 'int(int)' is a function.
2769 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
2770 // considered to be a type, not a K&R identifier-list.
2771 isGrouping = false;
2772 } else {
2773 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
2774 isGrouping = true;
2775 }
Mike Stump1eb44332009-09-09 15:08:12 +00002776
Chris Lattneref4715c2008-04-06 05:45:57 +00002777 // If this is a grouping paren, handle:
2778 // direct-declarator: '(' declarator ')'
2779 // direct-declarator: '(' attributes declarator ')'
2780 if (isGrouping) {
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002781 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002782 D.setGroupingParens(true);
Chris Lattner7399ee02008-10-20 02:05:46 +00002783 if (AttrList)
Ted Kremenek1e377652010-02-11 02:19:13 +00002784 D.AddAttributes(AttrList.take(), SourceLocation());
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00002785
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002786 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00002787 // Match the ')'.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002788 SourceLocation Loc = MatchRHSPunctuation(tok::r_paren, StartLoc);
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00002789
2790 D.setGroupingParens(hadGroupingParens);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002791 D.SetRangeEnd(Loc);
Chris Lattneref4715c2008-04-06 05:45:57 +00002792 return;
2793 }
Mike Stump1eb44332009-09-09 15:08:12 +00002794
Chris Lattneref4715c2008-04-06 05:45:57 +00002795 // Okay, if this wasn't a grouping paren, it must be the start of a function
2796 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00002797 // identifier (and remember where it would have been), then call into
2798 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00002799 D.SetIdentifier(0, Tok.getLocation());
2800
Ted Kremenek1e377652010-02-11 02:19:13 +00002801 ParseFunctionDeclarator(StartLoc, D, AttrList.take(), RequiresArg);
Chris Lattneref4715c2008-04-06 05:45:57 +00002802}
2803
2804/// ParseFunctionDeclarator - We are after the identifier and have parsed the
2805/// declarator D up to a paren, which indicates that we are parsing function
2806/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00002807///
Chris Lattner7399ee02008-10-20 02:05:46 +00002808/// If AttrList is non-null, then the caller parsed those arguments immediately
2809/// after the open paren - they should be considered to be the first argument of
2810/// a parameter. If RequiresArg is true, then the first argument of the
2811/// function is required to be present and required to not be an identifier
2812/// list.
2813///
Reid Spencer5f016e22007-07-11 17:01:13 +00002814/// This method also handles this portion of the grammar:
2815/// parameter-type-list: [C99 6.7.5]
2816/// parameter-list
2817/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00002818/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00002819///
2820/// parameter-list: [C99 6.7.5]
2821/// parameter-declaration
2822/// parameter-list ',' parameter-declaration
2823///
2824/// parameter-declaration: [C99 6.7.5]
2825/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00002826/// [C++] declaration-specifiers declarator '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002827/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00002828/// declaration-specifiers abstract-declarator[opt]
2829/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00002830/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00002831/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
2832///
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002833/// For C++, after the parameter-list, it also parses "cv-qualifier-seq[opt]"
Sebastian Redl50de12f2009-03-24 22:27:57 +00002834/// and "exception-specification[opt]".
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002835///
Chris Lattner7399ee02008-10-20 02:05:46 +00002836void Parser::ParseFunctionDeclarator(SourceLocation LParenLoc, Declarator &D,
2837 AttributeList *AttrList,
2838 bool RequiresArg) {
Chris Lattneref4715c2008-04-06 05:45:57 +00002839 // lparen is already consumed!
2840 assert(D.isPastIdentifier() && "Should not call before identifier!");
Mike Stump1eb44332009-09-09 15:08:12 +00002841
Chris Lattner7399ee02008-10-20 02:05:46 +00002842 // This parameter list may be empty.
Chris Lattner04d66662007-10-09 17:33:22 +00002843 if (Tok.is(tok::r_paren)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002844 if (RequiresArg) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00002845 Diag(Tok, diag::err_argument_required_after_attribute);
Chris Lattner7399ee02008-10-20 02:05:46 +00002846 delete AttrList;
2847 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002848
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002849 SourceLocation RParenLoc = ConsumeParen(); // Eat the closing ')'.
2850 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002851
2852 // cv-qualifier-seq[opt].
2853 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002854 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002855 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00002856 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00002857 llvm::SmallVector<TypeTy*, 2> Exceptions;
2858 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002859 if (getLang().CPlusPlus) {
Chris Lattner5a69d1c2008-12-18 07:02:59 +00002860 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002861 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002862 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00002863
2864 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00002865 if (Tok.is(tok::kw_throw)) {
2866 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00002867 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002868 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00002869 hasAnyExceptionSpec);
2870 assert(Exceptions.size() == ExceptionRanges.size() &&
2871 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00002872 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002873 }
2874
Chris Lattnerf97409f2008-04-06 06:57:35 +00002875 // Remember that we parsed a function type, and remember the attributes.
Reid Spencer5f016e22007-07-11 17:01:13 +00002876 // int() -> no prototype, no '...'.
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002877 D.AddTypeInfo(DeclaratorChunk::getFunction(/*prototype*/getLang().CPlusPlus,
Chris Lattnerf97409f2008-04-06 06:57:35 +00002878 /*variadic*/ false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00002879 SourceLocation(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00002880 /*arglist*/ 0, 0,
2881 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00002882 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00002883 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00002884 Exceptions.data(),
2885 ExceptionRanges.data(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00002886 Exceptions.size(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00002887 LParenLoc, RParenLoc, D),
2888 EndLoc);
Chris Lattnerf97409f2008-04-06 06:57:35 +00002889 return;
Sebastian Redlef65f062009-05-29 18:02:33 +00002890 }
2891
Chris Lattner7399ee02008-10-20 02:05:46 +00002892 // Alternatively, this parameter list may be an identifier list form for a
2893 // K&R-style function: void foo(a,b,c)
John Thompson82287d12010-02-05 00:12:22 +00002894 if (!getLang().CPlusPlus && Tok.is(tok::identifier)
2895 && !TryAltiVecVectorToken()) {
John McCall9ba61662010-02-26 08:45:28 +00002896 if (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) {
Chris Lattner7399ee02008-10-20 02:05:46 +00002897 // K&R identifier lists can't have typedefs as identifiers, per
2898 // C99 6.7.5.3p11.
Steve Naroff2d081c42009-01-28 19:16:40 +00002899 if (RequiresArg) {
2900 Diag(Tok, diag::err_argument_required_after_attribute);
2901 delete AttrList;
2902 }
Steve Naroff2d081c42009-01-28 19:16:40 +00002903 // Identifier list. Note that '(' identifier-list ')' is only allowed for
2904 // normal declarators, not for abstract-declarators.
2905 return ParseFunctionDeclaratorIdentifierList(LParenLoc, D);
Chris Lattner7399ee02008-10-20 02:05:46 +00002906 }
Chris Lattnerf97409f2008-04-06 06:57:35 +00002907 }
Mike Stump1eb44332009-09-09 15:08:12 +00002908
Chris Lattnerf97409f2008-04-06 06:57:35 +00002909 // Finally, a normal, non-empty parameter type list.
Mike Stump1eb44332009-09-09 15:08:12 +00002910
Chris Lattnerf97409f2008-04-06 06:57:35 +00002911 // Build up an array of information about the parsed arguments.
2912 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Chris Lattner04421082008-04-08 04:40:51 +00002913
2914 // Enter function-declaration scope, limiting any declarators to the
2915 // function prototype scope, including parameter declarators.
Chris Lattnerae50fa02009-03-05 00:00:31 +00002916 ParseScope PrototypeScope(this,
2917 Scope::FunctionPrototypeScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00002918
Chris Lattnerf97409f2008-04-06 06:57:35 +00002919 bool IsVariadic = false;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002920 SourceLocation EllipsisLoc;
Chris Lattnerf97409f2008-04-06 06:57:35 +00002921 while (1) {
2922 if (Tok.is(tok::ellipsis)) {
2923 IsVariadic = true;
Douglas Gregor965acbb2009-02-18 07:07:28 +00002924 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002925 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002926 }
Mike Stump1eb44332009-09-09 15:08:12 +00002927
Chris Lattnerf97409f2008-04-06 06:57:35 +00002928 SourceLocation DSStart = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00002929
Chris Lattnerf97409f2008-04-06 06:57:35 +00002930 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00002931 // Just use the ParsingDeclaration "scope" of the declarator.
Chris Lattnerf97409f2008-04-06 06:57:35 +00002932 DeclSpec DS;
Chris Lattner7399ee02008-10-20 02:05:46 +00002933
2934 // If the caller parsed attributes for the first argument, add them now.
2935 if (AttrList) {
2936 DS.AddAttributes(AttrList);
2937 AttrList = 0; // Only apply the attributes to the first parameter.
2938 }
Chris Lattnere64c5492009-02-27 18:38:20 +00002939 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002940
Chris Lattnerf97409f2008-04-06 06:57:35 +00002941 // Parse the declarator. This is "PrototypeContext", because we must
2942 // accept either 'declarator' or 'abstract-declarator' here.
2943 Declarator ParmDecl(DS, Declarator::PrototypeContext);
2944 ParseDeclarator(ParmDecl);
2945
2946 // Parse GNU attributes, if present.
Sebastian Redlab197ba2009-02-09 18:23:29 +00002947 if (Tok.is(tok::kw___attribute)) {
2948 SourceLocation Loc;
Sean Huntbbd37c62009-11-21 08:43:09 +00002949 AttributeList *AttrList = ParseGNUAttributes(&Loc);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002950 ParmDecl.AddAttributes(AttrList, Loc);
2951 }
Mike Stump1eb44332009-09-09 15:08:12 +00002952
Chris Lattnerf97409f2008-04-06 06:57:35 +00002953 // Remember this parsed parameter in ParamInfo.
2954 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00002955
Douglas Gregor72b505b2008-12-16 21:30:33 +00002956 // DefArgToks is used when the parsing of default arguments needs
2957 // to be delayed.
2958 CachedTokens *DefArgToks = 0;
2959
Chris Lattnerf97409f2008-04-06 06:57:35 +00002960 // If no parameter was specified, verify that *something* was specified,
2961 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00002962 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
2963 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00002964 // Completely missing, emit error.
2965 Diag(DSStart, diag::err_missing_param);
2966 } else {
2967 // Otherwise, we have something. Add it and let semantic analysis try
2968 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00002969
Chris Lattnerf97409f2008-04-06 06:57:35 +00002970 // Inform the actions module about the parameter declarator, so it gets
2971 // added to the current scope.
Chris Lattnerb28317a2009-03-28 19:18:32 +00002972 DeclPtrTy Param = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00002973
2974 // Parse the default argument, if any. We parse the default
2975 // arguments in all dialects; the semantic analysis in
2976 // ActOnParamDefaultArgument will reject the default argument in
2977 // C.
2978 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00002979 SourceLocation EqualLoc = Tok.getLocation();
2980
Chris Lattner04421082008-04-08 04:40:51 +00002981 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00002982 if (D.getContext() == Declarator::MemberContext) {
2983 // If we're inside a class definition, cache the tokens
2984 // corresponding to the default argument. We'll actually parse
2985 // them when we see the end of the class definition.
2986 // FIXME: Templates will require something similar.
2987 // FIXME: Can we use a smart pointer for Toks?
2988 DefArgToks = new CachedTokens;
2989
Mike Stump1eb44332009-09-09 15:08:12 +00002990 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00002991 /*StopAtSemi=*/true,
2992 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00002993 delete DefArgToks;
2994 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00002995 Actions.ActOnParamDefaultArgumentError(Param);
2996 } else
Mike Stump1eb44332009-09-09 15:08:12 +00002997 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00002998 (*DefArgToks)[1].getLocation());
Chris Lattner04421082008-04-08 04:40:51 +00002999 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00003000 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00003001 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003002
Douglas Gregor72b505b2008-12-16 21:30:33 +00003003 OwningExprResult DefArgResult(ParseAssignmentExpression());
3004 if (DefArgResult.isInvalid()) {
3005 Actions.ActOnParamDefaultArgumentError(Param);
3006 SkipUntil(tok::comma, tok::r_paren, true, true);
3007 } else {
3008 // Inform the actions module about the default argument
3009 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Sebastian Redlf53597f2009-03-15 17:47:39 +00003010 move(DefArgResult));
Douglas Gregor72b505b2008-12-16 21:30:33 +00003011 }
Chris Lattner04421082008-04-08 04:40:51 +00003012 }
3013 }
Mike Stump1eb44332009-09-09 15:08:12 +00003014
3015 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
3016 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00003017 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00003018 }
3019
3020 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00003021 if (Tok.isNot(tok::comma)) {
3022 if (Tok.is(tok::ellipsis)) {
3023 IsVariadic = true;
3024 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
3025
3026 if (!getLang().CPlusPlus) {
3027 // We have ellipsis without a preceding ',', which is ill-formed
3028 // in C. Complain and provide the fix.
3029 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00003030 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00003031 }
3032 }
3033
3034 break;
3035 }
Mike Stump1eb44332009-09-09 15:08:12 +00003036
Chris Lattnerf97409f2008-04-06 06:57:35 +00003037 // Consume the comma.
3038 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003039 }
Mike Stump1eb44332009-09-09 15:08:12 +00003040
Chris Lattnerf97409f2008-04-06 06:57:35 +00003041 // Leave prototype scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00003042 PrototypeScope.Exit();
Mike Stump1eb44332009-09-09 15:08:12 +00003043
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003044 // If we have the closing ')', eat it.
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003045 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3046 SourceLocation EndLoc = RParenLoc;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003047
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003048 DeclSpec DS;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003049 bool hasExceptionSpec = false;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003050 SourceLocation ThrowLoc;
Sebastian Redl7dc81342009-04-29 17:30:04 +00003051 bool hasAnyExceptionSpec = false;
Sebastian Redlef65f062009-05-29 18:02:33 +00003052 llvm::SmallVector<TypeTy*, 2> Exceptions;
3053 llvm::SmallVector<SourceRange, 2> ExceptionRanges;
Sean Huntbbd37c62009-11-21 08:43:09 +00003054
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003055 if (getLang().CPlusPlus) {
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003056 // Parse cv-qualifier-seq[opt].
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003057 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003058 if (!DS.getSourceRange().getEnd().isInvalid())
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003059 EndLoc = DS.getSourceRange().getEnd();
Douglas Gregor0fe7bea2008-11-25 03:22:00 +00003060
3061 // Parse exception-specification[opt].
Sebastian Redl7dc81342009-04-29 17:30:04 +00003062 if (Tok.is(tok::kw_throw)) {
3063 hasExceptionSpec = true;
Sebastian Redl3cc97262009-05-31 11:47:27 +00003064 ThrowLoc = Tok.getLocation();
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003065 ParseExceptionSpecification(EndLoc, Exceptions, ExceptionRanges,
Sebastian Redlef65f062009-05-29 18:02:33 +00003066 hasAnyExceptionSpec);
3067 assert(Exceptions.size() == ExceptionRanges.size() &&
3068 "Produced different number of exception types and ranges.");
Sebastian Redl7dc81342009-04-29 17:30:04 +00003069 }
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003070 }
3071
Reid Spencer5f016e22007-07-11 17:01:13 +00003072 // Remember that we parsed a function type, and remember the attributes.
Chris Lattnerf97409f2008-04-06 06:57:35 +00003073 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/true, IsVariadic,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003074 EllipsisLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00003075 ParamInfo.data(), ParamInfo.size(),
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00003076 DS.getTypeQualifiers(),
Sebastian Redl3cc97262009-05-31 11:47:27 +00003077 hasExceptionSpec, ThrowLoc,
Sebastian Redl7dc81342009-04-29 17:30:04 +00003078 hasAnyExceptionSpec,
Sebastian Redlef65f062009-05-29 18:02:33 +00003079 Exceptions.data(),
3080 ExceptionRanges.data(),
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003081 Exceptions.size(),
3082 LParenLoc, RParenLoc, D),
3083 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003084}
3085
Chris Lattner66d28652008-04-06 06:34:08 +00003086/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
3087/// we found a K&R-style identifier list instead of a type argument list. The
3088/// current token is known to be the first identifier in the list.
3089///
3090/// identifier-list: [C99 6.7.5]
3091/// identifier
3092/// identifier-list ',' identifier
3093///
3094void Parser::ParseFunctionDeclaratorIdentifierList(SourceLocation LParenLoc,
3095 Declarator &D) {
3096 // Build up an array of information about the parsed arguments.
3097 llvm::SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
3098 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
Mike Stump1eb44332009-09-09 15:08:12 +00003099
Chris Lattner66d28652008-04-06 06:34:08 +00003100 // If there was no identifier specified for the declarator, either we are in
3101 // an abstract-declarator, or we are in a parameter declarator which was found
3102 // to be abstract. In abstract-declarators, identifier lists are not valid:
3103 // diagnose this.
3104 if (!D.getIdentifier())
3105 Diag(Tok, diag::ext_ident_list_in_param);
3106
3107 // Tok is known to be the first identifier in the list. Remember this
3108 // identifier in ParamInfo.
Chris Lattner3825c2e2008-04-06 06:50:56 +00003109 ParamsSoFar.insert(Tok.getIdentifierInfo());
Chris Lattner66d28652008-04-06 06:34:08 +00003110 ParamInfo.push_back(DeclaratorChunk::ParamInfo(Tok.getIdentifierInfo(),
Chris Lattnerb28317a2009-03-28 19:18:32 +00003111 Tok.getLocation(),
3112 DeclPtrTy()));
Mike Stump1eb44332009-09-09 15:08:12 +00003113
Chris Lattner50c64772008-04-06 06:39:19 +00003114 ConsumeToken(); // eat the first identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00003115
Chris Lattner66d28652008-04-06 06:34:08 +00003116 while (Tok.is(tok::comma)) {
3117 // Eat the comma.
3118 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003119
Chris Lattner50c64772008-04-06 06:39:19 +00003120 // If this isn't an identifier, report the error and skip until ')'.
Chris Lattner66d28652008-04-06 06:34:08 +00003121 if (Tok.isNot(tok::identifier)) {
3122 Diag(Tok, diag::err_expected_ident);
Chris Lattner50c64772008-04-06 06:39:19 +00003123 SkipUntil(tok::r_paren);
3124 return;
Chris Lattner66d28652008-04-06 06:34:08 +00003125 }
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003126
Chris Lattner66d28652008-04-06 06:34:08 +00003127 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
Chris Lattneraaf9ddb2008-04-06 06:47:48 +00003128
3129 // Reject 'typedef int y; int test(x, y)', but continue parsing.
Douglas Gregorb696ea32009-02-04 17:00:24 +00003130 if (Actions.getTypeName(*ParmII, Tok.getLocation(), CurScope))
Chris Lattnerda83bac2008-11-19 07:37:42 +00003131 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
Mike Stump1eb44332009-09-09 15:08:12 +00003132
Chris Lattner66d28652008-04-06 06:34:08 +00003133 // Verify that the argument identifier has not already been mentioned.
3134 if (!ParamsSoFar.insert(ParmII)) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00003135 Diag(Tok, diag::err_param_redefinition) << ParmII;
Chris Lattner50c64772008-04-06 06:39:19 +00003136 } else {
3137 // Remember this identifier in ParamInfo.
Chris Lattner66d28652008-04-06 06:34:08 +00003138 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Chris Lattnerb28317a2009-03-28 19:18:32 +00003139 Tok.getLocation(),
3140 DeclPtrTy()));
Chris Lattner50c64772008-04-06 06:39:19 +00003141 }
Mike Stump1eb44332009-09-09 15:08:12 +00003142
Chris Lattner66d28652008-04-06 06:34:08 +00003143 // Eat the identifier.
3144 ConsumeToken();
3145 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003146
3147 // If we have the closing ')', eat it and we're done.
3148 SourceLocation RLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
3149
Chris Lattner50c64772008-04-06 06:39:19 +00003150 // Remember that we parsed a function type, and remember the attributes. This
3151 // function type is always a K&R style function type, which is not varargs and
3152 // has no prototype.
3153 D.AddTypeInfo(DeclaratorChunk::getFunction(/*proto*/false, /*varargs*/false,
Douglas Gregor965acbb2009-02-18 07:07:28 +00003154 SourceLocation(),
Chris Lattner50c64772008-04-06 06:39:19 +00003155 &ParamInfo[0], ParamInfo.size(),
Sebastian Redl7dc81342009-04-29 17:30:04 +00003156 /*TypeQuals*/0,
Sebastian Redl3cc97262009-05-31 11:47:27 +00003157 /*exception*/false,
3158 SourceLocation(), false, 0, 0, 0,
Argyrios Kyrtzidis82bf0102009-08-19 23:14:54 +00003159 LParenLoc, RLoc, D),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003160 RLoc);
Chris Lattner66d28652008-04-06 06:34:08 +00003161}
Chris Lattneref4715c2008-04-06 05:45:57 +00003162
Reid Spencer5f016e22007-07-11 17:01:13 +00003163/// [C90] direct-declarator '[' constant-expression[opt] ']'
3164/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
3165/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
3166/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
3167/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
3168void Parser::ParseBracketDeclarator(Declarator &D) {
3169 SourceLocation StartLoc = ConsumeBracket();
Mike Stump1eb44332009-09-09 15:08:12 +00003170
Chris Lattner378c7e42008-12-18 07:27:21 +00003171 // C array syntax has many features, but by-far the most common is [] and [4].
3172 // This code does a fast path to handle some of the most obvious cases.
3173 if (Tok.getKind() == tok::r_square) {
Sebastian Redlab197ba2009-02-09 18:23:29 +00003174 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003175 //FIXME: Use these
3176 CXX0XAttributeList Attr;
3177 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier(true)) {
3178 Attr = ParseCXX0XAttributes();
3179 }
3180
Chris Lattner378c7e42008-12-18 07:27:21 +00003181 // Remember that we parsed the empty array type.
3182 OwningExprResult NumElements(Actions);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003183 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
3184 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003185 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003186 return;
3187 } else if (Tok.getKind() == tok::numeric_constant &&
3188 GetLookAheadToken(1).is(tok::r_square)) {
3189 // [4] is very common. Parse the numeric constant expression.
Sebastian Redlcd965b92009-01-18 18:53:16 +00003190 OwningExprResult ExprRes(Actions.ActOnNumericConstant(Tok));
Chris Lattner378c7e42008-12-18 07:27:21 +00003191 ConsumeToken();
3192
Sebastian Redlab197ba2009-02-09 18:23:29 +00003193 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
Sean Huntbbd37c62009-11-21 08:43:09 +00003194 //FIXME: Use these
3195 CXX0XAttributeList Attr;
3196 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3197 Attr = ParseCXX0XAttributes();
3198 }
Chris Lattner378c7e42008-12-18 07:27:21 +00003199
3200 // If there was an error parsing the assignment-expression, recover.
3201 if (ExprRes.isInvalid())
3202 ExprRes.release(); // Deallocate expr, just use [].
Mike Stump1eb44332009-09-09 15:08:12 +00003203
Chris Lattner378c7e42008-12-18 07:27:21 +00003204 // Remember that we parsed a array type, and remember its features.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003205 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, ExprRes.release(),
3206 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003207 EndLoc);
Chris Lattner378c7e42008-12-18 07:27:21 +00003208 return;
3209 }
Mike Stump1eb44332009-09-09 15:08:12 +00003210
Reid Spencer5f016e22007-07-11 17:01:13 +00003211 // If valid, this location is the position where we read the 'static' keyword.
3212 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00003213 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003214 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003215
Reid Spencer5f016e22007-07-11 17:01:13 +00003216 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003217 // Type qualifiers in an array subscript are a C99 feature.
Reid Spencer5f016e22007-07-11 17:01:13 +00003218 DeclSpec DS;
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003219 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00003220
Reid Spencer5f016e22007-07-11 17:01:13 +00003221 // If we haven't already read 'static', check to see if there is one after the
3222 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003223 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00003224 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003225
Reid Spencer5f016e22007-07-11 17:01:13 +00003226 // Handle "direct-declarator [ type-qual-list[opt] * ]".
3227 bool isStar = false;
Sebastian Redl15faa7f2008-12-09 20:22:58 +00003228 OwningExprResult NumElements(Actions);
Mike Stump1eb44332009-09-09 15:08:12 +00003229
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003230 // Handle the case where we have '[*]' as the array size. However, a leading
3231 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
3232 // the the token after the star is a ']'. Since stars in arrays are
3233 // infrequent, use of lookahead is not costly here.
3234 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00003235 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003236
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003237 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003238 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003239 StaticLoc = SourceLocation(); // Drop the static.
3240 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00003241 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00003242 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00003243 // Note, in C89, this production uses the constant-expr production instead
3244 // of assignment-expr. The only difference is that assignment-expr allows
3245 // things like '=' and '*='. Sema rejects these in C89 mode because they
3246 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00003247
Douglas Gregore0762c92009-06-19 23:52:42 +00003248 // Parse the constant-expression or assignment-expression now (depending
3249 // on dialect).
3250 if (getLang().CPlusPlus)
3251 NumElements = ParseConstantExpression();
3252 else
3253 NumElements = ParseAssignmentExpression();
Reid Spencer5f016e22007-07-11 17:01:13 +00003254 }
Mike Stump1eb44332009-09-09 15:08:12 +00003255
Reid Spencer5f016e22007-07-11 17:01:13 +00003256 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003257 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00003258 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003259 // If the expression was invalid, skip it.
3260 SkipUntil(tok::r_square);
3261 return;
3262 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003263
3264 SourceLocation EndLoc = MatchRHSPunctuation(tok::r_square, StartLoc);
3265
Sean Huntbbd37c62009-11-21 08:43:09 +00003266 //FIXME: Use these
3267 CXX0XAttributeList Attr;
3268 if (getLang().CPlusPlus0x && isCXX0XAttributeSpecifier()) {
3269 Attr = ParseCXX0XAttributes();
3270 }
3271
Chris Lattner378c7e42008-12-18 07:27:21 +00003272 // Remember that we parsed a array type, and remember its features.
Reid Spencer5f016e22007-07-11 17:01:13 +00003273 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
3274 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003275 NumElements.release(),
3276 StartLoc, EndLoc),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003277 EndLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003278}
3279
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003280/// [GNU] typeof-specifier:
3281/// typeof ( expressions )
3282/// typeof ( type-name )
3283/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00003284///
3285void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00003286 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003287 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003288 SourceLocation StartLoc = ConsumeToken();
3289
John McCallcfb708c2010-01-13 20:03:27 +00003290 const bool hasParens = Tok.is(tok::l_paren);
3291
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003292 bool isCastExpr;
3293 TypeTy *CastTy;
3294 SourceRange CastRange;
3295 OwningExprResult Operand = ParseExprAfterTypeofSizeofAlignof(OpTok,
3296 isCastExpr,
3297 CastTy,
3298 CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00003299 if (hasParens)
3300 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003301
3302 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003303 // FIXME: Not accurate, the range gets one token more than it should.
3304 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003305 else
3306 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00003307
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003308 if (isCastExpr) {
3309 if (!CastTy) {
3310 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003311 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00003312 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003313
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003314 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003315 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003316 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3317 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003318 DiagID, CastTy))
3319 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00003320 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003321 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003322
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003323 // If we get here, the operand to the typeof was an expresion.
3324 if (Operand.isInvalid()) {
3325 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00003326 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003327 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00003328
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003329 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003330 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00003331 // Check for duplicate type specifiers (e.g. "int typeof(int)").
3332 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00003333 DiagID, Operand.release()))
3334 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00003335}
Chris Lattner1b492422010-02-28 18:33:55 +00003336
3337
3338/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
3339/// from TryAltiVecVectorToken.
3340bool Parser::TryAltiVecVectorTokenOutOfLine() {
3341 Token Next = NextToken();
3342 switch (Next.getKind()) {
3343 default: return false;
3344 case tok::kw_short:
3345 case tok::kw_long:
3346 case tok::kw_signed:
3347 case tok::kw_unsigned:
3348 case tok::kw_void:
3349 case tok::kw_char:
3350 case tok::kw_int:
3351 case tok::kw_float:
3352 case tok::kw_double:
3353 case tok::kw_bool:
3354 case tok::kw___pixel:
3355 Tok.setKind(tok::kw___vector);
3356 return true;
3357 case tok::identifier:
3358 if (Next.getIdentifierInfo() == Ident_pixel) {
3359 Tok.setKind(tok::kw___vector);
3360 return true;
3361 }
3362 return false;
3363 }
3364}
3365
3366bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
3367 const char *&PrevSpec, unsigned &DiagID,
3368 bool &isInvalid) {
3369 if (Tok.getIdentifierInfo() == Ident_vector) {
3370 Token Next = NextToken();
3371 switch (Next.getKind()) {
3372 case tok::kw_short:
3373 case tok::kw_long:
3374 case tok::kw_signed:
3375 case tok::kw_unsigned:
3376 case tok::kw_void:
3377 case tok::kw_char:
3378 case tok::kw_int:
3379 case tok::kw_float:
3380 case tok::kw_double:
3381 case tok::kw_bool:
3382 case tok::kw___pixel:
3383 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3384 return true;
3385 case tok::identifier:
3386 if (Next.getIdentifierInfo() == Ident_pixel) {
3387 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
3388 return true;
3389 }
3390 break;
3391 default:
3392 break;
3393 }
3394 } else if (Tok.getIdentifierInfo() == Ident_pixel &&
3395 DS.isTypeAltiVecVector()) {
3396 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
3397 return true;
3398 }
3399 return false;
3400}
3401